56#include <visp3/core/vpCameraParameters.h>
57#include <visp3/detection/vpDetectorAprilTag.h>
58#include <visp3/gui/vpDisplayGDI.h>
59#include <visp3/gui/vpDisplayX.h>
60#include <visp3/gui/vpPlot.h>
61#include <visp3/io/vpImageIo.h>
62#include <visp3/robot/vpRobotAfma6.h>
63#include <visp3/sensor/vpRealSense2.h>
64#include <visp3/visual_features/vpFeatureBuilder.h>
65#include <visp3/visual_features/vpFeaturePoint.h>
66#include <visp3/vs/vpServo.h>
67#include <visp3/vs/vpServoDisplay.h>
69#if defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && \
70 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_AFMA6)
73 std::vector<vpImagePoint> *traj_vip)
75 for (
size_t i = 0; i < vip.size(); i++) {
76 if (traj_vip[i].size()) {
79 traj_vip[i].push_back(vip[i]);
82 traj_vip[i].push_back(vip[i]);
85 for (
size_t i = 0; i < vip.size(); i++) {
86 for (
size_t j = 1; j < traj_vip[i].size(); j++) {
92int main(
int argc,
char **argv)
94 double opt_tagSize = 0.120;
95 bool display_tag =
true;
96 int opt_quad_decimate = 2;
97 bool opt_verbose =
false;
98 bool opt_plot =
false;
99 bool opt_adaptive_gain =
false;
100 bool opt_task_sequencing =
false;
101 double convergence_threshold = 0.00005;
103 for (
int i = 1; i < argc; i++) {
104 if (std::string(argv[i]) ==
"--tag_size" && i + 1 < argc) {
105 opt_tagSize = std::stod(argv[i + 1]);
106 }
else if (std::string(argv[i]) ==
"--verbose") {
108 }
else if (std::string(argv[i]) ==
"--plot") {
110 }
else if (std::string(argv[i]) ==
"--adaptive_gain") {
111 opt_adaptive_gain =
true;
112 }
else if (std::string(argv[i]) ==
"--task_sequencing") {
113 opt_task_sequencing =
true;
114 }
else if (std::string(argv[i]) ==
"--quad_decimate" && i + 1 < argc) {
115 opt_quad_decimate = std::stoi(argv[i + 1]);
116 }
else if (std::string(argv[i]) ==
"--no-convergence-threshold") {
117 convergence_threshold = 0.;
118 }
else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
120 << argv[0] <<
" --tag_size <marker size in meter; default " << opt_tagSize <<
">] "
121 <<
"[--quad_decimate <decimation; default " << opt_quad_decimate
122 <<
">] [--adaptive_gain] [--plot] [--task_sequencing] [--no-convergence-threshold] [--verbose] [--help] [-h]"
131 std::cout <<
"WARNING: This example will move the robot! "
132 <<
"Please make sure to have the user stop button at hand!" << std::endl
133 <<
"Press Enter to continue..." << std::endl;
147 std::cout <<
"Move to joint position: " << q.t() << std::endl;
153 unsigned int width = 640, height = 480;
154 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
155 config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
156 config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
162 std::cout <<
"cam:\n" << cam <<
"\n";
166#if defined(VISP_HAVE_X11)
168#elif defined(VISP_HAVE_GDI)
176 detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
177 detector.setDisplayTag(display_tag);
178 detector.setAprilTagQuadDecimate(opt_quad_decimate);
188 std::vector<vpFeaturePoint> p(4), pd(4);
191 std::vector<vpPoint> point(4);
192 point[0].setWorldCoordinates(-opt_tagSize / 2., -opt_tagSize / 2., 0);
193 point[1].setWorldCoordinates(opt_tagSize / 2., -opt_tagSize / 2., 0);
194 point[2].setWorldCoordinates(opt_tagSize / 2., opt_tagSize / 2., 0);
195 point[3].setWorldCoordinates(-opt_tagSize / 2., opt_tagSize / 2., 0);
199 for (
size_t i = 0; i < p.size(); i++) {
205 if (opt_adaptive_gain) {
212 vpPlot *plotter =
nullptr;
216 plotter =
new vpPlot(2,
static_cast<int>(250 * 2), 500,
static_cast<int>(I.
getWidth()) + 80, 10,
217 "Real time curves plotter");
218 plotter->
setTitle(0,
"Visual features error");
219 plotter->
setTitle(1,
"Camera velocities");
222 plotter->
setLegend(0, 0,
"error_feat_p1_x");
223 plotter->
setLegend(0, 1,
"error_feat_p1_y");
224 plotter->
setLegend(0, 2,
"error_feat_p2_x");
225 plotter->
setLegend(0, 3,
"error_feat_p2_y");
226 plotter->
setLegend(0, 4,
"error_feat_p3_x");
227 plotter->
setLegend(0, 5,
"error_feat_p3_y");
228 plotter->
setLegend(0, 6,
"error_feat_p4_x");
229 plotter->
setLegend(0, 7,
"error_feat_p4_y");
238 bool final_quit =
false;
239 bool has_converged =
false;
240 bool send_velocities =
false;
241 bool servo_started =
false;
242 std::vector<vpImagePoint> *traj_corners =
nullptr;
248 while (!has_converged && !final_quit) {
255 std::vector<vpHomogeneousMatrix> cMo_vec;
256 detector.detect(I, opt_tagSize, cam, cMo_vec);
259 std::stringstream ss;
260 ss <<
"Left click to " << (send_velocities ?
"stop the robot" :
"servo the robot") <<
", right click to quit.";
267 if (cMo_vec.size() == 1) {
270 static bool first_time =
true;
273 std::vector<vpHomogeneousMatrix> v_oMo(2), v_cdMc(2);
274 v_oMo[1].buildFrom(0, 0, 0, 0, 0, M_PI);
275 for (
size_t i = 0; i < 2; i++) {
276 v_cdMc[i] = cdMo * v_oMo[i] * cMo.
inverse();
278 if (std::fabs(v_cdMc[0].getThetaUVector().getTheta()) < std::fabs(v_cdMc[1].getThetaUVector().getTheta())) {
281 std::cout <<
"Desired frame modified to avoid PI rotation of the camera" << std::endl;
286 for (
size_t i = 0; i < point.size(); i++) {
288 point[i].changeFrame(cdMo * oMo, cP);
289 point[i].projection(cP, p_);
298 std::vector<vpImagePoint> corners = detector.getPolygon(0);
301 for (
size_t i = 0; i < corners.size(); i++) {
306 point[i].changeFrame(cMo, cP);
311 if (opt_task_sequencing) {
312 if (!servo_started) {
313 if (send_velocities) {
314 servo_started =
true;
325 for (
size_t i = 0; i < corners.size(); i++) {
326 std::stringstream ss;
336 traj_corners =
new std::vector<vpImagePoint>[corners.size()];
339 display_point_trajectory(I, corners, traj_corners);
343 plotter->
plot(1, iter_plot, v_c);
348 std::cout <<
"v_c: " << v_c.t() << std::endl;
352 std::stringstream ss;
353 ss <<
"error: " << error;
357 std::cout <<
"error: " << error << std::endl;
359 if (error < convergence_threshold) {
360 has_converged =
true;
361 std::cout <<
"Servo task has converged"
373 if (!send_velocities) {
381 std::stringstream ss;
391 send_velocities = !send_velocities;
404 std::cout <<
"Stop the robot " << std::endl;
407 if (opt_plot && plotter !=
nullptr) {
413 while (!final_quit) {
428 delete[] traj_corners;
431 std::cout <<
"ViSP exception: " << e.
what() << std::endl;
432 std::cout <<
"Stop the robot " << std::endl;
435 }
catch (
const std::exception &e) {
436 std::cout <<
"ur_rtde exception: " << e.what() << std::endl;
445#if !defined(VISP_HAVE_REALSENSE2)
446 std::cout <<
"Install librealsense-2.x" << std::endl;
448#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
449 std::cout <<
"Build ViSP with c++11 or higher compiler flag (cmake -DUSE_CXX_STANDARD=11)." << std::endl;
451#if !defined(VISP_HAVE_AFMA6)
452 std::cout <<
"ViSP is not build with Afma6 robot support..." << std::endl;
Adaptive gain computation.
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with 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).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
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 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 * what() const
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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 ...
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition of the vpImage class member functions.
unsigned int getWidth() const
static double rad(double deg)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
void initGraph(unsigned int graphNum, unsigned int curveNbr)
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
void setTitle(unsigned int graphNum, const std::string &title)
void acquire(vpImage< unsigned char > &grey, double *ts=NULL)
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
bool open(const rs2::config &cfg=rs2::config())
Control of Irisa's gantry robot named Afma6.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ STATE_POSITION_CONTROL
Initialize the position controller.
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
@ STATE_STOP
Stops robot motion especially in velocity and acceleration control.
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Implementation of a rotation matrix and operations on such kind of matrices.
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
void setServo(const vpServoType &servo_type)
vpColVector getError() const
vpColVector computeControlLaw()
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Class that consider the case of a translation vector.
VISP_EXPORT double measureTimeMs()