63 #include <visp3/core/vpCameraParameters.h> 64 #include <visp3/core/vpXmlParserCamera.h> 65 #include <visp3/detection/vpDetectorAprilTag.h> 66 #include <visp3/gui/vpDisplayGDI.h> 67 #include <visp3/gui/vpDisplayX.h> 68 #include <visp3/gui/vpPlot.h> 69 #include <visp3/io/vpImageIo.h> 70 #include <visp3/robot/vpRobotFlirPtu.h> 71 #include <visp3/sensor/vpFlyCaptureGrabber.h> 72 #include <visp3/visual_features/vpFeatureBuilder.h> 73 #include <visp3/visual_features/vpFeaturePoint.h> 74 #include <visp3/vs/vpServo.h> 75 #include <visp3/vs/vpServoDisplay.h> 77 #if defined(VISP_HAVE_FLIR_PTU_SDK) && defined(VISP_HAVE_FLYCAPTURE) && \ 78 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) 81 std::vector<vpImagePoint> &traj_ip)
86 traj_ip.push_back(ip);
89 traj_ip.push_back(ip);
91 for (
size_t j = 1; j < traj_ip.size(); j++) {
96 int main(
int argc,
char **argv)
98 std::string opt_portname;
99 int opt_baudrate = 9600;
100 bool opt_network =
false;
101 std::string opt_extrinsic;
102 std::string opt_intrinsic;
103 std::string opt_camera_name;
104 bool display_tag =
true;
105 int opt_quad_decimate = 2;
106 double opt_tag_size = 0.120;
107 bool opt_verbose =
false;
108 bool opt_plot =
false;
109 bool opt_adaptive_gain =
false;
110 bool opt_task_sequencing =
false;
111 double opt_constant_gain = 0.5;
112 bool opt_display_trajectory =
true;
113 double convergence_threshold = 0.00005;
116 std::cout <<
"To see how to use this example, run: " << argv[0] <<
" --help" << std::endl;
120 for (
int i = 1; i < argc; i++) {
121 if ((std::string(argv[i]) ==
"--portname" || std::string(argv[i]) ==
"-p") && (i + 1 < argc)) {
122 opt_portname = std::string(argv[i + 1]);
123 }
else if ((std::string(argv[i]) ==
"--baudrate" || std::string(argv[i]) ==
"-b") && (i + 1 < argc)) {
124 opt_baudrate = std::atoi(argv[i + 1]);
125 }
else if ((std::string(argv[i]) ==
"--network" || std::string(argv[i]) ==
"-n")) {
127 }
else if (std::string(argv[i]) ==
"--extrinsic" && i + 1 < argc) {
128 opt_extrinsic = std::string(argv[i + 1]);
129 }
else if (std::string(argv[i]) ==
"--intrinsic" && i + 1 < argc) {
130 opt_intrinsic = std::string(argv[i + 1]);
131 }
else if (std::string(argv[i]) ==
"--camera-name" && i + 1 < argc) {
132 opt_camera_name = std::string(argv[i + 1]);
133 }
else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
135 }
else if (std::string(argv[i]) ==
"--plot" || std::string(argv[i]) ==
"-p") {
137 }
else if (std::string(argv[i]) ==
"--display-image-trajectory" || std::string(argv[i]) ==
"-traj") {
138 opt_display_trajectory =
true;
139 }
else if (std::string(argv[i]) ==
"--adaptive-gain" || std::string(argv[i]) ==
"-a") {
140 opt_adaptive_gain =
true;
141 }
else if (std::string(argv[i]) ==
"--constant-gain" || std::string(argv[i]) ==
"-g") {
142 opt_constant_gain = std::stod(argv[i + 1]);
144 }
else if (std::string(argv[i]) ==
"--task-sequencing") {
145 opt_task_sequencing =
true;
146 }
else if (std::string(argv[i]) ==
"--quad-decimate" && i + 1 < argc) {
147 opt_quad_decimate = std::stoi(argv[i + 1]);
149 if (std::string(argv[i]) ==
"--tag-size" && i + 1 < argc) {
150 opt_tag_size = std::stod(argv[i + 1]);
151 }
else if (std::string(argv[i]) ==
"--no-convergence-threshold") {
152 convergence_threshold = 0.;
153 }
else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
154 std::cout <<
"SYNOPSIS" << std::endl
155 <<
" " << argv[0] <<
" [--portname <portname>] [--baudrate <rate>] [--network] " 156 <<
"[--extrinsic <extrinsic.yaml>] [--intrinsic <intrinsic.xml>] [--camera-name <name>] " 157 <<
"[--quad-decimate <decimation>] [--tag-size <size>] " 158 <<
"[--adaptive-gain] [--constant-gain] [--display-image-trajectory] [--plot] [--task-sequencing] " 159 <<
"[--no-convergence-threshold] [--verbose] [--help] [-h]" << std::endl
161 std::cout <<
"DESCRIPTION" << std::endl
162 <<
" --portname, -p <portname>" << std::endl
163 <<
" Set serial or tcp port name." << std::endl
165 <<
" --baudrate, -b <rate>" << std::endl
166 <<
" Set serial communication baud rate. Default: " << opt_baudrate <<
"." << std::endl
168 <<
" --network, -n" << std::endl
169 <<
" Get PTU network information (Hostname, IP, Gateway) and exit. " << std::endl
171 <<
" --reset, -r" << std::endl
172 <<
" Reset PTU axis and exit. " << std::endl
174 <<
" --extrinsic <extrinsic.yaml>" << std::endl
175 <<
" YAML file containing extrinsic camera parameters as a vpHomogeneousMatrix." << std::endl
176 <<
" It corresponds to the homogeneous transformation eMc, between end-effector" << std::endl
177 <<
" and camera frame." << std::endl
179 <<
" --intrinsic <intrinsic.xml>" << std::endl
180 <<
" Intrinsic camera parameters obtained after camera calibration." << std::endl
182 <<
" --camera-name <name>" << std::endl
183 <<
" Name of the camera to consider in the xml file provided for intrinsic camera parameters." 186 <<
" --quad-decimate <decimation>" << std::endl
187 <<
" Decimation factor used to detect AprilTag. Default " << opt_quad_decimate <<
"." << std::endl
189 <<
" --tag-size <size>" << std::endl
190 <<
" Width in meter or the black part of the AprilTag used as target. Default " << opt_tag_size
193 <<
" --adaptive-gain, -a" << std::endl
194 <<
" Enable adaptive gain instead of constant gain to speed up convergence. " << std::endl
196 <<
" --constant-gain, -g" << std::endl
197 <<
" Constant gain value. Default value: " << opt_constant_gain << std::endl
199 <<
" --display-image-trajectory, -traj" << std::endl
200 <<
" Display the trajectory of the target cog in the image. " << std::endl
202 <<
" --plot, -p" << std::endl
203 <<
" Enable curve plotter. " << std::endl
205 <<
" --task-sequencing" << std::endl
206 <<
" Enable task sequencing that allows to smoothly control the velocity of the robot. " << std::endl
208 <<
" --no-convergence-threshold" << std::endl
209 <<
" Disable ending servoing when it reaches the desired position." << std::endl
211 <<
" --verbose, -v" << std::endl
212 <<
" Additional printings. " << std::endl
214 <<
" --help, -h" << std::endl
215 <<
" Print this helper message. " << std::endl
217 std::cout <<
"EXAMPLE" << std::endl
218 <<
" - How to get network IP" << std::endl
220 <<
" $ " << argv[0] <<
" --portname COM1 --network" << std::endl
221 <<
" Try to connect FLIR PTU to port: COM1 with baudrate: 9600" << std::endl
223 <<
" $ " << argv[0] <<
" -p /dev/ttyUSB0 --network" << std::endl
224 <<
" Try to connect FLIR PTU to port: /dev/ttyUSB0 with baudrate: 9600" << std::endl
226 <<
" PTU HostName: PTU-5" << std::endl
227 <<
" PTU IP : 169.254.110.254" << std::endl
228 <<
" PTU Gateway : 0.0.0.0" << std::endl
229 <<
" - How to run this binary using network communication" << std::endl
230 <<
" $ " << argv[0] <<
" --portname tcp:169.254.110.254 --tag-size 0.1 --gain 0.1" << std::endl;
239 std::cout <<
"Try to connect FLIR PTU to port: " << opt_portname <<
" with baudrate: " << opt_baudrate << std::endl;
240 robot.
connect(opt_portname, opt_baudrate);
244 std::cout <<
"PTU IP : " << robot.
getNetworkIP() << std::endl;
257 eRc << 0, 0, 1, -1, 0, 0, 0, -1, 0;
258 etc << -0.1, -0.123, 0.035;
262 if (!opt_extrinsic.empty()) {
267 std::cout <<
"***************************************************************" << std::endl;
268 std::cout <<
"Warning, use hard coded values for extrinsic camera parameters." << std::endl;
269 std::cout <<
"Create a yaml file that contains the extrinsic:" << std::endl
271 <<
"$ cat eMc.yaml" << std::endl
272 <<
"rows: 4" << std::endl
273 <<
"cols: 4" << std::endl
274 <<
"data:" << std::endl
275 <<
" - [0, 0, 1, -0.1]" << std::endl
276 <<
" - [-1, 0, 0, -0.123]" << std::endl
277 <<
" - [0, -1, 0, 0.035]" << std::endl
278 <<
" - [0, 0, 0, 1]" << std::endl
280 <<
"and load this file with [--extrinsic <extrinsic.yaml] command line option, like:" << std::endl
282 <<
"$ " << argv[0] <<
"-p " << opt_portname <<
" --extrinsic eMc.yaml" << std::endl
284 std::cout <<
"***************************************************************" << std::endl;
287 std::cout <<
"Considered extrinsic transformation eMc:\n" << eMc << std::endl;
293 if (!opt_intrinsic.empty() && !opt_camera_name.empty()) {
294 #ifdef VISP_HAVE_PUGIXML 299 std::cout <<
"***************************************************************" << std::endl;
300 std::cout <<
"Warning, use hard coded values for intrinsic camera parameters." << std::endl;
301 std::cout <<
"Calibrate your camera and load the parameters from command line options, like:" << std::endl
303 <<
"$ " << argv[0] <<
"-p " << opt_portname <<
" --intrinsic camera.xml --camera-name \"Camera\"" 306 std::cout <<
"***************************************************************" << std::endl;
309 std::cout <<
"Considered intrinsic camera parameters:\n" << cam <<
"\n";
311 #if defined(VISP_HAVE_X11) 313 #elif defined(VISP_HAVE_GDI) 337 if (opt_adaptive_gain) {
344 vpPlot *plotter =
nullptr;
348 plotter =
new vpPlot(2, static_cast<int>(250 * 2), 500, static_cast<int>(I.
getWidth()) + 80, 10,
349 "Real time curves plotter");
350 plotter->
setTitle(0,
"Visual features error");
351 plotter->
setTitle(1,
"Joint velocities");
354 plotter->
setLegend(0, 0,
"error_feat_p_x");
355 plotter->
setLegend(0, 1,
"error_feat_p_y");
360 bool final_quit =
false;
361 bool has_converged =
false;
362 bool send_velocities =
false;
363 bool servo_started =
false;
364 std::vector<vpImagePoint> traj_cog;
372 std::cout << cVe << std::endl;
377 while (!has_converged && !final_quit) {
384 std::vector<vpHomogeneousMatrix> cMo_vec;
385 detector.
detect(I, opt_tag_size, cam, cMo_vec);
387 std::stringstream ss;
388 ss <<
"Left click to " << (send_velocities ?
"stop the robot" :
"servo the robot") <<
", right click to quit.";
397 double Z = cMo_vec[0][2][3];
403 std::cout <<
"Z: " << Z << std::endl;
412 if (opt_task_sequencing) {
413 if (!servo_started) {
414 if (send_velocities) {
415 servo_started =
true;
428 if (opt_display_trajectory) {
429 display_point_trajectory(I, cog, traj_cog);
434 plotter->
plot(1, iter_plot, qdot);
439 std::cout <<
"qdot: " << qdot.t() << std::endl;
444 ss <<
"error: " << error;
448 std::cout <<
"error: " << error << std::endl;
450 if (error < convergence_threshold) {
451 has_converged =
true;
452 std::cout <<
"Servo task has converged" 461 if (!send_velocities) {
477 send_velocities = !send_velocities;
490 std::cout <<
"Stop the robot " << std::endl;
493 if (opt_plot && plotter !=
nullptr) {
501 while (!final_quit) {
516 std::cout <<
"Catch Flir Ptu signal exception: " << e.
getMessage() << std::endl;
519 std::cout <<
"ViSP exception: " << e.
what() << std::endl;
520 std::cout <<
"Stop the robot " << std::endl;
529 #if !defined(VISP_HAVE_FLYCAPTURE) 530 std::cout <<
"Install FLIR Flycapture" << std::endl;
532 #if !defined(VISP_HAVE_FLIR_PTU_SDK) 533 std::cout <<
"Install FLIR PTU SDK." << std::endl;
Implementation of a matrix and operations on matrices.
Adaptive gain computation.
Error that can be emited by the vpRobot class and its derivates.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod)
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=NULL)
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)
Display for windows using GDI (available on any windows 32 platform).
void set_eJe(const vpMatrix &eJe_)
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...
void acquire(vpImage< unsigned char > &I)
std::string getNetworkHostName()
error that can be emited by ViSP classes.
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
size_t getNbObjects() const
XML parser to load and save intrinsic camera parameters.
static const vpColor green
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
void open(vpImage< unsigned char > &I)
Implementation of a rotation matrix and operations on such kind of matrices.
void setAprilTagQuadDecimate(float quadDecimate)
std::string getNetworkGateway()
Initialize the velocity controller.
vpColVector computeControlLaw()
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void set_eMc(vpHomogeneousMatrix &eMc)
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void set_xyZ(double x, double y, double Z)
void setTitle(unsigned int graphNum, const std::string &title)
void get_eJe(vpMatrix &eJe)
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
vpImagePoint getCog(size_t i) const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
const char * what() const
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)
Stops robot motion especially in velocity and acceleration control.
const char * getMessage(void) const
void connect(const std::string &portname, int baudrate=9600)
void initGraph(unsigned int graphNum, unsigned int curveNbr)
unsigned int getHeight() const
std::string getNetworkIP()
Implementation of column vector and the associated operations.
void setDisplayTag(bool display, const vpColor &color=vpColor::none, unsigned int thickness=2)
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Implementation of a pose vector and operations on poses.
vpVelocityTwistMatrix get_cVe() const
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
vpColVector getError() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
void setServo(const vpServoType &servo_type)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
Class that consider the case of a translation vector.
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)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
bool detect(const vpImage< unsigned char > &I)