RobotsIO
Loading...
Searching...
No Matches
RealsenseCameraYarp.cpp
1/*
2 * Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Camera/RealsenseCameraYarp.h>
9
10#include <yarp/dev/IRGBDSensor.h>
11#include <yarp/dev/PolyDriver.h>
12#include <yarp/os/Property.h>
13
14using namespace RobotsIO::Camera;
15using namespace yarp::dev;
16using namespace yarp::os;
17
18
19RealsenseCameraYarp::RealsenseCameraYarp(const std::string& port_prefix, const std::size_t& width, const std::size_t& height) :
20 RealsenseCameraYarp(port_prefix, true, width, height)
21{}
22
23
24RealsenseCameraYarp::RealsenseCameraYarp(const std::string& port_prefix) :
25 RealsenseCameraYarp(port_prefix, false)
26{}
27
28
29RealsenseCameraYarp::RealsenseCameraYarp(const std::string& port_prefix, const bool& enforce_resolution, const std::size_t& width, const std::size_t& height) :
30 YarpCamera(port_prefix)
31{
32 /* Extract camera parameters. */
33 yarp::dev::PolyDriver driver;
34 yarp::dev::IRGBDSensor* interface;
35
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");
44
45 if (driver.open(driver_properties) && driver.view(interface) && (interface != nullptr))
46 {
47 Property camera_intrinsics;
48 interface->getDepthIntrinsicParam(camera_intrinsics);
49
50 std::size_t camera_width = interface->getRgbWidth();
51 std::size_t camera_height = interface->getRgbHeight();
52
53 double scaler_x = 1.0;
54 double scaler_y = 1.0;
55 if (enforce_resolution)
56 {
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"));
59
60 scaler_x = width / camera_width;
61 scaler_y = height / camera_height;
62 }
63
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);
71
72 driver.close();
73 }
74 else
75 throw(std::runtime_error(log_name_ + "::ctor. Cannot get camera parameters."));
76
77 Camera::initialize();
78
79 /* Log 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;
87}
88
89
90RealsenseCameraYarp::~RealsenseCameraYarp()
91{}