8#include <RobotsIO/Camera/RealsenseCameraYarp.h>
10#include <yarp/dev/IRGBDSensor.h>
11#include <yarp/dev/PolyDriver.h>
12#include <yarp/os/Property.h>
14using namespace RobotsIO::Camera;
15using namespace yarp::dev;
16using namespace yarp::os;
19RealsenseCameraYarp::RealsenseCameraYarp(
const std::string& port_prefix,
const std::size_t& width,
const std::size_t& height) :
24RealsenseCameraYarp::RealsenseCameraYarp(
const std::string& port_prefix) :
29RealsenseCameraYarp::RealsenseCameraYarp(
const std::string& port_prefix,
const bool& enforce_resolution,
const std::size_t& width,
const std::size_t& height) :
33 yarp::dev::PolyDriver driver;
34 yarp::dev::IRGBDSensor* interface;
36 Property driver_properties;
37 driver_properties.put(
"device",
"RGBDSensorClient");
38 driver_properties.put(
"localImagePort",
"/" + port_prefix +
"/RGBDSensorClient/image:i");
39 driver_properties.put(
"localDepthPort",
"/" + port_prefix +
"/RGBDSensorClient/depth:i");
40 driver_properties.put(
"localRpcPort",
"/" + port_prefix +
"/RGBDSensorClient/rpc:i");
41 driver_properties.put(
"remoteImagePort",
"/depthCamera/rgbImage:o");
42 driver_properties.put(
"remoteDepthPort",
"/depthCamera/depthImage:o");
43 driver_properties.put(
"remoteRpcPort",
"/depthCamera/rpc:i");
45 if (driver.open(driver_properties) && driver.view(interface) && (interface !=
nullptr))
47 Property camera_intrinsics;
48 interface->getDepthIntrinsicParam(camera_intrinsics);
50 std::size_t camera_width = interface->getRgbWidth();
51 std::size_t camera_height = interface->getRgbHeight();
53 double scaler_x = 1.0;
54 double scaler_y = 1.0;
55 if (enforce_resolution)
57 if ((width > camera_width) || (height > camera_height))
58 throw(std::runtime_error(log_name_ +
"::ctor. Cannot enforce a resolution higher than the source resolution"));
60 scaler_x = width / camera_width;
61 scaler_y = height / camera_height;
64 parameters_.width(camera_width * scaler_x);
65 parameters_.height(camera_height * scaler_y);
66 parameters_.fx(camera_intrinsics.find(
"focalLengthX").asFloat64() * scaler_x);
67 parameters_.fy(camera_intrinsics.find(
"focalLengthY").asFloat64() * scaler_y);
68 parameters_.cx(camera_intrinsics.find(
"principalPointX").asFloat64() * scaler_x);
69 parameters_.cy(camera_intrinsics.find(
"principalPointY").asFloat64() * scaler_y);
70 parameters_.initialized(
true);
75 throw(std::runtime_error(log_name_ +
"::ctor. Cannot get camera parameters."));
80 std::cout << log_name_ +
"::ctor. Camera parameters:" << std::endl;
81 std::cout << log_name_ +
" - width: " << parameters_.width() << std::endl;
82 std::cout << log_name_ +
" - height: " << parameters_.height() << std::endl;
83 std::cout << log_name_ +
" - fx: " << parameters_.fx() << std::endl;
84 std::cout << log_name_ +
" - fy: " << parameters_.fy() << std::endl;
85 std::cout << log_name_ +
" - cx: " << parameters_.cx() << std::endl;
86 std::cout << log_name_ +
" - cy: " << parameters_.cy() << std::endl;
90RealsenseCameraYarp::~RealsenseCameraYarp()