Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
visp-acquire-universal-robots-calib-data.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Universal Robots (UR5 or UR10) data acquisition to prepare hand-eye calibration.
32 */
33
35#include <iostream>
36
37#include <visp3/core/vpConfig.h>
38#include <visp3/core/vpCameraParameters.h>
39#include <visp3/core/vpIoTools.h>
40#include <visp3/core/vpXmlParserCamera.h>
41#include <visp3/gui/vpDisplayFactory.h>
42#include <visp3/io/vpImageIo.h>
43#include <visp3/robot/vpRobotUniversalRobots.h>
44#include <visp3/sensor/vpRealSense2.h>
45
46#if defined(VISP_HAVE_REALSENSE2) && \
47 defined(VISP_HAVE_DISPLAY) && defined(VISP_HAVE_UR_RTDE) && defined(VISP_HAVE_PUGIXML) && \
48 defined(VISP_HAVE_MODULE_GUI) && defined(VISP_HAVE_MODULE_ROBOT) && defined(VISP_HAVE_MODULE_SENSOR) // optional
49
50void usage(const char **argv, int error, const std::string &robot_ip)
51{
52 std::cout << "Synopsis" << std::endl
53 << " " << argv[0]
54 << " [--ip <address>] [--output-folder <name>] [--help, -h]" << std::endl
55 << std::endl;
56 std::cout << "Description" << std::endl
57 << " --ip <address>" << std::endl
58 << " Ethernet address to dial with the Panda robot." << std::endl
59 << " Default: " << robot_ip << std::endl
60 << std::endl
61 << " --output-folder <name>" << std::endl
62 << " Acquired data output folder." << std::endl
63 << " Default: ./" << std::endl
64 << std::endl
65 << " --help, -h Print this helper message." << std::endl
66 << std::endl;
67 if (error) {
68 std::cout << "Error" << std::endl
69 << " "
70 << "Unsupported parameter " << argv[error] << std::endl;
71 }
72}
73
74int main(int argc, const char **argv)
75{
76#if defined(ENABLE_VISP_NAMESPACE)
77 using namespace VISP_NAMESPACE_NAME;
78#endif
79#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
80 vpDisplay *pdisp = nullptr;
81#endif
82 try {
83 std::string opt_robot_ip = "192.168.0.100";
84 std::string opt_output_folder = "./";
85
86 for (int i = 1; i < argc; i++) {
87 if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
88 opt_robot_ip = std::string(argv[++i]);
89 }
90 else if (std::string(argv[i]) == "--output-folder" && i + 1 < argc) {
91 opt_output_folder = std::string(argv[++i]);
92 }
93 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
94 usage(argv, 0, opt_robot_ip);
95 return EXIT_SUCCESS;
96 }
97 else {
98 usage(argv, i, opt_robot_ip);
99 return EXIT_FAILURE;
100 }
101 }
102
103 // Create output folder if necessary
104 if (!vpIoTools::checkDirectory(opt_output_folder)) {
105 std::cout << "Create output directory: " << opt_output_folder << std::endl;
106 vpIoTools::makeDirectory(opt_output_folder);
107 }
108
110
112
113 vpRealSense2 g;
114 rs2::config config;
115 config.disable_stream(RS2_STREAM_DEPTH);
116 config.disable_stream(RS2_STREAM_INFRARED);
117 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
118 g.open(config);
119 g.acquire(I);
120
121 unsigned int width = I.getWidth();
122 unsigned int height = I.getHeight();
123
124 std::cout << "Image size: " << width << " x " << height << std::endl;
125 // Save intrinsics
127 vpXmlParserCamera xml_camera;
129 xml_camera.save(cam, opt_output_folder + "/ur_camera.xml", "Camera", width, height);
130
131#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
132 std::shared_ptr<vpDisplay> pdisp = vpDisplayFactory::createDisplay(I, 10, 10, "Color image");
133#else
134 pdisp = vpDisplayFactory::allocateDisplay(I, 10, 10, "Color image");
135#endif
136
137 bool end = false;
138 unsigned cpt = 0;
139 while (!end) {
140 g.acquire(I);
141
143
144 vpDisplay::displayText(I, 15, 15, "Left click to acquire data", vpColor::red);
145 vpDisplay::displayText(I, 30, 15, "Right click to quit", vpColor::red);
147 if (vpDisplay::getClick(I, button, false)) {
148 if (button == vpMouseButton::button1) {
149 cpt++;
150
151 vpPoseVector rPe;
152 std::cout << "Connect to robot to get its position..." << std::endl;
153 robot.connect(opt_robot_ip);
154 robot.getPosition(vpRobot::END_EFFECTOR_FRAME, rPe);
155 robot.disconnect();
156
157 std::stringstream ss_img, ss_pos;
158
159 ss_img << opt_output_folder + "/ur_image-" << cpt << ".png";
160 ss_pos << opt_output_folder + "/ur_pose_rPe_" << cpt << ".yaml";
161 std::cout << "Save: " << ss_img.str() << " and " << ss_pos.str() << std::endl;
162 vpImageIo::write(I, ss_img.str());
163 rPe.saveYAML(ss_pos.str(), rPe);
164 }
165 else if (button == vpMouseButton::button3) {
166 end = true;
167 }
168 }
170 }
171 }
172 catch (const vpException &e) {
173 std::cerr << "ViSP exception " << e.what() << std::endl;
174 }
175 catch (const std::exception &e) {
176 std::cerr << e.what() << std::endl;
177 }
178#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
179 if (pdisp != nullptr) {
180 delete pdisp;
181 }
182#endif
183 return EXIT_SUCCESS;
184}
185#else
186int main()
187{
188#if !defined(VISP_HAVE_MODULE_GUI)
189 std::cout << "visp_gui module is not available?" << std::endl;
190#endif
191#if !defined(VISP_HAVE_MODULE_ROBOT)
192 std::cout << "visp_robot module is not available?" << std::endl;
193#endif
194#if !defined(VISP_HAVE_MODULE_SENSOR)
195 std::cout << "visp_sensor module is not available?" << std::endl;
196#endif
197#if !defined(VISP_HAVE_REALSENSE2)
198 std::cout << "Install librealsense-2.x." << std::endl;
199#endif
200#if !defined(VISP_HAVE_UR_RTDE)
201 std::cout << "ViSP is not build with libur_rtde 3rd party used to control a robot from Universal Robots..."
202 << std::endl;
203#endif
204#if !defined(VISP_HAVE_PUGIXML)
205 std::cout << "Enable pugyxml built-in usage." << std::endl;
206#endif
207
208 std::cout << "After installation of the missing 3rd parties, configure ViSP with cmake"
209 << " and build ViSP again." << std::endl;
210 return EXIT_SUCCESS;
211}
212#endif
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
Definition vpArray2D.h:1061
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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.
Definition vpException.h:60
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static bool checkDirectory(const std::string &dirname)
static void makeDirectory(const std::string &dirname)
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())
@ END_EFFECTOR_FRAME
Definition vpRobot.h:80
XML parser to load and save intrinsic camera parameters.
int save(const vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, unsigned int image_width=0, unsigned int image_height=0, const std::string &additionalInfo="", bool verbose=true)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.