61 #include <visp3/core/vpCameraParameters.h>
62 #include <visp3/core/vpConfig.h>
63 #include <visp3/detection/vpDetectorAprilTag.h>
64 #include <visp3/gui/vpDisplayGDI.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/gui/vpPlot.h>
67 #include <visp3/io/vpImageIo.h>
68 #include <visp3/robot/vpRobotFranka.h>
69 #include <visp3/sensor/vpRealSense2.h>
70 #include <visp3/visual_features/vpFeatureThetaU.h>
71 #include <visp3/visual_features/vpFeatureTranslation.h>
72 #include <visp3/vs/vpServo.h>
73 #include <visp3/vs/vpServoDisplay.h>
75 #if defined(VISP_HAVE_REALSENSE2) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_FRANKA)
77 #ifdef ENABLE_VISP_NAMESPACE
82 std::vector<vpImagePoint> *traj_vip)
84 for (
size_t i = 0; i < vip.size(); i++) {
85 if (traj_vip[i].size()) {
88 traj_vip[i].push_back(vip[i]);
92 traj_vip[i].push_back(vip[i]);
95 for (
size_t i = 0; i < vip.size(); i++) {
96 for (
size_t j = 1; j < traj_vip[i].size(); j++) {
102 int main(
int argc,
char **argv)
104 double opt_tagSize = 0.120;
105 std::string opt_robot_ip =
"192.168.1.1";
106 std::string opt_eMc_filename =
"";
107 bool display_tag =
true;
108 int opt_quad_decimate = 2;
109 bool opt_verbose =
false;
110 bool opt_plot =
false;
111 bool opt_adaptive_gain =
false;
112 bool opt_task_sequencing =
false;
113 double convergence_threshold_t = 0.0005;
114 double convergence_threshold_tu = 0.5;
116 for (
int i = 1; i < argc; i++) {
117 if ((std::string(argv[i]) ==
"--tag_size") && (i + 1 < argc)) {
118 opt_tagSize = std::stod(argv[i + 1]);
121 else if ((std::string(argv[i]) ==
"--ip") && (i + 1 < argc)) {
122 opt_robot_ip = std::string(argv[i + 1]);
125 else if ((std::string(argv[i]) ==
"--eMc") && (i + 1 < argc)) {
126 opt_eMc_filename = std::string(argv[i + 1]);
129 else if (std::string(argv[i]) ==
"--verbose") {
132 else if (std::string(argv[i]) ==
"--plot") {
135 else if (std::string(argv[i]) ==
"--adaptive_gain") {
136 opt_adaptive_gain =
true;
138 else if (std::string(argv[i]) ==
"--task_sequencing") {
139 opt_task_sequencing =
true;
141 else if ((std::string(argv[i]) ==
"--quad_decimate") && (i + 1 < argc)) {
142 opt_quad_decimate = std::stoi(argv[i + 1]);
145 else if (std::string(argv[i]) ==
"--no-convergence-threshold") {
146 convergence_threshold_t = 0.;
147 convergence_threshold_tu = 0.;
149 else if ((std::string(argv[i]) ==
"--help") || (std::string(argv[i]) ==
"-h")) {
150 std::cout <<
"SYNOPSYS" << std::endl
152 <<
" [--ip <controller ip>]"
153 <<
" [--tag-size <size>]"
154 <<
" [--eMc <extrinsic transformation file>]"
155 <<
" [--quad-decimate <decimation factor>]"
156 <<
" [--adaptive-gain]"
158 <<
" [--task-sequencing]"
159 <<
" [--no-convergence-threshold]"
161 <<
" [--help] [-h]\n"
163 std::cout <<
"DESCRIPTION" << std::endl
164 <<
" Use a position-based visual-servoing scheme to position the camera in front of an Apriltag." << std::endl
166 <<
" --ip <controller ip>" << std::endl
167 <<
" Franka controller ip address" << std::endl
168 <<
" Default: " << opt_robot_ip << std::endl
170 <<
" --tag-size <size>" << std::endl
171 <<
" Apriltag size in [m]." << std::endl
172 <<
" Default: " << opt_tagSize <<
" [m]" << std::endl
174 <<
" --eMc <extrinsic transformation file>" << std::endl
175 <<
" File containing the homogeneous transformation matrix between" << std::endl
176 <<
" robot end-effector and camera frame." << std::endl
178 <<
" --quad-decimate <decimation factor>" << std::endl
179 <<
" Decimation factor used during Apriltag detection." << std::endl
180 <<
" Default: " << opt_quad_decimate << std::endl
182 <<
" --adaptive-gain" << std::endl
183 <<
" Flag to enable adaptive gain to speed up visual servo near convergence." << std::endl
185 <<
" --plot" << std::endl
186 <<
" Flag to enable curve plotter." << std::endl
188 <<
" --task-sequencing" << std::endl
189 <<
" Flag to enable task sequencing scheme." << std::endl
191 <<
" --no-convergence-threshold" << std::endl
192 <<
" Flag to disable convergence threshold used to stop the visual servo." << std::endl
194 <<
" --verbose" << std::endl
195 <<
" Flag to enable extra verbosity." << std::endl
197 <<
" --help, -h" << std::endl
198 <<
" Print this helper message." << std::endl
207 robot.connect(opt_robot_ip);
211 unsigned int width = 640, height = 480;
212 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
213 config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
214 config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
221 ePc[1] = -0.00535012;
228 if (!opt_eMc_filename.empty()) {
229 ePc.
loadYAML(opt_eMc_filename, ePc);
232 std::cout <<
"Warning, opt_eMc_filename is empty! Use hard coded values." << std::endl;
235 std::cout <<
"eMc:\n" << eMc << std::endl;
239 std::cout <<
"cam:\n" << cam << std::endl;
243 #if defined(VISP_HAVE_X11)
244 vpDisplayX dc(I, 10, 10,
"Color image");
245 #elif defined(VISP_HAVE_GDI)
253 detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
254 detector.setDisplayTag(display_tag);
255 detector.setAprilTagQuadDecimate(opt_quad_decimate);
279 if (opt_adaptive_gain) {
287 vpPlot *plotter =
nullptr;
291 plotter =
new vpPlot(2,
static_cast<int>(250 * 2), 500,
static_cast<int>(I.
getWidth()) + 80, 10,
292 "Real time curves plotter");
293 plotter->
setTitle(0,
"Visual features error");
294 plotter->
setTitle(1,
"Camera velocities");
297 plotter->
setLegend(0, 0,
"error_feat_tx");
298 plotter->
setLegend(0, 1,
"error_feat_ty");
299 plotter->
setLegend(0, 2,
"error_feat_tz");
300 plotter->
setLegend(0, 3,
"error_feat_theta_ux");
301 plotter->
setLegend(0, 4,
"error_feat_theta_uy");
302 plotter->
setLegend(0, 5,
"error_feat_theta_uz");
311 bool final_quit =
false;
312 bool has_converged =
false;
313 bool send_velocities =
false;
314 bool servo_started =
false;
315 std::vector<vpImagePoint> *traj_vip =
nullptr;
322 while (!has_converged && !final_quit) {
329 std::vector<vpHomogeneousMatrix> cMo_vec;
330 detector.detect(I, opt_tagSize, cam, cMo_vec);
332 std::stringstream ss;
333 ss <<
"Left click to " << (send_velocities ?
"stop the robot" :
"servo the robot") <<
", right click to quit.";
339 if (cMo_vec.size() == 1) {
342 static bool first_time =
true;
345 std::vector<vpHomogeneousMatrix> v_oMo(2), v_cdMc(2);
346 v_oMo[1].buildFrom(0, 0, 0, 0, 0, M_PI);
347 for (
size_t i = 0; i < 2; i++) {
348 v_cdMc[i] = cdMo * v_oMo[i] * cMo.
inverse();
350 if (std::fabs(v_cdMc[0].getThetaUVector().getTheta()) < std::fabs(v_cdMc[1].getThetaUVector().getTheta())) {
354 std::cout <<
"Desired frame modified to avoid PI rotation of the camera" << std::endl;
360 cdMc = cdMo * oMo * cMo.
inverse();
364 if (opt_task_sequencing) {
365 if (!servo_started) {
366 if (send_velocities) {
367 servo_started =
true;
381 std::vector<vpImagePoint> vip = detector.getPolygon(0);
383 vip.push_back(detector.getCog(0));
386 traj_vip =
new std::vector<vpImagePoint>[vip.size()];
388 display_point_trajectory(I, vip, traj_vip);
392 plotter->
plot(1, iter_plot, v_c);
397 std::cout <<
"v_c: " << v_c.t() << std::endl;
402 double error_tr = sqrt(cd_t_c.
sumSquare());
406 ss <<
"error_t: " << error_tr;
409 ss <<
"error_tu: " << error_tu;
413 std::cout <<
"error translation: " << error_tr <<
" ; error rotation: " << error_tu << std::endl;
415 if (error_tr < convergence_threshold_t && error_tu < convergence_threshold_tu) {
416 has_converged =
true;
417 std::cout <<
"Servo task has converged" << std::endl;
430 if (!send_velocities) {
446 send_velocities = !send_velocities;
459 std::cout <<
"Stop the robot " << std::endl;
462 if (opt_plot && plotter !=
nullptr) {
468 while (!final_quit) {
488 std::cout <<
"ViSP exception: " << e.
what() << std::endl;
489 std::cout <<
"Stop the robot " << std::endl;
493 catch (
const franka::NetworkException &e) {
494 std::cout <<
"Franka network exception: " << e.what() << std::endl;
495 std::cout <<
"Check if you are connected to the Franka robot"
496 <<
" or if you specified the right IP using --ip command line option set by default to 192.168.1.1. "
500 catch (
const std::exception &e) {
501 std::cout <<
"Franka exception: " << e.what() << std::endl;
510 #if !defined(VISP_HAVE_REALSENSE2)
511 std::cout <<
"Install librealsense-2.x" << std::endl;
513 #if !defined(VISP_HAVE_FRANKA)
514 std::cout <<
"Install libfranka." << std::endl;
Adaptive gain computation.
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=nullptr)
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
static const vpColor none
static const vpColor yellow
static const vpColor green
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
Display for windows using GDI (available on any windows 32 platform).
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 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
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Class that defines the translation visual feature .
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpThetaUVector getThetaUVector() const
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
vpTranslationVector getTranslationVector() const
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
unsigned int getWidth() const
static double deg(double rad)
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)
Implementation of a pose vector and operations on poses.
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ 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.
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 setServo(const vpServoType &servo_type)
vpColVector getError() const
vpColVector computeControlLaw()
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
VISP_EXPORT double measureTimeMs()