Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
simulateFourPoints2DCartesianCamVelocity.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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*****************************************************************************/
35
48#include <visp3/core/vpConfig.h>
49#include <visp3/core/vpDebug.h>
50
51#ifdef VISP_HAVE_COIN3D_AND_GUI
52
53#include <visp3/ar/vpSimulator.h>
54#include <visp3/core/vpCameraParameters.h>
55#include <visp3/core/vpHomogeneousMatrix.h>
56#include <visp3/core/vpImage.h>
57#include <visp3/core/vpIoTools.h>
58#include <visp3/core/vpMath.h>
59#include <visp3/core/vpTime.h>
60#include <visp3/io/vpParseArgv.h>
61#include <visp3/robot/vpSimulatorCamera.h>
62#include <visp3/visual_features/vpFeatureBuilder.h>
63#include <visp3/visual_features/vpFeaturePoint.h>
64#include <visp3/vs/vpServo.h>
65
66#define GETOPTARGS "di:h"
67#define SAVE 0
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], 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);
171
172 std::cout << std::endl;
173 std::cout << "-------------------------------------------------------" << std::endl;
174 std::cout << " Test program for vpServo " << std::endl;
175 std::cout << " Eye-in-hand task control, articular velocities are computed" << std::endl;
176 std::cout << " Simulation " << std::endl;
177 std::cout << " task : servo 4 points " << std::endl;
178 std::cout << "-------------------------------------------------------" << std::endl;
179 std::cout << std::endl;
180
181 // Sets the initial camera location
182 vpPoseVector vcMo;
183
184 vcMo[0] = 0.3;
185 vcMo[1] = 0.2;
186 vcMo[2] = 3;
187 vcMo[3] = 0;
188 vcMo[4] = vpMath::rad(0);
189 vcMo[5] = vpMath::rad(40);
190
191 vpHomogeneousMatrix cMo(vcMo);
192 vpHomogeneousMatrix wMo; // Set to identity
193 vpHomogeneousMatrix wMc; // Camera location in world frame
194 wMc = wMo * cMo.inverse();
195 robot.setPosition(wMc);
196 simu->setCameraPosition(cMo);
197
198 simu->getCameraPosition(cMo);
199 wMc = wMo * cMo.inverse();
200 robot.setPosition(wMc);
202
204
205 // Sets the point coordinates in the world frame
206 vpPoint point[4];
207 point[0].setWorldCoordinates(-0.1, -0.1, 0);
208 point[1].setWorldCoordinates(0.1, -0.1, 0);
209 point[2].setWorldCoordinates(0.1, 0.1, 0);
210 point[3].setWorldCoordinates(-0.1, 0.1, 0);
211
212 // Project : computes the point coordinates in the camera frame and its 2D
213 // coordinates
214 for (int i = 0; i < 4; i++)
215 point[i].track(cMo);
216
217 // Sets the desired position of the point
218 vpFeaturePoint p[4];
219 for (int i = 0; i < 4; i++)
220 vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
221
222 // Sets the desired position of the point
223 vpFeaturePoint pd[4];
224
225 pd[0].buildFrom(-0.1, -0.1, 1);
226 pd[1].buildFrom(0.1, -0.1, 1);
227 pd[2].buildFrom(0.1, 0.1, 1);
228 pd[3].buildFrom(-0.1, 0.1, 1);
229
230 // Define the task
231 // We want an eye-in-hand control law
232 // Articular velocity are computed
235
236 // Set the position of the end-effector frame in the camera frame as identity
238 vpVelocityTwistMatrix cVe(cMe);
239 task.set_cVe(cVe);
240
241 // Set the Jacobian (expressed in the end-effector frame)
242 vpMatrix eJe;
243 robot.get_eJe(eJe);
244 task.set_eJe(eJe);
245
246 // We want to see a point on a point
247 for (int i = 0; i < 4; i++)
248 task.addFeature(p[i], pd[i]);
249
250 // Set the gain
251 task.setLambda(1.0);
252
253 std::cout << "Display task information" << std::endl;
254 task.print();
255
256 vpTime::wait(1000); // Sleep 1s to ensure that all the thread are initialized
257
258 unsigned int iter = 0;
259 // visual servo loop
260 while (iter++ < 100) {
261 double t = vpTime::measureTimeMs();
262
263 vpColVector v;
264
265 robot.get_eJe(eJe);
266 task.set_eJe(eJe);
267
268 wMc = robot.getPosition();
269 cMo = wMc.inverse() * wMo;
270 for (int i = 0; i < 4; i++) {
271 point[i].track(cMo);
272 vpFeatureBuilder::create(p[i], point[i]);
273 }
274
275 v = task.computeControlLaw();
277
278 simu->setCameraPosition(cMo);
279
280 if (SAVE == 1) {
281 char name[FILENAME_MAX];
282 snprintf(name, FILENAME_MAX, "/tmp/image.%04u.external.png", iter);
283 std::cout << name << std::endl;
284 simu->write(name);
285 snprintf(name, FILENAME_MAX, "/tmp/image.%04u.internal.png", iter);
286 simu->write(name);
287 }
288
289 vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
290 }
291 std::cout << "\nDisplay task information" << std::endl;
292 task.print();
293
294 simu->closeMainApplication();
295
296 void *a = NULL;
297 return a;
298}
299
300int main(int argc, const char **argv)
301{
302 try {
303 std::string env_ipath;
304 std::string opt_ipath;
305 std::string ipath;
306 std::string filename;
307 bool opt_display = true;
308
309 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
310 // environment variable value
312
313 // Set the default input path
314 if (!env_ipath.empty())
315 ipath = env_ipath;
316
317 // Read the command line options
318 if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
319 return EXIT_FAILURE;
320 }
321
322 // Get the option values
323 if (!opt_ipath.empty())
324 ipath = opt_ipath;
325
326 // Compare ipath and env_ipath. If they differ, we take into account
327 // the input path comming from the command line option
328 if (!opt_ipath.empty() && !env_ipath.empty()) {
329 if (ipath != env_ipath) {
330 std::cout << std::endl << "WARNING: " << std::endl;
331 std::cout << " Since -i <visp image path=" << ipath << "> "
332 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
333 << " we skip the environment variable." << std::endl;
334 }
335 }
336
337 // Test if an input path is set
338 if (opt_ipath.empty() && env_ipath.empty()) {
339 usage(argv[0], NULL, ipath);
340 std::cerr << std::endl << "ERROR:" << std::endl;
341 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
342 << " environment variable to specify the location of the " << std::endl
343 << " image path where test images are located." << std::endl
344 << std::endl;
345 return EXIT_FAILURE;
346 }
347
350 fMo[2][3] = 0;
351
352 if (opt_display) {
353 vpSimulator simu;
354 simu.initInternalViewer(300, 300);
355 simu.initExternalViewer(300, 300);
356
357 vpTime::wait(1000);
358 simu.setZoomFactor(1.0f);
359
360 // Load the cad model
361 filename = vpIoTools::createFilePath(ipath, "iv/4points.iv");
362 simu.load(filename.c_str());
363
366 simu.initApplication(&mainLoop);
367
368 simu.mainLoop();
369 }
370 return EXIT_SUCCESS;
371 } catch (const vpException &e) {
372 std::cout << "Catch an exception: " << e << std::endl;
373 return EXIT_FAILURE;
374 }
375}
376
377#else
378int main()
379{
380 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
381 std::cout << "Tip:" << std::endl;
382 std::cout
383 << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example"
384 << std::endl;
385 return EXIT_SUCCESS;
386}
387#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:59
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void buildFrom(double x, double y, double Z)
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:116
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:152
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:77
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:110
Implementation of a pose vector and operations on poses.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void get_eJe(vpMatrix &eJe)
@ CAMERA_FRAME
Definition vpRobot.h:80
void setMaxTranslationVelocity(double maxVt)
Definition vpRobot.cpp:236
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition vpServo.cpp:564
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:155
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:299
void setLambda(double c)
Definition vpServo.h:403
void set_eJe(const vpMatrix &eJe_)
Definition vpServo.h:506
void setServo(const vpServoType &servo_type)
Definition vpServo.cpp:210
vpColVector computeControlLaw()
Definition vpServo.cpp:930
@ CURRENT
Definition vpServo.h:179
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition vpServo.cpp:487
Class that defines the simplest robot: a free flying camera.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition vpSimulator.h:99
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)