Loading [MathJax]/extensions/tex2jax.js
ed_sensor_integration
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
kinect_plugin.cpp
Go to the documentation of this file.
1 #include "kinect_plugin.h"
2 
3 #include <rgbd/image.h>
4 
5 #include <ros/node_handle.h>
6 
7 #include <ed/entity.h>
8 #include <ed/error_context.h>
9 #include <ed/update_request.h>
10 #include <ed/uuid.h>
11 #include <ed/world_model.h>
12 
13 #include <tue/config/reader.h>
14 
15 #include <rgbd/view.h>
16 
17 #include "ed/kinect/association.h"
18 
19 // GetImage
20 #include <rgbd/serialization.h>
22 #include <ed/io/json_writer.h>
24 
26 
27 //#include <opencv2/highgui/highgui.hpp>
28 
29 #include "ray_tracer.h"
30 
31 // ----------------------------------------------------------------------------------------------------
32 
34 {
35  ed::ErrorContext errc("RGBDPlugin", "constructor");
36 }
37 
38 // ----------------------------------------------------------------------------------------------------
39 
41 {
42  ed::ErrorContext errc("RGBDPlugin", "destructor");
43 }
44 
45 // ----------------------------------------------------------------------------------------------------
46 
48 {
49  ed::ErrorContext errc("RGBDPlugin", "initialize");
51 
52  std::string topic;
53  if (config.value("topic", topic))
54  {
55  ROS_INFO_STREAM("[ED KINECT PLUGIN] Initializing kinect client with topic '" << topic << "'.");
56  image_buffer_.initialize(topic, "map");
57  }
58 
59  // - - - - - - - - - - - - - - - - - -
60  // Services
61 
62  ros::NodeHandle nh("~");
63  nh.setCallbackQueue(&cb_queue_);
64 
65  srv_get_image_ = nh.advertiseService("kinect/get_image", &KinectPlugin::srvGetImage, this);
66  srv_update_ = nh.advertiseService("kinect/update", &KinectPlugin::srvUpdate, this);
67  srv_ray_trace_ = nh.advertiseService("ray_trace", &KinectPlugin::srvRayTrace, this);
68 
69  ray_trace_visualization_publisher_ = nh.advertise<visualization_msgs::Marker>("ray_trace_visualization", 10);
70 }
71 
72 // ----------------------------------------------------------------------------------------------------
73 
75 {
76  ed::ErrorContext errc("RGBDPlugin", "process");
77  // - - - - - - - - - - - - - - - - - -
78  // Fetch kinect image and pose
79 
80 // last_image_.reset();
81 // if (!image_buffer_.nextImage("map", last_image_, last_sensor_pose_))
82 // return;
83 
84  // - - - - - - - - - - - - - - - - - -
85  // Check ROS callback queue
86 
87  world_ = &data.world;
88  update_req_ = &req;
89 
90  cb_queue_.callAvailable();
91 
92  // - - - - - - - - - - - - - - - - - -
93 
94 // cv::imshow("kinect", last_image_->getRGBImage());
95 // cv::waitKey(3);
96 }
97 
98 // ----------------------------------------------------------------------------------------------------
99 
100 bool KinectPlugin::srvGetImage(ed_sensor_integration_msgs::GetImage::Request& req, ed_sensor_integration_msgs::GetImage::Response& res)
101 {
102  ed::ErrorContext errc("RGBDPlugin", "srvGetImage");
103  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104  // Get new image
105 
107  geo::Pose3D sensor_pose;
108 
109  if (!image_buffer_.waitForRecentImage(image, sensor_pose, 2.0))
110  {
111  res.error_msg = "Could not get image";
112  return true;
113  }
114 
115  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116  // Serialize RGBD image
117 
118  std::stringstream stream;
121  tue::serialization::convert(stream, res.rgbd_data);
122 
123  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124  // Write meta data
125 
126  std::stringstream meta_data;
127  ed::io::JSONWriter w(meta_data);
128 
129  // Write sensor pose
130  w.writeGroup("sensor_pose");
131  ed::serialize(sensor_pose, w);
132  w.endGroup();
133 
134  // Write rgbd filename
135  w.writeValue("rgbd_filename", req.filename + ".rgbd");
136 
137  // Write timestamp
138  w.writeGroup("timestamp");
139  ed::serializeTimestamp(image->getTimestamp(), w);
140  w.endGroup();
141 
142  w.finish();
143 
144  res.json_meta_data = meta_data.str();
145 
146  ROS_INFO_STREAM("[ED KINECT] Requested image. Image size: " << res.rgbd_data.size()
147  << " bytes. Meta-data size: " << res.json_meta_data.size() << " bytes.");
148 
149  return true;
150 }
151 
152 // ----------------------------------------------------------------------------------------------------
153 
154 bool KinectPlugin::srvUpdate(ed_sensor_integration_msgs::Update::Request& req, ed_sensor_integration_msgs::Update::Response& res)
155 {
156  ed::ErrorContext errc("RGBDPlugin", "srvUpdate");
157  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158  // Get new image
159 
161  geo::Pose3D sensor_pose;
162 
163  if (!image_buffer_.waitForRecentImage(image, sensor_pose, 2.0))
164  {
165  res.error_msg = "Could not get image";
166  return true;
167  }
168 
169  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
170  // Perform update
171 
172  UpdateRequest kinect_update_req;
173  kinect_update_req.area_description = req.area_description;
174  kinect_update_req.background_padding = req.background_padding;
175  kinect_update_req.fit_supporting_entity = req.fit_supporting_entity;
176 
177  // We expect the orientation of the supporting entity to be approximately correct.
178  // Therefore, only allow rotation updates up to 45 degrees (both clock-wise and anti-clock-wise)
179  kinect_update_req.max_yaw_change = 0.25 * M_PI;
180 
181  UpdateResult kinect_update_res(*update_req_);
182  if (!updater_.update(*world_, image, sensor_pose, kinect_update_req, kinect_update_res))
183  {
184  res.error_msg = kinect_update_res.error.str();
185  return true;
186  }
187 
188  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
189  // Set result
190 
191  for(unsigned int i = 0; i < kinect_update_res.entity_updates.size(); ++i)
192  {
193  EntityUpdate& e_update = kinect_update_res.entity_updates[i];
194  if (e_update.is_new)
195  res.new_ids.push_back(e_update.id.str());
196  else
197  res.updated_ids.push_back(e_update.id.str());
198 
199  // Lock it, such that is won't be cleared by the clearer plugin
200  update_req_->setFlag(e_update.id, "locked");
201  }
202 
203  for(unsigned int i = 0; i < kinect_update_res.removed_entity_ids.size(); ++i)
204  res.deleted_ids.push_back(kinect_update_res.removed_entity_ids[i].str());
205 
206  return true;
207 }
208 
209 // ----------------------------------------------------------------------------------------------------
210 
211 bool KinectPlugin::srvRayTrace(ed_sensor_integration_msgs::RayTrace::Request& req, ed_sensor_integration_msgs::RayTrace::Response& res)
212 {
213  ed::ErrorContext errc("RGBDPlugin", "srvRayTrace");
214  if (req.raytrace_pose.header.frame_id != "/map" && req.raytrace_pose.header.frame_id != "map")
215  {
216  ROS_ERROR("KinectPlugin::srvRayTrace only works with poses expressed in map frame");
217  return false;
218  }
219 
220  geo::Pose3D ray_trace_pose;
221  geo::convert(req.raytrace_pose.pose, ray_trace_pose);
222 
223  ed_ray_tracer::RayTraceResult ray_trace_result = ed_ray_tracer::ray_trace(*world_, ray_trace_pose);
224 
225  if (!ray_trace_result.success_)
226  {
227  ROS_ERROR("ed_ray_tracer::RayTrace failed!");
228  return false;
229  }
230 
231  res.entity_id = ray_trace_result.entity_id_;
232  geo::convert(ray_trace_result.intersection_point_, res.intersection_point.point);
233  res.intersection_point.header.stamp = ros::Time::now();
234  res.intersection_point.header.frame_id = "map";
235 
236  visualization_msgs::Marker marker_msg;
237  marker_msg.header.frame_id = "map";
238  marker_msg.header.stamp = ros::Time::now();
239  marker_msg.action = visualization_msgs::Marker::ADD;
240  marker_msg.color.a = 0.5;
241  marker_msg.lifetime = ros::Duration(10.0);
242  marker_msg.scale.x = 0.05;
243 
244  static int iter = 0;
245  if (++iter % 2 == 0)
246  {
247  marker_msg.color.b = marker_msg.color.r = 1;
248  }
249  else
250  {
251  marker_msg.color.b = marker_msg.color.g = 1;
252  }
253 
254  marker_msg.type = visualization_msgs::Marker::LINE_STRIP;
255 
256  marker_msg.points.push_back(req.raytrace_pose.pose.position);
257  marker_msg.points.push_back(res.intersection_point.point);
258  ray_trace_visualization_publisher_.publish(marker_msg);
259  marker_msg.color.a = marker_msg.color.r = marker_msg.color.g = marker_msg.color.b = 1;
260  marker_msg.scale.x = 0.02;
261  marker_msg.id = 1;
262  ray_trace_visualization_publisher_.publish(marker_msg);
263 
264  //
265  for(ed::WorldModel::const_iterator it = world_->begin(); it != world_->end(); ++it)
266  {
267  const ed::EntityConstPtr& e = *it;
268  if (e->hasFlag("highlighted"))
269  update_req_->removeFlag(e->id(), "highlighted");
270  }
271 
272  //
273  ed::EntityConstPtr highlighted_e = world_->getEntity(res.entity_id);
274  if (highlighted_e)
275  {
276  ROS_INFO("Hit on %s", res.entity_id.c_str());
277  update_req_->setFlag(res.entity_id, "highlighted");
278  ROS_INFO("Setting %s to highlighted", res.entity_id.c_str());
279  }
280 
281  return true;
282 }
283 
284 // ----------------------------------------------------------------------------------------------------
285 
KinectPlugin::update_req_
ed::UpdateRequest * update_req_
Definition: kinect_plugin.h:54
ed::WorldModel::getEntity
EntityConstPtr getEntity(const ed::UUID &id) const
KinectPlugin::srv_ray_trace_
ros::ServiceServer srv_ray_trace_
Definition: kinect_plugin.h:70
uuid.h
ed::io::JSONWriter::writeValue
void writeValue(const std::string &key, const float *fs, std::size_t size)
ed::UpdateRequest
std::string
std::shared_ptr
EntityUpdate
collection structure for laser entities
Definition: kinect/entity_update.h:9
a
void a()
json_writer.h
update_request.h
ED_REGISTER_PLUGIN
#define ED_REGISTER_PLUGIN(Derived)
ed_ray_tracer::RayTraceResult::success_
bool success_
Definition: ray_tracer.h:20
std::vector::size
T size(T... args)
kinect_plugin.h
ray_tracer.h
std::stringstream
KinectPlugin::srvUpdate
bool srvUpdate(ed_sensor_integration_msgs::Update::Request &req, ed_sensor_integration_msgs::Update::Response &res)
Definition: kinect_plugin.cpp:154
reader.h
association.h
ed::io::JSONWriter::writeGroup
void writeGroup(const std::string &name)
geo::Transform3T
tue::serialization::OutputArchive
ed::InitData
ed::InitData::config
tue::Configuration & config
ed::ErrorContext
ed::WorldModel::begin
const_iterator begin() const
UpdateRequest::background_padding
double background_padding
Definition: kinect/updater.h:22
KinectPlugin::updater_
Updater updater_
Definition: kinect_plugin.h:47
ed::WorldModel::const_iterator
EntityIterator const_iterator
ed_ray_tracer::ray_trace
RayTraceResult ray_trace(const ed::WorldModel &world, const geo::Pose3D &raytrace_pose)
Definition: ray_tracer.cpp:49
ed_ray_tracer::RayTraceResult::intersection_point_
geo::Vector3 intersection_point_
Definition: ray_tracer.h:22
ed::EntityConstPtr
shared_ptr< const Entity > EntityConstPtr
ed::WorldModel::end
const_iterator end() const
std::vector::push_back
T push_back(T... args)
image
cv::Mat image
UpdateResult::entity_updates
std::vector< EntityUpdate > entity_updates
Definition: kinect/updater.h:38
tue::config::ReaderWriter
tue::serialization::convert
void convert(Archive &a, std::vector< unsigned char > &data)
KinectPlugin::ray_trace_visualization_publisher_
ros::Publisher ray_trace_visualization_publisher_
Definition: kinect_plugin.h:74
rgbd::serialize
bool serialize(const Image &image, tue::serialization::OutputArchive &a, RGBStorageType rgb_type=RGB_STORAGE_JPG, DepthStorageType depth_type=DEPTH_STORAGE_PNG)
image.h
KinectPlugin::world_
const ed::WorldModel * world_
Definition: kinect_plugin.h:52
ed::UpdateRequest::setFlag
void setFlag(const UUID &id, const std::string &flag)
rgbd::RGB_STORAGE_JPG
RGB_STORAGE_JPG
UpdateResult::error
std::stringstream error
Definition: kinect/updater.h:41
ed_ray_tracer::RayTraceResult
Definition: ray_tracer.h:13
serialization.h
ed::io::JSONWriter::endGroup
void endGroup()
ed_ray_tracer::RayTraceResult::entity_id_
std::string entity_id_
Definition: ray_tracer.h:21
ed::PluginInput
KinectPlugin
Definition: kinect_plugin.h:23
ed::serialize
void serialize(const ConvexHull &ch, ed::io::Writer &w)
req
string req
UpdateRequest::area_description
std::string area_description
Definition: kinect/updater.h:18
EntityUpdate::is_new
bool is_new
Definition: kinect/entity_update.h:12
rgbd::DEPTH_STORAGE_PNG
DEPTH_STORAGE_PNG
KinectPlugin::srvRayTrace
bool srvRayTrace(ed_sensor_integration_msgs::RayTrace::Request &req, ed_sensor_integration_msgs::RayTrace::Response &res)
Definition: kinect_plugin.cpp:211
geo::convert
void convert(const geo::DepthCamera &cam_model, sensor_msgs::CameraInfo &msg)
KinectPlugin::srv_get_image_
ros::ServiceServer srv_get_image_
Definition: kinect_plugin.h:61
KinectPlugin::cb_queue_
ros::CallbackQueue cb_queue_
Definition: kinect_plugin.h:59
UpdateResult::removed_entity_ids
std::vector< ed::UUID > removed_entity_ids
Definition: kinect/updater.h:39
KinectPlugin::~KinectPlugin
~KinectPlugin()
Definition: kinect_plugin.cpp:40
view.h
UpdateRequest::max_yaw_change
double max_yaw_change
Definition: kinect/updater.h:26
tue::config::ReaderWriter::value
bool value(const std::string &name, T &value, RequiredOrOptional opt=REQUIRED)
ed::io::JSONWriter::finish
void finish()
world_model.h
Updater::update
bool update(const ed::WorldModel &world, const rgbd::ImageConstPtr &image, const geo::Pose3D &sensor_pose, const UpdateRequest &req, UpdateResult &res)
Definition: kinect/updater.cpp:201
UpdateRequest::fit_supporting_entity
bool fit_supporting_entity
Definition: kinect/updater.h:29
ed::io::JSONWriter
KinectPlugin::srv_update_
ros::ServiceServer srv_update_
Definition: kinect_plugin.h:66
UpdateRequest
Definition: kinect/updater.h:13
ed::UpdateRequest::removeFlag
void removeFlag(const UUID &id, const std::string &flag)
rgbd::ImageBuffer::waitForRecentImage
bool waitForRecentImage(rgbd::ImageConstPtr &image, geo::Pose3D &sensor_pose, double timeout_sec, double check_rate)
rgbd::ImageBuffer::initialize
void initialize(const std::string &topic, const std::string &root_frame="map", const float worker_thread_frequency=20)
std::stringstream::str
T str(T... args)
conversions.h
entity.h
KinectPlugin::image_buffer_
rgbd::ImageBuffer image_buffer_
Definition: kinect_plugin.h:40
ed::Plugin::initialize
virtual void initialize()
KinectPlugin::KinectPlugin
KinectPlugin()
Definition: kinect_plugin.cpp:33
EntityUpdate::id
ed::UUID id
Definition: kinect/entity_update.h:13
KinectPlugin::process
void process(const ed::PluginInput &data, ed::UpdateRequest &req)
Definition: kinect_plugin.cpp:74
error_context.h
ed::UUID::str
const std::string & str() const
msg_conversions.h
KinectPlugin::srvGetImage
bool srvGetImage(ed_sensor_integration_msgs::GetImage::Request &req, ed_sensor_integration_msgs::GetImage::Response &res)
Definition: kinect_plugin.cpp:100
UpdateResult
Definition: kinect/updater.h:34
ed::serializeTimestamp
void serializeTimestamp(double time, ed::io::Writer &w)
config
tue::config::ReaderWriter config