Visual Servoing Platform  version 3.6.1 under development (2025-03-06)
visp-acquire-universal-robots-calib-data.cpp
/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2025 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Universal Robots (UR5 or UR10) data acquisition to prepare hand-eye calibration.
*/
#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpCameraParameters.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpXmlParserCamera.h>
#include <visp3/gui/vpDisplayFactory.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/robot/vpRobotUniversalRobots.h>
#include <visp3/sensor/vpRealSense2.h>
#if defined(VISP_HAVE_REALSENSE2) && \
defined(VISP_HAVE_DISPLAY) && defined(VISP_HAVE_UR_RTDE) && defined(VISP_HAVE_PUGIXML) && \
defined(VISP_HAVE_MODULE_GUI) && defined(VISP_HAVE_MODULE_ROBOT) && defined(VISP_HAVE_MODULE_SENSOR) // optional
void usage(const char **argv, int error, const std::string &robot_ip)
{
std::cout << "Synopsis" << std::endl
<< " " << argv[0]
<< " [--ip <address>] [--output-folder <name>] [--help, -h]" << std::endl
<< std::endl;
std::cout << "Description" << std::endl
<< " --ip <address>" << std::endl
<< " Ethernet address to dial with the Panda robot." << std::endl
<< " Default: " << robot_ip << std::endl
<< std::endl
<< " --output-folder <name>" << std::endl
<< " Acquired data output folder." << std::endl
<< " Default: ./" << std::endl
<< std::endl
<< " --help, -h Print this helper message." << std::endl
<< std::endl;
if (error) {
std::cout << "Error" << std::endl
<< " "
<< "Unsupported parameter " << argv[error] << std::endl;
}
}
int main(int argc, const char **argv)
{
#if defined(ENABLE_VISP_NAMESPACE)
using namespace VISP_NAMESPACE_NAME;
#endif
#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
vpDisplay *pdisp = nullptr;
#endif
try {
std::string opt_robot_ip = "192.168.0.100";
std::string opt_output_folder = "./";
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
opt_robot_ip = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--output-folder" && i + 1 < argc) {
opt_output_folder = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
usage(argv, 0, opt_robot_ip);
return EXIT_SUCCESS;
}
else {
usage(argv, i, opt_robot_ip);
return EXIT_FAILURE;
}
}
// Create output folder if necessary
if (!vpIoTools::checkDirectory(opt_output_folder)) {
std::cout << "Create output directory: " << opt_output_folder << std::endl;
vpIoTools::makeDirectory(opt_output_folder);
}
rs2::config config;
config.disable_stream(RS2_STREAM_DEPTH);
config.disable_stream(RS2_STREAM_INFRARED);
config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
g.open(config);
g.acquire(I);
unsigned int width = I.getWidth();
unsigned int height = I.getHeight();
std::cout << "Image size: " << width << " x " << height << std::endl;
// Save intrinsics
vpXmlParserCamera xml_camera;
xml_camera.save(cam, opt_output_folder + "/ur_camera.xml", "Camera", width, height);
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
std::shared_ptr<vpDisplay> pdisp = vpDisplayFactory::createDisplay(I, 10, 10, "Color image");
#else
pdisp = vpDisplayFactory::allocateDisplay(I, 10, 10, "Color image");
#endif
bool end = false;
unsigned cpt = 0;
while (!end) {
g.acquire(I);
vpDisplay::displayText(I, 15, 15, "Left click to acquire data", vpColor::red);
vpDisplay::displayText(I, 30, 15, "Right click to quit", vpColor::red);
if (vpDisplay::getClick(I, button, false)) {
if (button == vpMouseButton::button1) {
cpt++;
std::cout << "Connect to robot to get its position..." << std::endl;
robot.connect(opt_robot_ip);
robot.getPosition(vpRobot::END_EFFECTOR_FRAME, rPe);
robot.disconnect();
std::stringstream ss_img, ss_pos;
ss_img << opt_output_folder + "/ur_image-" << cpt << ".png";
ss_pos << opt_output_folder + "/ur_pose_rPe_" << cpt << ".yaml";
std::cout << "Save: " << ss_img.str() << " and " << ss_pos.str() << std::endl;
vpImageIo::write(I, ss_img.str());
rPe.saveYAML(ss_pos.str(), rPe);
}
else if (button == vpMouseButton::button3) {
end = true;
}
}
}
}
catch (const vpException &e) {
std::cerr << "ViSP exception " << e.what() << std::endl;
}
catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
if (pdisp != nullptr) {
delete pdisp;
}
#endif
return EXIT_SUCCESS;
}
#else
int main()
{
#if !defined(VISP_HAVE_MODULE_GUI)
std::cout << "visp_gui module is not available?" << std::endl;
#endif
#if !defined(VISP_HAVE_MODULE_ROBOT)
std::cout << "visp_robot module is not available?" << std::endl;
#endif
#if !defined(VISP_HAVE_MODULE_SENSOR)
std::cout << "visp_sensor module is not available?" << std::endl;
#endif
#if !defined(VISP_HAVE_REALSENSE2)
std::cout << "Install librealsense-2.x." << std::endl;
#endif
#if !defined(VISP_HAVE_UR_RTDE)
std::cout << "ViSP is not build with libur_rtde 3rd party used to control a robot from Universal Robots..."
<< std::endl;
#endif
#if !defined(VISP_HAVE_PUGIXML)
std::cout << "Enable pugyxml built-in usage." << std::endl;
#endif
std::cout << "After installation of the missing 3rd parties, configure ViSP with cmake"
<< " and build ViSP again." << std::endl;
return EXIT_SUCCESS;
}
#endif
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
Definition: vpArray2D.h:1057
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:178
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
const char * what() const
Definition: vpException.cpp:71
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
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:83
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.