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