43#include <visp3/core/vpCameraParameters.h>
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46#include <visp3/core/vpHomogeneousMatrix.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/core/vpMath.h>
49#include <visp3/core/vpMomentCommon.h>
50#include <visp3/core/vpMomentDatabase.h>
51#include <visp3/core/vpMomentObject.h>
52#include <visp3/core/vpPlane.h>
53#include <visp3/gui/vpDisplayD3D.h>
54#include <visp3/gui/vpDisplayGDI.h>
55#include <visp3/gui/vpDisplayGTK.h>
56#include <visp3/gui/vpDisplayOpenCV.h>
57#include <visp3/gui/vpDisplayX.h>
58#include <visp3/gui/vpPlot.h>
59#include <visp3/robot/vpSimulatorAfma6.h>
60#include <visp3/visual_features/vpFeatureBuilder.h>
61#include <visp3/visual_features/vpFeatureMomentCommon.h>
62#include <visp3/visual_features/vpFeaturePoint.h>
63#include <visp3/vs/vpServo.h>
65#if !defined(_WIN32) && !defined(VISP_HAVE_PTHREAD)
69 std::cout <<
"Can't run this example since vpSimulatorAfma6 capability is "
72 std::cout <<
"You should install pthread third-party library." << std::endl;
76#elif !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_OPENCV) && !defined(VISP_HAVE_GDI) && !defined(VISP_HAVE_D3D9) && \
77 !defined(VISP_HAVE_GTK)
80 std::cout <<
"Can't run this example since no display capability is available." << std::endl;
81 std::cout <<
"You should install one of the following third-party library: "
82 "X11, OpenCV, GDI, GTK."
88#ifndef DOXYGEN_SHOULD_SKIP_THIS
93 : m_width(640), m_height(480), m_cMo(), m_cdMo(), m_robot(false), m_Iint(m_height, m_width, 255), m_task(), m_cam(),
94 m_error(0), m_imsim(), m_interaction_type(), m_src(6), m_dst(6), m_moments(NULL), m_momentsDes(NULL),
95 m_featureMoments(NULL), m_featureMomentsDes(NULL), m_displayInt(NULL)
99#ifdef VISP_HAVE_DISPLAY
106 delete m_featureMoments;
107 delete m_featureMomentsDes;
113 std::vector<vpPoint> src_pts;
114 std::vector<vpPoint> dst_pts;
116 double x[8] = { 1, 3, 4, -1, -3, -2, -1, 1 };
117 double y[8] = { 0, 1, 4, 4, -2, -2, 1, 0 };
120 for (
int i = 0; i < nbpoints; i++) {
121 vpPoint p(x[i] / 20, y[i] / 20, 0.0);
123 src_pts.push_back(p);
127 m_src.fromVector(src_pts);
128 for (
int i = 0; i < nbpoints; i++) {
129 vpPoint p(x[i] / 20, y[i] / 20, 0.0);
131 dst_pts.push_back(p);
134 m_dst.fromVector(dst_pts);
153 planeToABC(pl, A, B, C);
157 planeToABC(pl, Ad, Bd, Cd);
160 vpTranslationVector vec;
172 m_featureMoments =
new vpFeatureMomentCommon(*m_moments);
173 m_featureMomentsDes =
new vpFeatureMomentCommon(*m_momentsDes);
175 m_moments->updateAll(m_src);
176 m_momentsDes->updateAll(m_dst);
178 m_featureMoments->updateAll(A, B, C);
179 m_featureMomentsDes->updateAll(Ad, Bd, Cd);
182 m_task.setInteractionMatrixType(m_interaction_type);
185 m_task.addFeature(m_featureMoments->getFeatureGravityNormalized(),
186 m_featureMomentsDes->getFeatureGravityNormalized());
187 m_task.addFeature(m_featureMoments->getFeatureAn(), m_featureMomentsDes->getFeatureAn());
188 m_task.addFeature(m_featureMoments->getFeatureCInvariant(), m_featureMomentsDes->getFeatureCInvariant(),
189 (1 << 3) | (1 << 5));
190 m_task.addFeature(m_featureMoments->getFeatureAlpha(), m_featureMomentsDes->getFeatureAlpha());
192 m_task.setLambda(1.);
196 void refreshScene(vpMomentObject &obj)
200 double x[8] = { 1, 3, 4, -1, -3, -2, -1, 1 };
201 double y[8] = { 0, 1, 4, 4, -2, -2, 1, 0 };
203 std::vector<vpPoint> cur_pts;
205 for (
int i = 0; i < nbpoints; i++) {
206 vpPoint p(x[i] / 20, y[i] / 20, 0.0);
208 cur_pts.push_back(p);
213 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo)
220#ifdef VISP_HAVE_DISPLAY
222#if defined(VISP_HAVE_X11)
223 m_displayInt =
new vpDisplayX;
224#elif defined(HAVE_OPENCV_HIGHGUI)
225 m_displayInt =
new vpDisplayOpenCV;
226#elif defined(VISP_HAVE_GDI)
227 m_displayInt =
new vpDisplayGDI;
228#elif defined(VISP_HAVE_D3D9)
229 m_displayInt =
new vpDisplayD3D;
230#elif defined(VISP_HAVE_GTK)
231 m_displayInt =
new vpDisplayGTK;
233 m_displayInt->
init(m_Iint, 50, 50,
"Visual servoing with moments");
244 void execute(
unsigned int nbIter)
247 init_visp_plot(ViSP_plot);
250 vpMomentObject obj(6);
254 std::cout <<
"Display task information " << std::endl;
258 m_robot.getInternalView(m_Iint);
260 unsigned int iter = 0;
263 while (iter++ < nbIter) {
266 m_cMo = m_robot.get_cMo();
272 planeToABC(pl, A, B, C);
277 m_moments->updateAll(obj);
280 m_featureMoments->updateAll(A, B, C);
283 m_robot.getInternalView(m_Iint);
290 v = m_task.computeControlLaw();
296 ViSP_plot.
plot(0, iter, v);
297 ViSP_plot.
plot(1, iter, vpPoseVector(m_cMo));
298 ViSP_plot.
plot(2, iter, m_task.getError());
300 m_error = (m_task.getError()).sumSquare();
310 m_robot.getInternalView(m_Iint);
316 void removeJointLimits(vpSimulatorAfma6 &robot)
318 vpColVector limMin(6);
319 vpColVector limMax(6);
334 robot.setJointLimit(limMin, limMax);
339 void planeToABC(vpPlane &pl,
double &A,
double &B,
double &C)
341 if (fabs(pl.
getD()) < std::numeric_limits<double>::epsilon()) {
342 std::cout <<
"Invalid position:" << std::endl;
343 std::cout << m_cMo << std::endl;
344 std::cout <<
"Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
357 m_robot.setCurrentViewColor(vpColor(150, 150, 150));
358 m_robot.setDesiredViewColor(vpColor(200, 200, 200));
360 removeJointLimits(m_robot);
362 m_robot.setConstantSamplingTimeMode(
true);
365 m_robot.initialiseObjectRelativeToCamera(m_cMo);
368 m_robot.setDesiredCameraPosition(m_cdMo);
369 m_robot.getCameraParameters(m_cam, m_Iint);
374 double error() {
return m_error; }
376 void init_visp_plot(vpPlot &ViSP_plot)
382 const unsigned int NbGraphs = 3;
383 const unsigned int NbCurves_in_graph[NbGraphs] = { 6, 6, 6 };
385 ViSP_plot.
init(NbGraphs, 800, 800, 100 +
static_cast<int>(m_width), 50,
"Visual Servoing results...");
387 vpColor Colors[6] = {
390 for (
unsigned int p = 0; p < NbGraphs; p++) {
391 ViSP_plot.
initGraph(p, NbCurves_in_graph[p]);
392 for (
unsigned int c = 0; c < NbCurves_in_graph[p]; c++)
393 ViSP_plot.
setColor(p, c, Colors[c]);
396 ViSP_plot.
setTitle(0,
"Robot velocities");
404 ViSP_plot.
setTitle(1,
"Camera pose cMo");
412 ViSP_plot.
setTitle(2,
"Error in visual features: ");
423 unsigned int m_width;
424 unsigned int m_height;
427 vpHomogeneousMatrix m_cMo;
428 vpHomogeneousMatrix m_cdMo;
430 vpSimulatorAfma6 m_robot;
431 vpImage<vpRGBa> m_Iint;
433 vpCameraParameters m_cam;
435 vpImageSimulator m_imsim;
439 vpMomentObject m_src;
440 vpMomentObject m_dst;
443 vpMomentCommon *m_moments;
444 vpMomentCommon *m_momentsDes;
445 vpFeatureMomentCommon *m_featureMoments;
446 vpFeatureMomentCommon *m_featureMomentsDes;
448 vpDisplay *m_displayInt;
461 servo.init(cMo, cdMo);
466 std::cout <<
"Catch an exception: " << e << std::endl;
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor cyan
static const vpColor orange
static const vpColor blue
static const vpColor purple
static const vpColor green
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
@ divideByZeroError
Division by zero.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vp_deprecated void init()
static double rad(double deg)
static std::vector< double > getMu3(vpMomentObject &object)
static double getAlpha(vpMomentObject &object)
static double getSurface(vpMomentObject &object)
void setType(vpObjectType input_type)
void fromVector(std::vector< vpPoint > &points)
void changeFrame(const vpHomogeneousMatrix &cMo)
void setABCD(double a, double b, double c, double d)
void initGraph(unsigned int graphNum, unsigned int curveNbr)
void init(unsigned int nbGraph, unsigned int height=700, unsigned int width=700, int x=-1, int y=-1, const std::string &title="")
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
void setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
void setTitle(unsigned int graphNum, const std::string &title)
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
void setMaxRotationVelocity(double maxVr)
void setMaxTranslationVelocity(double maxVt)
vpServoIteractionMatrixType