rgbd
client_shm.cpp
Go to the documentation of this file.
1 #include "rgbd/client_shm.h"
2 
3 #include "rgbd/image.h"
4 
5 #include <ros/duration.h>
6 #include <ros/init.h>
7 #include <ros/console.h>
8 #include <ros/time.h>
9 
10 #include <sensor_msgs/CameraInfo.h>
11 
12 #include <boost/interprocess/sync/scoped_lock.hpp>
13 
14 namespace ipc = boost::interprocess;
15 
16 namespace rgbd
17 {
18 
19 // ----------------------------------------------------------------------------------------------------
20 
21 ClientSHM::ClientSHM() : buffer_header_(nullptr), image_data_(nullptr)
22 {
23 }
24 
25 // ----------------------------------------------------------------------------------------------------
26 
28 {
29 }
30 
31 // ----------------------------------------------------------------------------------------------------
32 
33 bool ClientSHM::initialize(const std::string& server_name, float timeout)
34 {
35  std::string server_name_cp = server_name;
36  std::replace(server_name_cp.begin(), server_name_cp.end(), '/', '-');
37 
38  ros::Time start = ros::Time::now();
39  ros::Time now;
40  ros::WallDuration d(0.1);
41  do
42  {
43  try
44  {
45  // Open already created shared memory object.
46  shm_ = ipc::shared_memory_object(ipc::open_only, server_name_cp.c_str(), ipc::read_write);
47 
48  mem_buffer_header_ = ipc::mapped_region(shm_, ipc::read_write, 0, sizeof(BufferHeader));
49  mem_image_ = ipc::mapped_region(shm_, ipc::read_only, sizeof(BufferHeader));
50 
51  buffer_header_ = static_cast<BufferHeader*>(mem_buffer_header_.get_address());
52  image_data_ = static_cast<uchar*>(mem_image_.get_address());
53 
54  rgb_data_size_ = static_cast<uint64_t>(buffer_header_->rgb_width * buffer_header_->rgb_height * 3);
56 
57  sequence_nr_ = 0;
58  ROS_INFO_STREAM_NAMED("ClientSHM", "Opened shared memory on: '" << server_name_cp << "' succesfully.");
59  return true;
60  }
61  catch(ipc::interprocess_exception &ex)
62  {
63  ROS_DEBUG_STREAM_NAMED("ClientSHM", "Could not open shared memory: " << ex.what());
64  }
65  d.sleep();
66  now = ros::Time::now();
67  }
68  while (ros::ok() && (now - start).toSec() < static_cast<double>(timeout));
69 
70  ROS_INFO_STREAM_NAMED("ClientSHM", "Opening shared memory on: '" << server_name_cp << "' failed on timeout(" << timeout << ").");
71 
72  return false;
73 }
74 
75 // ----------------------------------------------------------------------------------------------------
76 
78 {
79  buffer_header_ = nullptr;
80  image_data_ = nullptr;
81  mem_image_ = ipc::mapped_region();
82  mem_buffer_header_ = ipc::mapped_region();
83  shm_= ipc::shared_memory_object();
84  return true;
85 }
86 
87 // ----------------------------------------------------------------------------------------------------
88 
90 {
91  if (!initialized())
92  return false;
93 
94  ipc::scoped_lock<ipc::interprocess_mutex> lock(buffer_header_->mutex);
95 
97  return false;
98 
99  cv::Mat* rgb = &(image.rgb_image_);
100  cv::Mat* depth = &(image.depth_image_);
101 
102  *rgb = cv::Mat(buffer_header_->rgb_height, buffer_header_->rgb_width, CV_8UC3);
103  *depth = cv::Mat(buffer_header_->depth_height, buffer_header_->depth_width, CV_32FC1);
104 
105  memcpy(rgb->data, image_data_, rgb_data_size_);
106  memcpy(depth->data, image_data_ + rgb_data_size_, depth_data_size_);
107 
110 
111  if (!image.getCameraModel().initialized())
112  {
113  sensor_msgs::CameraInfo cam_info_msg;
114 
115  cam_info_msg.height = buffer_header_->height;
116  cam_info_msg.width = buffer_header_->width;
117  cam_info_msg.binning_x = buffer_header_->binning_x;
118  cam_info_msg.binning_y = buffer_header_->binning_y;
119  cam_info_msg.distortion_model = buffer_header_->distortion_model;
120  cam_info_msg.D.resize(buffer_header_->size_D); // std::vector
121  memcpy(cam_info_msg.D.data(), buffer_header_->D, buffer_header_->size_D*sizeof(double)); // std::vector
122  memcpy(&(cam_info_msg.K.elems), buffer_header_->K, 9*sizeof(double)); // boost::array
123  memcpy(&(cam_info_msg.R.elems), buffer_header_->R, 9*sizeof(double)); // boost::array
124  memcpy(&(cam_info_msg.P.elems), buffer_header_->P, 12*sizeof(double)); // boost::array
125  // CameraInfo/roi
126  cam_info_msg.roi.x_offset = buffer_header_->roi_x_offset;
127  cam_info_msg.roi.y_offset = buffer_header_->roi_y_offset;
128  cam_info_msg.roi.height = buffer_header_->roi_height;
129  cam_info_msg.roi.width = buffer_header_->roi_width;
130  cam_info_msg.roi.do_rectify = buffer_header_->roi_do_rectify;
131 
132  image.cam_model_.fromCameraInfo(cam_info_msg);
133  }
134 
137 
139 
140  return true;
141 }
142 
143 // ----------------------------------------------------------------------------------------------------
144 
146 {
147  ImagePtr img(new Image);
148  if (nextImage(*img))
149  return img;
150  else
151  return nullptr;
152 }
153 
154 // ----------------------------------------------------------------------------------------------------
155 
156 } // end namespace rgbd
157 
rgbd::ClientSHM::initialized
bool initialized()
Check if the client is initialized. nextImage shouldn't be called if client is not initialized.
Definition: client_shm.h:53
rgbd::ClientSHM::buffer_header_
BufferHeader * buffer_header_
Definition: client_shm.h:77
rgbd::BufferHeader::depth_width
int depth_width
Definition: image_header.h:28
rgbd::BufferHeader::frame_id
char frame_id[1000]
Definition: image_header.h:34
std::string
std::shared_ptr
rgbd::Image::cam_model_
image_geometry::PinholeCameraModel cam_model_
Camera model Header in camera info should always be empty to prevent inconsistent behaviour of equali...
Definition: image.h:179
rgbd::ClientSHM::rgb_data_size_
uint64_t rgb_data_size_
Definition: client_shm.h:80
client_shm.h
rgbd::ClientSHM::depth_data_size_
uint64_t depth_data_size_
Definition: client_shm.h:81
rgbd::ClientSHM::mem_buffer_header_
boost::interprocess::mapped_region mem_buffer_header_
Definition: client_shm.h:74
rgbd::BufferHeader::rgb_height
int rgb_height
Definition: image_header.h:27
rgbd::BufferHeader::roi_width
uint32_t roi_width
Definition: image_header.h:41
rgbd::BufferHeader::roi_height
uint32_t roi_height
Definition: image_header.h:41
rgbd::BufferHeader::D
double D[5]
Definition: image_header.h:39
rgbd::BufferHeader::rgb_width
int rgb_width
Definition: image_header.h:26
rgbd::ClientSHM::mem_image_
boost::interprocess::mapped_region mem_image_
Definition: client_shm.h:75
rgbd::BufferHeader
Definition: image_header.h:12
rgbd::BufferHeader::timestamp
double timestamp
Definition: image_header.h:33
rgbd::BufferHeader::depth_height
int depth_height
Definition: image_header.h:29
std::replace
T replace(T... args)
rgbd::Image::setFrameId
void setFrameId(const std::string &frame_id)
Set the frame_id.
Definition: image.h:117
rgbd::BufferHeader::binning_y
uint32_t binning_y
Definition: image_header.h:36
rgbd::Image::frame_id_
std::string frame_id_
Definition: image.h:181
rgbd::Image
Definition: image.h:43
rgbd::Image::setTimestamp
void setTimestamp(double timestamp)
Set the timestamp.
Definition: image.h:123
rgbd::BufferHeader::distortion_model
char distortion_model[1000]
Definition: image_header.h:37
rgbd::BufferHeader::mutex
boost::interprocess::interprocess_mutex mutex
Definition: image_header.h:17
rgbd::ClientSHM::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize shared memory client.
Definition: client_shm.cpp:33
rgbd::BufferHeader::roi_x_offset
uint32_t roi_x_offset
Definition: image_header.h:41
rgbd::ClientSHM::sequence_nr_
uint64_t sequence_nr_
sequence_nr Contains the sequence nummer of the last NextImage call
Definition: client_shm.h:86
rgbd::BufferHeader::roi_do_rectify
bool roi_do_rectify
Definition: image_header.h:42
rgbd::ClientSHM::deinitialize
bool deinitialize()
Clears all the shared memory classes to nullptrs or empty classes. initialized will now return false.
Definition: client_shm.cpp:77
std::string::c_str
T c_str(T... args)
rgbd
Definition: client.h:24
rgbd::BufferHeader::height
uint32_t height
Definition: image_header.h:36
rgbd::ClientSHM::ClientSHM
ClientSHM()
Constructor.
Definition: client_shm.cpp:21
image.h
rgbd::BufferHeader::P
double P[12]
Definition: image_header.h:39
rgbd::ClientSHM::nextImage
ImagePtr nextImage()
Get a new Image. If no new image has been received, the sequence nummer is still the same as the prev...
Definition: client_shm.cpp:145
rgbd::Image::depth_image_
cv::Mat depth_image_
Definition: image.h:173
rgbd::BufferHeader::K
double K[9]
Definition: image_header.h:39
rgbd::ClientSHM::~ClientSHM
~ClientSHM()
Destructor.
Definition: client_shm.cpp:27
rgbd::BufferHeader::R
double R[9]
Definition: image_header.h:39
rgbd::Image::timestamp_
double timestamp_
Definition: image.h:182
rgbd::Image::getCameraModel
const image_geometry::PinholeCameraModel & getCameraModel() const
Get the camera model.
Definition: image.h:96
std::string::begin
T begin(T... args)
rgbd::BufferHeader::roi_y_offset
uint32_t roi_y_offset
Definition: image_header.h:41
rgbd::BufferHeader::binning_x
uint32_t binning_x
Definition: image_header.h:36
rgbd::BufferHeader::size_D
size_t size_D
Definition: image_header.h:38
rgbd::BufferHeader::sequence_nr
uint64_t sequence_nr
Definition: image_header.h:14
rgbd::ClientSHM::image_data_
unsigned char * image_data_
Definition: client_shm.h:78
std::string::end
T end(T... args)
rgbd::ClientSHM::shm_
boost::interprocess::shared_memory_object shm_
Definition: client_shm.h:72
rgbd::Image::rgb_image_
cv::Mat rgb_image_
Definition: image.h:172
rgbd::BufferHeader::width
uint32_t width
Definition: image_header.h:36