Visual Servoing Platform version 3.5.0
simulateFourPoints2DPolarCamVelocity.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Simulation of a visual servoing with visualization.
33 *
34 * Authors:
35 * Eric Marchand
36 * Fabien Spindler
37 *
38 *****************************************************************************/
39
50#include <visp3/core/vpConfig.h>
51#include <visp3/core/vpDebug.h>
52
53#ifdef VISP_HAVE_COIN3D_AND_GUI
54
55#include <visp3/ar/vpSimulator.h>
56#include <visp3/core/vpCameraParameters.h>
57#include <visp3/core/vpHomogeneousMatrix.h>
58#include <visp3/core/vpImage.h>
59#include <visp3/core/vpIoTools.h>
60#include <visp3/core/vpMath.h>
61#include <visp3/core/vpTime.h>
62#include <visp3/io/vpParseArgv.h>
63#include <visp3/robot/vpSimulatorCamera.h>
64#include <visp3/visual_features/vpFeatureBuilder.h>
65#include <visp3/visual_features/vpFeaturePointPolar.h>
66#include <visp3/vs/vpServo.h>
67
68#define GETOPTARGS "di:h"
69#define SAVE 0
70
80void usage(const char *name, const char *badparam, std::string ipath)
81{
82 fprintf(stdout, "\n\
83Simulation Servo 4points.\n\
84 \n\
85SYNOPSIS\n\
86 %s [-i <input image path>] [-d] [-h]\n", name);
87
88 fprintf(stdout, "\n\
89OPTIONS: Default\n\
90 -i <input image path> %s\n\
91 Set image input path.\n\
92 From this path read \"iv/4points.iv\"\n\
93 cad model.\n\
94 Setting the VISP_INPUT_IMAGE_PATH environment\n\
95 variable produces the same behaviour than using\n\
96 this option.\n\
97 \n\
98 -d \n\
99 Disable the image display. This can be useful \n\
100 for automatic tests using crontab under Unix or \n\
101 using the task manager under Windows.\n\
102 \n\
103 -h\n\
104 Print the help.\n\n", 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], NULL, ipath);
140 return false;
141 break;
142
143 default:
144 usage(argv[0], optarg, ipath);
145 return false;
146 break;
147 }
148 }
149
150 if ((c == 1) || (c == -1)) {
151 // standalone param or error
152 usage(argv[0], NULL, ipath);
153 std::cerr << "ERROR: " << std::endl;
154 std::cerr << " Bad argument " << optarg << std::endl << std::endl;
155 return false;
156 }
157
158 return true;
159}
160
161static void *mainLoop(void *_simu)
162{
163 vpSimulator *simu = static_cast<vpSimulator *>(_simu);
164 simu->initMainApplication();
165
166 vpServo task;
167 vpSimulatorCamera robot;
168
169 float sampling_time = 0.040f; // Sampling period in second
170 robot.setSamplingTime(sampling_time);
172
173 // Sets the initial camera location
174 vpPoseVector vcMo;
175
176 vcMo[0] = 0.;
177 vcMo[1] = 0.;
178 vcMo[2] = 3;
179 vcMo[3] = 0;
180 vcMo[4] = vpMath::rad(0);
181 vcMo[5] = vpMath::rad(90);
182
183 vpHomogeneousMatrix cMo(vcMo);
184 vpHomogeneousMatrix wMo; // Set to identity
185 vpHomogeneousMatrix wMc; // Camera location in world frame
186 wMc = wMo * cMo.inverse();
187 robot.setPosition(wMc);
188 simu->setCameraPosition(cMo);
189
190 simu->getCameraPosition(cMo);
191 wMc = wMo * cMo.inverse();
192 robot.setPosition(wMc);
193
195
196 // Sets the point coordinates in the world frame
197 vpPoint point[4];
198 point[0].setWorldCoordinates(-0.1, -0.1, 0);
199 point[1].setWorldCoordinates(0.1, -0.1, 0);
200 point[2].setWorldCoordinates(0.1, 0.1, 0);
201 point[3].setWorldCoordinates(-0.1, 0.1, 0);
202
203 // Project : computes the point coordinates in the camera frame and its 2D
204 // coordinates
205 for (int i = 0; i < 4; i++) {
206 point[i].changeFrame(cMo); // Compute point coordinates in the camera frame
207 point[i].project(); // Compute desired point doordinates in the camera frame
208 }
209
210 // Sets the desired position of the point
212 for (int i = 0; i < 4; i++)
214 point[i]); // retrieve x,y and Z of the
215 // vpPoint structure to build the
216 // polar coordinates
217
218 std::cout << "s: \n";
219 for (int i = 0; i < 4; i++) {
220 printf("[%d] rho %f theta %f Z %f\n", i, p[i].get_rho(), p[i].get_theta(), p[i].get_Z());
221 }
222
223 // Sets the desired position of the point
224 vcMo[0] = 0;
225 vcMo[1] = 0;
226 vcMo[2] = 1;
227 vcMo[3] = vpMath::rad(0);
228 vcMo[4] = vpMath::rad(0);
229 vcMo[5] = vpMath::rad(0);
230
231 vpHomogeneousMatrix cMod(vcMo);
232
234 vpPoint pointd[4]; // Desired position of the points
235 pointd[0].setWorldCoordinates(-0.1, -0.1, 0);
236 pointd[1].setWorldCoordinates(0.1, -0.1, 0);
237 pointd[2].setWorldCoordinates(0.1, 0.1, 0);
238 pointd[3].setWorldCoordinates(-0.1, 0.1, 0);
239 for (int i = 0; i < 4; i++) {
240 pointd[i].changeFrame(cMod); // Compute desired point doordinates in the camera frame
241 pointd[i].project(); // Compute desired point doordinates in the camera frame
242
243 vpFeatureBuilder::create(pd[i], pointd[i]); // retrieve x,y and Z of the
244 // vpPoint structure to build
245 // the polar coordinates
246 }
247 std::cout << "s*: \n";
248 for (int i = 0; i < 4; i++) {
249 printf("[%d] rho %f theta %f Z %f\n", i, pd[i].get_rho(), pd[i].get_theta(), pd[i].get_Z());
250 }
251
252 // Define the task
253 // We want an eye-in-hand control law
254 // Articular velocity are computed
257
258 // Set the position of the end-effector frame in the camera frame as identity
260 vpVelocityTwistMatrix cVe(cMe);
261 task.set_cVe(cVe);
262
263 // Set the Jacobian (expressed in the end-effector frame)
264 vpMatrix eJe;
265 robot.get_eJe(eJe);
266 task.set_eJe(eJe);
267
268 // We want to see a point on a point
269 for (int i = 0; i < 4; i++)
270 task.addFeature(p[i], pd[i]);
271
272 // Set the gain
273 task.setLambda(1.0);
274
275 // Display task information
276 task.print();
277
278 vpTime::wait(1000); // Sleep 1s
279
280 unsigned int iter = 0;
281 // Visual servo loop
282 while (iter++ < 200) {
283 double t = vpTime::measureTimeMs();
284
285 robot.get_eJe(eJe);
286 task.set_eJe(eJe);
287
288 wMc = robot.getPosition();
289 cMo = wMc.inverse() * wMo;
290 for (int i = 0; i < 4; i++) {
291 point[i].track(cMo);
292 vpFeatureBuilder::create(p[i], point[i]);
293 }
294
297
298 simu->setCameraPosition(cMo);
299
300 if (SAVE == 1) {
301 char name[FILENAME_MAX];
302 sprintf(name, "/tmp/image.%04u.external.png", iter);
303 std::cout << name << std::endl;
304 simu->write(name);
305 sprintf(name, "/tmp/image.%04u.internal.png", iter);
306 simu->write(name);
307 }
308
309 vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
310 }
311 // Display task information
312 task.print();
313
314 std::cout << "cMo:\n" << cMo << std::endl;
315 vpPoseVector pose(cMo);
316 std::cout << "final pose:\n" << pose.t() << std::endl;
317
318 simu->closeMainApplication();
319
320 void *a = NULL;
321 return a;
322}
323
324int main(int argc, const char **argv)
325{
326 try {
327 std::string env_ipath;
328 std::string opt_ipath;
329 std::string ipath;
330 std::string filename;
331 bool opt_display = true;
332
333 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
334 // environment variable value
336
337 // Set the default input path
338 if (!env_ipath.empty())
339 ipath = env_ipath;
340
341 // Read the command line options
342 if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
343 exit(-1);
344 }
345
346 // Get the option values
347 if (!opt_ipath.empty())
348 ipath = opt_ipath;
349
350 // Compare ipath and env_ipath. If they differ, we take into account
351 // the input path comming from the command line option
352 if (!opt_ipath.empty() && !env_ipath.empty()) {
353 if (ipath != env_ipath) {
354 std::cout << std::endl << "WARNING: " << std::endl;
355 std::cout << " Since -i <visp image path=" << ipath << "> "
356 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
357 << " we skip the environment variable." << std::endl;
358 }
359 }
360
361 // Test if an input path is set
362 if (opt_ipath.empty() && env_ipath.empty()) {
363 usage(argv[0], NULL, ipath);
364 std::cerr << std::endl << "ERROR:" << std::endl;
365 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
366 << " environment variable to specify the location of the " << std::endl
367 << " image path where test images are located." << std::endl
368 << std::endl;
369 exit(-1);
370 }
371
374 fMo[2][3] = 0;
375
376 if (opt_display) {
377 vpSimulator simu;
378 simu.initInternalViewer(300, 300);
379 simu.initExternalViewer(300, 300);
380
381 vpTime::wait(1000);
382 simu.setZoomFactor(1.0f);
383
384 // Load the cad model
385 filename = vpIoTools::createFilePath(ipath, "iv/4points.iv");
386 simu.load(filename.c_str());
387
390 simu.initApplication(&mainLoop);
391
392 simu.mainLoop();
393 }
394 return EXIT_SUCCESS;
395 } catch (const vpException &e) {
396 std::cout << "Catch an exception: " << e << std::endl;
397 return EXIT_FAILURE;
398 }
399}
400
401#else
402int main()
403{
404 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
405 std::cout << "Tip:" << std::endl;
406 std::cout << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example" << std::endl;
407 return EXIT_SUCCESS;
408}
409#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
error that can be emited by ViSP classes.
Definition: vpException.h:72
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines 2D image point visual feature with polar coordinates described in .
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
static double rad(double deg)
Definition: vpMath.h:110
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const
Definition: vpPoint.cpp:239
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void get_eJe(vpMatrix &eJe)
@ CAMERA_FRAME
Definition: vpRobot.h:82
void setMaxTranslationVelocity(double maxVt)
Definition: vpRobot.cpp:239
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:159
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
void setLambda(double c)
Definition: vpServo.h:404
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
@ CURRENT
Definition: vpServo.h:182
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
Class that defines the simplest robot: a free flying camera.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition: vpSimulator.h:100
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)
Definition: vpSimulator.h:249
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 int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()