ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
manServo4PointsDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: manServo4PointsDisplay.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 {
77  // sets the initial camera location
78  vpHomogeneousMatrix cMo(0.3,0.2,3,
80 
82  // initialize the robot
83  vpRobotCamera robot ;
84  robot.setSamplingTime(0.04); // 40ms
85  robot.setPosition(cMo) ;
86 
87  //initialize the camera parameters
88  vpCameraParameters cam(800,800,240,180);
89 
90  //Image definition
91  unsigned int height = 360 ;
92  unsigned int width = 480 ;
93  vpImage<unsigned char> I(height,width);
94 
95  //Display initialization
96  vpDisplayGTK disp;
97  disp.init(I,100,100,"Simulation display");
98 
100  // Desired visual features initialization
101 
102  // sets the points coordinates in the object frame (in meter)
103  vpPoint point[4] ;
104  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
105  point[1].setWorldCoordinates(0.1,-0.1,0) ;
106  point[2].setWorldCoordinates(0.1,0.1,0) ;
107  point[3].setWorldCoordinates(-0.1,0.1,0) ;
108 
109  // sets the desired camera location
110  vpHomogeneousMatrix cMo_d(0,0,1,0,0,0) ;
111 
112  // computes the 3D point coordinates in the camera frame and its 2D coordinates
113  for (int i = 0 ; i < 4 ; i++)
114  point[i].project(cMo_d) ;
115 
116  // creates the associated features
117  vpFeaturePoint pd[4] ;
118  for (int i = 0 ; i < 4 ; i++)
119  vpFeatureBuilder::create(pd[i],point[i]) ;
120 
121 
123  // Current visual features initialization
124 
125  // computes the 3D point coordinates in the camera frame and its 2D coordinates
126  for (int i = 0 ; i < 4 ; i++)
127  point[i].project(cMo) ;
128 
129  // creates the associated features
130  vpFeaturePoint p[4] ;
131  for (int i = 0 ; i < 4 ; i++)
132  vpFeatureBuilder::create(p[i],point[i]) ;
133 
134 
136  // Task defintion
137  vpServo task ;
138  // we want an eye-in-hand control law ;
141 
142  // Set the position of the camera in the end-effector frame
143  vpHomogeneousMatrix cMe ;
144  vpVelocityTwistMatrix cVe(cMe) ;
145  task.set_cVe(cVe) ;
146  // Set the Jacobian (expressed in the end-effector frame)
147  vpMatrix eJe ;
148  robot.get_eJe(eJe) ;
149  task.set_eJe(eJe) ;
150 
151  // we want to see a point on a point
152  for (int i = 0 ; i < 4 ; i++)
153  task.addFeature(p[i],pd[i]) ;
154  // Set the gain
155  task.setLambda(1.0) ;
156  // Print the current information about the task
157  task.print();
158 
159 
161  // The control loop
162  int k = 0;
163  while(k++ < 200){
164  double t = vpTime::measureTimeMs();
165 
166  // Display the image background
168 
169  // Update the current features
170  for (int i = 0 ; i < 4 ; i++)
171  {
172  point[i].project(cMo) ;
173  vpFeatureBuilder::create(p[i],point[i]) ;
174  }
175 
176  // Display the task features (current and desired)
177  vpServoDisplay::display(task,cam,I);
178  vpDisplay::flush(I);
179 
180  // Update the robot Jacobian
181  robot.get_eJe(eJe) ;
182  task.set_eJe(eJe) ;
183 
184  // Compute the control law
185  vpColVector v = task.computeControlLaw() ;
186 
187  // Send the computed velocity to the robot and compute the new robot position
189  robot.getPosition(cMo) ;
190 
191  // Print the current information about the task
192  task.print();
193 
194  // Wait 40 ms
195  vpTime::wait(t,40);
196  }
197  task.kill();
198  return 0;
199 }
200 
201 #else
202 int
203 main()
204 { vpTRACE("You should install GTK") ;
205 
206 }
207 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
void setPosition(const vpHomogeneousMatrix &cMw)
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
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
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines the simplest robot: a free flying camera.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
Generic class defining intrinsic camera parameters.
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)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
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 print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
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