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 2 1/2 D visual servoing.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 /*!
41   \example servoSimuPoint2DhalfCamVelocity1.cpp
42   Simulation of a 2 1/2 D visual servoing (theta U):
43   - (x,y,Z,theta U) features,
44   - eye-in-hand control law,
45   - velocity computed in the camera frame,
46   - no display.
47 
48 */
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include <visp3/core/vpHomogeneousMatrix.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpPoint.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/robot/vpSimulatorCamera.h>
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeaturePoint.h>
60 #include <visp3/visual_features/vpFeatureThetaU.h>
61 #include <visp3/visual_features/vpGenericFeature.h>
62 #include <visp3/vs/vpServo.h>
63 
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 void usage(const char *name, const char *badparam);
68 bool getOptions(int argc, const char **argv);
69 
70 /*!
71 
72   Print the program options.
73 
74   \param name : Program name.
75   \param badparam : Bad parameter name.
76 
77 */
usage(const char * name,const char * badparam)78 void usage(const char *name, const char *badparam)
79 {
80   fprintf(stdout, "\n\
81 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
82 - eye-in-hand control law,\n\
83 - velocity computed in the camera frame,\n\
84 - without display.\n\
85           \n\
86 SYNOPSIS\n\
87   %s [-h]\n", name);
88 
89   fprintf(stdout, "\n\
90 OPTIONS:                                               Default\n\
91                   \n\
92   -h\n\
93      Print the help.\n");
94 
95   if (badparam) {
96     fprintf(stderr, "ERROR: \n");
97     fprintf(stderr, "\nBad parameter [%s]\n", badparam);
98   }
99 }
100 
101 /*!
102 
103   Set the program options.
104 
105   \param argc : Command line number of parameters.
106   \param argv : Array of command line parameters.
107 
108   \return false if the program has to be stopped, true otherwise.
109 
110 */
getOptions(int argc,const char ** argv)111 bool getOptions(int argc, const char **argv)
112 {
113   const char *optarg_;
114   int c;
115   while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117     switch (c) {
118     case 'h':
119       usage(argv[0], NULL);
120       return false;
121 
122     default:
123       usage(argv[0], optarg_);
124       return false;
125     }
126   }
127 
128   if ((c == 1) || (c == -1)) {
129     // standalone param or error
130     usage(argv[0], NULL);
131     std::cerr << "ERROR: " << std::endl;
132     std::cerr << "  Bad argument " << optarg_ << std::endl << std::endl;
133     return false;
134   }
135 
136   return true;
137 }
138 
main(int argc,const char ** argv)139 int main(int argc, const char **argv)
140 {
141 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
142   try {
143     // Read the command line options
144     if (getOptions(argc, argv) == false) {
145       exit(-1);
146     }
147 
148     vpServo task;
149     vpSimulatorCamera robot;
150 
151     std::cout << std::endl;
152     std::cout << "-------------------------------------------------------" << std::endl;
153     std::cout << " Test program for vpServo " << std::endl;
154     std::cout << " task :  2 1/2 D visual servoing " << std::endl;
155     std::cout << "-------------------------------------------------------" << std::endl;
156     std::cout << std::endl;
157 
158     // sets the initial camera location
159     vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
160 
161     vpHomogeneousMatrix cMo(c_r_o);
162     // Compute the position of the object in the world frame
163     vpHomogeneousMatrix wMc, wMo;
164     robot.getPosition(wMc);
165     wMo = wMc * cMo;
166 
167     // sets the desired camera location
168     vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
169     vpHomogeneousMatrix cdMo(cd_r_o);
170 
171     // sets the point coordinates in the world frame
172     vpPoint point(0, 0, 0);
173     // computes the point coordinates in the camera frame and its 2D
174     // coordinates
175     point.track(cMo);
176 
177     vpPoint pointd(0, 0, 0);
178     pointd.track(cdMo);
179     //------------------------------------------------------------------
180     // 1st feature (x,y)
181     // want to it at (0,0)
182     vpFeaturePoint p;
183     vpFeatureBuilder::create(p, point);
184 
185     vpFeaturePoint pd;
186     vpFeatureBuilder::create(pd, pointd);
187 
188     //------------------------------------------------------------------
189     // 2nd feature (Z)
190     // not necessary to project twice (reuse p)
191     vpFeaturePoint3D Z;
192     vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
193 
194     // want to see it one meter away (here again use pd)
195     vpFeaturePoint3D Zd;
196     vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
197 
198     //------------------------------------------------------------------
199     // 3rd feature ThetaU
200     // compute the rotation that the camera has to achieve
201     vpHomogeneousMatrix cdMc;
202     cdMc = cdMo * cMo.inverse();
203 
204     vpFeatureThetaU tu(vpFeatureThetaU::cdRc);
205     tu.buildFrom(cdMc);
206 
207     // sets the desired rotation (always zero !)
208     // since s is the rotation that the camera has to achieve
209 
210     //------------------------------------------------------------------
211     // define the task
212     // - we want an eye-in-hand control law
213     // - robot is controlled in the camera frame
214     task.setServo(vpServo::EYEINHAND_CAMERA);
215 
216     task.addFeature(p, pd);
217     task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
218     task.addFeature(tu);
219 
220     // set the gain
221     task.setLambda(1);
222 
223     // Display task information
224     task.print();
225 
226     unsigned int iter = 0;
227     // loop
228     while (iter++ < 200) {
229       std::cout << "---------------------------------------------" << iter << std::endl;
230       vpColVector v;
231 
232       // get the robot position
233       robot.getPosition(wMc);
234       // Compute the position of the object frame in the camera frame
235       cMo = wMc.inverse() * wMo;
236 
237       // update the feature
238       point.track(cMo);
239       vpFeatureBuilder::create(p, point);
240       vpFeatureBuilder::create(Z, point);
241 
242       cdMc = cdMo * cMo.inverse();
243       tu.buildFrom(cdMc);
244 
245       // compute the control law
246       v = task.computeControlLaw();
247       // send the camera velocity to the controller ") ;
248       robot.setVelocity(vpRobot::CAMERA_FRAME, v);
249 
250       std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
251     }
252 
253     // Display task information
254     task.print();
255     std::cout << "Final camera location:\n " << cMo << std::endl;
256     return EXIT_SUCCESS;
257   } catch (const vpException &e) {
258     std::cout << "Catch a ViSP exception: " << e << std::endl;
259     return EXIT_SUCCESS;
260   }
261 #else
262   (void)argc;
263   (void)argv;
264   std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
265   return EXIT_SUCCESS;
266 #endif
267 }
268