Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
simulateCircle2DCamVelocity.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#include <visp3/ar/vpSimulator.h>
53#include <visp3/core/vpCameraParameters.h>
54#include <visp3/core/vpCircle.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/vpFeatureEllipse.h>
64#include <visp3/vs/vpServo.h>
65
66#define GETOPTARGS "cdi:h"
67#define SAVE 0
68
78void usage(const char *name, const char *badparam, std::string ipath)
79{
80 fprintf(stdout, "\n\
81Simulation Servo Circle\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 vpPoseVector vcMo;
167
168 vcMo[0] = 0.3;
169 vcMo[1] = 0.2;
170 vcMo[2] = 3;
171 vcMo[3] = 0;
172 vcMo[4] = vpMath::rad(45);
173 vcMo[5] = vpMath::rad(40);
174 vpHomogeneousMatrix cMo(vcMo);
175 vpHomogeneousMatrix wMo; // Set to identity
176 vpHomogeneousMatrix wMc; // Robot (=camera) location in the world frame
177
179 cMod[0][3] = 0;
180 cMod[1][3] = 0;
181 cMod[2][3] = 1;
182
183 int it = 0;
184 unsigned int pos = 2;
185 while (pos != 0) {
186 vpServo task;
187 vpSimulatorCamera robot;
188
189 float sampling_time = 0.040f; // Sampling period in second
190 robot.setSamplingTime(sampling_time);
192
193 // Sets the initial camera location
194 wMc = wMo * cMo.inverse();
195 robot.setPosition(wMc);
196 simu->setCameraPosition(cMo);
197
198 if (pos == 1)
199 cMod[2][3] = 0.32;
200
201 // Sets the circle coordinates in the world frame
202 vpCircle circle;
203 circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
204
205 // Sets the desired position of the visual feature
207 circle.track(cMod);
208 vpFeatureBuilder::create(pd, circle);
209
210 // Project : computes the circle coordinates in the camera frame and its
211 // 2D coordinates Sets the current position of the visual feature
213 circle.track(cMo);
214 vpFeatureBuilder::create(p, circle);
215
216 // Define the task
217 // We want an eye-in-hand control law
218 // Robot is controlled in the camera frame
221
222 // We want to see a circle on a circle
223 std::cout << std::endl;
224 task.addFeature(p, pd);
225
226 // Set the gain
227 task.setLambda(1.0);
228
229 // Display task information
230 task.print();
231
232 vpTime::wait(1000); // Sleep 1s
233
234 unsigned int iter = 0;
235 // Visual servoing loop
236 unsigned int itermax;
237 if (pos == 2)
238 itermax = 75;
239 else
240 itermax = 100;
241 while (iter++ < itermax) {
242 double t = vpTime::measureTimeMs();
243
244 if (iter == 1)
245 std::cout << "get the robot position" << std::endl;
246 wMc = robot.getPosition();
247 if (iter == 1)
248 std::cout << "new circle position" << std::endl;
249 // retrieve x,y and Z of the vpCircle structure
250
251 cMo = wMc.inverse() * wMo;
252 circle.track(cMo);
253 vpFeatureBuilder::create(p, circle);
254
255 if (iter == 1)
256 std::cout << "compute the control law" << std::endl;
258 if (iter == 1) {
259 std::cout << "Task rank: " << task.getTaskRank() << std::endl;
260 std::cout << "send the camera velocity to the controller" << std::endl;
261 }
263
264 simu->setCameraPosition(cMo);
265
266 if (SAVE == 1) {
267 char name[FILENAME_MAX];
268 snprintf(name, FILENAME_MAX, "/tmp/image.%04d.external.png", it);
269 std::cout << "Save " << name << std::endl;
270 simu->write(name);
271 snprintf(name, FILENAME_MAX, "/tmp/image.%04u.internal.png", iter);
272 std::cout << "Save " << name << std::endl;
273 simu->write(name);
274 it++;
275 }
276 // std::cout << "\t\t || s - s* || "
277 // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
278 vpTime::wait(t, sampling_time * 1000); // Wait 40 ms
279 }
280 pos--;
281 }
282
283 simu->closeMainApplication();
284
285 void *a = NULL;
286 return a;
287}
288
289int main(int argc, const char **argv)
290{
291 try {
292 std::string env_ipath;
293 std::string opt_ipath;
294 std::string ipath;
295 std::string filename;
296 bool opt_display = true;
297
298 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
299 // environment variable value
301
302 // Set the default input path
303 if (!env_ipath.empty())
304 ipath = env_ipath;
305
306 // Read the command line options
307 if (getOptions(argc, argv, opt_ipath, opt_display) == false) {
308 return EXIT_FAILURE;
309 }
310
311 // Get the option values
312 if (!opt_ipath.empty())
313 ipath = opt_ipath;
314
315 // Compare ipath and env_ipath. If they differ, we take into account
316 // the input path comming from the command line option
317 if (!opt_ipath.empty() && !env_ipath.empty()) {
318 if (ipath != env_ipath) {
319 std::cout << std::endl << "WARNING: " << std::endl;
320 std::cout << " Since -i <visp image path=" << ipath << "> "
321 << " is different from VISP_INPUT_IMAGE_PATH=" << env_ipath << std::endl
322 << " we skip the environment variable." << std::endl;
323 }
324 }
325
326 // Test if an input path is set
327 if (opt_ipath.empty() && env_ipath.empty()) {
328 usage(argv[0], NULL, ipath);
329 std::cerr << std::endl << "ERROR:" << std::endl;
330 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
331 << " environment variable to specify the location of the " << std::endl
332 << " image path where test images are located." << std::endl
333 << std::endl;
334 return EXIT_FAILURE;
335 }
336
339 fMo[2][3] = 0;
340
341 if (opt_display) {
342
343 vpSimulator simu;
344 simu.initInternalViewer(300, 300);
345 simu.initExternalViewer(300, 300);
346
347 vpTime::wait(1000);
348 simu.setZoomFactor(1.0f);
349 simu.addAbsoluteFrame();
350
351 // Load the cad model
352 filename = vpIoTools::createFilePath(ipath, "iv/circle.iv");
353 simu.load(filename.c_str(), fMo);
354
356
357 simu.initApplication(&mainLoop);
358 simu.mainLoop();
359 }
360 return EXIT_SUCCESS;
361 } catch (const vpException &e) {
362 std::cout << "Catch an exception: " << e << std::endl;
363 return EXIT_FAILURE;
364 }
365}
366
367#else
368int main()
369{
370 std::cout << "You do not have Coin3D and SoQT or SoWin or SoXt functionalities enabled..." << std::endl;
371 std::cout << "Tip:" << std::endl;
372 std::cout
373 << "- Install Coin3D and SoQT or SoWin or SoXt, configure ViSP again using cmake and build again this example"
374 << std::endl;
375 return EXIT_SUCCESS;
376}
377#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)
Definition vpCircle.cpp:57
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 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 createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:116
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Implementation of a pose vector and operations on poses.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ 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_CAMERA
Definition vpServo.h:151
unsigned int getTaskRank() const
Definition vpServo.cpp:1796
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 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 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 int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()