Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuAfma6FourPoints2DCamVelocity.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 cartesian
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_THREADS) && defined(VISP_HAVE_DISPLAY) \
55 && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
56
57// We need to use threading capabilities. Thus on Unix-like
58// platforms, the libpthread third-party library need to be
59// installed. On Windows, we use the native threading capabilities.
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/vpDisplayFactory.h>
72#include <visp3/io/vpParseArgv.h>
73#include <visp3/robot/vpSimulatorAfma6.h>
74#include <visp3/visual_features/vpFeatureBuilder.h>
75#include <visp3/visual_features/vpFeaturePoint.h>
76#include <visp3/vs/vpServo.h>
77
78// List of allowed command line options
79#define GETOPTARGS "cdh"
80
81#ifdef ENABLE_VISP_NAMESPACE
82using namespace VISP_NAMESPACE_NAME;
83#endif
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",
107 name);
108
109 fprintf(stdout, "\n\
110OPTIONS: Default\n\
111 -c\n\
112 Disable the mouse click. Useful to automate the \n\
113 execution of this program without human 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], nullptr);
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], nullptr);
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#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
173 std::shared_ptr<vpDisplay> displayInt;
174#else
175 vpDisplay *displayInt = nullptr;
176#endif
177 try {
178 bool opt_click_allowed = true;
179 bool opt_display = true;
180
181 // Read the command line options
182 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
183 return EXIT_FAILURE;
184 }
185
186 // We open two displays, one for the internal camera view, the other one for
187 // the external view, using either X11, GTK or GDI.
188 vpImage<unsigned char> Iint(480, 640, 255);
189
190 if (opt_display) {
191 // open a display for the visualization
192#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
193 displayInt = vpDisplayFactory::createDisplay(Iint, 700, 0, "Internal view");
194#else
195 displayInt = vpDisplayFactory::allocateDisplay(Iint, 700, 0, "Internal view");
196#endif
197 }
198
200
201 std::cout << std::endl;
202 std::cout << "----------------------------------------------" << std::endl;
203 std::cout << " Test program for vpServo " << std::endl;
204 std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
205 std::cout << " Simulation " << std::endl;
206 std::cout << " task : servo 4 points " << std::endl;
207 std::cout << "----------------------------------------------" << std::endl;
208 std::cout << std::endl;
209
210 // sets the initial camera location
211 vpHomogeneousMatrix cMo(-0.05, -0.05, 0.7, vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
212
213 // sets the point coordinates in the object frame
214 vpPoint point[4];
215 point[0].setWorldCoordinates(-0.045, -0.045, 0);
216 point[3].setWorldCoordinates(-0.045, 0.045, 0);
217 point[2].setWorldCoordinates(0.045, 0.045, 0);
218 point[1].setWorldCoordinates(0.045, -0.045, 0);
219
220 // computes the point coordinates in the camera frame and its 2D
221 // coordinates
222 for (unsigned int i = 0; i < 4; i++)
223 point[i].track(cMo);
224
225 // sets the desired position of the point
226 vpFeaturePoint p[4];
227 for (unsigned int i = 0; i < 4; i++)
228 vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
229
230 // sets the desired position of the feature point s*
231 vpFeaturePoint pd[4];
232
233 // Desired pose
235
236 // Projection of the points
237 for (unsigned int i = 0; i < 4; i++)
238 point[i].track(cdMo);
239
240 for (unsigned int i = 0; i < 4; i++)
241 vpFeatureBuilder::create(pd[i], point[i]);
242
243 // define the task
244 // - we want an eye-in-hand control law
245 // - articular velocity are computed
247 task.setInteractionMatrixType(vpServo::DESIRED);
248
249 // we want to see a point on a point
250 for (unsigned int i = 0; i < 4; i++)
251 task.addFeature(p[i], pd[i]);
252
253 // set the gain
254 task.setLambda(0.8);
255
256 // Declaration of the robot
257 vpSimulatorAfma6 robot(opt_display);
258
259 // Initialise the robot and especially the camera
261 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
262
263 // Initialise the object for the display part*/
265
266 // Initialise the position of the object relative to the pose of the
267 // robot's camera
268 robot.initialiseObjectRelativeToCamera(cMo);
269
270 // Set the desired position (for the displaypart)
271 robot.setDesiredCameraPosition(cdMo);
272
273 // Get the internal robot's camera parameters
275 robot.getCameraParameters(cam, Iint);
276
277 if (opt_display) {
278 // Get the internal view
279 vpDisplay::display(Iint);
280 robot.getInternalView(Iint);
281 vpDisplay::flush(Iint);
282 }
283
284 // Display task information
285 task.print();
286
287 unsigned int iter = 0;
288 vpTRACE("\t loop");
289 while (iter++ < 500) {
290 std::cout << "---------------------------------------------" << iter << std::endl;
292
293 // Get the Time at the beginning of the loop
294 double t = vpTime::measureTimeMs();
295
296 // Get the current pose of the camera
297 cMo = robot.get_cMo();
298
299 if (iter == 1) {
300 std::cout << "Initial robot position with respect to the object frame:\n";
301 cMo.print();
302 }
303
304 // new point position
305 for (unsigned int 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
311 if (opt_display) {
312 // Get the internal view and display it
313 vpDisplay::display(Iint);
314 robot.getInternalView(Iint);
315 vpDisplay::flush(Iint);
316 }
317
318 if (opt_display && opt_click_allowed && iter == 1) {
319 // suppressed for automate test
320 std::cout << "Click in the internal view window to continue..." << std::endl;
322 }
323
324 // compute the control law
325 v = task.computeControlLaw();
326
327 // send the camera velocity to the controller
328 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
329
330 std::cout << "|| s - s* || " << (task.getError()).sumSquare() << std::endl;
331
332 // The main loop has a duration of 10 ms at minimum
333 vpTime::wait(t, 10);
334 }
335
336 // Display task information
337 task.print();
338
339 std::cout << "Final robot position with respect to the object frame:\n";
340 cMo.print();
341
342 if (opt_display && opt_click_allowed) {
343 // suppressed for automate test
344 std::cout << "Click in the internal view window to end..." << std::endl;
346 }
347
348#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
349 if (display != nullptr) {
350 delete display;
351 }
352#endif
353 return EXIT_SUCCESS;
354 }
355 catch (const vpException &e) {
356 std::cout << "Catch a ViSP exception: " << e << std::endl;
357#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
358 if (display != nullptr) {
359 delete display;
360 }
361#endif
362 return EXIT_FAILURE;
363 }
364#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
365 if (display != nullptr) {
366 delete display;
367 }
368#endif
369 return EXIT_SUCCESS;
370}
371#elif !(defined(VISP_HAVE_DISPLAY))
372int main()
373{
374 std::cout << "You do not have X11, or GDI (Graphical Device Interface) of OpenCV functionalities to display images..."
375 << std::endl;
376 std::cout << "Tip if you are on a unix-like system:" << std::endl;
377 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
378 std::cout << "Tip if you are on a windows-like system:" << std::endl;
379 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
380 return EXIT_SUCCESS;
381}
382#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
383int main()
384{
385 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
386 return EXIT_SUCCESS;
387}
388#else
389int main()
390{
391 std::cout << "You do not have threading capabilities" << std::endl;
392 std::cout << "Tip:" << std::endl;
393 std::cout << "- Install pthread, configure again ViSP using cmake and build again this example" << std::endl;
394 return EXIT_SUCCESS;
395}
396#endif
@ TOOL_CCMOP
Definition vpAfma6.h:125
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
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)
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 a 2D point visual feature which is composed by two parameters that are the cartes...
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double rad(double deg)
Definition vpMath.h:129
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
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ DESIRED
Definition vpServo.h:223
Simulator of Irisa's gantry robot named Afma6.
#define vpTRACE
Definition vpDebug.h:450
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)