ViSP  2.9.0
testFeatureMoment.cpp
1 /****************************************************************************
2  *
3  * $Id: testFeatureMoment.cpp 3323 2011-09-13 15:23:56Z fnovotny $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Example of visual servoing with moments using a polygon as object container
36  *
37  * Authors:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 #include <visp/vpMomentObject.h>
42 #include <visp/vpMomentDatabase.h>
43 #include <visp/vpMomentCommon.h>
44 #include <visp/vpFeatureMomentCommon.h>
45 #include <visp/vpHomogeneousMatrix.h>
46 #include <visp/vpServo.h>
47 #include <visp/vpDebug.h>
48 #include <visp/vpPlane.h>
49 
50 #include <limits>
51 #include <iostream>
52 
53 //initialize scene in the interface
54 void initScene(const vpHomogeneousMatrix& cMo, const vpHomogeneousMatrix& cdMo,
55  vpMomentObject &src, vpMomentObject &dst);
56 
57 vpMatrix execute(const vpHomogeneousMatrix& cMo, const vpHomogeneousMatrix& cdMo,
58  vpMomentObject &src, vpMomentObject &dst); //launch the test
59 void planeToABC(const vpPlane& pl, double& A,double& B, double& C);
60 int test(double x,double y,double z,double alpha);
61 
62 //Compute a set of parallel positions and check if the matrix is in the right form;
63 int main()
64 {
65  try {
66  int sum=0;
67  for(double i=-0.2;i<0.2;i+=0.1){
68  for(double j=-0.2;j<0.2;j+=0.1){
69  for(double k=-vpMath::rad(30);k<vpMath::rad(30);k+=vpMath::rad(10)){
70  for(double l=0.5;l<1.5;l+=0.1){
71  sum+=test(i,j,l,k);
72  }
73  }
74 
75  }
76  }
77  if(sum<0) return -1;
78  else return 0;
79  }
80  catch(vpException e) {
81  std::cout << "Catch an exception: " << e << std::endl;
82  return 1;
83  }
84 }
85 
86 int test(double x,double y,double z,double alpha){
87  //intial pose
88  vpHomogeneousMatrix cMo(x,y,z,-vpMath::rad(0),vpMath::rad(0),alpha);
89  //Desired pose
91 
92  //source and destination objects for moment manipulation
93  vpMomentObject src(6);
94  vpMomentObject dst(6);
95 
96  //init and run the simulation
97  initScene(cMo, cdMo, src, dst); //initialize graphical scene (for interface)
98 
99  vpMatrix mat = execute(cMo, cdMo, src, dst);
100 
101  if(fabs(mat[0][0]-(-1)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
102  if(fabs(mat[0][1]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
103  if(fabs(mat[0][2]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
104 
105  if(fabs(mat[1][0]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
106  if(fabs(mat[1][1]-(-1)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
107  if(fabs(mat[1][2]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
108 
109  if(fabs(mat[2][0]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
110  if(fabs(mat[2][1]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
111  if(fabs(mat[2][2]-(-1)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
112  if(fabs(mat[2][5]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
113 
114  if(fabs(mat[3][0]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
115  if(fabs(mat[3][1]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
116  if(fabs(mat[3][2]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
117  if(fabs(mat[3][5]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
118 
119  if(fabs(mat[4][0]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
120  if(fabs(mat[4][1]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
121  if(fabs(mat[4][2]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
122  if(fabs(mat[4][5]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
123 
124  if(fabs(mat[5][0]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
125  if(fabs(mat[5][1]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
126  if(fabs(mat[5][2]-(0)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
127  if(fabs(mat[5][5]-(-1)) > std::numeric_limits<double>::epsilon()*1e10) return -1;
128 
129  return 0;
130 }
131 
132 void initScene(const vpHomogeneousMatrix& cMo, const vpHomogeneousMatrix& cdMo,
133  vpMomentObject &src, vpMomentObject &dst)
134 {
135  std::vector<vpPoint> src_pts;
136  std::vector<vpPoint> dst_pts;
137 
138  double x[5] = { 0.2, 0.2,-0.2,-0.2, 0.2 };
139  double y[5] = {-0.1, 0.1, 0.1,-0.1,-0.1 };
140  int nbpoints = 4;
141 
142  for (int i = 0 ; i < nbpoints ; i++){
143  vpPoint p;
144  p.setWorldCoordinates(x[i],y[i],0.0);
145  p.track(cMo) ;
146  src_pts.push_back(p);
147  }
148 
150  src.fromVector(src_pts);
151  for (int i = 0 ; i < nbpoints ; i++){
152  vpPoint p;
153  p.setWorldCoordinates(x[i],y[i],0.0);
154  p.track(cdMo) ;
155  dst_pts.push_back(p);
156  }
158  dst.fromVector(dst_pts);
159 }
160 
161 
162 vpMatrix execute(const vpHomogeneousMatrix& cMo, const vpHomogeneousMatrix& cdMo,
163  vpMomentObject &src, vpMomentObject &dst)
164 {
165  vpServo::vpServoIteractionMatrixType interaction_type = vpServo::CURRENT; ; //current or desired
166 
167  vpServo task;
169  //A,B,C parameters of source and destination plane
170  double A; double B; double C;
171  double Ad; double Bd; double Cd;
172  //init main object: using moments up to order 6
173 
174  //Initializing values from regular plane (with ax+by+cz=d convention)
175  vpPlane pl;
176  pl.setABCD(0,0,1.0,0);
177  pl.changeFrame(cMo);
178  planeToABC(pl,A,B,C);
179 
180  pl.setABCD(0,0,1.0,0);
181  pl.changeFrame(cdMo);
182  planeToABC(pl,Ad,Bd,Cd);
183 
184  //extracting initial position (actually we only care about Zdst)
186  cdMo.extract(vec);
187 
189  //don't need to be specific, vpMomentCommon automatically loads Xg,Yg,An,Ci,Cj,Alpha moments
192  //same thing with common features
193  vpFeatureMomentCommon featureMoments(moments);
194  vpFeatureMomentCommon featureMomentsDes(momentsDes);
195 
196  moments.updateAll(src);
197  momentsDes.updateAll(dst);
198 
199  featureMoments.updateAll(A,B,C);
200  featureMomentsDes.updateAll(Ad,Bd,Cd);
201 
202  //setup the interaction type
203  task.setInteractionMatrixType(interaction_type) ;
205  task.addFeature(featureMoments.getFeatureGravityNormalized(),featureMomentsDes.getFeatureGravityNormalized());
206  task.addFeature(featureMoments.getFeatureAn(),featureMomentsDes.getFeatureAn());
207  //the moments are different in case of a symmetric object
208  task.addFeature(featureMoments.getFeatureCInvariant(),featureMomentsDes.getFeatureCInvariant(),(1 << 10) | (1 << 11));
209  task.addFeature(featureMoments.getFeatureAlpha(),featureMomentsDes.getFeatureAlpha());
210 
211  task.setLambda(0.4) ;
212 
213  task.computeControlLaw();
214  vpMatrix mat = task.computeInteractionMatrix();
215  task.kill();
216  return mat;
217 }
218 
219 
220 void planeToABC(const vpPlane& pl, double& A,double& B, double& C)
221 {
222  A=-pl.getA()/pl.getD();
223  B=-pl.getB()/pl.getD();
224  C=-pl.getC()/pl.getD();
225 }
226 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
This class allows to access common vpFeatureMoments in a pre-filled database.
error that can be emited by ViSP classes.
Definition: vpException.h:76
void setABCD(const double a, const double b, const double c, const double d)
Definition: vpPlane.h:98
Class for generic objects.
void track(const vpHomogeneousMatrix &cMo)
Class that defines what is a point.
Definition: vpPoint.h:65
vpMatrix computeInteractionMatrix()
Definition: vpServo.cpp:607
void kill()
Definition: vpServo.cpp:189
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
vpFeatureMomentAlpha & getFeatureAlpha()
void updateAll(double A, double B, double C)
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition: vpPlane.cpp:344
static std::vector< double > getMu3(vpMomentObject &object)
void setLambda(double c)
Definition: vpServo.h:370
void extract(vpRotationMatrix &R) const
vpServoIteractionMatrixType
Definition: vpServo.h:187
void fromVector(std::vector< vpPoint > &points)
static double getSurface(vpMomentObject &object)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
vpFeatureMomentCInvariant & getFeatureCInvariant()
static double rad(double deg)
Definition: vpMath.h:100
void updateAll(vpMomentObject &object)
This class initializes and allows access to commonly used moments.
static double getAlpha(vpMomentObject &object)
vpFeatureMomentGravityCenterNormalized & getFeatureGravityNormalized()
double getB() const
Definition: vpPlane.h:113
void setType(vpObjectType input_type)
double getA() const
Definition: vpPlane.h:111
double getC() const
Definition: vpPlane.h:115
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:67
vpFeatureMomentAreaNormalized & getFeatureAn()
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
Class that consider the case of a translation vector.
double getD() const
Definition: vpPlane.h:117
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74