Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
Realsense Class Reference

#include <DepthCamera.hpp>

Public Member Functions

 Realsense ()
 
void set_node (rclcpp::Node *ptr)
 
void init (std::string config)
 initializes camera with json settings More...
 
std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > pop_last_grabbed_pcl (bool blocking)
 
std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > get_last_grabbed_pcl ()
 

Private Member Functions

pcl::PointCloud< pcl::PointXYZ >::Ptr points_to_pcl (const rs2::points &points)
 

Private Attributes

rs2::config m_cfg
 
rs2::pipeline m_pipe
 
rs2::pipeline_profile m_profile
 
std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > m_currentPointCloudPairSPtr = nullptr
 
bool m_currentPointCloudPairNewData = false
 
rclcpp::Node * m_nodePtr
 
std::mutex m_mtx
 
std::condition_variable m_newPCLConditionVar
 

Detailed Description

Definition at line 24 of file DepthCamera.hpp.

Constructor & Destructor Documentation

◆ Realsense()

Realsense::Realsense ( )
inline

Definition at line 40 of file DepthCamera.hpp.

40 {};

Member Function Documentation

◆ get_last_grabbed_pcl()

std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > Realsense::get_last_grabbed_pcl ( )

Definition at line 156 of file DepthCamera.cpp.

156  {
157  std::unique_lock<std::mutex> lock(m_mtx);
158  std::shared_ptr<std::pair<rclcpp::Time, pcl::PointCloud<pcl::PointXYZ>::Ptr>> ret = m_currentPointCloudPairSPtr;
159  return ret;
160 }

◆ init()

void Realsense::init ( std::string  config)

initializes camera with json settings

Definition at line 14 of file DepthCamera.cpp.

14  {
15  log_warning("[Realsense::init] Configuration File ignored. (not implemented)");
16 
17  log("Enable depth stream...");
18  m_cfg.enable_stream(RS2_STREAM_DEPTH, RS2_FORMAT_Z16);
19 
20  // callback for depth pointcloud
21  auto callback = [&](const rs2::frame& frame) {
22  log_debug("[Realsense::Callback] New Frame");
23  auto now = this->m_nodePtr->get_clock()->now();
24 
25  // check if frame has desired data
26  if(!frame.is<rs2::frameset>()) {
27  log_warning("[Realsense::Callback] Wrong Frame");
28  return;
29  }
30 
31  // retrieve point cloud
32  auto frameset = frame.as<rs2::frameset>();
33 
34  auto it = frameset.begin();
35  if (it == frameset.end()) {
36  log_warning("Realsense::Callback] Frameset empty");
37  return;
38  }
39 
40  if((*it).get_profile().stream_type() != RS2_STREAM_DEPTH) {
41  log_warning("[Realsense::Callback] No Depth Frame");
42  return;
43  }
44 
45  // retrieve point cloud in pcl
46  rs2::depth_frame depthFrame = frameset.get_depth_frame();
47  rs2::pointcloud pointcloud;
48  rs2::points pc = pointcloud.calculate(depthFrame);
49 
50  auto pointCloudPairSPtr = std::make_shared<std::pair<rclcpp::Time, pcl::PointCloud<pcl::PointXYZ>::Ptr>>();
51  std::get<0>(*pointCloudPairSPtr) = now;
52  std::get<1>(*pointCloudPairSPtr) = points_to_pcl(pc);
53 
54  m_mtx.lock();
55  this->m_currentPointCloudPairSPtr = pointCloudPairSPtr;
56  this->m_currentPointCloudPairNewData = true;
57  m_newPCLConditionVar.notify_all();
58  m_mtx.unlock();
59 
60  // log_debug("[Realsense::Callback] Frame fully processed");
61  // DEBUG
62  // Get the depth frame's dimensions
63  // auto width = depthFrame.get_width();
64  // auto height = depthFrame.get_height();
65 
66  // // Query the distance from the camera to the object in the center of the image
67  // float dist_to_center = depthFrame.get_distance(width / 2, height / 2);
68 
69  // // Print the distance
70  // std::cout << "The camera is facing an object " << dist_to_center << " meters away \r";
71 
72  };
73 
74  log("Pipeline started...");
75  // m_profile = m_pipe.start(m_cfg, callback); // starts thread
76  m_profile = m_pipe.start(callback); // starts thread without config
77 
78  // std::cout << "Get device" << std::endl;
79  // rs2::device device = m_profile.get_device();
80 
81  // std::cout << "Get depth sensor" << std::endl;
82  // rs2::depth_sensor depth_sensor(device.first<rs2::depth_sensor>());
83  // rs400::advanced_mode advancedDevice(device);
84 
85  // if(advancedDevice.is_enabled() && fexists(p_jsonSettingsFilename.c_str())) {
86  // std::cout << "rs400 advanced mode" << std::endl;
87  // std::cout << "Camera Infos: " << std::endl;
88  // for(auto i = 0; i < RS2_CAMERA_INFO_COUNT; i++) {
89  // if(!advancedDevice.supports((rs2_camera_info)i))
90  // continue;
91  // std::cout << static_cast<rs2_camera_info>(i) << ": " << advancedDevice.get_info(static_cast<rs2_camera_info>(i)) << std::endl;
92  // }
93 
94  // std::ifstream jsonSettingsFile(p_jsonSettingsFilename);
95  // std::stringstream jsonSettings;
96  // jsonSettings << jsonSettingsFile.rdbuf();
97 
98  // std::cout << "Load json settings: " << std::endl;
99  // std::cout << jsonSettings.str() << std::endl;
100  // advancedDevice.load_json(jsonSettings.str());
101  // } else {
102  // std::cout << "Error: no advanced mode toggled or json settings file not available" << std::endl;
103 
104  // /** comment from rs-measure.cpp
105  // * TODO: At the moment the SDK does not offer a closed enum for D400 visual presets
106  // * (because they keep changing)
107  // * As a work-around we try to find the High-Density preset by name
108  // * We do this to reduce the number of black pixels
109  // * The hardware can perform hole-filling much better and much more power efficient then our software
110  // **/
111  // auto range = depth_sensor.get_option_range(RS2_OPTION_VISUAL_PRESET);
112  // for(auto i = range.min; i < range.max; i += range.step) {
113  // if(std::string(depth_sensor.get_option_value_description(RS2_OPTION_VISUAL_PRESET, i)) == "High Accuracy") {
114  // depth_sensor.set_option(RS2_OPTION_VISUAL_PRESET, i);
115  // }
116  // }
117  // }
118  // depth_sensor.set_option(RS2_OPTION_DEPTH_UNITS, 0.001);
119 
120  // std::cout << "Wait for autoexposure..." << std::endl;
121  // // wait for autoexposure
122  // for(auto i = 0; i < 10; i++) {
123  // m_pipe.wait_for_frames();
124  // }
125 
126  // // Get stream m_profile of depthstream and show info
127  // m_streamProfile = m_profile.get_stream(RS2_STREAM_DEPTH);
128  // std::cout << "FPS: " << m_streamProfile.fps() << std::endl;
129  // std::cout << "ID: " << m_streamProfile.unique_id() << std::endl;
130  // std::cout << "Format: " << m_streamProfile.format() << std::endl;
131 
132  // m_running = true;
133 
134  // //create thread with process
135 }

References galaxis::utils::log(), galaxis::utils::log_debug(), and galaxis::utils::log_warning().

Referenced by RealSenseNode::RealSenseNode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ points_to_pcl()

pcl::PointCloud< pcl::PointXYZ >::Ptr Realsense::points_to_pcl ( const rs2::points &  points)
private

Definition at line 162 of file DepthCamera.cpp.

162  {
163  // https://pointclouds.org/documentation/classpcl_1_1_point_cloud.html
164  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
165 
166  auto sp = points.get_profile().as<rs2::video_stream_profile>();
167  // log_debug("[Realsense] PointCloud (W x H): ", sp.width(), " x ", sp.height());
168 
169  cloud->width = points.size();
170  cloud->height = 1;
171  cloud->is_dense = true;
172  cloud->points.reserve(points.size());
173 
174  auto ptr = points.get_vertices();
175 
176  size_t pointsAppended = 0;
177 
178  float m_minY = -0.08; // meter
179  float m_maxY = 0.08f; // meter;
180  float m_maxZ = 1.8f; // 2 meter max distance
181 
182  for (size_t i = 0; i < points.size(); ++i) {
183  if(ptr->z <= m_maxZ && ptr->y >= m_minY && ptr->y <= m_maxY) {
184  cloud->push_back(pcl::PointXYZ(ptr->x, ptr->y, ptr->z));
185  pointsAppended++;
186  }
187  ptr++;
188  }
189 
190  // log_debug("Original Size: ", points.size());
191  // log_debug("Postprocessed Size: ", pointsAppended);
192  cloud->points.resize(pointsAppended);
193  cloud->width = pointsAppended;
194 
195  return cloud;
196 }

◆ pop_last_grabbed_pcl()

std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > Realsense::pop_last_grabbed_pcl ( bool  blocking)

Definition at line 137 of file DepthCamera.cpp.

137  {
138  std::unique_lock<std::mutex> lock(m_mtx);
139  std::shared_ptr<std::pair<rclcpp::Time, pcl::PointCloud<pcl::PointXYZ>::Ptr>> ret = nullptr;
140 
144  }
145 
146  // if return image is nullptr and blocking is enabled then wait for new image
147  if(ret == nullptr && blocking) {
148  m_newPCLConditionVar.wait(lock); // release lock and wait until notify_one called
151  }
152 
153  return ret;
154 }

Referenced by RealSenseNode::object_detection_loop().

Here is the caller graph for this function:

◆ set_node()

void Realsense::set_node ( rclcpp::Node *  ptr)

Definition at line 10 of file DepthCamera.cpp.

10  {
11  m_nodePtr = ptr;
12 }

Referenced by RealSenseNode::RealSenseNode().

Here is the caller graph for this function:

Member Data Documentation

◆ m_cfg

rs2::config Realsense::m_cfg
private

Definition at line 26 of file DepthCamera.hpp.

◆ m_currentPointCloudPairNewData

bool Realsense::m_currentPointCloudPairNewData = false
private

Definition at line 30 of file DepthCamera.hpp.

◆ m_currentPointCloudPairSPtr

std::shared_ptr<std::pair<rclcpp::Time, pcl::PointCloud<pcl::PointXYZ>::Ptr> > Realsense::m_currentPointCloudPairSPtr = nullptr
private

Definition at line 29 of file DepthCamera.hpp.

◆ m_mtx

std::mutex Realsense::m_mtx
private

Definition at line 34 of file DepthCamera.hpp.

◆ m_newPCLConditionVar

std::condition_variable Realsense::m_newPCLConditionVar
private

Definition at line 35 of file DepthCamera.hpp.

◆ m_nodePtr

rclcpp::Node* Realsense::m_nodePtr
private

Definition at line 32 of file DepthCamera.hpp.

◆ m_pipe

rs2::pipeline Realsense::m_pipe
private

Definition at line 27 of file DepthCamera.hpp.

◆ m_profile

rs2::pipeline_profile Realsense::m_profile
private

Definition at line 28 of file DepthCamera.hpp.


The documentation for this class was generated from the following files:
Realsense::m_newPCLConditionVar
std::condition_variable m_newPCLConditionVar
Definition: DepthCamera.hpp:35
galaxis::utils::log_warning
void log_warning(const Args &... args)
Definition: logger.hpp:41
Realsense::m_nodePtr
rclcpp::Node * m_nodePtr
Definition: DepthCamera.hpp:32
Realsense::m_currentPointCloudPairSPtr
std::shared_ptr< std::pair< rclcpp::Time, pcl::PointCloud< pcl::PointXYZ >::Ptr > > m_currentPointCloudPairSPtr
Definition: DepthCamera.hpp:29
Realsense::m_profile
rs2::pipeline_profile m_profile
Definition: DepthCamera.hpp:28
Realsense::m_pipe
rs2::pipeline m_pipe
Definition: DepthCamera.hpp:27
Realsense::m_mtx
std::mutex m_mtx
Definition: DepthCamera.hpp:34
Realsense::m_cfg
rs2::config m_cfg
Definition: DepthCamera.hpp:26
Realsense::points_to_pcl
pcl::PointCloud< pcl::PointXYZ >::Ptr points_to_pcl(const rs2::points &points)
Definition: DepthCamera.cpp:162
Realsense::m_currentPointCloudPairNewData
bool m_currentPointCloudPairNewData
Definition: DepthCamera.hpp:30
galaxis::utils::log
void log(const Args &... args)
Definition: logger.hpp:33
galaxis::utils::log_debug
void log_debug(const Args &... args)
Definition: logger.hpp:62