Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
simulateFourPoints2DPolarCamVelocity.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 visual servoing with visualization.
32 */
33
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46
47#ifdef VISP_HAVE_COIN3D_AND_GUI
48
49#include <visp3/ar/vpSimulator.h>
50#include <visp3/core/vpCameraParameters.h>
51#include <visp3/core/vpHomogeneousMatrix.h>
52#include <visp3/core/vpImage.h>
53#include <visp3/core/vpIoTools.h>
54#include <visp3/core/vpMath.h>
55#include <visp3/core/vpTime.h>
56#include <visp3/io/vpParseArgv.h>
57#include <visp3/robot/vpSimulatorCamera.h>
58#include <visp3/visual_features/vpFeatureBuilder.h>
59#include <visp3/visual_features/vpFeaturePointPolar.h>
60#include <visp3/vs/vpServo.h>
61
62#define GETOPTARGS "di:h"
63#define SAVE 0
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
78void usage(const char *name, const char *badparam, std::string ipath)
79{
80 fprintf(stdout, "\n\
81Simulation Servo 4points.\n\
82 \n\
83SYNOPSIS\n\
84 %s [-i <input image path>] [-d] [-h]\n",
85 name);
86
87 fprintf(stdout, "\n\
88OPTIONS: Default\n\
89 -i <input image path> %s\n\
90 Set image input path.\n\
91 From this path read \"iv/4points.iv\"\n\
92 cad model.\n\
93 Setting the VISP_INPUT_IMAGE_PATH environment\n\
94 variable produces the same behaviour than using\n\
95 this option.\n\
96 \n\
97 -d \n\
98 Disable the image display. This can be useful \n\
99 for automatic tests using crontab under Unix or \n\
100 using the task manager under Windows.\n\
101 \n\
102 -h\n\
103 Print the help.\n\n",
104 ipath.c_str());
105
106 if (badparam)
107 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
108}
109
125bool getOptions(int argc, const char **argv, std::string &ipath, bool &display)
126{
127 const char *optarg;
128 int c;
129 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
130
131 switch (c) {
132 case 'i':
133 ipath = optarg;
134 break;
135 case 'd':
136 display = false;
137 break;
138 case 'h':
139 usage(argv[0], nullptr, ipath);
140 return false;
141
142 default:
143 usage(argv[0], optarg, ipath);
144 return false;
145 }
146 }
147
148 if ((c == 1) || (c == -1)) {
149 // standalone param or error
150 usage(argv[0], nullptr, ipath);
151 std::cerr << "ERROR: " << std::endl;
152 std::cerr << " Bad argument " << optarg << std::endl << std::endl;
153 return false;
154 }
155
156 return true;
157}
158
159static void *mainLoop(void *_simu)
160{
161 vpSimulator *simu = static_cast<vpSimulator *>(_simu);
162 simu->initMainApplication();
163
165 vpSimulatorCamera robot;
166
167 float sampling_time = 0.040f; // Sampling period in second
168 robot.setSamplingTime(sampling_time);
169 robot.setMaxTranslationVelocity(4.);
170
171 // Sets the initial camera location
172 vpPoseVector vcMo;
173
174 vcMo[0] = 0.;
175 vcMo[1] = 0.;
176 vcMo[2] = 3;
177 vcMo[3] = 0;
178 vcMo[4] = vpMath::rad(0);
179 vcMo[5] = vpMath::rad(90);
180
182 vpHomogeneousMatrix wMo; // Set to identity
183 vpHomogeneousMatrix wMc; // Camera location in world frame
184 wMc = wMo * cMo.inverse();
185 robot.setPosition(wMc);
186 simu->setCameraPosition(cMo);
187
188 simu->getCameraPosition(cMo);
189 wMc = wMo * cMo.inverse();
190 robot.setPosition(wMc);
191
193
194 // Sets the point coordinates in the world frame
195 vpPoint point[4];
196 point[0].setWorldCoordinates(-0.1, -0.1, 0);
197 point[1].setWorldCoordinates(0.1, -0.1, 0);
198 point[2].setWorldCoordinates(0.1, 0.1, 0);
199 point[3].setWorldCoordinates(-0.1, 0.1, 0);
200
201 // Project : computes the point coordinates in the camera frame and its 2D
202 // coordinates
203 for (int i = 0; i < 4; i++) {
204 point[i].changeFrame(cMo); // Compute point coordinates in the camera frame
205 point[i].project(); // Compute desired point doordinates in the camera frame
206 }
207
208 // Sets the desired position of the point
210 for (int i = 0; i < 4; i++)
212 point[i]); // retrieve x,y and Z of the
213 // vpPoint structure to build the
214 // polar coordinates
215
216 std::cout << "s: \n";
217 for (int i = 0; i < 4; i++) {
218 printf("[%d] rho %f theta %f Z %f\n", i, p[i].get_rho(), p[i].get_theta(), p[i].get_Z());
219 }
220
221 // Sets the desired position of the point
222 vcMo[0] = 0;
223 vcMo[1] = 0;
224 vcMo[2] = 1;
225 vcMo[3] = vpMath::rad(0);
226 vcMo[4] = vpMath::rad(0);
227 vcMo[5] = vpMath::rad(0);
228
229 vpHomogeneousMatrix cMod(vcMo);
230
232 vpPoint pointd[4]; // Desired position of the points
233 pointd[0].setWorldCoordinates(-0.1, -0.1, 0);
234 pointd[1].setWorldCoordinates(0.1, -0.1, 0);
235 pointd[2].setWorldCoordinates(0.1, 0.1, 0);
236 pointd[3].setWorldCoordinates(-0.1, 0.1, 0);
237 for (int i = 0; i < 4; i++) {
238 pointd[i].changeFrame(cMod); // Compute desired point doordinates in the camera frame
239 pointd[i].project(); // Compute desired point doordinates in the camera frame
240
241 vpFeatureBuilder::create(pd[i], pointd[i]); // retrieve x,y and Z of the
242 // vpPoint structure to build
243 // the polar coordinates
244 }
245 std::cout << "s*: \n";
246 for (int i = 0; i < 4; i++) {
247 printf("[%d] rho %f theta %f Z %f\n", i, pd[i].get_rho(), pd[i].get_theta(), pd[i].get_Z());
248 }
249
250 // Define the task
251 // We want an eye-in-hand control law
252 // Articular velocity are computed
254 task.setInteractionMatrixType(vpServo::CURRENT);
255
256 // Set the position of the end-effector frame in the camera frame as identity
258 vpVelocityTwistMatrix cVe(cMe);
259 task.set_cVe(cVe);
260
261 // Set the Jacobian (expressed in the end-effector frame)
262 vpMatrix eJe;
263 robot.get_eJe(eJe);
264 task.set_eJe(eJe);
265
266 // We want to see a point on a point
267 for (int i = 0; i < 4; i++)
268 task.addFeature(p[i], pd[i]);
269
270 // Set the gain
271 task.setLambda(1.0);
272
273 // Display task information
274 task.print();
275
276 vpTime::wait(1000); // Sleep 1s
277
278 unsigned int iter = 0;
279 // Visual servo loop
280 while (iter++ < 200) {
281 double t = vpTime::measureTimeMs();
282
283 robot.get_eJe(eJe);
284 task.set_eJe(eJe);
285
286 wMc = robot.getPosition();
287 cMo = wMc.inverse() * wMo;
288 for (int i = 0; i < 4; i++) {
289 point[i].track(cMo);
290 vpFeatureBuilder::create(p[i], point[i]);
291 }
292
293 vpColVector v = task.computeControlLaw();
294 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
295
296 simu->setCameraPosition(cMo);
297
298 if (SAVE == 1) {
299 char name[FILENAME_MAX];
300 snprintf(name, FILENAME_MAX, "/tmp/image.%04u.external.png", iter);
301 std::cout << name << std::endl;
302 simu->write(name);
303 snprintf(name, FILENAME_MAX, "/tmp/image.%04u.internal.png", iter);
304 simu->write(name);
305 }
306
307 vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
308 }
309 // Display task information
310 task.print();
311
312 std::cout << "cMo:\n" << cMo << std::endl;
313 vpPoseVector pose(cMo);
314 std::cout << "final pose:\n" << pose.t() << std::endl;
315
316 simu->closeMainApplication();
317
318 void *a = nullptr;
319 return a;
320}
321
322int main(int argc, const char **argv)
323{
324 try {
325 std::string env_ipath;
326 std::string opt_ipath;
327 std::string ipath;
328 std::string filename;
329 bool opt_display = true;
330
331 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
332 // environment variable value
334
335 // Set the default input path
336 if (!env_ipath.empty())
337 ipath = env_ipath;
338
339 // Read the command line options
340 if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
341 return EXIT_FAILURE;
342 }
343
344 // Get the option values
345 if (!opt_ipath.empty())
346 ipath = opt_ipath;
347
348 // Compare ipath and env_ipath. If they differ, we take into account
349 // the input path coming from the command line option
350 if (!opt_ipath.empty() && !env_ipath.empty()) {
351 if (ipath != env_ipath) {
352 std::cout << std::endl << "WARNING: " << std::endl;
353 std::cout << " Since -i <visp image path=" << ipath << "> "
354 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
355 << " we skip the environment variable." << std::endl;
356 }
357 }
358
359 // Test if an input path is set
360 if (opt_ipath.empty() && env_ipath.empty()) {
361 usage(argv[0], nullptr, ipath);
362 std::cerr << std::endl << "ERROR:" << std::endl;
363 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
364 << " environment variable to specify the location of the " << std::endl
365 << " image path where test images are located." << std::endl
366 << std::endl;
367 return EXIT_FAILURE;
368 }
369
372 fMo[2][3] = 0;
373
374 if (opt_display) {
375 vpSimulator simu;
376 simu.initInternalViewer(300, 300);
377 simu.initExternalViewer(300, 300);
378
379 vpTime::wait(1000);
380 simu.setZoomFactor(1.0f);
381
382 // Load the cad model
383 filename = vpIoTools::createFilePath(ipath, "iv/4points.iv");
384 simu.load(filename.c_str());
385
388 simu.initApplication(&mainLoop);
389
390 simu.mainLoop();
391 }
392 return EXIT_SUCCESS;
393 }
394 catch (const vpException &e) {
395 std::cout << "Catch an exception: " << e << std::endl;
396 return EXIT_FAILURE;
397 }
398}
399
400#else
401int main()
402{
403 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
404 std::cout << "Tip:" << std::endl;
405 std::cout
406 << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example"
407 << std::endl;
408 return EXIT_SUCCESS;
409}
410#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
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 2D image point visual feature with polar coordinates described in corke09a.
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
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 changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpPoint.cpp:273
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
Implementation of a pose vector and operations on poses.
virtual void setSamplingTime(const double &delta_t)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:183
@ CURRENT
Definition vpServo.h:217
Class that defines the simplest robot: a free flying camera.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition vpSimulator.h:95
void load(const char *file_name)
load an iv file
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
virtual void mainLoop()
activate the mainloop
void setExternalCameraParameters(vpCameraParameters &cam)
set external camera parameters
void initMainApplication()
perform some initialization in the main program thread
void initApplication(void *(*start_routine)(void *))
begin the main program
void getCameraPosition(vpHomogeneousMatrix &_cMf)
get the camera position (from an homogeneous matrix)
void setZoomFactor(float zoom)
set the size of the camera/frame
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
void initExternalViewer(unsigned int nlig, unsigned int ncol)
initialize the external view
void write(const char *fileName)
virtual void initInternalViewer(unsigned int nlig, unsigned int ncol)
initialize the camera view
void closeMainApplication()
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)