41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpMomentAreaNormalized.h>
43 #include <visp3/core/vpMomentBasic.h>
44 #include <visp3/core/vpMomentCentered.h>
45 #include <visp3/core/vpMomentDatabase.h>
46 #include <visp3/core/vpMomentGravityCenter.h>
47 #include <visp3/core/vpMomentGravityCenterNormalized.h>
48 #include <visp3/core/vpMomentObject.h>
49 #include <visp3/core/vpPixelMeterConversion.h>
50 #include <visp3/core/vpPoint.h>
51 #include <visp3/core/vpTime.h>
52 #include <visp3/core/vpXmlParserCamera.h>
53 #include <visp3/detection/vpDetectorAprilTag.h>
54 #include <visp3/gui/vpDisplayX.h>
55 #include <visp3/gui/vpPlot.h>
56 #include <visp3/robot/vpRobotBebop2.h>
57 #include <visp3/visual_features/vpFeatureBuilder.h>
58 #include <visp3/visual_features/vpFeatureMomentAreaNormalized.h>
59 #include <visp3/visual_features/vpFeatureMomentGravityCenterNormalized.h>
60 #include <visp3/visual_features/vpFeatureVanishingPoint.h>
61 #include <visp3/vs/vpServo.h>
62 #include <visp3/vs/vpServoDisplay.h>
64 #if !defined(VISP_HAVE_ARSDK)
67 std::cout <<
"\nThis example requires Parrot ARSDK3 library. You should install it.\n" << std::endl;
70 #elif !defined(VISP_HAVE_FFMPEG)
73 std::cout <<
"\nThis example requires ffmpeg library. You should install it.\n" << std::endl;
76 #elif !defined(VISP_HAVE_PUGIXML)
79 std::cout <<
"\nThis example requires pugixml built-in 3rdparty library. You should enable it.\n" << std::endl;
85 #ifdef ENABLE_VISP_NAMESPACE
89 bool compareImagePoint(std::pair<size_t, vpImagePoint> p1, std::pair<size_t, vpImagePoint> p2)
91 return (p1.second.get_v() < p2.second.get_v());
105 int main(
int argc,
char **argv)
109 std::string ip_address =
"192.168.42.1";
110 std::string opt_cam_parameters;
111 bool opt_has_cam_parameters =
false;
115 double opt_distance_to_tag = -1;
116 bool opt_has_distance_to_tag =
false;
120 bool verbose =
false;
122 if (argc >= 3 && std::string(argv[1]) ==
"--tag_size") {
123 tagSize = std::atof(argv[2]);
125 std::cout <<
"Error : invalid tag size." << std::endl <<
"See " << argv[0] <<
" --help" << std::endl;
128 for (
int i = 3; i < argc; i++) {
129 if (std::string(argv[i]) ==
"--ip" && i + 1 < argc) {
130 ip_address = std::string(argv[i + 1]);
133 else if (std::string(argv[i]) ==
"--distance_to_tag" && i + 1 < argc) {
134 opt_distance_to_tag = std::atof(argv[i + 1]);
135 if (opt_distance_to_tag <= 0) {
136 std::cout <<
"Error : invalid distance to tag." << std::endl <<
"See " << argv[0] <<
" --help" << std::endl;
139 opt_has_distance_to_tag =
true;
142 else if (std::string(argv[i]) ==
"--intrinsic") {
144 opt_cam_parameters = std::string(argv[i + 1]);
145 opt_has_cam_parameters =
true;
148 else if (std::string(argv[i]) ==
"--hd_stream") {
151 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
155 std::cout <<
"Error : unknown parameter " << argv[i] << std::endl
156 <<
"See " << argv[0] <<
" --help" << std::endl;
161 else if (argc >= 2 && (std::string(argv[1]) ==
"--help" || std::string(argv[1]) ==
"-h")) {
162 std::cout <<
"\nUsage:\n"
164 <<
" [--tag_size <size>] [--ip <drone ip>] [--distance_to_tag <distance>] [--intrinsic <xml file>] "
165 <<
"[--hd_stream] [--verbose] [-v] [--help] [-h]\n"
168 <<
" --tag_size <size>\n"
169 <<
" The size of the tag to detect in meters, required.\n\n"
170 <<
" --ip <drone ip>\n"
171 <<
" Ip address of the drone to which you want to connect (default : 192.168.42.1).\n\n"
172 <<
" --distance_to_tag <distance>\n"
173 <<
" The desired distance to the tag in meters (default: 1 meter).\n\n"
174 <<
" --intrinsic <xml file>\n"
175 <<
" XML file containing computed intrinsic camera parameters (default: empty).\n\n"
177 <<
" Enables HD 720p streaming instead of default 480p.\n"
178 <<
" Allows to increase range and accuracy of the tag detection,\n"
179 <<
" but increases latency and computation time.\n"
180 <<
" Caution: camera calibration settings are different for the two resolutions.\n"
181 <<
" Make sure that if you pass custom intrinsic camera parameters,\n"
182 <<
" they were obtained with the correct resolution.\n\n"
183 <<
" --verbose, -v\n"
184 <<
" Enables verbose (drone information messages and velocity commands\n"
185 <<
" are then displayed).\n\n"
187 <<
" Print help message.\n"
192 std::cout <<
"Error : tag size parameter required." << std::endl <<
"See " << argv[0] <<
" --help" << std::endl;
197 <<
"\nWARNING: \n - This program does no sensing or avoiding of "
199 "the drone WILL collide with any objects in the way! Make sure "
201 "drone has approximately 3 meters of free space on all sides.\n"
202 " - The drone uses a downward-facing camera for horizontal speed estimation,\n make sure the drone flies "
203 "above a non-uniform flooring,\n or its movement will be inacurate and dangerous !\n"
208 verbose,
true, ip_address);
210 if (drone.isRunning()) {
212 drone.setVideoResolution(stream_res);
214 drone.setStreamingMode(0);
215 drone.setVideoStabilisationMode(0);
219 drone.startStreaming();
221 drone.setExposure(1.5f);
223 drone.setCameraOrientation(-5., 0.,
229 drone.getGrayscaleImage(I);
231 #if defined(VISP_HAVE_X11)
233 #elif defined(VISP_HAVE_GTK)
235 #elif defined(VISP_HAVE_GDI)
237 #elif defined(HAVE_OPENCV_HIGHGUI)
240 int orig_displayX = 100;
241 int orig_displayY = 100;
242 display.init(I, orig_displayX, orig_displayY,
"DRONE VIEW");
246 vpPlot plotter(1, 700, 700, orig_displayX +
static_cast<int>(I.
getWidth()) + 20, orig_displayY,
247 "Visual servoing tasks");
248 unsigned int iter = 0;
252 detector.setAprilTagQuadDecimate(4.0);
253 detector.setAprilTagNbThreads(4);
254 detector.setDisplayTag(
true);
258 if (opt_has_cam_parameters) {
265 std::cout <<
"Cannot find parameters in XML file " << opt_cam_parameters << std::endl;
266 if (drone.getVideoHeight() == 720) {
275 std::cout <<
"Setting default camera parameters ... " << std::endl;
276 if (drone.getVideoHeight() == 720) {
325 double Z_d = (opt_has_distance_to_tag ? opt_distance_to_tag : 1.);
328 double X[4] = { tagSize / 2., tagSize / 2., -tagSize / 2., -tagSize / 2. };
329 double Y[4] = { tagSize / 2., -tagSize / 2., -tagSize / 2., tagSize / 2. };
330 std::vector<vpPoint> vec_P, vec_P_d;
332 for (
int i = 0; i < 4; i++) {
336 vec_P_d.push_back(P_d);
348 m_obj_d.fromVector(vec_P_d);
361 area = mb_d.
get(2, 0) + mb_d.
get(0, 2);
363 area = mb_d.
get(0, 0);
365 man_d.setDesiredArea(area);
373 double C = 1.0 / Z_d;
385 plotter.initGraph(0, 4);
386 plotter.setLegend(0, 0,
"Xn");
387 plotter.setLegend(0, 1,
"Yn");
388 plotter.setLegend(0, 2,
"an");
389 plotter.setLegend(0, 3,
"atan(1/rho)");
392 s_mgn_d.update(A, B, C);
393 s_mgn_d.compute_interaction();
395 s_man_d.update(A, B, C);
396 s_man_d.compute_interaction();
403 bool vec_ip_has_been_sorted =
false;
404 std::vector<std::pair<size_t, vpImagePoint> > vec_ip_sorted;
407 while (drone.isRunning() && drone.isStreaming() && runLoop) {
411 drone.getGrayscaleImage(I);
414 std::vector<vpHomogeneousMatrix> cMo_vec;
415 detector.detect(I, tagSize, cam, cMo_vec);
419 std::stringstream ss;
420 ss <<
"Detection time: " << t <<
" ms";
424 if (detector.getNbObjects() == 1) {
427 std::vector<vpImagePoint> vec_ip = detector.getPolygon(0);
429 for (
size_t i = 0; i < vec_ip.size(); i++) {
440 m_obj.fromVector(vec_P);
449 man.setDesiredArea(area);
453 s_mgn.update(A, B, C);
454 s_mgn.compute_interaction();
455 s_man.update(A, B, C);
456 s_man.compute_interaction();
461 if (!vec_ip_has_been_sorted) {
462 for (
size_t i = 0; i < vec_ip.size(); i++) {
465 std::pair<size_t, vpImagePoint> index_pair = std::pair<size_t, vpImagePoint>(i, vec_ip[i]);
466 vec_ip_sorted.push_back(index_pair);
470 std::sort(vec_ip_sorted.begin(), vec_ip_sorted.end(), compareImagePoint);
472 vec_ip_has_been_sorted =
true;
477 vec_ip[vec_ip_sorted[2].first], vec_ip[vec_ip_sorted[3].first],
488 std::cout <<
"ve: " << ve.
t() << std::endl;
490 drone.setVelocity(ve, 1.0);
492 for (
size_t i = 0; i < 4; i++) {
494 std::stringstream ss;
518 std::stringstream sserr;
519 sserr <<
"Failed to detect an Apriltag, or detected multiple ones";
532 plotter.plot(0, iter, task.
getError());
535 std::stringstream sstime;
536 sstime <<
"Total time: " << totalTime <<
" ms";
548 std::cout <<
"ERROR : failed to setup drone control." << std::endl;
553 std::cout <<
"Caught an exception: " << e << std::endl;
Adaptive gain computation.
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
vpCameraParametersProjType
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
static const vpColor green
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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 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)
static void displayPolygon(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, const vpColor &color, unsigned int thickness=1, bool closed=true)
error that can be emitted by ViSP classes.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Functionality computation for normalized surface moment feature. Computes the interaction matrix asso...
Functionality computation for centered and normalized moment feature. Computes the interaction matrix...
static unsigned int selectAtanOneOverRho()
void setAlpha(double alpha)
Set vanishing point feature value.
void setAtanOneOverRho(double atan_one_over_rho)
Set vanishing point feature value.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
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 rad(double deg)
Implementation of a matrix and operations on matrices.
Class handling the normalized surface moment that is invariant in scale and used to estimate depth.
This class defines the 2D basic moment . This class is a wrapper for vpMomentObject which allows to u...
const std::vector< double > & get() const
This class defines the double-indexed centered moment descriptor .
This class allows to register all vpMoments so they can access each other according to their dependen...
virtual void updateAll(vpMomentObject &object)
Class describing 2D normalized gravity center moment.
Class describing 2D gravity center moment.
Class for generic objects.
void linkTo(vpMomentDatabase &moments)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
void set_x(double x)
Set the point x coordinate in the image plane.
void set_y(double y)
Set the point y coordinate in the image plane.
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
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 set_eJe(const vpMatrix &eJe_)
void setServo(const vpServoType &servo_type)
vpColVector getError() const
vpColVector computeControlLaw()
Class that consider the case of a translation vector.
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 int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()