1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpPolygon.h>
4 #include <visp3/core/vpSerial.h>
5 #include <visp3/core/vpXmlParserCamera.h>
6 #include <visp3/detection/vpDetectorAprilTag.h>
7 #include <visp3/gui/vpDisplayX.h>
8 #include <visp3/io/vpImageIo.h>
9 #include <visp3/robot/vpUnicycle.h>
10 #include <visp3/sensor/vpV4l2Grabber.h>
11 #include <visp3/visual_features/vpFeatureBuilder.h>
12 #include <visp3/visual_features/vpFeatureDepth.h>
13 #include <visp3/visual_features/vpFeaturePoint.h>
14 #include <visp3/vs/vpServo.h>
16 int main(
int argc,
const char **argv)
18 #if defined(VISP_HAVE_APRILTAG) && defined(VISP_HAVE_V4L2)
19 #ifdef ENABLE_VISP_NAMESPACE
26 double tagSize = 0.065;
27 float quad_decimate = 4.0;
29 std::string intrinsic_file =
"";
30 std::string camera_name =
"";
31 bool display_tag =
false;
32 bool display_on =
false;
33 bool serial_off =
false;
35 bool save_image =
false;
37 for (
int i = 1; i < argc; i++) {
38 if (std::string(argv[i]) ==
"--without_pose_computation") {
41 else if (std::string(argv[i]) ==
"--tag_size" && i + 1 < argc) {
42 tagSize = std::atof(argv[i + 1]);
44 else if (std::string(argv[i]) ==
"--input" && i + 1 < argc) {
45 device = std::atoi(argv[i + 1]);
47 else if (std::string(argv[i]) ==
"--quad_decimate" && i + 1 < argc) {
48 quad_decimate = (float)atof(argv[i + 1]);
50 else if (std::string(argv[i]) ==
"--nthreads" && i + 1 < argc) {
51 nThreads = std::atoi(argv[i + 1]);
53 else if (std::string(argv[i]) ==
"--intrinsic" && i + 1 < argc) {
54 intrinsic_file = std::string(argv[i + 1]);
56 else if (std::string(argv[i]) ==
"--camera_name" && i + 1 < argc) {
57 camera_name = std::string(argv[i + 1]);
59 else if (std::string(argv[i]) ==
"--display_tag") {
61 #if defined(VISP_HAVE_X11)
63 else if (std::string(argv[i]) ==
"--display_on") {
66 else if (std::string(argv[i]) ==
"--save_image") {
70 else if (std::string(argv[i]) ==
"--serial_off") {
73 else if (std::string(argv[i]) ==
"--tag_family" && i + 1 < argc) {
76 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
77 std::cout <<
"Usage: " << argv[0]
78 <<
" [--input <camera input>] [--tag_size <tag_size in m>]"
79 " [--quad_decimate <quad_decimate>] [--nthreads <nb>]"
80 " [--intrinsic <intrinsic file>] [--camera_name <camera name>] [--without_pose_computation]"
81 " [--tag_family <family> (0: TAG_36h11, 1: TAG_36h10, 2: TAG_36ARTOOLKIT,"
82 " 3: TAG_25h9, 4: TAG_25h7, 5: TAG_16h5)]"
84 #if defined(VISP_HAVE_X11)
85 std::cout <<
" [--display_on] [--save_image]";
87 std::cout <<
" [--serial_off] [--help]" << std::endl;
101 serial =
new vpSerial(
"/dev/ttyAMA0", 115200);
103 serial->
write(
"LED_RING=0,0,0,0\n");
104 serial->
write(
"LED_RING=1,0,10,0\n");
111 std::ostringstream device_name;
112 device_name <<
"/dev/video" << device;
121 d =
new vpDisplayX(I);
128 if (!intrinsic_file.empty() && !camera_name.empty())
131 std::cout <<
"cam:\n" << cam << std::endl;
132 std::cout <<
"use pose: " << use_pose << std::endl;
133 std::cout <<
"tagFamily: " << tagFamily << std::endl;
137 detector.setAprilTagQuadDecimate(quad_decimate);
139 detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
140 detector.setAprilTagNbThreads(nThreads);
141 detector.setDisplayTag(display_tag);
170 eJe[0][0] = eJe[5][1] = 1.0;
172 std::cout <<
"eJe: \n" << eJe << std::endl;
192 std::cout <<
"Z " << Z << std::endl;
199 std::vector<double> time_vec;
206 std::vector<vpHomogeneousMatrix> cMo_vec;
208 detector.detect(I, tagSize, cam, cMo_vec);
213 time_vec.push_back(t);
216 std::stringstream ss;
217 ss <<
"Detection time: " << t <<
" ms";
221 if (detector.getNbObjects() == 1) {
235 serial->
write(
"LED_RING=2,0,10,0\n");
239 Z = cMo_vec[0][2][3];
242 vpPolygon polygon(detector.getPolygon(0));
243 double surface = polygon.getArea();
244 std::cout <<
"Surface: " << surface << std::endl;
247 Z = tagSize * cam.
get_px() / sqrt(surface);
256 std::cout <<
"cog: " << detector.getCog(0) <<
" Z: " << Z << std::endl;
264 std::cout <<
"Send velocity to the mbot: " << v[0] <<
" m/s " <<
vpMath::deg(v[1]) <<
" deg/s" << std::endl;
267 double radius = 0.0325;
269 double motor_left = (-v[0] - L * v[1]) / radius;
270 double motor_right = (v[0] - L * v[1]) / radius;
271 std::cout <<
"motor left vel: " << motor_left <<
" motor right vel: " << motor_right << std::endl;
276 std::stringstream ss;
277 double rpm_left = motor_left * 30. / M_PI;
278 double rpm_right = motor_right * 30. / M_PI;
280 std::cout <<
"Send: " << ss.str() << std::endl;
282 serial->
write(ss.str());
288 serial->
write(
"LED_RING=2,10,0,0\n");
291 serial->
write(
"MOTOR_RPM=0,-0\n");
297 if (display_on && save_image) {
306 serial->
write(
"LED_RING=0,0,0,0\n");
309 std::cout <<
"Benchmark computation time" << std::endl;
310 std::cout <<
"Mean / Median / Std: " <<
vpMath::getMean(time_vec) <<
" ms"
321 std::cerr <<
"Catch an exception: " << e.
getMessage() << std::endl;
323 serial->
write(
"LED_RING=1,10,0,0\n");
331 #ifndef VISP_HAVE_APRILTAG
332 std::cout <<
"ViSP is not build with Apriltag support" << std::endl;
334 #ifndef VISP_HAVE_V4L2
335 std::cout <<
"ViSP is not build with v4l2 support" << std::endl;
337 std::cout <<
"Install missing 3rd parties, configure and build ViSP to run this tutorial" << std::endl;
Adaptive gain computation.
void initStandard(double gain_at_zero, double gain_at_infinity, double slope_at_zero)
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
static const vpColor none
static const vpColor green
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
Class that defines generic functionalities for display.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
const char * getMessage() const
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
vpFeatureDepth & buildFrom(const double &x, const double &y, const double &Z, const double &LogZoverZstar)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
static unsigned int selectX()
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
unsigned int getWidth() const
unsigned int getHeight() const
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static int round(double x)
static double getMean(const std::vector< double > &v)
static double deg(double rad)
Implementation of a matrix and operations on matrices.
Defines a generic 2D polygon.
Implementation of a rotation matrix and operations on such kind of matrices.
void write(const std::string &s)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
void set_cVe(const vpVelocityTwistMatrix &cVe_)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
void set_eJe(const vpMatrix &eJe_)
void setServo(const vpServoType &servo_type)
vpColVector computeControlLaw()
Class that consider the case of a translation vector.
Generic functions for unicycle mobile robots.
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
XML parser to load and save intrinsic camera parameters.
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, bool verbose=true)
VISP_EXPORT double measureTimeMs()