Visual Servoing Platform  version 3.4.0
mbot-apriltag-pbvs.cpp
1 #include <visp3/core/vpXmlParserCamera.h>
3 #include <visp3/core/vpSerial.h>
4 #include <visp3/detection/vpDetectorAprilTag.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/sensor/vpV4l2Grabber.h>
7 #include <visp3/io/vpImageIo.h>
8 #include <visp3/visual_features/vpFeaturePoint3D.h>
9 #include <visp3/vs/vpServo.h>
10 #include <visp3/robot/vpUnicycle.h>
11 
12 int main(int argc, const char **argv)
13 {
14 #if defined(VISP_HAVE_APRILTAG) && defined(VISP_HAVE_V4L2)
15  int device = 0;
18  double tagSize = 0.065;
19  float quad_decimate = 4.0;
20  int nThreads = 2;
21  std::string intrinsic_file = "";
22  std::string camera_name = "";
23  bool display_tag = false;
24  bool display_on = false;
25  bool serial_off = false;
26  bool save_image = false; // Only possible if display_on = true
27 
28  for (int i = 1; i < argc; i++) {
29  if (std::string(argv[i]) == "--tag_size" && i + 1 < argc) {
30  tagSize = std::atof(argv[i + 1]);
31  } else if (std::string(argv[i]) == "--input" && i + 1 < argc) {
32  device = std::atoi(argv[i + 1]);
33  } else if (std::string(argv[i]) == "--quad_decimate" && i + 1 < argc) {
34  quad_decimate = (float)atof(argv[i + 1]);
35  } else if (std::string(argv[i]) == "--nthreads" && i + 1 < argc) {
36  nThreads = std::atoi(argv[i + 1]);
37  } else if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
38  intrinsic_file = std::string(argv[i + 1]);
39  } else if (std::string(argv[i]) == "--camera_name" && i + 1 < argc) {
40  camera_name = std::string(argv[i + 1]);
41  } else if (std::string(argv[i]) == "--display_tag") {
42  display_tag = true;
43 #if defined(VISP_HAVE_X11)
44  } else if (std::string(argv[i]) == "--display_on") {
45  display_on = true;
46  } else if (std::string(argv[i]) == "--save_image") {
47  save_image = true;
48 #endif
49  } else if (std::string(argv[i]) == "--serial_off") {
50  serial_off = true;
51  } else if (std::string(argv[i]) == "--tag_family" && i + 1 < argc) {
52  tagFamily = (vpDetectorAprilTag::vpAprilTagFamily)atoi(argv[i + 1]);
53  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
54  std::cout << "Usage: " << argv[0]
55  << " [--input <camera input>] [--tag_size <tag_size in m>]"
56  " [--quad_decimate <quad_decimate>] [--nthreads <nb>]"
57  " [--intrinsic <intrinsic file>] [--camera_name <camera name>]"
58  " [--tag_family <family> (0: TAG_36h11, 1: TAG_36h10, 2: TAG_36ARTOOLKIT,"
59  " 3: TAG_25h9, 4: TAG_25h7, 5: TAG_16h5)]"
60  " [--display_tag]";
61 #if defined(VISP_HAVE_X11)
62  std::cout << " [--display_on] [--save_image]";
63 #endif
64  std::cout << " [--serial_off] [--help]" << std::endl;
65  return EXIT_SUCCESS;
66  }
67  }
68 
69  // Me Auriga led ring
70  // if serial com ok: led 1 green
71  // if exception: led 1 red
72  // if tag detected: led 2 green, else led 2 red
73  // if motor left: led 3 blue
74  // if motor right: led 4 blue
75 
76  vpSerial *serial = NULL;
77  if (! serial_off) {
78  serial = new vpSerial("/dev/ttyAMA0", 115200);
79 
80  serial->write("LED_RING=0,0,0,0\n"); // Switch off all led
81  serial->write("LED_RING=1,0,10,0\n"); // Switch on led 1 to green: serial ok
82  }
83 
84  try {
86 
87  vpV4l2Grabber g;
88  std::ostringstream device_name;
89  device_name << "/dev/video" << device;
90  g.setDevice(device_name.str());
91  g.setScale(1);
92  g.acquire(I);
93 
94  vpDisplay *d = NULL;
96 #ifdef VISP_HAVE_X11
97  if (display_on) {
98  d = new vpDisplayX(I);
99  }
100 #endif
101 
102  vpCameraParameters cam;
103  cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, I.getWidth() / 2., I.getHeight() / 2.);
104  vpXmlParserCamera parser;
105  if (!intrinsic_file.empty() && !camera_name.empty())
106  parser.parse(cam, intrinsic_file, camera_name, vpCameraParameters::perspectiveProjWithoutDistortion);
107 
108  std::cout << "cam:\n" << cam << std::endl;
109  std::cout << "tagFamily: " << tagFamily << std::endl;
110 
111  vpDetectorAprilTag detector(tagFamily);
112 
113  detector.setAprilTagQuadDecimate(quad_decimate);
114  detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
115  detector.setAprilTagNbThreads(nThreads);
116  detector.setDisplayTag(display_tag);
117 
118  vpServo task;
119  vpAdaptiveGain lambda;
120  if (display_on)
121  lambda.initStandard(2.5, 0.4, 30); // lambda(0)=2.5, lambda(oo)=0.4 and lambda'(0)=30
122  else
123  lambda.initStandard(4, 0.4, 30); // lambda(0)=4, lambda(oo)=0.4 and lambda'(0)=30
124 
125  vpUnicycle robot;
128  task.setLambda(lambda);
129  vpRotationMatrix cRe;
130  cRe[0][0] = 0; cRe[0][1] = -1; cRe[0][2] = 0;
131  cRe[1][0] = 0; cRe[1][1] = 0; cRe[1][2] = -1;
132  cRe[2][0] = 1; cRe[2][1] = 0; cRe[2][2] = 0;
133 
135  vpVelocityTwistMatrix cVe(cMe);
136  task.set_cVe(cVe);
137 
138  vpMatrix eJe(6, 2, 0);
139  eJe[0][0] = eJe[5][1] = 1.0;
140 
141  std::cout << "eJe: \n" << eJe << std::endl;
142 
143  // Desired distance to the target
144  double Z_d = 0.4;
145  double X = 0, Y = 0, Z = Z_d;
146 
147  // Create X_3D visual features
148  vpFeaturePoint3D s_XZ, s_XZ_d;
149  s_XZ.buildFrom(0, 0, Z_d);
150  s_XZ_d.buildFrom(0, 0, Z_d);
151 
152  // Create Point 3D X, Z coordinates visual features
153  s_XZ.buildFrom(X, Y, Z);
154  s_XZ_d.buildFrom(0, 0, Z_d); // The value of s* is X=Y=0 and Z=Z_d meter
155 
156  // Add the features
158 
159  std::vector<double> time_vec;
160  for (;;) {
161  g.acquire(I);
162 
164 
165  double t = vpTime::measureTimeMs();
166  std::vector<vpHomogeneousMatrix> cMo_vec;
167  detector.detect(I, tagSize, cam, cMo_vec);
168 
169  t = vpTime::measureTimeMs() - t;
170  time_vec.push_back(t);
171 
172  {
173  std::stringstream ss;
174  ss << "Detection time: " << t << " ms";
175  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
176  }
177 
178  if (detector.getNbObjects() == 1) {
179  // Display visual features
180  vpHomogeneousMatrix cdMo(0, 0, Z_d, 0, 0, 0);
181  vpDisplay::displayFrame(I, cMo_vec[0], cam, tagSize / 2, vpColor::none, 3);
182  vpDisplay::displayFrame(I, cdMo, cam, tagSize / 3, vpColor::red, 3);
183 
184  if (! serial_off) {
185  serial->write("LED_RING=2,0,10,0\n"); // Switch on led 2 to green: tag detected
186  }
187 
188  X = cMo_vec[0][0][3];
189  Y = cMo_vec[0][1][3];
190  Z = cMo_vec[0][2][3];
191 
192  // Update Point 3D feature
193  s_XZ.set_XYZ(X, Y, Z);
194 
195  std::cout << "X: " << X << " Z: " << Z << std::endl;
196 
197  task.set_cVe(cVe);
198  task.set_eJe(eJe);
199 
200  // Compute the control law. Velocities are computed in the mobile robot reference frame
201  vpColVector v = task.computeControlLaw();
202 
203  std::cout << "Send velocity to the mbot: " << v[0] << " m/s " << vpMath::deg(v[1]) << " deg/s" << std::endl;
204 
205  task.print();
206  double radius = 0.0325;
207  double L = 0.0725;
208  double motor_left = (-v[0] - L * v[1]) / radius;
209  double motor_right = ( v[0] - L * v[1]) / radius;
210  std::cout << "motor left vel: " << motor_left << " motor right vel: " << motor_right << std::endl;
211  if (! serial_off) {
212 // serial->write("LED_RING=3,0,0,10\n"); // Switch on led 3 to blue: motor left servoed
213 // serial->write("LED_RING=4,0,0,10\n"); // Switch on led 4 to blue: motor right servoed
214  }
215  std::stringstream ss;
216  double rpm_left = motor_left * 30. / M_PI;
217  double rpm_right = motor_right * 30. / M_PI;
218  ss << "MOTOR_RPM=" << vpMath::round(rpm_left) << "," << vpMath::round(rpm_right) << "\n";
219  std::cout << "Send: " << ss.str() << std::endl;
220  if (! serial_off) {
221  serial->write(ss.str());
222  }
223  }
224  else {
225  // stop the robot
226  if (! serial_off) {
227  serial->write("LED_RING=2,10,0,0\n"); // Switch on led 2 to red: tag not detected
228 // serial->write("LED_RING=3,0,0,0\n"); // Switch on led 3 to blue: motor left not servoed
229 // serial->write("LED_RING=4,0,0,0\n"); // Switch on led 4 to blue: motor right not servoed
230  serial->write("MOTOR_RPM=0,-0\n"); // Stop the robot
231  }
232  }
233 
234  vpDisplay::displayText(I, 20, 20, "Click to quit.", vpColor::red);
235  vpDisplay::flush(I);
236  if (display_on && save_image) {
237  vpDisplay::getImage(I, O);
238  vpImageIo::write(O, "image.png");
239  }
240  if (vpDisplay::getClick(I, false))
241  break;
242  }
243 
244  if (! serial_off) {
245  serial->write("LED_RING=0,0,0,0\n"); // Switch off all led
246  }
247 
248  std::cout << "Benchmark computation time" << std::endl;
249  std::cout << "Mean / Median / Std: " << vpMath::getMean(time_vec) << " ms"
250  << " ; " << vpMath::getMedian(time_vec) << " ms"
251  << " ; " << vpMath::getStdev(time_vec) << " ms" << std::endl;
252 
253  if (display_on)
254  delete d;
255  if (! serial_off) {
256  delete serial;
257  }
258  } catch (const vpException &e) {
259  std::cerr << "Catch an exception: " << e.getMessage() << std::endl;
260  if (! serial_off) {
261  serial->write("LED_RING=1,10,0,0\n"); // Switch on led 1 to red
262  }
263  }
264 
265  return EXIT_SUCCESS;
266 #else
267  (void)argc;
268  (void)argv;
269 #ifndef VISP_HAVE_APRILTAG
270  std::cout << "ViSP is not build with Apriltag support" << std::endl;
271 #endif
272 #ifndef VISP_HAVE_V4L2
273  std::cout << "ViSP is not build with v4l2 support" << std::endl;
274 #endif
275  std::cout << "Install missing 3rd parties, configure and build ViSP to run this tutorial" << std::endl;
276  return EXIT_SUCCESS;
277 #endif
278 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
void acquire(vpImage< unsigned char > &I)
Adaptive gain computation.
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:177
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod)
void set_XYZ(double X, double Y, double Z)
unsigned int getWidth() const
Definition: vpImage.h:246
Implementation of an homogeneous matrix and operations on such kind of matrices.
AprilTag 36h11 pattern (recommended)
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:220
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:250
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
void setDevice(const std::string &devname)
static const vpColor none
Definition: vpColor.h:229
error that can be emited by ViSP classes.
Definition: vpException.h:71
size_t getNbObjects() const
void buildFrom(const vpPoint &p)
XML parser to load and save intrinsic camera parameters.
static void flush(const vpImage< unsigned char > &I)
void write(const std::string &s)
Definition: vpSerial.cpp:343
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:217
Implementation of a rotation matrix and operations on such kind of matrices.
void setAprilTagQuadDecimate(float quadDecimate)
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:445
Generic functions for unicycle mobile robots.
Definition: vpUnicycle.h:56
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:200
void initStandard(double gain_at_zero, double gain_at_infinity, double slope_at_zero)
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
Class that defines the 3D point visual feature.
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void setAprilTagNbThreads(int nThreads)
void setLambda(double c)
Definition: vpServo.h:404
static unsigned int selectZ()
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:144
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0)
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static int round(double x)
Definition: vpMath.h:245
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
static double deg(double rad)
Definition: vpMath.h:103
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
void setDisplayTag(bool display, const vpColor &color=vpColor::none, unsigned int thickness=2)
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
unsigned int getHeight() const
Definition: vpImage.h:188
static unsigned int selectX()
const char * getMessage() const
Definition: vpException.cpp:90
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
Class that consider the case of a translation vector.
bool detect(const vpImage< unsigned char > &I)