Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuCylinder2DCamVelocityDisplay.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 on a cylinder.
32 */
33
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46
47#if defined(VISP_HAVE_DISPLAY) && \
48 (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
49
50#include <stdio.h>
51#include <stdlib.h>
52
53#include <visp3/core/vpCameraParameters.h>
54#include <visp3/core/vpCylinder.h>
55#include <visp3/core/vpHomogeneousMatrix.h>
56#include <visp3/core/vpImage.h>
57#include <visp3/core/vpMath.h>
58#include <visp3/gui/vpDisplayFactory.h>
59#include <visp3/io/vpParseArgv.h>
60#include <visp3/robot/vpSimulatorCamera.h>
61#include <visp3/visual_features/vpFeatureBuilder.h>
62#include <visp3/visual_features/vpFeatureLine.h>
63#include <visp3/vs/vpServo.h>
64#include <visp3/vs/vpServoDisplay.h>
65
66// List of allowed command line options
67#define GETOPTARGS "cdh"
68
69#ifdef ENABLE_VISP_NAMESPACE
70using namespace VISP_NAMESPACE_NAME;
71#endif
72
73void usage(const char *name, const char *badparam);
74bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
75
84void usage(const char *name, const char *badparam)
85{
86 fprintf(stdout, "\n\
87Simulation of a 2D visual servoing on a cylinder:\n\
88- eye-in-hand control law,\n\
89- velocity computed in the camera frame,\n\
90- display the camera view.\n\
91 \n\
92SYNOPSIS\n\
93 %s [-c] [-d] [-h]\n",
94 name);
95
96 fprintf(stdout, "\n\
97OPTIONS: Default\n\
98 \n\
99 -c\n\
100 Disable the mouse click. Useful to automate the \n\
101 execution of this program without human intervention.\n\
102 \n\
103 -d \n\
104 Turn off the display.\n\
105 \n\
106 -h\n\
107 Print the help.\n");
108
109 if (badparam)
110 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
111}
112
124bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
125{
126 const char *optarg_;
127 int c;
128 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
129
130 switch (c) {
131 case 'c':
132 click_allowed = false;
133 break;
134 case 'd':
135 display = false;
136 break;
137 case 'h':
138 usage(argv[0], nullptr);
139 return false;
140
141 default:
142 usage(argv[0], optarg_);
143 return false;
144 }
145 }
146
147 if ((c == 1) || (c == -1)) {
148 // standalone param or error
149 usage(argv[0], nullptr);
150 std::cerr << "ERROR: " << std::endl;
151 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
152 return false;
153 }
154
155 return true;
156}
157
158int main(int argc, const char **argv)
159{
160#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
161 std::shared_ptr<vpDisplay> display;
162#else
163 vpDisplay *display = nullptr;
164#endif
165 try {
166 bool opt_display = true;
167 bool opt_click_allowed = true;
168
169 // Read the command line options
170 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
171 return EXIT_FAILURE;
172 }
173
174 vpImage<unsigned char> I(512, 512, 255);
175
176 if (opt_display) {
177 try {
178 // Display size is automatically defined by the image (I) size
179#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
180 display = vpDisplayFactory::createDisplay(I, 100, 100, "Camera view...");
181#else
182 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Camera view...");
183#endif
184 // Display the image
185 // The image class has a member that specify a pointer toward
186 // the display that has been initialized in the display declaration
187 // therefore is is no longer necessary to make a reference to the
188 // display variable.
191 }
192 catch (...) {
193 vpERROR_TRACE("Error while displaying the image");
194#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
195 if (display != nullptr) {
196 delete display;
197 }
198#endif
199 return EXIT_FAILURE;
200 }
201 }
202
203 double px, py;
204 px = py = 600;
205 double u0, v0;
206 u0 = v0 = 256;
207
208 vpCameraParameters cam(px, py, u0, v0);
209
211 vpSimulatorCamera robot;
212
213 // sets the initial camera location
214 vpHomogeneousMatrix cMo(-0.2, 0.1, 2, vpMath::rad(5), vpMath::rad(5), vpMath::rad(20));
215
216 vpHomogeneousMatrix wMc, wMo;
217 robot.getPosition(wMc);
218 wMo = wMc * cMo; // Compute the position of the object in the world frame
219
220 // sets the final camera location (for simulation purpose)
221 vpHomogeneousMatrix cMod(0, 0, 1, vpMath::rad(-60), vpMath::rad(0), vpMath::rad(0));
222
223 // sets the cylinder coordinates in the world frame
224 vpCylinder cylinder(0, 1, 0, // direction
225 0, 0, 0, // point of the axis
226 0.1); // radius
227
228 // sets the desired position of the visual feature
229 cylinder.track(cMod);
230 cylinder.print();
231
232 vpFeatureLine ld[2];
233 for (unsigned int i = 0; i < 2; i++)
234 vpFeatureBuilder::create(ld[i], cylinder, i);
235
236 // computes the cylinder coordinates in the camera frame and its 2D
237 // coordinates sets the current position of the visual feature
238 cylinder.track(cMo);
239 cylinder.print();
240
241 vpFeatureLine l[2];
242 for (unsigned int i = 0; i < 2; i++) {
243 vpFeatureBuilder::create(l[i], cylinder, i);
244 l[i].print();
245 }
246
247 // define the task
248 // - we want an eye-in-hand control law
249 // - robot is controlled in the camera frame
251 // task.setInteractionMatrixType(vpServo::CURRENT,
252 // vpServo::PSEUDO_INVERSE) ;
253 // it can also be interesting to test these possibilities
254 // task.setInteractionMatrixType(vpServo::MEAN,
255 // vpServo::PSEUDO_INVERSE) ;
256 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
257 // task.setInteractionMatrixType(vpServo::DESIRED, vpServo::TRANSPOSE) ;
258 // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::TRANSPOSE) ;
259
260 // - we want to see 2 lines on 2 lines
261 task.addFeature(l[0], ld[0]);
262 task.addFeature(l[1], ld[1]);
263
264 vpServoDisplay::display(task, cam, I);
266
267 // Display task information
268 task.print();
269
270 if (opt_display && opt_click_allowed) {
271 std::cout << "\n\nClick in the camera view window to start..." << std::endl;
273 }
274
275 // - set the gain
276 task.setLambda(1);
277
278 // Display task information
279 task.print();
280
281 unsigned int iter = 0;
282 // loop
283 do {
284 std::cout << "---------------------------------------------" << iter++ << std::endl;
286
287 // get the robot position
288 robot.getPosition(wMc);
289 // Compute the position of the object frame in the camera frame
290 cMo = wMc.inverse() * wMo;
291
292 // new line position
293 // retrieve x,y and Z of the vpLine structure
294 cylinder.track(cMo);
295 // cylinder.print() ;
296 for (unsigned int i = 0; i < 2; i++) {
297 vpFeatureBuilder::create(l[i], cylinder, i);
298 // l[i].print() ;
299 }
300
301 if (opt_display) {
303 vpServoDisplay::display(task, cam, I);
305 }
306
307 // compute the control law
308 v = task.computeControlLaw();
309
310 // send the camera velocity to the controller
311 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
312
313 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
314
315 // vpDisplay::getClick(I) ;
316 } while ((task.getError()).sumSquare() > 1e-9);
317
318 if (opt_display && opt_click_allowed) {
319 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::black);
322 }
323
324 // Display task information
325 task.print();
326#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
327 if (display != nullptr) {
328 delete display;
329 }
330#endif
331 return EXIT_SUCCESS;
332 }
333 catch (const vpException &e) {
334 std::cout << "Catch a ViSP exception: " << e << std::endl;
335#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
336 if (display != nullptr) {
337 delete display;
338 }
339#endif
340 return EXIT_FAILURE;
341 }
342}
343
344#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
345int main()
346{
347 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
348 return EXIT_SUCCESS;
349}
350#else
351int main()
352{
353 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) or OpenCV functionalities to display "
354 "images..."
355 << std::endl;
356 std::cout << "Tip if you are on a unix-like system:" << std::endl;
357 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
358 std::cout << "Tip if you are on a windows-like system:" << std::endl;
359 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
360 return EXIT_SUCCESS;
361}
362#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
static const vpColor black
Definition vpColor.h:192
Class that defines a 3D cylinder in the object frame and allows forward projection of a 3D cylinder i...
Definition vpCylinder.h:101
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 a 2D line visual feature which is composed by two parameters that are and ,...
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
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)
@ 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_CAMERA
Definition vpServo.h:176
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ DESIRED
Definition vpServo.h:223
Class that defines the simplest robot: a free flying camera.
#define vpERROR_TRACE
Definition vpDebug.h:423
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.