#include <iostream>
#include <visp3/core/vpCameraParameters.h>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpMomentCommon.h>
#include <visp3/core/vpMomentDatabase.h>
#include <visp3/core/vpMomentObject.h>
#include <visp3/core/vpPlane.h>
#include <visp3/gui/vpDisplayD3D.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpPlot.h>
#include <visp3/robot/vpSimulatorAfma6.h>
#include <visp3/visual_features/vpFeatureBuilder.h>
#include <visp3/visual_features/vpFeatureMomentCommon.h>
#include <visp3/visual_features/vpFeaturePoint.h>
#include <visp3/vs/vpServo.h>
#if !defined(_WIN32) && !defined(VISP_HAVE_PTHREAD)
int main()
{
std::cout << "Can't run this example since vpSimulatorAfma6 capability is "
"not available."
<< std::endl;
std::cout << "You should install pthread third-party library." << std::endl;
return EXIT_SUCCESS;
}
#elif !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_OPENCV) && !defined(VISP_HAVE_GDI) && !defined(VISP_HAVE_D3D9) && \
!defined(VISP_HAVE_GTK)
int main()
{
std::cout << "Can't run this example since no display capability is available." << std::endl;
std::cout << "You should install one of the following third-party library: "
"X11, OpenCV, GDI, GTK."
<< std::endl;
return EXIT_SUCCESS;
}
#else
#ifndef DOXYGEN_SHOULD_SKIP_THIS
class servoMoment
{
public:
servoMoment()
: m_width(640), m_height(480), m_cMo(), m_cdMo(), m_robot(false), m_Iint(m_height, m_width, 255), m_task(), m_cam(),
m_error(0), m_imsim(), m_interaction_type(), m_src(6), m_dst(6), m_moments(NULL), m_momentsDes(NULL),
m_featureMoments(NULL), m_featureMomentsDes(NULL), m_displayInt(NULL)
{
}
~servoMoment()
{
#ifdef VISP_HAVE_DISPLAY
if (m_displayInt) {
delete m_displayInt;
}
#endif
delete m_moments;
delete m_momentsDes;
delete m_featureMoments;
delete m_featureMomentsDes;
}
void initScene()
{
std::vector<vpPoint> src_pts;
std::vector<vpPoint> dst_pts;
double x[8] = {1, 3, 4, -1, -3, -2, -1, 1};
double y[8] = {0, 1, 4, 4, -2, -2, 1, 0};
int nbpoints = 8;
for (int i = 0; i < nbpoints; i++) {
vpPoint p(x[i] / 20, y[i] / 20, 0.0);
src_pts.push_back(p);
}
m_src.fromVector(src_pts);
for (int i = 0; i < nbpoints; i++) {
vpPoint p(x[i] / 20, y[i] / 20, 0.0);
dst_pts.push_back(p);
}
m_dst.fromVector(dst_pts);
}
void initFeatures()
{
double A;
double B;
double C;
double Ad;
double Bd;
double Cd;
planeToABC(pl, A, B, C);
planeToABC(pl, Ad, Bd, Cd);
m_cdMo.extract(vec);
m_moments->updateAll(m_src);
m_momentsDes->updateAll(m_dst);
m_featureMoments->updateAll(A, B, C);
m_featureMomentsDes->updateAll(Ad, Bd, Cd);
m_task.setInteractionMatrixType(m_interaction_type);
m_task.addFeature(m_featureMoments->getFeatureGravityNormalized(),
m_featureMomentsDes->getFeatureGravityNormalized());
m_task.addFeature(m_featureMoments->getFeatureAn(), m_featureMomentsDes->getFeatureAn());
m_task.addFeature(m_featureMoments->getFeatureCInvariant(), m_featureMomentsDes->getFeatureCInvariant(),
(1 << 3) | (1 << 5));
m_task.addFeature(m_featureMoments->getFeatureAlpha(), m_featureMomentsDes->getFeatureAlpha());
m_task.setLambda(1.);
}
{
double x[8] = {1, 3, 4, -1, -3, -2, -1, 1};
double y[8] = {0, 1, 4, 4, -2, -2, 1, 0};
int nbpoints = 8;
std::vector<vpPoint> cur_pts;
for (int i = 0; i < nbpoints; i++) {
vpPoint p(x[i] / 20, y[i] / 20, 0.0);
cur_pts.push_back(p);
}
}
{
m_cMo = cMo;
m_cdMo = cdMo;
#ifdef VISP_HAVE_DISPLAY
#if defined VISP_HAVE_X11
#elif defined VISP_HAVE_OPENCV
#elif defined VISP_HAVE_GDI
#elif defined VISP_HAVE_D3D9
#elif defined VISP_HAVE_GTK
#endif
m_displayInt->
init(m_Iint, 50, 50,
"Visual servoing with moments");
#endif
paramRobot();
initScene();
initFeatures();
}
void execute(unsigned int nbIter)
{
init_visp_plot(ViSP_plot);
std::cout << "Display task information " << std::endl;
m_task.print();
m_robot.getInternalView(m_Iint);
unsigned int iter = 0;
while (iter++ < nbIter) {
m_cMo = m_robot.get_cMo();
double A, B, C;
planeToABC(pl, A, B, C);
refreshScene(obj);
m_moments->updateAll(obj);
m_featureMoments->updateAll(A, B, C);
m_robot.getInternalView(m_Iint);
if (iter == 1) {
}
v = m_task.computeControlLaw();
ViSP_plot.
plot(0, iter, v);
ViSP_plot.
plot(2, iter, m_task.getError());
m_error = (m_task.getError()).sumSquare();
break;
}
}
m_robot.getInternalView(m_Iint);
}
{
}
void planeToABC(
vpPlane &pl,
double &A,
double &B,
double &C)
{
if (fabs(pl.
getD()) < std::numeric_limits<double>::epsilon()) {
std::cout << "Invalid position:" << std::endl;
std::cout << m_cMo << std::endl;
std::cout << "Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
}
}
void paramRobot()
{
m_robot.setCurrentViewColor(
vpColor(150, 150, 150));
m_robot.setDesiredViewColor(
vpColor(200, 200, 200));
removeJointLimits(m_robot);
m_robot.setConstantSamplingTimeMode(true);
m_robot.initialiseObjectRelativeToCamera(m_cMo);
m_robot.setDesiredCameraPosition(m_cdMo);
m_robot.getCameraParameters(m_cam, m_Iint);
}
double error() { return m_error; }
void init_visp_plot(
vpPlot &ViSP_plot)
{
const unsigned int NbGraphs = 3;
const unsigned int NbCurves_in_graph[NbGraphs] = {6, 6, 6};
ViSP_plot.
init(NbGraphs, 800, 800, 100 + static_cast<int>(m_width), 50,
"Visual Servoing results...");
for (unsigned int p = 0; p < NbGraphs; p++) {
ViSP_plot.
initGraph(p, NbCurves_in_graph[p]);
for (unsigned int c = 0; c < NbCurves_in_graph[p]; c++)
}
ViSP_plot.
setTitle(0,
"Robot velocities");
ViSP_plot.
setTitle(1,
"Camera pose cMo");
ViSP_plot.
setTitle(2,
"Error in visual features: ");
}
protected:
unsigned int m_width;
unsigned int m_height;
double m_error;
};
#endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
int main()
{
try {
servoMoment servo;
servo.init(cMo, cdMo);
servo.execute(1500);
return EXIT_SUCCESS;
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}
#endif