Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
simulateCircle2DCamVelocity.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
39
45
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpDebug.h>
48
49#ifdef VISP_HAVE_COIN3D_AND_GUI
50#include <visp3/ar/vpSimulator.h>
51#include <visp3/core/vpCameraParameters.h>
52#include <visp3/core/vpCircle.h>
53#include <visp3/core/vpHomogeneousMatrix.h>
54#include <visp3/core/vpImage.h>
55#include <visp3/core/vpIoTools.h>
56#include <visp3/core/vpMath.h>
57#include <visp3/core/vpTime.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/vpFeatureEllipse.h>
62#include <visp3/vs/vpServo.h>
63
64#define GETOPTARGS "cdi:hs"
65
66#ifdef ENABLE_VISP_NAMESPACE
67using namespace VISP_NAMESPACE_NAME;
68#endif
69
70bool opt_save = false;
71
81void usage(const char *name, const char *badparam, std::string ipath)
82{
83 fprintf(stdout, "\n\
84Simulation Servo Circle\n\
85 \n\
86SYNOPSIS\n\
87 %s [-i <input image path>] [-d] [-s] [-h]\n",
88 name);
89
90 fprintf(stdout, "\n\
91OPTIONS: Default\n\
92 -i <input image path> %s\n\
93 Set image input path.\n\
94 From this path read \"iv/4points.iv\"\n\
95 cad model.\n\
96 Setting the VISP_INPUT_IMAGE_PATH environment\n\
97 variable produces the same behaviour than using\n\
98 this option.\n\
99 \n\
100 -d \n\
101 Disable the image display. This can be useful \n\
102 for automatic tests using crontab under Unix or \n\
103 using the task manager under Windows.\n\
104 \n\
105 -s \n\
106 Save images\n\
107 \n\
108 -h\n\
109 Print the help.\n\n",
110 ipath.c_str());
111
112 if (badparam)
113 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
114}
115
130bool getOptions(int argc, const char **argv, std::string &ipath, bool &display, bool &save)
131{
132 const char *optarg;
133 int c;
134 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
135
136 switch (c) {
137 case 'i':
138 ipath = optarg;
139 break;
140 case 'd':
141 display = false;
142 break;
143 case 's':
144 opt_save = true;
145 break;
146 case 'h':
147 usage(argv[0], nullptr, ipath);
148 return false;
149
150 default:
151 usage(argv[0], optarg, ipath);
152 return false;
153 }
154 }
155
156 if ((c == 1) || (c == -1)) {
157 // standalone param or error
158 usage(argv[0], nullptr, ipath);
159 std::cerr << "ERROR: " << std::endl;
160 std::cerr << " Bad argument " << optarg << std::endl << std::endl;
161 return false;
162 }
163
164 return true;
165}
166
167static void *mainLoop(void *_simu)
168{
169 vpSimulator *simu = static_cast<vpSimulator *>(_simu);
170 simu->initMainApplication();
171
172 vpPoseVector vcMo;
173
174 vcMo[0] = 0.3;
175 vcMo[1] = 0.2;
176 vcMo[2] = 3;
177 vcMo[3] = 0;
178 vcMo[4] = vpMath::rad(45);
179 vcMo[5] = vpMath::rad(40);
181 vpHomogeneousMatrix wMo; // Set to identity
182 vpHomogeneousMatrix wMc; // Robot (=camera) location in the world frame
183
185 cMod[0][3] = 0;
186 cMod[1][3] = 0;
187 cMod[2][3] = 1;
188
189 int it = 0;
190 unsigned int pos = 2;
191 while (pos != 0) {
193 vpSimulatorCamera robot;
194
195 float sampling_time = 0.040f; // Sampling period in second
196 robot.setSamplingTime(sampling_time);
197 robot.setMaxTranslationVelocity(4.);
198
199 // Sets the initial camera location
200 wMc = wMo * cMo.inverse();
201 robot.setPosition(wMc);
202 simu->setCameraPosition(cMo);
203
204 if (pos == 1)
205 cMod[2][3] = 0.32;
206
207 // Sets the circle coordinates in the world frame
208 vpCircle circle;
209 circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
210
211 // Sets the desired position of the visual feature
213 circle.track(cMod);
214 vpFeatureBuilder::create(pd, circle);
215
216 // Project : computes the circle coordinates in the camera frame and its
217 // 2D coordinates Sets the current position of the visual feature
219 circle.track(cMo);
220 vpFeatureBuilder::create(p, circle);
221
222 // Define the task
223 // We want an eye-in-hand control law
224 // Robot is controlled in the camera frame
226 task.setInteractionMatrixType(vpServo::CURRENT);
227
228 // We want to see a circle on a circle
229 std::cout << std::endl;
230 task.addFeature(p, pd);
231
232 // Set the gain
233 task.setLambda(1.0);
234
235 // Display task information
236 task.print();
237
238 vpTime::wait(1000); // Sleep 1s
239
240 unsigned int iter = 0;
241 // Visual servoing loop
242 unsigned int itermax;
243 if (pos == 2)
244 itermax = 75;
245 else
246 itermax = 100;
247 while (iter++ < itermax) {
248 double t = vpTime::measureTimeMs();
249
250 if (iter == 1)
251 std::cout << "get the robot position" << std::endl;
252 wMc = robot.getPosition();
253 if (iter == 1)
254 std::cout << "new circle position" << std::endl;
255 // retrieve x,y and Z of the vpCircle structure
256
257 cMo = wMc.inverse() * wMo;
258 circle.track(cMo);
259 vpFeatureBuilder::create(p, circle);
260
261 if (iter == 1)
262 std::cout << "compute the control law" << std::endl;
263 vpColVector v = task.computeControlLaw();
264 if (iter == 1) {
265 std::cout << "Task rank: " << task.getTaskRank() << std::endl;
266 std::cout << "send the camera velocity to the controller" << std::endl;
267 }
268 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
269
270 simu->setCameraPosition(cMo);
271
272 if (SAVE == 1) {
273 std::string filename;
274 filename = vpIoTools::formatString("/tmp/image.%04d.external.png", iter);
275 std::cout << "Save " << filename << std::endl;
276 simu->write(filename);
277 filename = vpIoTools::formatString("/tmp/image.%04u.internal.png", iter);
278 std::cout << "Save " << filename << std::endl;
279 simu->write(filename);
280 }
281 // std::cout << "\t\t || s - s* || "
282 // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
283 vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
284 }
285 pos--;
286 }
287
288 simu->closeMainApplication();
289
290 void *a = nullptr;
291 return a;
292}
293
294int main(int argc, const char **argv)
295{
296 try {
297 std::string env_ipath;
298 std::string opt_ipath;
299 std::string ipath;
300 std::string filename;
301 bool opt_display = true;
302
303 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
304 // environment variable value
306
307 // Set the default input path
308 if (!env_ipath.empty())
309 ipath = env_ipath;
310
311 // Read the command line options
312 if (getOptions(argc, argv, opt_ipath, opt_display, opt_save) == false) {
313 return EXIT_FAILURE;
314 }
315
316 // Get the option values
317 if (!opt_ipath.empty())
318 ipath = opt_ipath;
319
320 // Compare ipath and env_ipath. If they differ, we take into account
321 // the input path coming from the command line option
322 if (!opt_ipath.empty() && !env_ipath.empty()) {
323 if (ipath != env_ipath) {
324 std::cout << std::endl << "WARNING: " << std::endl;
325 std::cout << " Since -i <visp image path=" << ipath << "> "
326 << " is different from VISP_INPUT_IMAGE_PATH=" << env_ipath << std::endl
327 << " we skip the environment variable." << std::endl;
328 }
329 }
330
331 // Test if an input path is set
332 if (opt_ipath.empty() && env_ipath.empty()) {
333 usage(argv[0], nullptr, ipath);
334 std::cerr << std::endl << "ERROR:" << std::endl;
335 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
336 << " environment variable to specify the location of the " << std::endl
337 << " image path where test images are located." << std::endl
338 << std::endl;
339 return EXIT_FAILURE;
340 }
341
344 fMo[2][3] = 0;
345
346 if (opt_display) {
347
348 vpSimulator simu;
349 simu.initInternalViewer(300, 300);
350 simu.initExternalViewer(300, 300);
351
352 vpTime::wait(1000);
353 simu.setZoomFactor(1.0f);
354 simu.addAbsoluteFrame();
355
356 // Load the cad model
357 filename = vpIoTools::createFilePath(ipath, "iv/circle.iv");
358 simu.load(filename.c_str(), fMo);
359
361
362 simu.initApplication(&mainLoop);
363 simu.mainLoop();
364 }
365 return EXIT_SUCCESS;
366 }
367 catch (const vpException &e) {
368 std::cout << "Catch an exception: " << e << std::endl;
369 return EXIT_FAILURE;
370 }
371}
372
373#else
374int main()
375{
376 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
377 std::cout << "Tip:" << std::endl;
378 std::cout
379 << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example"
380 << std::endl;
381 return EXIT_SUCCESS;
382}
383#endif
Generic class defining intrinsic camera parameters.
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition vpCircle.h:87
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition vpCircle.cpp:58
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 ellipse visual feature.
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 formatString(const std::string &name, unsigned int val)
static std::string createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Implementation of a pose vector and operations on poses.
virtual void setSamplingTime(const double &delta_t)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ 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 initMainApplication()
perform some initialization in the main program thread
void initApplication(void *(*start_routine)(void *))
begin the main program
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()
void addAbsoluteFrame(float zoom=1)
Add the representation of the absolute frame.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)