Visual Servoing Platform version 3.5.0
servoSimuFourPoints2DPolarCamVelocityDisplay.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Simulation of a 2D visual servoing using 4 points with polar
33 * coordinates as visual feature.
34 *
35 * Authors:
36 * Fabien Spindler
37 *
38 *****************************************************************************/
39
56#include <visp3/core/vpConfig.h>
57#include <visp3/core/vpDebug.h>
58
59#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60
61#include <stdio.h>
62#include <stdlib.h>
63
64#include <visp3/core/vpCameraParameters.h>
65#include <visp3/core/vpHomogeneousMatrix.h>
66#include <visp3/core/vpImage.h>
67#include <visp3/core/vpImagePoint.h>
68#include <visp3/core/vpIoTools.h>
69#include <visp3/core/vpMath.h>
70#include <visp3/core/vpMeterPixelConversion.h>
71#include <visp3/gui/vpDisplayGDI.h>
72#include <visp3/gui/vpDisplayGTK.h>
73#include <visp3/gui/vpDisplayOpenCV.h>
74#include <visp3/gui/vpDisplayX.h>
75#include <visp3/gui/vpProjectionDisplay.h>
76#include <visp3/io/vpParseArgv.h>
77#include <visp3/robot/vpSimulatorCamera.h>
78#include <visp3/visual_features/vpFeatureBuilder.h>
79#include <visp3/visual_features/vpFeaturePointPolar.h>
80#include <visp3/vs/vpServo.h>
81#include <visp3/vs/vpServoDisplay.h>
82
83// List of allowed command line options
84#define GETOPTARGS "cdh"
85
86void usage(const char *name, const char *badparam);
87bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
88
97void usage(const char *name, const char *badparam)
98{
99 fprintf(stdout, "\n\
100Tests a control law with the following characteristics:\n\
101- eye-in-hand control\n\
102- articular velocity are computed\n\
103- servo on 4 points,\n\
104- internal and external camera view displays.\n\
105\n\
106SYNOPSIS\n\
107 %s [-c] [-d] [-h]\n", name);
108
109 fprintf(stdout, "\n\
110OPTIONS: Default\n\
111 -c\n\
112 Disable the mouse click. Useful to automaze the \n\
113 execution of this program without humain intervention.\n\
114\n\
115 -d \n\
116 Turn off the display.\n\
117\n\
118 -h\n\
119 Print the help.\n");
120
121 if (badparam)
122 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123}
136bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
137{
138 const char *optarg_;
139 int c;
140 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141
142 switch (c) {
143 case 'c':
144 click_allowed = false;
145 break;
146 case 'd':
147 display = false;
148 break;
149 case 'h':
150 usage(argv[0], NULL);
151 return false;
152
153 default:
154 usage(argv[0], optarg_);
155 return false;
156 }
157 }
158
159 if ((c == 1) || (c == -1)) {
160 // standalone param or error
161 usage(argv[0], NULL);
162 std::cerr << "ERROR: " << std::endl;
163 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
164 return false;
165 }
166
167 return true;
168}
169
170int main(int argc, const char **argv)
171{
172 try {
173 // Log file creation in /tmp/$USERNAME/log.dat
174 // This file contains by line:
175 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
176 // - the 6 mesured camera velocities (m/s, rad/s)
177 // - the 6 mesured joint positions (m, rad)
178 // - the 8 values of s - s*
179 std::string username;
180 // Get the user login name
181 vpIoTools::getUserName(username);
182
183 // Create a log filename to save velocities...
184 std::string logdirname;
185#if defined(_WIN32)
186 logdirname = "C:/temp/" + username;
187#else
188 logdirname = "/tmp/" + username;
189#endif
190
191 // Test if the output path exist. If no try to create it
192 if (vpIoTools::checkDirectory(logdirname) == false) {
193 try {
194 // Create the dirname
195 vpIoTools::makeDirectory(logdirname);
196 } catch (...) {
197 std::cerr << std::endl << "ERROR:" << std::endl;
198 std::cerr << " Cannot create " << logdirname << std::endl;
199 exit(-1);
200 }
201 }
202 std::string logfilename;
203 logfilename = logdirname + "/log.dat";
204
205 // Open the log file name
206 std::ofstream flog(logfilename.c_str());
207
208 bool opt_click_allowed = true;
209 bool opt_display = true;
210
211 // Read the command line options
212 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
213 exit(-1);
214 }
215
216// We open two displays, one for the internal camera view, the other one for
217// the external view, using either X11, GTK or GDI.
218#if defined VISP_HAVE_X11
219 vpDisplayX displayInt;
220 vpDisplayX displayExt;
221#elif defined VISP_HAVE_GTK
222 vpDisplayGTK displayInt;
223 vpDisplayGTK displayExt;
224#elif defined VISP_HAVE_GDI
225 vpDisplayGDI displayInt;
226 vpDisplayGDI displayExt;
227#elif defined VISP_HAVE_OPENCV
228 vpDisplayOpenCV displayInt;
229 vpDisplayOpenCV displayExt;
230#endif
231
232 // open a display for the visualization
233
234 vpImage<unsigned char> Iint(300, 300, 0);
235 vpImage<unsigned char> Iext(300, 300, 0);
236
237 if (opt_display) {
238 displayInt.init(Iint, 0, 0, "Internal view");
239 displayExt.init(Iext, 330, 000, "External view");
240 }
241 vpProjectionDisplay externalview;
242
243 double px = 500, py = 500;
244 double u0 = 150, v0 = 160;
245
246 vpCameraParameters cam(px, py, u0, v0);
247
248 vpServo task;
249 vpSimulatorCamera robot;
250
251 std::cout << std::endl;
252 std::cout << "----------------------------------------------" << std::endl;
253 std::cout << " Test program for vpServo " << std::endl;
254 std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
255 std::cout << " Simulation " << std::endl;
256 std::cout << " task : servo 4 points " << std::endl;
257 std::cout << "----------------------------------------------" << std::endl;
258 std::cout << std::endl;
259
260// #define TRANS_Z_PURE
261// #define TRANS_X_PURE
262// #define ROT_Z_PURE
263// #define ROT_X_PURE
264#define COMPLEX
265//#define PROBLEM
266
267#if defined(TRANS_Z_PURE)
268 // sets the initial camera location
270 // sets the desired camera location
271 vpHomogeneousMatrix cMod(0, 0, 2, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
272#elif defined(TRANS_X_PURE)
273 // sets the initial camera location
274 vpHomogeneousMatrix cMo(0.3, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
275 // sets the desired camera location
276 vpHomogeneousMatrix cMod(0.5, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
277
278#elif defined(ROT_Z_PURE)
279 // sets the initial camera location
281 // sets the desired camera location
282 vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
283
284#elif defined(ROT_X_PURE)
285 // sets the initial camera location
287 // sets the desired camera location
288 vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
289
290#elif defined(COMPLEX)
291 // sets the initial camera location
292 vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
293 // sets the desired camera location
294 vpHomogeneousMatrix cMod(0, 0, 2.5, vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
295
296#elif defined(PROBLEM)
297 // Bad behavior with an interaction matrix computed from the desired
298 // features sets the initial camera location
299 vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
300 // sets the desired camera location
301 vpHomogeneousMatrix cMod(0.4, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
302
303#endif
304 // Compute the position of the object in the world frame
305 vpHomogeneousMatrix wMc, wMo;
306 robot.getPosition(wMc);
307 wMo = wMc * cMo;
308
309 vpHomogeneousMatrix cextMo(0, 0, 6, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
310
311 // sets the point coordinates in the object frame
312 vpPoint point[4];
313 point[0].setWorldCoordinates(-0.25, -0.25, 0);
314 point[1].setWorldCoordinates(0.25, -0.25, 0);
315 point[2].setWorldCoordinates(0.25, 0.25, 0);
316 point[3].setWorldCoordinates(-0.25, 0.25, 0);
317
318 for (unsigned int i = 0; i < 4; i++)
319 externalview.insert(point[i]);
320
321 // sets the desired position of the feature point s*"
323
324 // computes the point coordinates in the desired camera frame and
325 // its 2D coordinates
326 for (unsigned int i = 0; i < 4; i++) {
327 point[i].track(cMod);
328 // Computes the polar coordinates from the image point
329 // cartesian coordinates
330 vpFeatureBuilder::create(pd[i], point[i]);
331 }
332
333 // computes the point coordinates in the camera frame and its 2D
334 // coordinates
335 for (unsigned int i = 0; i < 4; i++)
336 point[i].track(cMo);
337
338 // sets the desired position of the point
340 for (unsigned int i = 0; i < 4; i++) {
341 // retrieve x,y and Z of the vpPoint structure to initialize the
342 // visual feature
343 vpFeatureBuilder::create(p[i], point[i]);
344 }
345
346 // Define the task;
347 // - we want an eye-in-hand control law
348 // - articular velocity are computed
350 // task.setInteractionMatrixType(vpServo::MEAN) ;
351 // task.setInteractionMatrixType(vpServo::DESIRED) ;
353
354 // Set the position of the end-effector frame in the camera frame as identity
356 vpVelocityTwistMatrix cVe(cMe);
357 task.set_cVe(cVe);
358
359 // Set the Jacobian (expressed in the end-effector frame)
360 vpMatrix eJe;
361 robot.get_eJe(eJe);
362 task.set_eJe(eJe);
363
364 // we want to see a point on a point
365 for (unsigned int i = 0; i < 4; i++)
366 task.addFeature(p[i], pd[i]);
367
368 // set the gain
369 task.setLambda(1);
370
371 std::cout << "\nDisplay task information: " << std::endl;
372 task.print();
373
374 unsigned int iter = 0;
375 // loop
376 while (iter++ < 200) {
377 std::cout << "---------------------------------------------" << iter << std::endl;
378 vpColVector v;
379
380 // Set the Jacobian (expressed in the end-effector frame)
381 // Since q is modified eJe is modified
382 robot.get_eJe(eJe);
383 task.set_eJe(eJe);
384
385 // get the robot position
386 robot.getPosition(wMc);
387 // Compute the position of the object frame in the camera frame
388 cMo = wMc.inverse() * wMo;
389
390 // Compute new point position
391 for (unsigned int i = 0; i < 4; i++) {
392 point[i].track(cMo);
393 // retrieve x,y and Z of the vpPoint structure to compute the feature
394 vpFeatureBuilder::create(p[i], point[i]);
395 }
396
397 if (opt_display) {
398 vpDisplay::display(Iint);
399 vpDisplay::display(Iext);
400
401 vpServoDisplay::display(task, cam, Iint);
402 externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
403 vpDisplay::flush(Iint);
404 vpDisplay::flush(Iext);
405 }
406
407 // Compute the control law
408 v = task.computeControlLaw();
409
410 if (iter == 1) {
411 std::cout << "Display task information: " << std::endl;
412 task.print();
413 }
414
417
418 // Send the camera velocity to the controller
420 // Save velocities applied to the robot in the log file
421 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
422 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
423 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
424
425 std::cout << "v: " << v.t() << std::endl;
426
427 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
428
429 // Save feature error (s-s*) for the 4 feature points. For each feature
430 // point, we have 2 errors (along x and y axis). This error is
431 // expressed in meters in the camera frame
432 flog << (task.getError()).t() << " "; // s-s* for point 4
433 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
434
435 // Save current visual feature s = (rho,theta)
436 for (unsigned int i = 0; i < 4; i++) {
437 flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
438 }
439 // Save current position of the points
440 for (unsigned int i = 0; i < 4; i++) {
441 flog << point[i].get_x() << " " << point[i].get_y() << " ";
442 }
443 flog << std::endl;
444
445 if (iter == 1) {
446 vpImagePoint ip;
447 ip.set_i(10);
448 ip.set_j(10);
449
450 std::cout << "\nClick in the internal camera view to continue..." << std::endl;
451 vpDisplay::displayText(Iint, ip, "A click to continue...", vpColor::red);
452 vpDisplay::flush(Iint);
454 }
455 }
456
457 flog.close(); // Close the log file
458
459 // Display task information
460 task.print();
461
462 // Kill the task
463
464 std::cout << "Final robot position with respect to the object frame:\n";
465 cMo.print();
466
467 if (opt_display && opt_click_allowed) {
468 vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
469 vpDisplay::flush(Iint);
471 }
472 return EXIT_SUCCESS;
473 } catch (const vpException &e) {
474 std::cout << "Catch a ViSP exception: " << e << std::endl;
475 return EXIT_FAILURE;
476 }
477}
478#else
479int main()
480{
481 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
482 std::cout << "Tip if you are on a unix-like system:" << std::endl;
483 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
484 std::cout << "Tip if you are on a windows-like system:" << std::endl;
485 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
486 return EXIT_SUCCESS;
487}
488#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
vpRowVector t() const
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 emited by ViSP classes.
Definition: vpException.h:72
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines 2D image point visual feature with polar coordinates described in .
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 ...
Definition: vpImagePoint.h:88
void set_j(double jj)
Definition: vpImagePoint.h:177
void set_i(double ii)
Definition: vpImagePoint.h:166
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
static double rad(double deg)
Definition: vpMath.h:110
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:472
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:470
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
interface with the image for feature display
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)
void insert(vpForwardProjection &fp)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void get_eJe(vpMatrix &eJe)
@ CAMERA_FRAME
Definition: vpRobot.h:82
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:159
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
void setLambda(double c)
Definition: vpServo.h:404
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
@ FEATURE_CURRENT
Definition: vpServo.h:210
@ FEATURE_DESIRED
Definition: vpServo.h:211
@ CURRENT
Definition: vpServo.h:182
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
Class that defines the simplest robot: a free flying camera.