Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoMomentPolygon.cpp
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  * Example of visual servoing with moments using a polygon as object container
33  *
34  * Authors:
35  * Filip Novotny
36  *
37  *****************************************************************************/
38 
44 #include <iostream>
45 #include <visp3/core/vpCameraParameters.h>
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 #include <visp3/core/vpHomogeneousMatrix.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/core/vpMath.h>
51 #include <visp3/core/vpMomentCommon.h>
52 #include <visp3/core/vpMomentDatabase.h>
53 #include <visp3/core/vpMomentObject.h>
54 #include <visp3/core/vpPlane.h>
55 #include <visp3/gui/vpDisplayGDI.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/gui/vpDisplayX.h>
58 #include <visp3/robot/vpSimulatorAfma6.h>
59 #include <visp3/visual_features/vpFeatureBuilder.h>
60 #include <visp3/visual_features/vpFeatureMomentCommon.h>
61 #include <visp3/visual_features/vpFeaturePoint.h>
62 #include <visp3/vs/vpServo.h>
63 
64 #if !defined(_WIN32) && !defined(VISP_HAVE_PTHREAD)
65 // Robot simulator used in this example is not available
66 int main()
67 {
68  std::cout << "Can't run this example since vpSimulatorAfma6 capability is "
69  "not available."
70  << std::endl;
71  std::cout << "You should install pthread third-party library." << std::endl;
72  return EXIT_SUCCESS;
73 }
74 // No display available
75 #elif !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_OPENCV) && !defined(VISP_HAVE_GDI) && !defined(VISP_HAVE_D3D9) && \
76  !defined(VISP_HAVE_GTK)
77 int main()
78 {
79  std::cout << "Can't run this example since no display capability is available." << std::endl;
80  std::cout << "You should install one of the following third-party library: "
81  "X11, OpenCV, GDI, GTK."
82  << std::endl;
83  return EXIT_SUCCESS;
84 }
85 #else
86 
87 // setup robot parameters
88 void paramRobot();
89 
90 // update moment objects and interface
91 void refreshScene(vpMomentObject &obj);
92 // initialize scene in the interface
93 void initScene();
94 // initialize the moment features
95 void initFeatures();
96 
97 void init(vpHomogeneousMatrix &cMo, vpHomogeneousMatrix &cdMo);
98 void execute(unsigned int nbIter); // launch the simulation
99 void setInteractionMatrixType(vpServo::vpServoIteractionMatrixType type);
100 double error();
101 void planeToABC(vpPlane &pl, double &A, double &B, double &C);
102 void paramRobot();
103 void removeJointLimits(vpSimulatorAfma6 &robot);
104 
105 int main()
106 {
107  try { // intial pose
108  vpHomogeneousMatrix cMo(-0.1, -0.1, 1.5, -vpMath::rad(20), -vpMath::rad(20), -vpMath::rad(30));
109  // Desired pose
111 
112  // init and run the simulation
113  init(cMo, cdMo);
114  execute(1500);
115  return EXIT_SUCCESS;
116  } catch (const vpException &e) {
117  std::cout << "Catch an exception: " << e << std::endl;
118  return EXIT_FAILURE;
119  }
120 }
121 
122 // init the right display
123 #if defined VISP_HAVE_X11
124 vpDisplayX displayInt;
125 #elif defined VISP_HAVE_OPENCV
126 vpDisplayOpenCV displayInt;
127 #elif defined VISP_HAVE_GDI
128 vpDisplayGDI displayInt;
129 #elif defined VISP_HAVE_D3D9
130 vpDisplayD3D displayInt;
131 #elif defined VISP_HAVE_GTK
132 vpDisplayGTK displayInt;
133 #endif
134 
135 // start and destination positioning matrices
138 
139 vpSimulatorAfma6 robot(false); // robot used in this simulation
140 vpImage<vpRGBa> Iint(480, 640,
141  255); // internal image used for interface display
142 vpServo task; // servoing task
143 vpCameraParameters cam; // robot camera parameters
144 double _error; // current error
145 vpServo::vpServoIteractionMatrixType interaction_type; // current or desired
146 vpImageSimulator imsim; // image simulator used to simulate the
147  // perspective-projection camera
148 
149 // source and destination objects for moment manipulation
150 vpMomentObject src(6);
151 vpMomentObject dst(6);
152 // moment sets and their corresponding features
153 vpMomentCommon *moments;
154 vpMomentCommon *momentsDes;
155 vpFeatureMomentCommon *featureMoments;
156 vpFeatureMomentCommon *featureMomentsDes;
157 
158 using namespace std;
159 
160 void initScene()
161 {
162  vector<vpPoint> src_pts;
163  vector<vpPoint> dst_pts;
164 
165  double x[5] = {0.2, 0.2, -0.2, -0.2, 0.2};
166  double y[5] = {-0.1, 0.1, 0.1, -0.1, -0.1};
167  int nbpoints = 4;
168 
169  for (int i = 0; i < nbpoints; i++) {
170  vpPoint p(x[i], y[i], 0.0);
171  p.track(cMo);
172  src_pts.push_back(p);
173  }
174 
176  src.fromVector(src_pts);
177  for (int i = 0; i < nbpoints; i++) {
178  vpPoint p(x[i], y[i], 0.0);
179  p.track(cdMo);
180  dst_pts.push_back(p);
181  }
183  dst.fromVector(dst_pts);
184 }
185 
186 void refreshScene(vpMomentObject &obj)
187 {
188  double x[5] = {0.2, 0.2, -0.2, -0.2, 0.2};
189  double y[5] = {-0.1, 0.1, 0.1, -0.1, -0.1};
190  int nbpoints = 5;
191  vector<vpPoint> cur_pts;
192 
193  for (int i = 0; i < nbpoints; i++) {
194  vpPoint p(x[i], y[i], 0.0);
195  p.track(cMo);
196  cur_pts.push_back(p);
197  }
198  obj.fromVector(cur_pts);
199 }
200 
201 void init(vpHomogeneousMatrix &_cMo, vpHomogeneousMatrix &_cdMo)
202 {
203  cMo = _cMo;
204  cdMo = _cdMo;
205  interaction_type = vpServo::CURRENT;
206  displayInt.init(Iint, 700, 0, "Visual servoing with moments");
207 
208  paramRobot(); // set up robot parameters
209 
211  initScene(); // initialize graphical scene (for interface)
212  initFeatures(); // initialize moment features
213 }
214 
215 void initFeatures()
216 {
217  // A,B,C parameters of source and destination plane
218  double A;
219  double B;
220  double C;
221  double Ad;
222  double Bd;
223  double Cd;
224  // init main object: using moments up to order 6
225 
226  // Initializing values from regular plane (with ax+by+cz=d convention)
227  vpPlane pl;
228  pl.setABCD(0, 0, 1.0, 0);
229  pl.changeFrame(cMo);
230  planeToABC(pl, A, B, C);
231 
232  pl.setABCD(0, 0, 1.0, 0);
233  pl.changeFrame(cdMo);
234  planeToABC(pl, Ad, Bd, Cd);
235 
236  // extracting initial position (actually we only care about Zdst)
238  cdMo.extract(vec);
239 
242  // don't need to be specific, vpMomentCommon automatically loads
243  // Xg,Yg,An,Ci,Cj,Alpha moments
245  vpMomentCommon::getAlpha(dst), vec[2]);
247  vpMomentCommon::getAlpha(dst), vec[2]);
248  // same thing with common features
249  featureMoments = new vpFeatureMomentCommon(*moments);
250  featureMomentsDes = new vpFeatureMomentCommon(*momentsDes);
251 
252  moments->updateAll(src);
253  momentsDes->updateAll(dst);
254 
255  featureMoments->updateAll(A, B, C);
256  featureMomentsDes->updateAll(Ad, Bd, Cd);
257 
258  // setup the interaction type
259  task.setInteractionMatrixType(interaction_type);
262  task.addFeature(featureMoments->getFeatureGravityNormalized(), featureMomentsDes->getFeatureGravityNormalized());
263  task.addFeature(featureMoments->getFeatureAn(), featureMomentsDes->getFeatureAn());
264  // the moments are different in case of a symmetric object
265  task.addFeature(featureMoments->getFeatureCInvariant(), featureMomentsDes->getFeatureCInvariant(),
266  (1 << 10) | (1 << 11));
267  task.addFeature(featureMoments->getFeatureAlpha(), featureMomentsDes->getFeatureAlpha());
268 
269  task.setLambda(0.4);
270 }
271 
272 void execute(unsigned int nbIter)
273 {
274  // init main object: using moments up to order 5
275  vpMomentObject obj(6);
276  // setting object type (disrete, continuous[form polygon])
278 
279  vpTRACE("Display task information ");
280  task.print();
281 
282  vpDisplay::display(Iint);
283  robot.getInternalView(Iint);
284  vpDisplay::flush(Iint);
285  unsigned int iter = 0;
286 
288  while (iter++ < nbIter) {
289  vpColVector v;
290  double t = vpTime::measureTimeMs();
291  // get the cMo
292  cMo = robot.get_cMo();
293  // setup the plane in A,B,C style
294  vpPlane pl;
295  double A, B, C;
296  pl.setABCD(0, 0, 1.0, 0);
297  pl.changeFrame(cMo);
298  planeToABC(pl, A, B, C);
299 
300  // track points, draw points and add refresh our object
301  refreshScene(obj);
302  // this is the most important thing to do: update our moments
303  moments->updateAll(obj);
304  // and update our features. Do it in that order. Features need to use the
305  // information computed by moments
306  featureMoments->updateAll(A, B, C);
307 
308  vpDisplay::display(Iint);
309  robot.getInternalView(Iint);
310  vpDisplay::flush(Iint);
311 
312  if (iter == 1)
313  vpDisplay::getClick(Iint);
314  v = task.computeControlLaw();
315 
316  // pilot robot using position control. The displacement is t*v with t=10ms
317  // step
318  robot.setPosition(vpRobot::CAMERA_FRAME, 0.01 * v);
319 
320  vpTime::wait(t, 10);
321  _error = (task.getError()).sumSquare();
322  }
323 
324  task.kill();
325 
326  vpTRACE("\n\nClick in the internal view window to end...");
327  vpDisplay::getClick(Iint);
328 
329  delete moments;
330  delete momentsDes;
331  delete featureMoments;
332  delete featureMomentsDes;
333 }
334 
335 void setInteractionMatrixType(vpServo::vpServoIteractionMatrixType type) { interaction_type = type; }
336 double error() { return _error; }
337 
338 void removeJointLimits(vpSimulatorAfma6 &robot_)
339 {
340  vpColVector limMin(6);
341  vpColVector limMax(6);
342  limMin[0] = vpMath::rad(-3600);
343  limMin[1] = vpMath::rad(-3600);
344  limMin[2] = vpMath::rad(-3600);
345  limMin[3] = vpMath::rad(-3600);
346  limMin[4] = vpMath::rad(-3600);
347  limMin[5] = vpMath::rad(-3600);
348 
349  limMax[0] = vpMath::rad(3600);
350  limMax[1] = vpMath::rad(3600);
351  limMax[2] = vpMath::rad(3600);
352  limMax[3] = vpMath::rad(3600);
353  limMax[4] = vpMath::rad(3600);
354  limMax[5] = vpMath::rad(3600);
355 
356  robot_.setJointLimit(limMin, limMax);
357 }
358 
359 void planeToABC(vpPlane &pl, double &A, double &B, double &C)
360 {
361  if (fabs(pl.getD()) < std::numeric_limits<double>::epsilon()) {
362  std::cout << "Invalid position:" << std::endl;
363  std::cout << cMo << std::endl;
364  std::cout << "Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
365  throw vpException(vpException::divideByZeroError, "invalid position!");
366  }
367  A = -pl.getA() / pl.getD();
368  B = -pl.getB() / pl.getD();
369  C = -pl.getC() / pl.getD();
370 }
371 
372 void paramRobot()
373 {
374  /*Initialise the robot and especially the camera*/
376  robot.setCurrentViewColor(vpColor(150, 150, 150));
377  robot.setDesiredViewColor(vpColor(200, 200, 200));
379  removeJointLimits(robot);
381  /*Initialise the position of the object relative to the pose of the robot's
382  * camera*/
383  robot.initialiseObjectRelativeToCamera(cMo);
384 
385  /*Set the desired position (for the displaypart)*/
386  robot.setDesiredCameraPosition(cdMo);
387  robot.getCameraParameters(cam, Iint);
388 }
389 
390 #endif
void setPosition(const vpHomogeneousMatrix &wMc)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
This class allows to access common vpFeatureMoments in a pre-filled database.
Initialize the position controller.
Definition: vpRobot.h:68
error that can be emited by ViSP classes.
Definition: vpException.h:71
void setJointLimit(const vpColVector &limitMin, const vpColVector &limitMax)
void setABCD(const double a, const double b, const double c, const double d)
Definition: vpPlane.h:90
Class for generic objects.
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
Class that defines what is a point.
Definition: vpPoint.h:58
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
void kill()
Definition: vpServo.cpp:192
vpColVector getError() const
Definition: vpServo.h:282
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
vpFeatureMomentAlpha & getFeatureAlpha()
void updateAll(double A, double B, double C)
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition: vpPlane.cpp:354
#define vpTRACE
Definition: vpDebug.h:416
static std::vector< double > getMu3(vpMomentObject &object)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
Class which enables to project an image in the 3D space and get the view of a virtual camera...
Simulator of Irisa&#39;s gantry robot named Afma6.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
void extract(vpRotationMatrix &R) const
vpServoIteractionMatrixType
Definition: vpServo.h:185
void fromVector(std::vector< vpPoint > &points)
static double getSurface(vpMomentObject &object)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
vpFeatureMomentCInvariant & getFeatureCInvariant()
static double rad(double deg)
Definition: vpMath.h:102
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void updateAll(vpMomentObject &object)
This class initializes and allows access to commonly used moments.
static double getAlpha(vpMomentObject &object)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpFeatureMomentGravityCenterNormalized & getFeatureGravityNormalized()
double getB() const
Definition: vpPlane.h:104
void setType(vpObjectType input_type)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
double getA() const
Definition: vpPlane.h:102
double getC() const
Definition: vpPlane.h:106
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
vpFeatureMomentAreaNormalized & getFeatureAn()
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
Class that consider the case of a translation vector.
double getD() const
Definition: vpPlane.h:108