Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuSquareLine2DCamVelocityDisplay.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
42
43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpDebug.h>
45
46#if defined(VISP_HAVE_DISPLAY) && \
47 (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
48
49#include <stdio.h>
50#include <stdlib.h>
51
52#include <visp3/core/vpCameraParameters.h>
53#include <visp3/core/vpHomogeneousMatrix.h>
54#include <visp3/core/vpImage.h>
55#include <visp3/core/vpLine.h>
56#include <visp3/core/vpMath.h>
57#include <visp3/gui/vpDisplayFactory.h>
58#include <visp3/io/vpParseArgv.h>
59#include <visp3/robot/vpSimulatorCamera.h>
60#include <visp3/visual_features/vpFeatureBuilder.h>
61#include <visp3/visual_features/vpFeatureLine.h>
62#include <visp3/vs/vpServo.h>
63#include <visp3/vs/vpServoDisplay.h>
64
65// List of allowed command line options
66#define GETOPTARGS "cdh"
67
68#ifdef ENABLE_VISP_NAMESPACE
69using namespace VISP_NAMESPACE_NAME;
70#endif
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 2D a visual servoing on a line:\n\
87- eye-in-hand control law,\n\
88- velocity computed in the camera frame,\n\
89- display the camera view.\n\
90 \n\
91SYNOPSIS\n\
92 %s [-c] [-d] [-h]\n",
93 name);
94
95 fprintf(stdout, "\n\
96OPTIONS: Default\n\
97 \n\
98 -c\n\
99 Disable the mouse click. Useful to automate the \n\
100 execution of this program without human intervention.\n\
101 \n\
102 -d \n\
103 Turn off the display.\n\
104 \n\
105 -h\n\
106 Print the help.\n");
107
108 if (badparam)
109 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
110}
111
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, 0);
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 return EXIT_FAILURE;
195 }
196 }
197
198 // Set the camera parameters
199 double px, py;
200 px = py = 600;
201 double u0, v0;
202 u0 = v0 = 256;
203
204 vpCameraParameters cam(px, py, u0, v0);
205
207 vpSimulatorCamera robot;
208
209 // sets the initial camera location
210 vpHomogeneousMatrix cMo(0.2, 0.2, 1, vpMath::rad(45), vpMath::rad(45), vpMath::rad(125));
211
212 // Compute the position of the object in the world frame
213 vpHomogeneousMatrix wMc, wMo;
214 robot.getPosition(wMc);
215 wMo = wMc * cMo;
216
217 // sets the final camera location (for simulation purpose)
218 vpHomogeneousMatrix cMod(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
219
220 int nbline = 4;
221
222 // sets the line coordinates (2 planes) in the world frame
223 vpLine line[4];
224 line[0].setWorldCoordinates(1, 0, 0, 0.05, 0, 0, 1, 0);
225 line[1].setWorldCoordinates(0, 1, 0, 0.05, 0, 0, 1, 0);
226 line[2].setWorldCoordinates(1, 0, 0, -0.05, 0, 0, 1, 0);
227 line[3].setWorldCoordinates(0, 1, 0, -0.05, 0, 0, 1, 0);
228
229 vpFeatureLine ld[4];
230 vpFeatureLine l[4];
231
232 // sets the desired position of the visual feature
233 for (int i = 0; i < nbline; i++) {
234 line[i].track(cMod);
235 line[i].print();
236
237 vpFeatureBuilder::create(ld[i], line[i]);
238 }
239
240 // computes the line coordinates in the camera frame and its 2D
241 // coordinates sets the current position of the visual feature
242 for (int i = 0; i < nbline; i++) {
243 line[i].track(cMo);
244 line[i].print();
245
246 vpFeatureBuilder::create(l[i], line[i]);
247 l[i].print();
248 }
249
250 // define the task
251 // - we want an eye-in-hand control law
252 // - robot is controlled in the camera frame
254 task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE);
255 // It could be also interesting to test the following tasks
256 // task.setInteractionMatrixType(vpServo::DESIRED,
257 // vpServo::PSEUDO_INVERSE); task.setInteractionMatrixType(vpServo::MEAN,
258 // vpServo::PSEUDO_INVERSE);
259
260 // we want to see a four lines on four lines
261 for (int i = 0; i < nbline; i++)
262 task.addFeature(l[i], ld[i]);
263
265 vpServoDisplay::display(task, cam, I);
267
268 // set the gain
269 task.setLambda(1);
270
271 // Display task information
272 task.print();
273
274 if (opt_display && opt_click_allowed) {
275 std::cout << "\n\nClick in the camera view window to start..." << std::endl;
277 }
278
279 unsigned int iter = 0;
280 // loop
281 while (iter++ < 200) {
282 std::cout << "---------------------------------------------" << iter << std::endl;
284
285 // get the robot position
286 robot.getPosition(wMc);
287 // Compute the position of the object frame in the camera frame
288 cMo = wMc.inverse() * wMo;
289
290 // new line position: retrieve x,y and Z of the vpLine structure
291 for (int i = 0; i < nbline; i++) {
292 line[i].track(cMo);
293 vpFeatureBuilder::create(l[i], line[i]);
294 }
295
296 if (opt_display) {
298 vpServoDisplay::display(task, cam, I);
300 }
301
302 // compute the control law
303 v = task.computeControlLaw();
304
305 // send the camera velocity to the controller
306 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
307
308 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
309 }
310
311 if (opt_display && opt_click_allowed) {
312 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
315 }
316
317 // Display task information
318 task.print();
319#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
320 if (display != nullptr) {
321 delete display;
322 }
323#endif
324 return EXIT_SUCCESS;
325 }
326 catch (const vpException &e) {
327 std::cout << "Catch a ViSP exception: " << e << std::endl;
328#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
329 if (display != nullptr) {
330 delete display;
331 }
332#endif
333 return EXIT_FAILURE;
334 }
335}
336
337#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
338int main()
339{
340 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
341 return EXIT_SUCCESS;
342}
343#else
344int main()
345{
346 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) or OpenCV functionalities to display "
347 "images..."
348 << std::endl;
349 std::cout << "Tip if you are on a unix-like system:" << std::endl;
350 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
351 std::cout << "Tip if you are on a windows-like system:" << std::endl;
352 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
353 return EXIT_SUCCESS;
354}
355#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
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ CURRENT
Definition vpServo.h:217
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.