Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
servoSimuSphere2DCamVelocity.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 2D visual servoing on a sphere.
33 *
34*****************************************************************************/
35
45#include <stdio.h>
46#include <stdlib.h>
47
48#include <visp3/core/vpHomogeneousMatrix.h>
49#include <visp3/core/vpMath.h>
50#include <visp3/core/vpSphere.h>
51#include <visp3/io/vpParseArgv.h>
52#include <visp3/robot/vpSimulatorCamera.h>
53#include <visp3/visual_features/vpFeatureBuilder.h>
54#include <visp3/visual_features/vpFeatureEllipse.h>
55#include <visp3/vs/vpServo.h>
56
57// List of allowed command line options
58#define GETOPTARGS "h"
59
60void usage(const char *name, const char *badparam);
61bool getOptions(int argc, const char **argv);
62
71void usage(const char *name, const char *badparam)
72{
73 fprintf(stdout, "\n\
74Simulation of a 2D visual servoing on a sphere:\n\
75- eye-in-hand control law,\n\
76- velocity computed in the camera frame,\n\
77- without display.\n\
78 \n\
79SYNOPSIS\n\
80 %s [-h]\n",
81 name);
82
83 fprintf(stdout, "\n\
84OPTIONS: Default\n\
85 \n\
86 -h\n\
87 Print the help.\n");
88
89 if (badparam)
90 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
91}
92
103bool getOptions(int argc, const char **argv)
104{
105 const char *optarg_;
106 int c;
107 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
108
109 switch (c) {
110 case 'h':
111 usage(argv[0], NULL);
112 return false;
113
114 default:
115 usage(argv[0], optarg_);
116 return false;
117 }
118 }
119
120 if ((c == 1) || (c == -1)) {
121 // standalone param or error
122 usage(argv[0], NULL);
123 std::cerr << "ERROR: " << std::endl;
124 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
125 return false;
126 }
127
128 return true;
129}
130
131int main(int argc, const char **argv)
132{
133#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
134 try {
135 // Read the command line options
136 if (getOptions(argc, argv) == false) {
137 return EXIT_FAILURE;
138 }
139
140 vpServo task;
141 vpSimulatorCamera robot;
142
143 std::cout << std::endl;
144 std::cout << "-------------------------------------------------------" << std::endl;
145 std::cout << " Test program for vpServo " << std::endl;
146 std::cout << " Simulation " << std::endl;
147 std::cout << " task : servo a sphere " << std::endl;
148 std::cout << "-------------------------------------------------------" << std::endl;
149 std::cout << std::endl;
150
151 // sets the initial camera location
153 cMo[0][3] = 0.1;
154 cMo[1][3] = 0.2;
155 cMo[2][3] = 2;
156 // Compute the position of the object in the world frame
157 vpHomogeneousMatrix wMc, wMo;
158 robot.getPosition(wMc);
159 wMo = wMc * cMo;
160
162 cMod[0][3] = 0;
163 cMod[1][3] = 0;
164 cMod[2][3] = 1;
165
166 // sets the sphere coordinates in the world frame
167 vpSphere sphere;
168 sphere.setWorldCoordinates(0, 0, 0, 0.1);
169
170 // sets the desired position of the visual feature
172 sphere.track(cMod);
173 vpFeatureBuilder::create(pd, sphere);
174
175 // computes the sphere coordinates in the camera frame and its 2D
176 // coordinates sets the current position of the visual feature
178 sphere.track(cMo);
179 vpFeatureBuilder::create(p, sphere);
180
181 // define the task
182 // - we want an eye-in-hand control law
183 // - robot is controlled in the camera frame
185
186 // we want to see a sphere on a sphere
187 task.addFeature(p, pd);
188
189 // set the gain
190 task.setLambda(1);
191
192 // Display task information
193 task.print();
194
195 unsigned int iter = 0;
196 // loop
197 while (iter++ < 200) {
198 std::cout << "---------------------------------------------" << iter << std::endl;
199 vpColVector v;
200
201 // get the robot position
202 robot.getPosition(wMc);
203 // Compute the position of the object frame in the camera frame
204 cMo = wMc.inverse() * wMo;
205
206 // new sphere position: retrieve x,y and Z of the vpSphere structure
207 sphere.track(cMo);
208 vpFeatureBuilder::create(p, sphere);
209
210 // compute the control law
211 v = task.computeControlLaw();
212
213 std::cout << "Task rank: " << task.getTaskRank() << std::endl;
214 // send the camera velocity to the controller
216
217 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
218 }
219
220 // Display task information
221 task.print();
222 return EXIT_SUCCESS;
223 } catch (const vpException &e) {
224 std::cout << "Catch a ViSP exception: " << e << std::endl;
225 return EXIT_FAILURE;
226 }
227#else
228 (void)argc;
229 (void)argv;
230 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
231 return EXIT_SUCCESS;
232#endif
233}
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 bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ CAMERA_FRAME
Definition vpRobot.h:80
@ 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 getError() const
Definition vpServo.h:276
vpColVector computeControlLaw()
Definition vpServo.cpp:930
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.
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition vpSphere.h:78
void setWorldCoordinates(const vpColVector &oP)
Definition vpSphere.cpp:59