tutorial-ros-pioneer-visual-servo.cpp
Go to the documentation of this file.
1 
21 #include <iostream>
22 
23 #include <visp/vpCameraParameters.h>
24 #include <visp/vpDisplayX.h>
25 #include <visp/vpDot2.h>
26 #include <visp/vpFeatureBuilder.h>
27 #include <visp/vpFeatureDepth.h>
28 #include <visp/vpFeaturePoint.h>
29 #include <visp/vpHomogeneousMatrix.h>
30 #include <visp/vpImage.h>
31 #include <visp/vpImageConvert.h>
32 #include <visp/vpServo.h>
33 #include <visp/vpVelocityTwistMatrix.h>
34 
35 #include <visp_ros/vpROSGrabber.h>
37 
38 #if defined(VISP_HAVE_DC1394_2) && defined(VISP_HAVE_X11)
39 # define TEST_COULD_BE_ACHIEVED
40 #endif
41 
42 #ifdef TEST_COULD_BE_ACHIEVED
43 int main(int argc, char **argv)
44 {
45  try {
46  vpImage<unsigned char> I; // Create a gray level image container
47  double depth = 1.;
48  double lambda = 0.6;
49  double coef = 1./6.77; // Scale parameter used to estimate the depth Z of the blob from its surface
50 
51  vpROSRobotPioneer robot;
52  robot.setCmdVelTopic("/RosAria/cmd_vel");
53  robot.init();
54 
55  // Wait 3 sec to be sure that the low level Aria thread used to control
56  // the robot is started. Without this delay we experienced a delay (arround 2.2 sec)
57  // between the velocity send to the robot and the velocity that is really applied
58  // to the wheels.
59  vpTime::sleepMs(3000);
60 
61  std::cout << "Robot connected" << std::endl;
62 
63  // Camera parameters. In this experiment we don't need a precise calibration of the camera
64  vpCameraParameters cam;
65 
66  // Create a grabber based on libdc1394-2.x third party lib (for firewire cameras under Linux)
67  vpROSGrabber g;
68  g.setCameraInfoTopic("/camera/camera_info");
69  g.setImageTopic("/camera/image_raw");
70  g.setRectify(true);
71 
72  // Get camera parameters from /camera/camera_info topic
73  if (g.getCameraInfo(cam) == false)
74  cam.initPersProjWithoutDistortion(600,600,I.getWidth()/2, I.getHeight()/2);
75 
76  g.acquire(I);
77 
78  // Create an image viewer
79  vpDisplayX d(I, 10, 10, "Current frame");
80  vpDisplay::display(I);
81  vpDisplay::flush(I);
82 
83  // Create a blob tracker
84  vpDot2 dot;
85  dot.setGraphics(true);
86  dot.setComputeMoments(true);
87  dot.setEllipsoidShapePrecision(0.); // to track a blob without any constraint on the shape
88  dot.setGrayLevelPrecision(0.9); // to set the blob gray level bounds for binarisation
89  dot.setEllipsoidBadPointsPercentage(0.5); // to be accept 50% of bad inner and outside points with bad gray level
90  dot.initTracking(I);
91  vpDisplay::flush(I);
92 
93  vpServo task;
94  task.setServo(vpServo::EYEINHAND_L_cVe_eJe) ;
95  task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE) ;
96  task.setLambda(lambda) ;
97  vpVelocityTwistMatrix cVe ;
98  cVe = robot.get_cVe() ;
99  task.set_cVe(cVe) ;
100 
101  std::cout << "cVe: \n" << cVe << std::endl;
102 
103  vpMatrix eJe;
104  robot.get_eJe(eJe) ;
105  task.set_eJe(eJe) ;
106  std::cout << "eJe: \n" << eJe << std::endl;
107 
108  // Current and desired visual feature associated to the x coordinate of the point
109  vpFeaturePoint s_x, s_xd;
110 
111  // Create the current x visual feature
112  vpFeatureBuilder::create(s_x, cam, dot);
113 
114  // Create the desired x* visual feature
115  s_xd.buildFrom(0, 0, depth);
116 
117  // Add the feature
118  task.addFeature(s_x, s_xd) ;
119 
120  // Create the current log(Z/Z*) visual feature
121  vpFeatureDepth s_Z, s_Zd;
122  // Surface of the blob estimated from the image moment m00 and converted in meters
123  double surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
124  double Z, Zd;
125  // Initial depth of the blob in from of the camera
126  Z = coef * surface ;
127  // Desired depth Z* of the blob. This depth is learned and equal to the initial depth
128  Zd = Z;
129 
130  std::cout << "Z " << Z << std::endl;
131  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z , 0); // log(Z/Z*) = 0 that's why the last parameter is 0
132  s_Zd.buildFrom(s_x.get_x(), s_x.get_y(), Zd , 0); // log(Z/Z*) = 0 that's why the last parameter is 0
133 
134  // Add the feature
135  task.addFeature(s_Z, s_Zd) ;
136 
137  vpColVector v; // vz, wx
138 
139  while(1)
140  {
141  // Acquire a new image
142  g.acquire(I);
143 
144  // Set the image as background of the viewer
145  vpDisplay::display(I);
146 
147  // Does the blob tracking
148  dot.track(I);
149  // Update the current x feature
150  vpFeatureBuilder::create(s_x, cam, dot);
151 
152  // Update log(Z/Z*) feature. Since the depth Z change, we need to update the intection matrix
153  surface = 1./sqrt(dot.m00/(cam.get_px()*cam.get_py()));
154  Z = coef * surface ;
155  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd)) ;
156 
157  robot.get_cVe(cVe) ;
158  task.set_cVe(cVe) ;
159 
160  robot.get_eJe(eJe) ;
161  task.set_eJe(eJe) ;
162 
163  // Compute the control law. Velocities are computed in the mobile robot reference frame
164  v = task.computeControlLaw() ;
165 
166  std::cout << "Send velocity to the pionner: " << v[0] << " m/s "
167  << vpMath::deg(v[1]) << " deg/s" << std::endl;
168 
169  // Send the velocity to the robot
170  robot.setVelocity(vpRobot::REFERENCE_FRAME, v);
171 
172  // Draw a vertical line which corresponds to the desired x coordinate of the dot cog
173  vpDisplay::displayLine(I, 0, 320, 479, 320, vpColor::red);
174  vpDisplay::flush(I);
175 
176  // A click in the viewer to exit
177  if ( vpDisplay::getClick(I, false) )
178  break;
179  }
180 
181  std::cout << "Ending robot thread..." << std::endl;
182 
183  // Kill the servo task
184  task.print() ;
185  task.kill();
186  }
187  catch(vpException e) {
188  std::cout << "Catch an exception: " << e << std::endl;
189  return 1;
190  }
191 }
192 #else
193 int main()
194 {
195  std::cout << "You don't have the right 3rd party libraries to run this example..." << std::endl;
196 }
197 #endif
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
d
Class for cameras video capture for ROS cameras.
Definition: vpROSGrabber.h:104
void setCmdVelTopic(std::string topic_name)
Definition: vpROSRobot.cpp:285
void setRectify(bool rectify)
void init()
basic initialization
void setCameraInfoTopic(std::string topic_name)
int main(int argc, char **argv)
bool getCameraInfo(vpCameraParameters &cam)
TFSIMD_FORCE_INLINE tfScalar dot(const Quaternion &q1, const Quaternion &q2)
class for Camera video capture for ROS middleware.
void get_eJe(vpMatrix &eJe)
Interface for Pioneer mobile robots based on ROS.
void setImageTopic(std::string topic_name)
void acquire(vpImage< unsigned char > &I)


visp_ros
Author(s): Francois Pasteau, Fabien Spindler, Gatien Gaumerais
autogenerated on Fri Jul 3 2020 03:41:43