ViSP
manServo4PointsDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: manServo4PointsDisplay.cpp 4574 2014-01-09 08:48:51Z fspindle $
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  * Description:
34  * Simulation of a visual servoing with display.
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
52 #include <visp/vpConfig.h>
53 #include <visp/vpDebug.h>
54 
55 #ifdef VISP_HAVE_GTK
56 
57 #include <visp/vpImage.h>
58 #include <visp/vpCameraParameters.h>
59 #include <visp/vpTime.h>
60 #include <visp/vpImage.h>
61 #include <visp/vpImageConvert.h>
62 #include <visp/vpDisplayGTK.h>
63 
64 #include <visp/vpMath.h>
65 #include <visp/vpHomogeneousMatrix.h>
66 #include <visp/vpPose.h>
67 #include <visp/vpFeaturePoint.h>
68 #include <visp/vpServo.h>
69 #include <visp/vpServoDisplay.h>
70 #include <visp/vpRobotCamera.h>
71 #include <visp/vpFeatureBuilder.h>
72 #include <visp/vpIoTools.h>
73 
74 int main()
75 {
76  try {
78  // sets the initial camera location
79  vpHomogeneousMatrix cMo(0.3,0.2,3,
81 
83  // initialize the robot
84  vpRobotCamera robot ;
85  robot.setSamplingTime(0.04); // 40ms
86  robot.setPosition(cMo) ;
87 
88  //initialize the camera parameters
89  vpCameraParameters cam(800,800,240,180);
90 
91  //Image definition
92  unsigned int height = 360 ;
93  unsigned int width = 480 ;
94  vpImage<unsigned char> I(height,width);
95 
96  //Display initialization
97  vpDisplayGTK disp;
98  disp.init(I,100,100,"Simulation display");
99 
101  // Desired visual features initialization
102 
103  // sets the points coordinates in the object frame (in meter)
104  vpPoint point[4] ;
105  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
106  point[1].setWorldCoordinates(0.1,-0.1,0) ;
107  point[2].setWorldCoordinates(0.1,0.1,0) ;
108  point[3].setWorldCoordinates(-0.1,0.1,0) ;
109 
110  // sets the desired camera location
111  vpHomogeneousMatrix cMo_d(0,0,1,0,0,0) ;
112 
113  // computes the 3D point coordinates in the camera frame and its 2D coordinates
114  for (int i = 0 ; i < 4 ; i++)
115  point[i].project(cMo_d) ;
116 
117  // creates the associated features
118  vpFeaturePoint pd[4] ;
119  for (int i = 0 ; i < 4 ; i++)
120  vpFeatureBuilder::create(pd[i],point[i]) ;
121 
122 
124  // Current visual features initialization
125 
126  // computes the 3D point coordinates in the camera frame and its 2D coordinates
127  for (int i = 0 ; i < 4 ; i++)
128  point[i].project(cMo) ;
129 
130  // creates the associated features
131  vpFeaturePoint p[4] ;
132  for (int i = 0 ; i < 4 ; i++)
133  vpFeatureBuilder::create(p[i],point[i]) ;
134 
135 
137  // Task defintion
138  vpServo task ;
139  // we want an eye-in-hand control law ;
142 
143  // Set the position of the camera in the end-effector frame
144  vpHomogeneousMatrix cMe ;
145  vpVelocityTwistMatrix cVe(cMe) ;
146  task.set_cVe(cVe) ;
147  // Set the Jacobian (expressed in the end-effector frame)
148  vpMatrix eJe ;
149  robot.get_eJe(eJe) ;
150  task.set_eJe(eJe) ;
151 
152  // we want to see a point on a point
153  for (int i = 0 ; i < 4 ; i++)
154  task.addFeature(p[i],pd[i]) ;
155  // Set the gain
156  task.setLambda(1.0) ;
157  // Print the current information about the task
158  task.print();
159 
160 
162  // The control loop
163  int k = 0;
164  while(k++ < 200){
165  double t = vpTime::measureTimeMs();
166 
167  // Display the image background
169 
170  // Update the current features
171  for (int i = 0 ; i < 4 ; i++)
172  {
173  point[i].project(cMo) ;
174  vpFeatureBuilder::create(p[i],point[i]) ;
175  }
176 
177  // Display the task features (current and desired)
178  vpServoDisplay::display(task,cam,I);
179  vpDisplay::flush(I);
180 
181  // Update the robot Jacobian
182  robot.get_eJe(eJe) ;
183  task.set_eJe(eJe) ;
184 
185  // Compute the control law
186  vpColVector v = task.computeControlLaw() ;
187 
188  // Send the computed velocity to the robot and compute the new robot position
190  robot.getPosition(cMo) ;
191 
192  // Print the current information about the task
193  task.print();
194 
195  // Wait 40 ms
196  vpTime::wait(t,40);
197  }
198  task.kill();
199  return 0;
200  }
201  catch(vpException e) {
202  std::cout << "Catch an exception: " << e << std::endl;
203  return 1;
204  }
205 }
206 
207 #else
208 int
209 main()
210 { vpTRACE("You should install GTK") ;
211 
212 }
213 #endif
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...
#define vpTRACE
Definition: vpDebug.h:418
void setPosition(const vpHomogeneousMatrix &cMw)
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
Class that defines the simplest robot: a free flying camera.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void getPosition(vpHomogeneousMatrix &cMw) const
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void get_eJe(vpMatrix &eJe)
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
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