Visual Servoing Platform version 3.5.0
servoSimuSphere2DCamVelocityDisplaySecondaryTask.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 on a sphere.
33 *
34 * Authors:
35 * Eric Marchand
36 * Fabien Spindler
37 *
38 *****************************************************************************/
39
50#include <stdio.h>
51#include <stdlib.h>
52
53#include <visp3/core/vpHomogeneousMatrix.h>
54#include <visp3/core/vpMath.h>
55#include <visp3/core/vpSphere.h>
56#include <visp3/gui/vpDisplayD3D.h>
57#include <visp3/gui/vpDisplayGDI.h>
58#include <visp3/gui/vpDisplayGTK.h>
59#include <visp3/gui/vpDisplayOpenCV.h>
60#include <visp3/gui/vpDisplayX.h>
61#include <visp3/gui/vpProjectionDisplay.h>
62#include <visp3/io/vpParseArgv.h>
63#include <visp3/robot/vpSimulatorCamera.h>
64#include <visp3/visual_features/vpFeatureBuilder.h>
65#include <visp3/visual_features/vpFeatureEllipse.h>
66#include <visp3/vs/vpServo.h>
67#include <visp3/vs/vpServoDisplay.h>
68
69// List of allowed command line options
70#define GETOPTARGS "cdho"
71
72void usage(const char *name, const char *badparam);
73bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
74
83void usage(const char *name, const char *badparam)
84{
85 fprintf(stdout, "\n\
86Simulation of a 2D visual servoing on a sphere:\n\
87- eye-in-hand control law,\n\
88- velocity computed in the camera frame,\n\
89- display the camera view,\n\
90- a secondary task is the added.\n\
91 \n\
92SYNOPSIS\n\
93 %s [-c] [-d] [-o] [-h]\n", name);
94
95 fprintf(stdout, "\n\
96OPTIONS: Default\n\
97 \n\
98 -c\n\
99 Disable the mouse click. Useful to automaze the \n\
100 execution of this program without humain intervention.\n\
101 \n\
102 -d \n\
103 Turn off the display.\n\
104 \n\
105 -o \n\
106 Disable new projection operator usage for secondary task.\n\
107 \n\
108 -h\n\
109 Print the help.\n");
110
111 if (badparam)
112 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
113}
114
128bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display, bool &new_proj_operator)
129{
130 const char *optarg_;
131 int c;
132 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
133
134 switch (c) {
135 case 'c':
136 click_allowed = false;
137 break;
138 case 'd':
139 display = false;
140 break;
141 case 'o':
142 new_proj_operator = false;
143 break;
144 case 'h':
145 usage(argv[0], NULL);
146 return false;
147
148 default:
149 usage(argv[0], optarg_);
150 return false;
151 }
152 }
153
154 if ((c == 1) || (c == -1)) {
155 // standalone param or error
156 usage(argv[0], NULL);
157 std::cerr << "ERROR: " << std::endl;
158 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
159 return false;
160 }
161
162 return true;
163}
164
165int main(int argc, const char **argv)
166{
167#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
168 try {
169 bool opt_display = true;
170 bool opt_click_allowed = true;
171 bool opt_new_proj_operator = true;
172
173 // Read the command line options
174 if (getOptions(argc, argv, opt_click_allowed, opt_display, opt_new_proj_operator) == false) {
175 return(EXIT_FAILURE);
176 }
177
178 vpImage<unsigned char> I(512, 512, 0);
179 vpImage<unsigned char> Iext(512, 512, 0);
180
181 // We open a window if a display is available
182#ifdef VISP_HAVE_DISPLAY
183# if defined VISP_HAVE_X11
184 vpDisplayX displayI;
185 vpDisplayX displayExt;
186# elif defined VISP_HAVE_GTK
187 vpDisplayGTK displayI;
188 vpDisplayGTK displayExt;
189# elif defined VISP_HAVE_GDI
190 vpDisplayGDI displayI;
191 vpDisplayGDI displayExt;
192# elif defined VISP_HAVE_OPENCV
193 vpDisplayOpenCV displayI;
194 vpDisplayOpenCV displayExt;
195# elif defined VISP_HAVE_D3D9
196 vpDisplayD3D displayI;
197 vpDisplayD3D displayExt;
198# endif
199#endif
200
201 if (opt_display) {
202#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
203 // Display size is automatically defined by the image (I) size
204 displayI.init(I, 100, 100, "Camera view...");
205 displayExt.init(Iext, 130 + static_cast<int>(I.getWidth()), 100, "External view");
206#endif
207 // Display the image
208 // The image class has a member that specify a pointer toward
209 // the display that has been initialized in the display declaration
210 // therefore is is no longuer necessary to make a reference to the
211 // display variable.
213 vpDisplay::display(Iext);
215 vpDisplay::flush(Iext);
216 }
217
218#ifdef VISP_HAVE_DISPLAY
219 vpProjectionDisplay externalview;
220#endif
221
222 double px = 600, py = 600;
223 double u0 = I.getWidth()/2., v0 = I.getHeight() / 2.;
224
225 vpCameraParameters cam(px, py, u0, v0);
226
227 vpServo task;
228 vpSimulatorCamera robot;
229
230 // sets the initial camera location
232 cMo[0][3] = 0.1;
233 cMo[1][3] = 0.2;
234 cMo[2][3] = 2;
235 // Compute the position of the object in the world frame
236 vpHomogeneousMatrix wMc, wMo;
237 robot.getPosition(wMc);
238 wMo = wMc * cMo;
239
241 cMod[0][3] = 0;
242 cMod[1][3] = 0;
243 cMod[2][3] = 1;
244
245 // sets the sphere coordinates in the world frame
246 vpSphere sphere;
247 sphere.setWorldCoordinates(0, 0, 0, 0.1);
248
249#ifdef VISP_HAVE_DISPLAY
250 externalview.insert(sphere);
251#endif
252 // sets the desired position of the visual feature
254 sphere.track(cMod);
255 vpFeatureBuilder::create(pd, sphere);
256
257 // computes the sphere coordinates in the camera frame and its 2D
258 // coordinates sets the current position of the visual feature
260 sphere.track(cMo);
261 vpFeatureBuilder::create(p, sphere);
262
263 // define the task
264 // - we want an eye-in-hand control law
265 // - robot is controlled in the camera frame
267
268 // we want to see a sphere on a sphere
269 std::cout << std::endl;
270 task.addFeature(p, pd);
271
272 // set the gain
273 task.setLambda(1);
274
275 // Set the point of view of the external view
276 vpHomogeneousMatrix cextMo(0, 0, 4, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
277
278 // Display the initial scene
279 vpServoDisplay::display(task, cam, I);
280#ifdef VISP_HAVE_DISPLAY
281 externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
282#endif
284 vpDisplay::flush(Iext);
285
286 // Display task information
287 task.print();
288
289 if (opt_display && opt_click_allowed) {
290 vpDisplay::displayText(I, 20, 20, "Click to start visual servo...", vpColor::white);
293 }
294
295 unsigned int iter = 0;
296 bool stop = false;
297 bool start_secondary_task = false;
298
299 // loop
300 while (iter++ < 2000 && !stop) {
301 std::cout << "---------------------------------------------" << iter << std::endl;
302
303 // get the robot position
304 robot.getPosition(wMc);
305 // Compute the position of the object frame in the camera frame
306 cMo = wMc.inverse() * wMo;
307
308 // new sphere position: retrieve x,y and Z of the vpSphere structure
309 sphere.track(cMo);
310 vpFeatureBuilder::create(p, sphere);
311
312 if (opt_display) {
314 vpDisplay::display(Iext);
315 vpServoDisplay::display(task, cam, I);
316#ifdef VISP_HAVE_DISPLAY
317 externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
318#endif
319 }
320
321 // compute the control law
323
324 // Wait primary task convergence before considering secondary task
325 if (task.getError().sumSquare() < 1e-6) {
326 start_secondary_task = true;
327 }
328
329 if (start_secondary_task) {
330 // Only 3 dof are required to achieve primary task: vz, wx, wy
331 // It remains 3 free dof (vx, vy, wz) that could be used in a secondary task for example to move arround the sphere
332 vpColVector de2dt(6);
333 de2dt[0] = 0.50; // vx = 0.50 m/s should also generate a motion on wy = (I-WpW)de2dt[4]
334 de2dt[1] = 0.25; // vy = 0.25 m/s should generate a motion on wx = (I-WpW)de2dt[3]
335 de2dt[2] = 1; // vz = 1 m/s should be zero in vz = (I-WpW)de2dt[2]
336 de2dt[5] = vpMath::rad(10); // wz = 10 rad/s should generate a motion on (I-WpW)de2dt[5]
337
338 std::cout << "de2dt :" << de2dt.t() << std::endl;
339 vpColVector sec = task.secondaryTask(de2dt, opt_new_proj_operator);
340 std::cout << "(I-WpW)de2dt :" << sec.t() << std::endl;
341
342 v += sec;
343
344 if (opt_display && opt_click_allowed) {
345 std::stringstream ss;
346 ss << std::string("New projection operator: ") + (opt_new_proj_operator ? std::string("yes (use option -o to use old one)") : std::string("no"));
347 vpDisplay::displayText(I, 20, 20, "Secondary task enabled: yes", vpColor::white);
348 vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::white);
349 }
350 }
351 else {
352 if (opt_display && opt_click_allowed) {
353 vpDisplay::displayText(I, 20, 20, "Secondary task enabled: no", vpColor::white);
354 }
355 }
356
357 // send the camera velocity to the controller
359
360 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
361
362 if (opt_display) {
363 vpDisplay::displayText(I, 60, 20, "Click to stop visual servo...", vpColor::white);
364 if (vpDisplay::getClick(I, false)) {
365 stop = true;
366 }
368 vpDisplay::flush(Iext);
369 }
370 }
371
372 if (opt_display && opt_click_allowed) {
374 vpServoDisplay::display(task, cam, I);
375 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
378 }
379
380 // Display task information
381 task.print();
382 return EXIT_SUCCESS;
383 } catch (const vpException &e) {
384 std::cout << "Catch a ViSP exception: " << e << std::endl;
385 return EXIT_FAILURE;
386 }
387#else
388 (void)argc;
389 (void)argv;
390 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
391 return EXIT_SUCCESS;
392#endif
393}
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
double sumSquare() const
vpRowVector t() const
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:107
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 ellipse visual feature.
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
static double rad(double deg)
Definition: vpMath.h:110
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
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)
@ 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)
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
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
vpColVector secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1446
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
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.
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition: vpSphere.h:83
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:62