Visual Servoing Platform version 3.5.0
servoSimuFourPoints2DCamVelocityDisplay.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 as visual feature.
33 *
34 * Authors:
35 * Eric Marchand
36 * Fabien Spindler
37 *
38 *****************************************************************************/
39
56#include <iostream>
57
58#include <visp3/core/vpConfig.h>
59
60#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) \
61 && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
62
63#include <stdio.h>
64#include <stdlib.h>
65
66#include <visp3/core/vpCameraParameters.h>
67#include <visp3/core/vpHomogeneousMatrix.h>
68#include <visp3/core/vpImage.h>
69#include <visp3/core/vpMath.h>
70#include <visp3/gui/vpDisplayGDI.h>
71#include <visp3/gui/vpDisplayGTK.h>
72#include <visp3/gui/vpDisplayOpenCV.h>
73#include <visp3/gui/vpDisplayX.h>
74#include <visp3/gui/vpProjectionDisplay.h>
75#include <visp3/io/vpParseArgv.h>
76#include <visp3/robot/vpSimulatorCamera.h>
77#include <visp3/visual_features/vpFeatureBuilder.h>
78#include <visp3/visual_features/vpFeaturePoint.h>
79#include <visp3/vs/vpServo.h>
80#include <visp3/vs/vpServoDisplay.h>
81
82// List of allowed command line options
83#define GETOPTARGS "cdh"
84
85void usage(const char *name, const char *badparam);
86bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
87
96void usage(const char *name, const char *badparam)
97{
98 fprintf(stdout, "\n\
99Tests a control law with the following characteristics:\n\
100- eye-in-hand control\n\
101- articular velocity are computed\n\
102- servo on 4 points,\n\
103- internal and external camera view displays.\n\
104 \n\
105SYNOPSIS\n\
106 %s [-c] [-d] [-h]\n", name);
107
108 fprintf(stdout, "\n\
109OPTIONS: Default\n\
110 -c\n\
111 Disable the mouse click. Useful to automaze the \n\
112 execution of this program without humain intervention.\n\
113 \n\
114 -d \n\
115 Turn off the display.\n\
116 \n\
117 -h\n\
118 Print the help.\n");
119
120 if (badparam)
121 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
122}
135bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
136{
137 const char *optarg_;
138 int c;
139 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
140
141 switch (c) {
142 case 'c':
143 click_allowed = false;
144 break;
145 case 'd':
146 display = false;
147 break;
148 case 'h':
149 usage(argv[0], NULL);
150 return false;
151
152 default:
153 usage(argv[0], optarg_);
154 return false;
155 }
156 }
157
158 if ((c == 1) || (c == -1)) {
159 // standalone param or error
160 usage(argv[0], NULL);
161 std::cerr << "ERROR: " << std::endl;
162 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
163 return false;
164 }
165
166 return true;
167}
168
169int main(int argc, const char **argv)
170{
171 try {
172 bool opt_click_allowed = true;
173 bool opt_display = true;
174
175 // Read the command line options
176 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
177 exit(-1);
178 }
179
180// We open two displays, one for the internal camera view, the other one for
181// the external view, using either X11, GTK or GDI.
182#if defined VISP_HAVE_X11
183 vpDisplayX displayInt;
184 vpDisplayX displayExt;
185#elif defined VISP_HAVE_GTK
186 vpDisplayGTK displayInt;
187 vpDisplayGTK displayExt;
188#elif defined VISP_HAVE_GDI
189 vpDisplayGDI displayInt;
190 vpDisplayGDI displayExt;
191#elif defined VISP_HAVE_OPENCV
192 vpDisplayOpenCV displayInt;
193 vpDisplayOpenCV displayExt;
194#endif
195
196 // open a display for the visualization
197
198 vpImage<unsigned char> Iint(300, 300, 0);
199 vpImage<unsigned char> Iext(300, 300, 0);
200
201 if (opt_display) {
202 displayInt.init(Iint, 0, 0, "Internal view");
203 displayExt.init(Iext, 330, 000, "External view");
204 }
205 vpProjectionDisplay externalview;
206
207 double px = 500, py = 500;
208 double u0 = 150, v0 = 160;
209
210 vpCameraParameters cam(px, py, u0, v0);
211
212 vpServo task;
213 vpSimulatorCamera robot;
214
215 std::cout << std::endl;
216 std::cout << "----------------------------------------------" << std::endl;
217 std::cout << " Test program for vpServo " << std::endl;
218 std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
219 std::cout << " Simulation " << std::endl;
220 std::cout << " task : servo 4 points " << std::endl;
221 std::cout << "----------------------------------------------" << std::endl;
222 std::cout << std::endl;
223
224 // sets the initial camera location
225 vpHomogeneousMatrix cMo(-0.1, -0.1, 1, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
226
227 // Compute the position of the object in the world frame
228 vpHomogeneousMatrix wMc, wMo;
229 robot.getPosition(wMc);
230 wMo = wMc * cMo;
231
232 vpHomogeneousMatrix cextMo(0, 0, 2, 0, 0, 0); // vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
233
234 // sets the point coordinates in the object frame
235 vpPoint point[4];
236 point[0].setWorldCoordinates(-0.1, -0.1, 0);
237 point[1].setWorldCoordinates(0.1, -0.1, 0);
238 point[2].setWorldCoordinates(0.1, 0.1, 0);
239 point[3].setWorldCoordinates(-0.1, 0.1, 0);
240
241 for (unsigned i = 0; i < 4; i++)
242 externalview.insert(point[i]);
243
244 // computes the point coordinates in the camera frame and its 2D
245 // coordinates
246 for (unsigned i = 0; i < 4; i++)
247 point[i].track(cMo);
248
249 // sets the desired position of the point
250 vpFeaturePoint p[4];
251 for (unsigned i = 0; i < 4; i++)
252 vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
253
254 // sets the desired position of the feature point s*
255 vpFeaturePoint pd[4];
256
257 pd[0].buildFrom(-0.1, -0.1, 1);
258 pd[1].buildFrom(0.1, -0.1, 1);
259 pd[2].buildFrom(0.1, 0.1, 1);
260 pd[3].buildFrom(-0.1, 0.1, 1);
261
262 // define the task
263 // - we want an eye-in-hand control law
264 // - articular velocity are computed
267
268 // Set the position of the end-effector frame in the camera frame as identity
270 vpVelocityTwistMatrix cVe(cMe);
271 task.set_cVe(cVe);
272
273 // Set the Jacobian (expressed in the end-effector frame
274 vpMatrix eJe;
275 robot.get_eJe(eJe);
276 task.set_eJe(eJe);
277
278 // we want to see a point on a point
279 for (unsigned i = 0; i < 4; i++)
280 task.addFeature(p[i], pd[i]);
281
282 // set the gain
283 task.setLambda(1);
284
285 // Display task information
286 task.print();
287
288 unsigned int iter = 0;
289 // loop
290 while (iter++ < 200) {
291 std::cout << "---------------------------------------------" << iter << std::endl;
292 vpColVector v;
293
294 // Set the Jacobian (expressed in the end-effector frame)
295 // since q is modified eJe is modified
296 robot.get_eJe(eJe);
297 task.set_eJe(eJe);
298
299 // get the robot position
300 robot.getPosition(wMc);
301 // Compute the position of the object frame in the camera frame
302 cMo = wMc.inverse() * wMo;
303
304 // update new point position and corresponding features
305 for (unsigned i = 0; i < 4; i++) {
306 point[i].track(cMo);
307 // retrieve x,y and Z of the vpPoint structure
308 vpFeatureBuilder::create(p[i], point[i]);
309 }
310 // since vpServo::MEAN interaction matrix is used, we need also to
311 // update the desired features at each iteration
312 pd[0].buildFrom(-0.1, -0.1, 1);
313 pd[1].buildFrom(0.1, -0.1, 1);
314 pd[2].buildFrom(0.1, 0.1, 1);
315 pd[3].buildFrom(-0.1, 0.1, 1);
316
317 if (opt_display) {
318 vpDisplay::display(Iint);
319 vpDisplay::display(Iext);
320 vpServoDisplay::display(task, cam, Iint);
321 externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
322 vpDisplay::flush(Iint);
323 vpDisplay::flush(Iext);
324 }
325
326 // compute the control law
327 v = task.computeControlLaw();
328
329 // send the camera velocity to the controller
331
332 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
333 }
334
335 // Display task information
336 task.print();
337
338 std::cout << "Final robot position with respect to the object frame:\n";
339 cMo.print();
340
341 if (opt_display && opt_click_allowed) {
342 vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
343 vpDisplay::flush(Iint);
345 }
346 return EXIT_SUCCESS;
347 } catch (const vpException &e) {
348 std::cout << "Catch a ViSP exception: " << e << std::endl;
349 return EXIT_FAILURE;
350 }
351}
352#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
353int main()
354{
355 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
356 return EXIT_SUCCESS;
357}
358#else
359int main()
360{
361 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
362 std::cout << "Tip if you are on a unix-like system:" << std::endl;
363 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
364 std::cout << "Tip if you are on a windows-like system:" << std::endl;
365 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
366 return EXIT_SUCCESS;
367}
368#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static const vpColor white
Definition: vpColor.h:212
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 a 2D point visual feature which is composed by two parameters that are the cartes...
void buildFrom(double x, double y, double Z)
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
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
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
@ MEAN
Definition: vpServo.h:190
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.