40#include <visp3/core/vpCameraParameters.h>
41#include <visp3/core/vpConfig.h>
42#include <visp3/core/vpDebug.h>
43#include <visp3/core/vpHomogeneousMatrix.h>
44#include <visp3/core/vpIoTools.h>
45#include <visp3/core/vpMath.h>
46#include <visp3/core/vpMomentCommon.h>
47#include <visp3/core/vpMomentDatabase.h>
48#include <visp3/core/vpMomentObject.h>
49#include <visp3/core/vpPlane.h>
50#include <visp3/gui/vpDisplayFactory.h>
51#include <visp3/gui/vpPlot.h>
52#include <visp3/robot/vpSimulatorAfma6.h>
53#include <visp3/visual_features/vpFeatureBuilder.h>
54#include <visp3/visual_features/vpFeatureMomentCommon.h>
55#include <visp3/visual_features/vpFeaturePoint.h>
56#include <visp3/vs/vpServo.h>
58#if !defined(VISP_HAVE_DISPLAY)
61 std::cout <<
"Can't run this example since no display capability is available." << std::endl;
62 std::cout <<
"You should install one of the following third-party library: X11, OpenCV, GDI, GTK." << std::endl;
65#elif !defined(VISP_HAVE_THREADS)
68 std::cout <<
"Can't run this example since multi-threading capability is not available." << std::endl;
69 std::cout <<
"You should maybe enable cxx11 standard." << std::endl;
74#ifdef ENABLE_VISP_NAMESPACE
78#ifndef DOXYGEN_SHOULD_SKIP_THIS
83 : 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(),
84 m_error(0), m_imsim(), m_interaction_type(), m_src(6), m_dst(6), m_moments(nullptr), m_momentsDes(nullptr),
85 m_featureMoments(nullptr), m_featureMomentsDes(nullptr), m_displayInt(nullptr)
89#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
96 delete m_featureMoments;
97 delete m_featureMomentsDes;
102 std::vector<vpPoint> src_pts;
103 std::vector<vpPoint> dst_pts;
105 double x[5] = { 0.2, 0.2, -0.2, -0.2, 0.2 };
106 double y[5] = { -0.1, 0.1, 0.1, -0.1, -0.1 };
109 for (
int i = 0;
i < nbpoints;
i++) {
110 vpPoint
p(x[i], y[i], 0.0);
112 src_pts.push_back(p);
116 m_src.fromVector(src_pts);
117 for (
int i = 0;
i < nbpoints;
i++) {
118 vpPoint
p(x[i], y[i], 0.0);
120 dst_pts.push_back(p);
123 m_dst.fromVector(dst_pts);
126 void refreshScene(vpMomentObject &obj)
128 double x[5] = { 0.2, 0.2, -0.2, -0.2, 0.2 };
129 double y[5] = { -0.1, 0.1, 0.1, -0.1, -0.1 };
131 std::vector<vpPoint> cur_pts;
133 for (
int i = 0;
i < nbpoints;
i++) {
134 vpPoint
p(x[i], y[i], 0.0);
136 cur_pts.push_back(p);
141 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo)
148#ifdef VISP_HAVE_DISPLAY
150#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
179 planeToABC(pl, A, B, C);
183 planeToABC(pl, Ad, Bd, Cd);
186 vpTranslationVector vec;
198 m_featureMoments =
new vpFeatureMomentCommon(*m_moments);
199 m_featureMomentsDes =
new vpFeatureMomentCommon(*m_momentsDes);
201 m_moments->updateAll(m_src);
202 m_momentsDes->updateAll(m_dst);
204 m_featureMoments->updateAll(A, B, C);
205 m_featureMomentsDes->updateAll(Ad, Bd, Cd);
208 m_task.setInteractionMatrixType(m_interaction_type);
211 m_task.addFeature(m_featureMoments->getFeatureGravityNormalized(),
212 m_featureMomentsDes->getFeatureGravityNormalized());
213 m_task.addFeature(m_featureMoments->getFeatureAn(), m_featureMomentsDes->getFeatureAn());
215 m_task.addFeature(m_featureMoments->getFeatureCInvariant(), m_featureMomentsDes->getFeatureCInvariant(),
216 (1 << 10) | (1 << 11));
217 m_task.addFeature(m_featureMoments->getFeatureAlpha(), m_featureMomentsDes->getFeatureAlpha());
219 m_task.setLambda(0.4);
222 void execute(
unsigned int nbIter)
225 init_visp_plot(ViSP_plot);
228 vpMomentObject obj(6);
232 std::cout <<
"Display task information " << std::endl;
236 m_robot.getInternalView(m_Iint);
238 unsigned int iter = 0;
241 while (iter++ < nbIter) {
245 m_cMo = m_robot.get_cMo();
251 planeToABC(pl, A, B, C);
256 m_moments->updateAll(obj);
259 m_featureMoments->updateAll(A, B, C);
262 m_robot.getInternalView(m_Iint);
270 v = m_task.computeControlLaw();
274 ViSP_plot.
plot(0, iter, v);
275 ViSP_plot.
plot(1, iter, vpPoseVector(m_cMo));
276 ViSP_plot.
plot(2, iter, m_task.getError());
278 m_error = (m_task.getError()).sumSquare();
289 m_robot.getInternalView(m_Iint);
297 double error() {
return m_error; }
299 void removeJointLimits(vpSimulatorAfma6 &robot)
301 vpColVector limMin(6);
302 vpColVector limMax(6);
317 robot.setJointLimit(limMin, limMax);
320 void planeToABC(vpPlane &pl,
double &A,
double &B,
double &C)
322 if (fabs(pl.
getD()) < std::numeric_limits<double>::epsilon()) {
323 std::cout <<
"Invalid position:" << std::endl;
324 std::cout << m_cMo << std::endl;
325 std::cout <<
"Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
337 m_robot.setCurrentViewColor(vpColor(150, 150, 150));
338 m_robot.setDesiredViewColor(vpColor(200, 200, 200));
340 removeJointLimits(m_robot);
344 m_robot.initialiseObjectRelativeToCamera(m_cMo);
347 m_robot.setDesiredCameraPosition(m_cdMo);
348 m_robot.getCameraParameters(m_cam, m_Iint);
351 void init_visp_plot(vpPlot &ViSP_plot)
357 const unsigned int NbGraphs = 3;
358 const unsigned int NbCurves_in_graph[NbGraphs] = { 6, 6, 6 };
360 ViSP_plot.
init(NbGraphs, 800, 800, 100 +
static_cast<int>(m_width), 50,
"Visual Servoing results...");
362 vpColor Colors[6] = {
365 for (
unsigned int p = 0;
p < NbGraphs;
p++) {
366 ViSP_plot.
initGraph(p, NbCurves_in_graph[p]);
367 for (
unsigned int c = 0; c < NbCurves_in_graph[
p]; c++)
368 ViSP_plot.
setColor(p, c, Colors[c]);
371 ViSP_plot.
setTitle(0,
"Robot velocities");
379 ViSP_plot.
setTitle(1,
"Camera pose cMo");
387 ViSP_plot.
setTitle(2,
"Error in visual features: ");
398 unsigned int m_width;
399 unsigned int m_height;
402 vpHomogeneousMatrix m_cMo;
403 vpHomogeneousMatrix m_cdMo;
405 vpSimulatorAfma6 m_robot;
406 vpImage<vpRGBa> m_Iint;
408 vpCameraParameters m_cam;
410 vpImageSimulator m_imsim;
414 vpMomentObject m_src;
415 vpMomentObject m_dst;
418 vpMomentCommon *m_moments;
419 vpMomentCommon *m_momentsDes;
420 vpFeatureMomentCommon *m_featureMoments;
421 vpFeatureMomentCommon *m_featureMomentsDes;
423#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
424 std::shared_ptr<vpDisplay> m_displayInt;
426 vpDisplay *m_displayInt;
440 servo.init(cMo, cdMo);
445 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.
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.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)