Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuFourPoints2DPolarCamVelocityDisplay.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 * Simulation of a 2D visual servoing using 4 points with polar
32 * coordinates as visual feature.
33 */
34
50
51#include <visp3/core/vpConfig.h>
52#include <visp3/core/vpDebug.h>
53
54#if defined(VISP_HAVE_DISPLAY)
55
56#include <stdio.h>
57#include <stdlib.h>
58
59#include <visp3/core/vpCameraParameters.h>
60#include <visp3/core/vpHomogeneousMatrix.h>
61#include <visp3/core/vpImage.h>
62#include <visp3/core/vpImagePoint.h>
63#include <visp3/core/vpIoTools.h>
64#include <visp3/core/vpMath.h>
65#include <visp3/core/vpMeterPixelConversion.h>
66#include <visp3/gui/vpDisplayFactory.h>
67#include <visp3/gui/vpProjectionDisplay.h>
68#include <visp3/io/vpParseArgv.h>
69#include <visp3/robot/vpSimulatorCamera.h>
70#include <visp3/visual_features/vpFeatureBuilder.h>
71#include <visp3/visual_features/vpFeaturePointPolar.h>
72#include <visp3/vs/vpServo.h>
73#include <visp3/vs/vpServoDisplay.h>
74
75// List of allowed command line options
76#define GETOPTARGS "cdh"
77
78#ifdef ENABLE_VISP_NAMESPACE
79using namespace VISP_NAMESPACE_NAME;
80#endif
81
82void usage(const char *name, const char *badparam);
83bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
84
93void usage(const char *name, const char *badparam)
94{
95 fprintf(stdout, "\n\
96Tests a control law with the following characteristics:\n\
97- eye-in-hand control\n\
98- articular velocity are computed\n\
99- servo on 4 points,\n\
100- internal and external camera view displays.\n\
101\n\
102SYNOPSIS\n\
103 %s [-c] [-d] [-h]\n",
104 name);
105
106 fprintf(stdout, "\n\
107OPTIONS: Default\n\
108 -c\n\
109 Disable the mouse click. Useful to automate the \n\
110 execution of this program without human intervention.\n\
111\n\
112 -d \n\
113 Turn off the display.\n\
114\n\
115 -h\n\
116 Print the help.\n");
117
118 if (badparam)
119 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
120}
133bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
134{
135 const char *optarg_;
136 int c;
137 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138
139 switch (c) {
140 case 'c':
141 click_allowed = false;
142 break;
143 case 'd':
144 display = false;
145 break;
146 case 'h':
147 usage(argv[0], nullptr);
148 return false;
149
150 default:
151 usage(argv[0], optarg_);
152 return false;
153 }
154 }
155
156 if ((c == 1) || (c == -1)) {
157 // standalone param or error
158 usage(argv[0], nullptr);
159 std::cerr << "ERROR: " << std::endl;
160 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161 return false;
162 }
163
164 return true;
165}
166
167int main(int argc, const char **argv)
168{
169 // We declare the windows variables to be able to free the memory in the catch sections if needed
170#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
171 std::shared_ptr<vpDisplay> displayInt;
172 std::shared_ptr<vpDisplay> displayExt;
173#else
174 vpDisplay *displayInt = nullptr;
175 vpDisplay *displayExt = nullptr;
176#endif
177 try {
178 // Log file creation in /tmp/$USERNAME/log.dat
179 // This file contains by line:
180 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
181 // - the 6 measured camera velocities (m/s, rad/s)
182 // - the 6 measured joint positions (m, rad)
183 // - the 8 values of s - s*
184 std::string username;
185 // Get the user login name
186 vpIoTools::getUserName(username);
187
188 // Create a log filename to save velocities...
189 std::string logdirname;
190#if defined(_WIN32)
191 logdirname = "C:/temp/" + username;
192#else
193 logdirname = "/tmp/" + username;
194#endif
195
196 // Test if the output path exist. If no try to create it
197 if (vpIoTools::checkDirectory(logdirname) == false) {
198 try {
199 // Create the dirname
200 vpIoTools::makeDirectory(logdirname);
201 }
202 catch (...) {
203 std::cerr << std::endl << "ERROR:" << std::endl;
204 std::cerr << " Cannot create " << logdirname << std::endl;
205 return EXIT_FAILURE;
206 }
207 }
208 std::string logfilename;
209 logfilename = logdirname + "/log.dat";
210
211 // Open the log file name
212 std::ofstream flog(logfilename.c_str());
213
214 bool opt_click_allowed = true;
215 bool opt_display = true;
216
217 // Read the command line options
218 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
219 return EXIT_FAILURE;
220 }
221
222 // open a display for the visualization
223
224 vpImage<unsigned char> Iint(300, 300, 0);
225 vpImage<unsigned char> Iext(300, 300, 0);
226
227 if (opt_display) {
228 // We open two displays, one for the internal camera view, the other one for
229 // the external view
230#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
231 displayInt = vpDisplayFactory::createDisplay(Iint, 0, 0, "Internal view");
232 displayExt = vpDisplayFactory::createDisplay(Iext, 330, 000, "External view");
233#else
234 displayInt = vpDisplayFactory::allocateDisplay(Iint, 0, 0, "Internal view");
235 displayExt = vpDisplayFactory::allocateDisplay(Iext, 330, 000, "External view");
236#endif
237 }
238 vpProjectionDisplay externalview;
239
240 double px = 500, py = 500;
241 double u0 = 150, v0 = 160;
242
243 vpCameraParameters cam(px, py, u0, v0);
244
246 vpSimulatorCamera robot;
247
248 std::cout << std::endl;
249 std::cout << "----------------------------------------------" << std::endl;
250 std::cout << " Test program for vpServo " << std::endl;
251 std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
252 std::cout << " Simulation " << std::endl;
253 std::cout << " task : servo 4 points " << std::endl;
254 std::cout << "----------------------------------------------" << std::endl;
255 std::cout << std::endl;
256
257// #define TRANS_Z_PURE
258// #define TRANS_X_PURE
259// #define ROT_Z_PURE
260// #define ROT_X_PURE
261#define COMPLEX
262 //#define PROBLEM
263
264#if defined(TRANS_Z_PURE)
265 // sets the initial camera location
267 // sets the desired camera location
268 vpHomogeneousMatrix cMod(0, 0, 2, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
269#elif defined(TRANS_X_PURE)
270 // sets the initial camera location
272 // sets the desired camera location
273 vpHomogeneousMatrix cMod(0.5, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
274
275#elif defined(ROT_Z_PURE)
276 // sets the initial camera location
278 // sets the desired camera location
279 vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
280
281#elif defined(ROT_X_PURE)
282 // sets the initial camera location
284 // sets the desired camera location
285 vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
286
287#elif defined(COMPLEX)
288 // sets the initial camera location
290 // sets the desired camera location
291 vpHomogeneousMatrix cMod(0, 0, 2.5, vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
292
293#elif defined(PROBLEM)
294 // Bad behavior with an interaction matrix computed from the desired
295 // features sets the initial camera location
297 // sets the desired camera location
298 vpHomogeneousMatrix cMod(0.4, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
299
300#endif
301 // Compute the position of the object in the world frame
302 vpHomogeneousMatrix wMc, wMo;
303 robot.getPosition(wMc);
304 wMo = wMc * cMo;
305
306 vpHomogeneousMatrix cextMo(0, 0, 6, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
307
308 // sets the point coordinates in the object frame
309 vpPoint point[4];
310 point[0].setWorldCoordinates(-0.25, -0.25, 0);
311 point[1].setWorldCoordinates(0.25, -0.25, 0);
312 point[2].setWorldCoordinates(0.25, 0.25, 0);
313 point[3].setWorldCoordinates(-0.25, 0.25, 0);
314
315 for (unsigned int i = 0; i < 4; i++)
316 externalview.insert(point[i]);
317
318 // sets the desired position of the feature point s*"
320
321 // computes the point coordinates in the desired camera frame and
322 // its 2D coordinates
323 for (unsigned int i = 0; i < 4; i++) {
324 point[i].track(cMod);
325 // Computes the polar coordinates from the image point
326 // cartesian coordinates
327 vpFeatureBuilder::create(pd[i], point[i]);
328 }
329
330 // computes the point coordinates in the camera frame and its 2D
331 // coordinates
332 for (unsigned int i = 0; i < 4; i++)
333 point[i].track(cMo);
334
335 // sets the desired position of the point
337 for (unsigned int i = 0; i < 4; i++) {
338 // retrieve x,y and Z of the vpPoint structure to initialize the
339 // visual feature
340 vpFeatureBuilder::create(p[i], point[i]);
341 }
342
343 // Define the task;
344 // - we want an eye-in-hand control law
345 // - articular velocity are computed
347 // task.setInteractionMatrixType(vpServo::MEAN) ;
348 // task.setInteractionMatrixType(vpServo::DESIRED) ;
349 task.setInteractionMatrixType(vpServo::CURRENT);
350
351 // Set the position of the end-effector frame in the camera frame as identity
353 vpVelocityTwistMatrix cVe(cMe);
354 task.set_cVe(cVe);
355
356 // Set the Jacobian (expressed in the end-effector frame)
357 vpMatrix eJe;
358 robot.get_eJe(eJe);
359 task.set_eJe(eJe);
360
361 // we want to see a point on a point
362 for (unsigned int i = 0; i < 4; i++)
363 task.addFeature(p[i], pd[i]);
364
365 // set the gain
366 task.setLambda(1);
367
368 std::cout << "\nDisplay task information: " << std::endl;
369 task.print();
370
371 unsigned int iter = 0;
372 // loop
373 while (iter++ < 200) {
374 std::cout << "---------------------------------------------" << iter << std::endl;
376
377 // Set the Jacobian (expressed in the end-effector frame)
378 // Since q is modified eJe is modified
379 robot.get_eJe(eJe);
380 task.set_eJe(eJe);
381
382 // get the robot position
383 robot.getPosition(wMc);
384 // Compute the position of the object frame in the camera frame
385 cMo = wMc.inverse() * wMo;
386
387 // Compute new point position
388 for (unsigned int i = 0; i < 4; i++) {
389 point[i].track(cMo);
390 // retrieve x,y and Z of the vpPoint structure to compute the feature
391 vpFeatureBuilder::create(p[i], point[i]);
392 }
393
394 if (opt_display) {
395 vpDisplay::display(Iint);
396 vpDisplay::display(Iext);
397
398 vpServoDisplay::display(task, cam, Iint);
399 externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
400 vpDisplay::flush(Iint);
401 vpDisplay::flush(Iext);
402 }
403
404 // Compute the control law
405 v = task.computeControlLaw();
406
407 if (iter == 1) {
408 std::cout << "Display task information: " << std::endl;
409 task.print();
410 }
411
414
415 // Send the camera velocity to the controller
416 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
417 // Save velocities applied to the robot in the log file
418 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
419 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
420 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
421
422 std::cout << "v: " << v.t() << std::endl;
423
424 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
425
426 // Save feature error (s-s*) for the 4 feature points. For each feature
427 // point, we have 2 errors (along x and y axis). This error is
428 // expressed in meters in the camera frame
429 flog << (task.getError()).t() << " "; // s-s* for point 4
430 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
431
432 // Save current visual feature s = (rho,theta)
433 for (unsigned int i = 0; i < 4; i++) {
434 flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
435 }
436 // Save current position of the points
437 for (unsigned int i = 0; i < 4; i++) {
438 flog << point[i].get_x() << " " << point[i].get_y() << " ";
439 }
440 flog << std::endl;
441
442 if (iter == 1) {
443 vpImagePoint ip;
444 ip.set_i(10);
445 ip.set_j(10);
446
447 std::cout << "\nClick in the internal camera view to continue..." << std::endl;
448 vpDisplay::displayText(Iint, ip, "A click to continue...", vpColor::red);
449 vpDisplay::flush(Iint);
451 }
452 }
453
454 flog.close(); // Close the log file
455
456 // Display task information
457 task.print();
458
459 // Kill the task
460
461 std::cout << "Final robot position with respect to the object frame:\n";
462 cMo.print();
463
464 if (opt_display && opt_click_allowed) {
465 vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
466 vpDisplay::flush(Iint);
468 }
469#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
470 if (displayInt != nullptr) {
471 delete displayInt;
472 }
473 if (displayExt != nullptr) {
474 delete displayExt;
475 }
476#endif
477 return EXIT_SUCCESS;
478 }
479 catch (const vpException &e) {
480 std::cout << "Catch a ViSP exception: " << e << std::endl;
481#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
482 if (displayInt != nullptr) {
483 delete displayInt;
484 }
485 if (displayExt != nullptr) {
486 delete displayExt;
487 }
488#endif
489 return EXIT_FAILURE;
490 }
491}
492#else
493int main()
494{
495 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
496 << std::endl;
497 std::cout << "Tip if you are on a unix-like system:" << std::endl;
498 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
499 std::cout << "Tip if you are on a windows-like system:" << std::endl;
500 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
501 return EXIT_SUCCESS;
502}
503#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
static const vpColor white
Definition vpColor.h:193
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines 2D image point visual feature with polar coordinates described in corke09a.
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_j(double jj)
void set_i(double ii)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
double get_y() const
Get the point y coordinate in the image plane.
Definition vpPoint.cpp:429
double get_x() const
Get the point x coordinate in the image plane.
Definition vpPoint.cpp:427
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
interface with the image for feature display
void insert(vpForwardProjection &fp)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, unsigned int thickness=1)
@ CAMERA_FRAME
Definition vpRobot.h:81
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:183
@ FEATURE_CURRENT
Print the current features .
Definition vpServo.h:261
@ FEATURE_DESIRED
Print the desired features .
Definition vpServo.h:262
@ CURRENT
Definition vpServo.h:217
Class that defines the simplest robot: a free flying camera.
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.