Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoMomentPolygon.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Example of visual servoing with moments using a polygon as object container
32 */
33
38
39#include <iostream>
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>
57
58#if !defined(VISP_HAVE_DISPLAY)
59int main()
60{
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;
63 return EXIT_SUCCESS;
64}
65#elif !defined(VISP_HAVE_THREADS)
66int main()
67{
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;
70 return EXIT_SUCCESS;
71}
72#else
73
74#ifdef ENABLE_VISP_NAMESPACE
75using namespace VISP_NAMESPACE_NAME;
76#endif
77
78#ifndef DOXYGEN_SHOULD_SKIP_THIS
79class servoMoment
80{
81public:
82 servoMoment()
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)
86 { }
87 ~servoMoment()
88 {
89#if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
90 if (m_displayInt) {
91 delete m_displayInt;
92 }
93#endif
94 delete m_moments;
95 delete m_momentsDes;
96 delete m_featureMoments;
97 delete m_featureMomentsDes;
98 }
99
100 void initScene()
101 {
102 std::vector<vpPoint> src_pts;
103 std::vector<vpPoint> dst_pts;
104
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 };
107 int nbpoints = 4;
108
109 for (int i = 0; i < nbpoints; i++) {
110 vpPoint p(x[i], y[i], 0.0);
111 p.track(m_cMo);
112 src_pts.push_back(p);
113 }
114
115 m_src.setType(vpMomentObject::DENSE_POLYGON);
116 m_src.fromVector(src_pts);
117 for (int i = 0; i < nbpoints; i++) {
118 vpPoint p(x[i], y[i], 0.0);
119 p.track(m_cdMo);
120 dst_pts.push_back(p);
121 }
122 m_dst.setType(vpMomentObject::DENSE_POLYGON);
123 m_dst.fromVector(dst_pts);
124 }
125
126 void refreshScene(vpMomentObject &obj)
127 {
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 };
130 int nbpoints = 5;
131 std::vector<vpPoint> cur_pts;
132
133 for (int i = 0; i < nbpoints; i++) {
134 vpPoint p(x[i], y[i], 0.0);
135 p.track(m_cMo);
136 cur_pts.push_back(p);
137 }
138 obj.fromVector(cur_pts);
139 }
140
141 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo)
142 {
143 m_cMo = cMo; // init source matrix
144 m_cdMo = cdMo; // init destination matrix
145
146 m_interaction_type = vpServo::CURRENT; // use interaction matrix for current position
147
148#ifdef VISP_HAVE_DISPLAY
149 // init the display
150#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
151 m_displayInt = vpDisplayFactory::createDisplay(m_Iint, 50, 50, "Visual servoing with moments");
152#else
153 m_displayInt = vpDisplayFactory::allocateDisplay(m_Iint, 50, 50, "Visual servoing with moments");
154#endif
155#endif
156
157 paramRobot(); // set up robot parameters
158
159 m_task.setServo(vpServo::EYEINHAND_CAMERA);
160 initScene(); // initialize graphical scene (for interface)
161 initFeatures(); // initialize moment features
162 }
163
164 void initFeatures()
165 {
166 // A,B,C parameters of source and destination plane
167 double A;
168 double B;
169 double C;
170 double Ad;
171 double Bd;
172 double Cd;
173 // init main object: using moments up to order 6
174
175 // Initializing values from regular plane (with ax+by+cz=d convention)
176 vpPlane pl;
177 pl.setABCD(0, 0, 1.0, 0);
178 pl.changeFrame(m_cMo);
179 planeToABC(pl, A, B, C);
180
181 pl.setABCD(0, 0, 1.0, 0);
182 pl.changeFrame(m_cdMo);
183 planeToABC(pl, Ad, Bd, Cd);
184
185 // extracting initial position (actually we only care about Zdst)
186 vpTranslationVector vec;
187 m_cdMo.extract(vec);
188
191 // don't need to be specific, vpMomentCommon automatically loads
192 // Xg,Yg,An,Ci,Cj,Alpha moments
193 m_moments = new vpMomentCommon(vpMomentCommon::getSurface(m_dst), vpMomentCommon::getMu3(m_dst),
194 vpMomentCommon::getAlpha(m_dst), vec[2]);
195 m_momentsDes = new vpMomentCommon(vpMomentCommon::getSurface(m_dst), vpMomentCommon::getMu3(m_dst),
196 vpMomentCommon::getAlpha(m_dst), vec[2]);
197 // same thing with common features
198 m_featureMoments = new vpFeatureMomentCommon(*m_moments);
199 m_featureMomentsDes = new vpFeatureMomentCommon(*m_momentsDes);
200
201 m_moments->updateAll(m_src);
202 m_momentsDes->updateAll(m_dst);
203
204 m_featureMoments->updateAll(A, B, C);
205 m_featureMomentsDes->updateAll(Ad, Bd, Cd);
206
207 // setup the interaction type
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());
214 // the moments are different in case of a symmetric object
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());
218
219 m_task.setLambda(0.4);
220 }
221
222 void execute(unsigned int nbIter)
223 {
224 vpPlot ViSP_plot;
225 init_visp_plot(ViSP_plot); // Initialize plot object
226
227 // init main object: using moments up to order 5
228 vpMomentObject obj(6);
229 // setting object type (disrete, continuous[form polygon])
231
232 std::cout << "Display task information " << std::endl;
233 m_task.print();
234
235 vpDisplay::display(m_Iint);
236 m_robot.getInternalView(m_Iint);
237 vpDisplay::flush(m_Iint);
238 unsigned int iter = 0;
239
241 while (iter++ < nbIter) {
242 vpColVector v;
243 double t = vpTime::measureTimeMs();
244 // get the cMo
245 m_cMo = m_robot.get_cMo();
246 // setup the plane in A,B,C style
247 vpPlane pl;
248 double A, B, C;
249 pl.setABCD(0, 0, 1.0, 0);
250 pl.changeFrame(m_cMo);
251 planeToABC(pl, A, B, C);
252
253 // track points, draw points and add refresh our object
254 refreshScene(obj);
255 // this is the most important thing to do: update our moments
256 m_moments->updateAll(obj);
257 // and update our features. Do it in that order. Features need to use the
258 // information computed by moments
259 m_featureMoments->updateAll(A, B, C);
260
261 vpDisplay::display(m_Iint);
262 m_robot.getInternalView(m_Iint);
263 vpDisplay::flush(m_Iint);
264
265 if (iter == 1) {
266 vpDisplay::displayText(m_Iint, 20, 20, "Click to start servoing", vpColor::red);
267 vpDisplay::flush(m_Iint);
268 vpDisplay::getClick(m_Iint);
269 }
270 v = m_task.computeControlLaw();
271
272 m_robot.setVelocity(vpRobot::CAMERA_FRAME, v);
273
274 ViSP_plot.plot(0, iter, v);
275 ViSP_plot.plot(1, iter, vpPoseVector(m_cMo)); // Plot the velocities
276 ViSP_plot.plot(2, iter, m_task.getError()); // cMo as translations and theta_u
277
278 m_error = (m_task.getError()).sumSquare();
279
280 vpDisplay::displayText(m_Iint, 20, 20, "Click to stop visual servo...", vpColor::red);
281 if (vpDisplay::getClick(m_Iint, false)) {
282 break;
283 }
284 vpDisplay::flush(m_Iint);
285 vpTime::wait(t, 10);
286 }
287
288 vpDisplay::display(m_Iint);
289 m_robot.getInternalView(m_Iint);
290 vpDisplay::displayText(m_Iint, 20, 20, "Click to quit...", vpColor::red);
291 vpDisplay::flush(m_Iint);
292 vpDisplay::getClick(m_Iint);
293 }
294
295 void setInteractionMatrixType(vpServo::vpServoIteractionMatrixType type) { m_interaction_type = type; }
296
297 double error() { return m_error; }
298
299 void removeJointLimits(vpSimulatorAfma6 &robot)
300 {
301 vpColVector limMin(6);
302 vpColVector limMax(6);
303 limMin[0] = vpMath::rad(-3600);
304 limMin[1] = vpMath::rad(-3600);
305 limMin[2] = vpMath::rad(-3600);
306 limMin[3] = vpMath::rad(-3600);
307 limMin[4] = vpMath::rad(-3600);
308 limMin[5] = vpMath::rad(-3600);
309
310 limMax[0] = vpMath::rad(3600);
311 limMax[1] = vpMath::rad(3600);
312 limMax[2] = vpMath::rad(3600);
313 limMax[3] = vpMath::rad(3600);
314 limMax[4] = vpMath::rad(3600);
315 limMax[5] = vpMath::rad(3600);
316
317 robot.setJointLimit(limMin, limMax);
318 }
319
320 void planeToABC(vpPlane &pl, double &A, double &B, double &C)
321 {
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;
326 throw vpException(vpException::divideByZeroError, "invalid position!");
327 }
328 A = -pl.getA() / pl.getD();
329 B = -pl.getB() / pl.getD();
330 C = -pl.getC() / pl.getD();
331 }
332
333 void paramRobot()
334 {
335 /*Initialise the robot and especially the camera*/
337 m_robot.setCurrentViewColor(vpColor(150, 150, 150));
338 m_robot.setDesiredViewColor(vpColor(200, 200, 200));
339 m_robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
340 removeJointLimits(m_robot);
342 /*Initialise the position of the object relative to the pose of the robot's
343 * camera*/
344 m_robot.initialiseObjectRelativeToCamera(m_cMo);
345
346 /*Set the desired position (for the displaypart)*/
347 m_robot.setDesiredCameraPosition(m_cdMo);
348 m_robot.getCameraParameters(m_cam, m_Iint);
349 }
350
351 void init_visp_plot(vpPlot &ViSP_plot)
352 {
353 /* -------------------------------------
354 * Initialize ViSP Plotting
355 * -------------------------------------
356 */
357 const unsigned int NbGraphs = 3; // No. of graphs
358 const unsigned int NbCurves_in_graph[NbGraphs] = { 6, 6, 6 }; // Curves in each graph
359
360 ViSP_plot.init(NbGraphs, 800, 800, 100 + static_cast<int>(m_width), 50, "Visual Servoing results...");
361
362 vpColor Colors[6] = {// Colour for s1, s2, s3, in 1st plot
364
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]);
369 }
370
371 ViSP_plot.setTitle(0, "Robot velocities");
372 ViSP_plot.setLegend(0, 0, "v_x");
373 ViSP_plot.setLegend(0, 1, "v_y");
374 ViSP_plot.setLegend(0, 2, "v_z");
375 ViSP_plot.setLegend(0, 3, "w_x");
376 ViSP_plot.setLegend(0, 4, "w_y");
377 ViSP_plot.setLegend(0, 5, "w_z");
378
379 ViSP_plot.setTitle(1, "Camera pose cMo");
380 ViSP_plot.setLegend(1, 0, "tx");
381 ViSP_plot.setLegend(1, 1, "ty");
382 ViSP_plot.setLegend(1, 2, "tz");
383 ViSP_plot.setLegend(1, 3, "tu_x");
384 ViSP_plot.setLegend(1, 4, "tu_y");
385 ViSP_plot.setLegend(1, 5, "tu_z");
386
387 ViSP_plot.setTitle(2, "Error in visual features: ");
388 ViSP_plot.setLegend(2, 0, "x_n");
389 ViSP_plot.setLegend(2, 1, "y_n");
390 ViSP_plot.setLegend(2, 2, "a_n");
391 ViSP_plot.setLegend(2, 3, "sx");
392 ViSP_plot.setLegend(2, 4, "sy");
393 ViSP_plot.setLegend(2, 5, "alpha");
394 }
395
396protected:
397 // start and destination positioning matrices
398 unsigned int m_width;
399 unsigned int m_height;
400
401 // start and destination positioning matrices
402 vpHomogeneousMatrix m_cMo;
403 vpHomogeneousMatrix m_cdMo;
404
405 vpSimulatorAfma6 m_robot; // robot used in this simulation
406 vpImage<vpRGBa> m_Iint; // internal image used for interface display
407 vpServo m_task; // servoing task
408 vpCameraParameters m_cam; // robot camera parameters
409 double m_error; // current error
410 vpImageSimulator m_imsim; // image simulator used to simulate the perspective-projection camera
411
412 vpServo::vpServoIteractionMatrixType m_interaction_type; // current or desired
413 // source and destination objects for moment manipulation
414 vpMomentObject m_src;
415 vpMomentObject m_dst;
416
417 // moment sets and their corresponding features
418 vpMomentCommon *m_moments;
419 vpMomentCommon *m_momentsDes;
420 vpFeatureMomentCommon *m_featureMoments;
421 vpFeatureMomentCommon *m_featureMomentsDes;
422
423#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
424 std::shared_ptr<vpDisplay> m_displayInt;
425#else
426 vpDisplay *m_displayInt;
427#endif
428};
429#endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
430
431int main()
432{
433 try { // intial pose
434 vpHomogeneousMatrix cMo(-0.1, -0.1, 1.5, -vpMath::rad(20), -vpMath::rad(20), -vpMath::rad(30));
435 // Desired pose
437
438 servoMoment servo;
439 // init and run the simulation
440 servo.init(cMo, cdMo);
441 servo.execute(1500);
442 return EXIT_SUCCESS;
443 }
444 catch (const vpException &e) {
445 std::cout << "Catch an exception: " << e << std::endl;
446 return EXIT_FAILURE;
447 }
448}
449
450#endif
@ TOOL_CCMOP
Definition vpAfma6.h:125
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition vpColor.h:198
static const vpColor cyan
Definition vpColor.h:207
static const vpColor orange
Definition vpColor.h:208
static const vpColor blue
Definition vpColor.h:204
static const vpColor purple
Definition vpColor.h:209
static const vpColor green
Definition vpColor.h:201
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.
Definition vpException.h:60
@ divideByZeroError
Division by zero.
Definition vpException.h:70
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double rad(double deg)
Definition vpMath.h:129
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)
Definition vpPlane.cpp:465
double getD() const
Definition vpPlane.h:106
double getA() const
Definition vpPlane.h:100
double getC() const
Definition vpPlane.h:104
void setABCD(double a, double b, double c, double d)
Definition vpPlane.h:88
double getB() const
Definition vpPlane.h:102
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition vpPlot.cpp:212
void init(unsigned int nbGraph, unsigned int height=700, unsigned int width=700, int x=-1, int y=-1, const std::string &title="")
Definition vpPlot.cpp:97
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
Definition vpPlot.cpp:561
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
Definition vpPlot.cpp:279
void setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
Definition vpPlot.cpp:255
void setTitle(unsigned int graphNum, const std::string &title)
Definition vpPlot.cpp:519
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
@ EYEINHAND_CAMERA
Definition vpServo.h:176
vpServoIteractionMatrixType
Definition vpServo.h:211
@ CURRENT
Definition vpServo.h:217
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)