1919
2020#pragma once
2121
22- #include < thread>
2322#include < chrono>
23+ #include < functional>
2424#include < memory>
2525#include < thread>
26- #include < functional >
26+ #include < thread >
2727
2828#include < fl/util/profiling.hpp>
2929
30+ #include < image_transport/image_transport.h>
3031#include < ros/ros.h>
31- #include < sensor_msgs/JointState.h>
3232#include < sensor_msgs/Image.h>
33- #include < sensor_msgs/fill_image .h>
33+ #include < sensor_msgs/JointState .h>
3434#include < sensor_msgs/distortion_models.h>
35- #include < image_transport/image_transport .h>
35+ #include < sensor_msgs/fill_image .h>
3636
3737#include < dbot/camera_data.hpp>
3838#include < dbot/object_model.hpp>
3939#include < dbot/rigid_body_renderer.hpp>
4040#include < dbot_ros/util/ros_interface.hpp>
4141
42- #include < dbrt/robot_publisher.h>
4342#include < dbrt/kinematics_from_urdf.h>
43+ #include < dbrt/robot_publisher.h>
4444#include < dbrt/util/robot_animator.h>
4545
4646namespace dbrt
4747{
48-
4948template <typename State>
5049class RobotEmulator
5150{
@@ -82,25 +81,23 @@ class RobotEmulator
8281 std::string prefix = " " ;
8382
8483 robot_publisher_ = std::make_shared<RobotPublisher<State>>(
85- urdf_kinematics_, prefix, " " );
84+ urdf_kinematics_, prefix, " " );
8685
8786 // camera publisher
8887 pub_camera_info_ = node_handle_.advertise <sensor_msgs::CameraInfo>(
89- prefix + " /XTION/depth/camera_info" , 0 );
88+ prefix + " /XTION/depth/camera_info" , 0 );
9089 boost::shared_ptr<image_transport::ImageTransport> it (
91- new image_transport::ImageTransport (node_handle_));
90+ new image_transport::ImageTransport (node_handle_));
9291 pub_depth_image_ = it->advertise (prefix + " /XTION/depth/image" , 0 );
93-
94-
9592 }
9693
9794 void run ()
9895 {
9996 running_ = true ;
10097 joint_sensor_thread_ =
101- std::thread (&RobotEmulator::run_joint_sensors, this );
98+ std::thread (&RobotEmulator::run_joint_sensors, this );
10299 visual_sensor_thread_ =
103- std::thread (&RobotEmulator::run_visual_sensors, this );
100+ std::thread (&RobotEmulator::run_visual_sensors, this );
104101 }
105102
106103 void pause () { paused_ = true ; }
@@ -155,43 +152,75 @@ class RobotEmulator
155152
156153 Eigen::VectorXd depth_image;
157154 renderer_->Render (
158- state, depth_image, std::numeric_limits<double >::quiet_NaN ());
155+ state, depth_image, std::numeric_limits<double >::quiet_NaN ());
159156
160157 auto end = std::chrono::system_clock::now ();
161158 auto elapsed_time =
162- std::chrono::duration<double >(end - start).count ();
163-
164- // publish in parallel
165- std::thread (
166- [state,
167- timestamp,
168- elapsed_time,
169- depth_image,
170- image_publishing_delay_,
171- &publisher_mutex_,
172- &camera_data_,
173- &robot_publisher_,
174- this ]()
175- {
176- double remaining_delay =
177- std::max (image_publishing_delay_ - elapsed_time, 0.0 );
159+ std::chrono::duration<double >(end - start).count ();
160+
161+ /*
162+ // publish in parallel
163+ std::thread(
164+ [state,
165+ timestamp,
166+ elapsed_time,
167+ depth_image,
168+ image_publishing_delay_,
169+ &publisher_mutex_,
170+ &camera_data_,
171+ &robot_publisher_,
172+ this]()
173+ {
174+ double remaining_delay =
175+ std::max(image_publishing_delay_ -
176+ elapsed_time, 0.0);
177+
178+ ROS_INFO_STREAM("Visual simulation computation delay
179+ "
180+ << elapsed_time);
181+
182+ std::this_thread::sleep_for(
183+ std::chrono::duration<double>(remaining_delay));
184+
185+ std::lock_guard<std::mutex> publisher_lock(
186+ publisher_mutex_);
187+ this->publish_camera_info(camera_data_,
188+ ros::Time(timestamp));
189+ this->publish_image(
190+ depth_image, camera_data_,
191+ ros::Time(timestamp));
192+
193+ })
194+ .detach();
195+ */
196+ std::thread (std::bind (&RobotEmulator<State>::publisher_thread,
197+ this ,
198+ state,
199+ timestamp,
200+ elapsed_time,
201+ depth_image,
202+ image_publishing_delay_))
203+ .detach ();
204+ }
205+ }
178206
179- ROS_INFO_STREAM (" Visual simulation computation delay "
180- << elapsed_time);
207+ void publisher_thread (State state,
208+ double timestamp,
209+ double elapsed_time,
210+ Eigen::VectorXd depth_image,
211+ double image_publishing_delay_)
212+ {
213+ double remaining_delay =
214+ std::max (image_publishing_delay_ - elapsed_time, 0.0 );
181215
182- std::this_thread::sleep_for (
183- std::chrono::duration<double >(remaining_delay));
216+ ROS_INFO_STREAM (" Visual simulation computation delay " << elapsed_time);
184217
185- std::lock_guard<std::mutex> publisher_lock (
186- publisher_mutex_);
187- this ->publish_camera_info (camera_data_,
188- ros::Time (timestamp));
189- this ->publish_image (
190- depth_image, camera_data_, ros::Time (timestamp));
218+ std::this_thread::sleep_for (
219+ std::chrono::duration<double >(remaining_delay));
191220
192- })
193- . detach ( );
194- }
221+ std::lock_guard<std::mutex> publisher_lock (publisher_mutex_);
222+ this -> publish_camera_info (camera_data_, ros::Time (timestamp) );
223+ this -> publish_image (depth_image, camera_data_, ros::Time (timestamp));
195224 }
196225
197226 void state_and_time (State& state, double & time) const
@@ -220,13 +249,10 @@ class RobotEmulator
220249 // }
221250
222251private:
223-
224-
225-
226252 void convert_to_depth_image_msg (
227- const std::shared_ptr<dbot::CameraData>& camera_data,
228- const Eigen::VectorXd& depth_image,
229- sensor_msgs::Image& image)
253+ const std::shared_ptr<dbot::CameraData>& camera_data,
254+ const Eigen::VectorXd& depth_image,
255+ sensor_msgs::Image& image)
230256 {
231257 Eigen::VectorXf float_vector = depth_image.cast <float >();
232258
@@ -238,18 +264,14 @@ class RobotEmulator
238264 float_vector.data ());
239265 }
240266
241-
242-
243267 bool has_image_subscribers () const
244268 {
245269 return pub_depth_image_.getNumSubscribers () > 0 ;
246270 }
247271
248-
249- void publish_image (
250- const Eigen::VectorXd& depth_image,
251- const std::shared_ptr<dbot::CameraData>& camera_data,
252- const ros::Time& time)
272+ void publish_image (const Eigen::VectorXd& depth_image,
273+ const std::shared_ptr<dbot::CameraData>& camera_data,
274+ const ros::Time& time)
253275 {
254276 if (pub_depth_image_.getNumSubscribers () > 0 )
255277 {
@@ -262,22 +284,20 @@ class RobotEmulator
262284 }
263285 }
264286
265-
266287 void publish_camera_info (
267- const std::shared_ptr<dbot::CameraData>& camera_data,
268- const ros::Time& time)
288+ const std::shared_ptr<dbot::CameraData>& camera_data,
289+ const ros::Time& time)
269290 {
270291 auto camera_info = create_camera_info (camera_data, time);
271292 pub_camera_info_.publish (camera_info);
272293 }
273294
274-
275295 sensor_msgs::CameraInfoPtr create_camera_info (
276- const std::shared_ptr<dbot::CameraData>& camera_data,
277- const ros::Time& time)
296+ const std::shared_ptr<dbot::CameraData>& camera_data,
297+ const ros::Time& time)
278298 {
279299 sensor_msgs::CameraInfoPtr info_msg =
280- boost::make_shared<sensor_msgs::CameraInfo>();
300+ boost::make_shared<sensor_msgs::CameraInfo>();
281301
282302 info_msg->header .stamp = time;
283303 // if internal registration is used, rgb camera intrinsic parameters are
@@ -298,7 +318,7 @@ class RobotEmulator
298318
299319 auto camera_matrix = camera_data->camera_matrix ();
300320 camera_matrix.topLeftCorner (2 , 3 ) *=
301- double (camera_data->downsampling_factor ());
321+ double (camera_data->downsampling_factor ());
302322
303323 for (size_t col = 0 ; col < 3 ; col++)
304324 for (size_t row = 0 ; row < 3 ; row++)
@@ -315,20 +335,6 @@ class RobotEmulator
315335 return info_msg;
316336 }
317337
318-
319-
320-
321-
322-
323-
324-
325-
326-
327-
328-
329-
330-
331-
332338 double time_;
333339 Eigen::VectorXd state_;
334340 // Eigen::VectorXd obsrv_vector_;
@@ -353,7 +359,6 @@ class RobotEmulator
353359 std::thread visual_sensor_thread_;
354360 bool paused_;
355361
356-
357362 ros::NodeHandle node_handle_;
358363 ros::Publisher pub_camera_info_;
359364 image_transport::Publisher pub_depth_image_;
0 commit comments