41#include <visp3/core/vpCameraParameters.h>
42#include <visp3/core/vpConfig.h>
43#include <visp3/core/vpDebug.h>
44#include <visp3/core/vpHomogeneousMatrix.h>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/core/vpMath.h>
47#include <visp3/core/vpMomentCommon.h>
48#include <visp3/core/vpMomentDatabase.h>
49#include <visp3/core/vpMomentObject.h>
50#include <visp3/core/vpPlane.h>
51#include <visp3/gui/vpDisplayFactory.h>
52#include <visp3/gui/vpPlot.h>
53#include <visp3/robot/vpSimulatorAfma6.h>
54#include <visp3/visual_features/vpFeatureBuilder.h>
55#include <visp3/visual_features/vpFeatureMomentCommon.h>
56#include <visp3/visual_features/vpFeaturePoint.h>
57#include <visp3/vs/vpServo.h>
59#if !defined(VISP_HAVE_DISPLAY)
62 std::cout <<
"Can't run this example since no display capability is available." << std::endl;
63 std::cout <<
"You should install one of the following third-party library: X11, OpenCV, GDI, GTK." << std::endl;
66#elif !defined(VISP_HAVE_THREADS)
69 std::cout <<
"Can't run this example since multi-threading capability is not available." << std::endl;
70 std::cout <<
"You should maybe enable cxx11 standard." << std::endl;
75#ifdef ENABLE_VISP_NAMESPACE
79#ifndef DOXYGEN_SHOULD_SKIP_THIS
84 : m_width(640), m_height(480), m_cMo(), m_cdMo(), m_robot(false), m_Iint(m_height, m_width, vpRGBa(255)), m_task(), m_cam(),
85 m_error(0), m_imsim(), m_interaction_type(), m_src(6), m_dst(6), m_moments(nullptr), m_momentsDes(nullptr),
86 m_featureMoments(nullptr), m_featureMomentsDes(nullptr), m_displayInt(nullptr)
90#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
97 delete m_featureMoments;
98 delete m_featureMomentsDes;
104 std::vector<vpPoint> src_pts;
105 std::vector<vpPoint> dst_pts;
107 double x[8] = { 1, 3, 4, -1, -3, -2, -1, 1 };
108 double y[8] = { 0, 1, 4, 4, -2, -2, 1, 0 };
111 for (
int i = 0;
i < nbpoints;
i++) {
112 vpPoint
p(x[i] / 20, y[i] / 20, 0.0);
114 src_pts.push_back(p);
118 m_src.fromVector(src_pts);
119 for (
int i = 0;
i < nbpoints;
i++) {
120 vpPoint
p(x[i] / 20, y[i] / 20, 0.0);
122 dst_pts.push_back(p);
125 m_dst.fromVector(dst_pts);
144 planeToABC(pl, A, B, C);
148 planeToABC(pl, Ad, Bd, Cd);
151 vpTranslationVector vec;
163 m_featureMoments =
new vpFeatureMomentCommon(*m_moments);
164 m_featureMomentsDes =
new vpFeatureMomentCommon(*m_momentsDes);
166 m_moments->updateAll(m_src);
167 m_momentsDes->updateAll(m_dst);
169 m_featureMoments->updateAll(A, B, C);
170 m_featureMomentsDes->updateAll(Ad, Bd, Cd);
173 m_task.setInteractionMatrixType(m_interaction_type);
176 m_task.addFeature(m_featureMoments->getFeatureGravityNormalized(),
177 m_featureMomentsDes->getFeatureGravityNormalized());
178 m_task.addFeature(m_featureMoments->getFeatureAn(), m_featureMomentsDes->getFeatureAn());
179 m_task.addFeature(m_featureMoments->getFeatureCInvariant(), m_featureMomentsDes->getFeatureCInvariant(),
180 (1 << 3) | (1 << 5));
181 m_task.addFeature(m_featureMoments->getFeatureAlpha(), m_featureMomentsDes->getFeatureAlpha());
183 m_task.setLambda(1.);
187 void refreshScene(vpMomentObject &obj)
191 double x[8] = { 1, 3, 4, -1, -3, -2, -1, 1 };
192 double y[8] = { 0, 1, 4, 4, -2, -2, 1, 0 };
194 std::vector<vpPoint> cur_pts;
196 for (
int i = 0;
i < nbpoints;
i++) {
197 vpPoint
p(x[i] / 20, y[i] / 20, 0.0);
199 cur_pts.push_back(p);
204 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo)
211#ifdef VISP_HAVE_DISPLAY
213#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
228 void execute(
unsigned int nbIter)
231 init_visp_plot(ViSP_plot);
234 vpMomentObject obj(6);
238 std::cout <<
"Display task information " << std::endl;
242 m_robot.getInternalView(m_Iint);
244 unsigned int iter = 0;
247 while (iter++ < nbIter) {
250 m_cMo = m_robot.get_cMo();
256 planeToABC(pl, A, B, C);
261 m_moments->updateAll(obj);
264 m_featureMoments->updateAll(A, B, C);
267 m_robot.getInternalView(m_Iint);
274 v = m_task.computeControlLaw();
280 ViSP_plot.
plot(0, iter, v);
281 ViSP_plot.
plot(1, iter, vpPoseVector(m_cMo));
282 ViSP_plot.
plot(2, iter, m_task.getError());
284 m_error = (m_task.getError()).sumSquare();
294 m_robot.getInternalView(m_Iint);
300 void removeJointLimits(vpSimulatorAfma6 &robot)
302 vpColVector limMin(6);
303 vpColVector limMax(6);
318 robot.setJointLimit(limMin, limMax);
323 void planeToABC(vpPlane &pl,
double &A,
double &B,
double &C)
325 if (fabs(pl.
getD()) < std::numeric_limits<double>::epsilon()) {
326 std::cout <<
"Invalid position:" << std::endl;
327 std::cout << m_cMo << std::endl;
328 std::cout <<
"Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
341 m_robot.setCurrentViewColor(vpColor(150, 150, 150));
342 m_robot.setDesiredViewColor(vpColor(200, 200, 200));
344 removeJointLimits(m_robot);
346 m_robot.setConstantSamplingTimeMode(
true);
349 m_robot.initialiseObjectRelativeToCamera(m_cMo);
352 m_robot.setDesiredCameraPosition(m_cdMo);
353 m_robot.getCameraParameters(m_cam, m_Iint);
358 double error() {
return m_error; }
360 void init_visp_plot(vpPlot &ViSP_plot)
366 const unsigned int NbGraphs = 3;
367 const unsigned int NbCurves_in_graph[NbGraphs] = { 6, 6, 6 };
369 ViSP_plot.
init(NbGraphs, 800, 800, 100 +
static_cast<int>(m_width), 50,
"Visual Servoing results...");
371 vpColor Colors[6] = {
374 for (
unsigned int p = 0;
p < NbGraphs;
p++) {
375 ViSP_plot.
initGraph(p, NbCurves_in_graph[p]);
376 for (
unsigned int c = 0; c < NbCurves_in_graph[
p]; c++)
377 ViSP_plot.
setColor(p, c, Colors[c]);
380 ViSP_plot.
setTitle(0,
"Robot velocities");
388 ViSP_plot.
setTitle(1,
"Camera pose cMo");
396 ViSP_plot.
setTitle(2,
"Error in visual features: ");
407 unsigned int m_width;
408 unsigned int m_height;
411 vpHomogeneousMatrix m_cMo;
412 vpHomogeneousMatrix m_cdMo;
414 vpSimulatorAfma6 m_robot;
415 vpImage<vpRGBa> m_Iint;
417 vpCameraParameters m_cam;
419 vpImageSimulator m_imsim;
423 vpMomentObject m_src;
424 vpMomentObject m_dst;
427 vpMomentCommon *m_moments;
428 vpMomentCommon *m_momentsDes;
429 vpFeatureMomentCommon *m_featureMoments;
430 vpFeatureMomentCommon *m_featureMomentsDes;
432#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
433 std::shared_ptr<vpDisplay> m_displayInt;
435 vpDisplay *m_displayInt;
449 servo.init(cMo, cdMo);
454 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.
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
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.