40#define PRINT_CONDITION_NUMBER
43#include <visp3/core/vpCameraParameters.h>
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpHomogeneousMatrix.h>
46#include <visp3/core/vpIoTools.h>
47#include <visp3/core/vpMath.h>
48#include <visp3/core/vpMomentCommon.h>
49#include <visp3/core/vpMomentDatabase.h>
50#include <visp3/core/vpMomentObject.h>
51#include <visp3/core/vpPlane.h>
52#include <visp3/core/vpPoseVector.h>
53#include <visp3/gui/vpDisplayFactory.h>
54#include <visp3/gui/vpPlot.h>
55#include <visp3/robot/vpImageSimulator.h>
56#include <visp3/robot/vpSimulatorCamera.h>
57#include <visp3/visual_features/vpFeatureBuilder.h>
58#include <visp3/visual_features/vpFeatureMomentCommon.h>
59#include <visp3/visual_features/vpFeaturePoint.h>
60#include <visp3/vs/vpServo.h>
62#if !defined(VISP_HAVE_DISPLAY)
65 std::cout <<
"Can't run this example since no display capability is available." << std::endl;
66 std::cout <<
"You should install one of the following third-party library: X11, OpenCV, GDI, GTK." << std::endl;
69#elif !defined(VISP_HAVE_THREADS)
72 std::cout <<
"Can't run this example since multi-threading capability is not available." << std::endl;
73 std::cout <<
"You should maybe enable cxx11 standard." << std::endl;
78#ifdef ENABLE_VISP_NAMESPACE
82#ifndef DOXYGEN_SHOULD_SKIP_THIS
87 : m_width(640), m_height(480), m_cMo(), m_cdMo(), m_robot(), m_Iint(m_height, m_width, vpRGBa(0)), m_task(), m_cam(),
88 m_error(0), m_imsim(), m_cur_img(m_height, m_width, 0), m_src_img(m_height, m_width, 0),
89 m_dst_img(m_height, m_width, 0), m_start_img(m_height, m_width, vpRGBa(0)), m_interaction_type(), m_src(6), m_dst(6),
90 m_moments(nullptr), m_momentsDes(nullptr), m_featureMoments(nullptr), m_featureMomentsDes(nullptr), m_displayInt(nullptr)
94#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
101 delete m_featureMoments;
102 delete m_featureMomentsDes;
106 void paramRobot() { m_cam = vpCameraParameters(600, 600, m_width / 2., m_height / 2.); }
109 void refreshScene(vpMomentObject &obj)
112 m_imsim.setCameraPosition(m_cMo);
113 m_imsim.getImage(m_cur_img, m_cam);
121 for (
int i = 0;
i < 4;
i++)
139 vpImage<unsigned char> tmp_img(m_height, m_width, 255);
140 vpImage<vpRGBa> tmp_start_img(m_height, m_width, vpRGBa(255, 0, 0));
142 vpImageSimulator imsim_start;
144 imsim_start.
init(tmp_start_img, X);
146 imsim_start.
getImage(m_start_img, m_cam);
149 m_imsim.init(tmp_img, X);
151 m_imsim.setCameraPosition(m_cMo);
152 m_imsim.getImage(m_src_img, m_cam);
155 m_src.fromImage(m_src_img, 128, m_cam);
158 m_imsim.setCameraPosition(m_cdMo);
159 m_imsim.getImage(m_dst_img, m_cam);
160 m_dst.fromImage(m_dst_img, 128, m_cam);
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(1.);
222 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo)
229#ifdef VISP_HAVE_DISPLAY
231#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
246 void execute(
unsigned int nbIter)
249 init_visp_plot(ViSP_plot);
252 vpMomentObject obj(6);
256 std::cout <<
"Display task information " << std::endl;
261 unsigned int iter = 0;
263 vpHomogeneousMatrix wMo;
264 vpHomogeneousMatrix wMc;
266 m_robot.setPosition(wMc);
267 double sampling_time = 0.010;
268 m_robot.setSamplingTime(sampling_time);
271 while (iter++ < nbIter) {
276 wMc = m_robot.getPosition();
283 planeToABC(pl, A, B, C);
288 m_moments->updateAll(obj);
291 m_featureMoments->updateAll(A, B, C);
293 m_imsim.setCameraPosition(m_cMo);
295 m_Iint = m_start_img;
297 m_imsim.getImage(m_Iint, m_cam);
305 v = m_task.computeControlLaw();
307 std::cout <<
" || s - s* || = " << m_task.error.sumSquare() << std::endl;
311 ViSP_plot.
plot(0, iter, v);
312 ViSP_plot.
plot(1, iter, vpPoseVector(m_cMo));
313 ViSP_plot.
plot(2, iter, m_task.getError());
315 m_error = (m_task.getError()).sumSquare();
317#if defined(PRINT_CONDITION_NUMBER)
321 vpMatrix Linteraction = m_task.L;
323 vpColVector singularvals;
324 Linteraction.
svd(singularvals, tmpry);
326 std::cout <<
"Condition Number: " << condno << std::endl;
336 m_imsim.getImage(m_Iint, m_cam);
345 double error() {
return m_error; }
347 void planeToABC(vpPlane &pl,
double &A,
double &B,
double &C)
349 if (fabs(pl.
getD()) < std::numeric_limits<double>::epsilon()) {
350 std::cout <<
"Invalid position:" << std::endl;
351 std::cout << m_cMo << std::endl;
352 std::cout <<
"Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
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 vpSimulatorCamera m_robot;
415 vpImage<vpRGBa> m_Iint;
417 vpCameraParameters m_cam;
419 vpImageSimulator m_imsim;
422 vpImage<unsigned char> m_cur_img;
423 vpImage<unsigned char> m_src_img;
424 vpImage<unsigned char> m_dst_img;
425 vpImage<vpRGBa> m_start_img;
428 vpMomentObject m_src;
429 vpMomentObject m_dst;
432 vpMomentCommon *m_moments;
433 vpMomentCommon *m_momentsDes;
434 vpFeatureMomentCommon *m_featureMoments;
435 vpFeatureMomentCommon *m_featureMomentsDes;
437#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
438 std::shared_ptr<vpDisplay> m_displayInt;
440 vpDisplay *m_displayInt;
455 servo.init(cMo, cdMo);
461 std::cout <<
"Catch an exception: " <<
e << std::endl;
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.
vpHomogeneousMatrix inverse() const
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
void init(const vpImage< unsigned char > &I, vpColVector *X)
void setInterpolationType(const vpInterpolationType interplt)
void setCameraPosition(const vpHomogeneousMatrix &cMt)
static double rad(double deg)
void svd(vpColVector &w, vpMatrix &V)
static std::vector< double > getMu3(vpMomentObject &object)
static double getAlpha(vpMomentObject &object)
static double getSurface(vpMomentObject &object)
void setType(vpObjectType input_type)
void fromImage(const vpImage< unsigned char > &image, unsigned char threshold, const vpCameraParameters &cam)
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)
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)