41 #include <visualization_msgs/Marker.h> 42 #include <nav_msgs/Odometry.h> 50 namespace rcd = dynamics;
52 bool Protobuf2RosStream::startReceivingAndPublishingAsRos()
54 unsigned int timeoutMillis = 500;
55 string pbMsgType = _rcdyn->getPbMsgTypeOfStream(_stream);
59 unsigned int cntNoListener = 0;
62 while (!_stop && !failed)
66 if (!rosPublisher.
used())
69 if (++cntNoListener > 200)
79 ROS_INFO_STREAM(
"rc_visard_driver: Enabled rc-dynamics stream: " << _stream);
83 rcd::DataReceiver::Ptr receiver;
86 receiver = _rcdyn->createReceiverForStream(_stream);
91 msg <<
"Could not initialize rc-dynamics stream: " << _stream <<
":" << endl << e.what();
97 ROS_ERROR_STREAM(std::string(
"Could not initialize rc-dynamics stream: ") + _stream +
": " + e.what());
101 receiver->setTimeout(timeoutMillis);
102 ROS_INFO_STREAM(
"rc_visard_driver: rc-dynamics stream ready: " << _stream);
106 shared_ptr<::google::protobuf::Message> pbMsg;
107 while (!_stop && rosPublisher.
used())
112 pbMsg = receiver->receive(pbMsgType);
115 catch (std::exception& e)
117 ROS_ERROR_STREAM(
"Caught exception during receiving " << _stream <<
": " << e.what());
125 ROS_WARN_STREAM(
"Did not receive any " << _stream <<
" message within the last " << timeoutMillis <<
" ms. " 126 <<
"Either rc_visard stopped streaming or is turned off, " 127 <<
"or you seem to have serious network/connection problems!");
137 ROS_INFO_STREAM(
"rc_visard_driver: Disabled rc-dynamics stream: " << _stream);
144 bool PoseStream::startReceivingAndPublishingAsRos()
146 unsigned int timeoutMillis = 500;
148 ros::Publisher pub = _nh.advertise<geometry_msgs::PoseStamped>(_stream, 1000);
151 unsigned int cntNoListener = 0;
154 while (!_stop && !failed)
163 if (++cntNoListener > 200)
173 ROS_INFO_STREAM(
"rc_visard_driver: Enabled rc-dynamics stream: " << _stream);
177 rcd::DataReceiver::Ptr receiver;
180 receiver = _rcdyn->createReceiverForStream(_stream);
185 msg <<
"Could not initialize rc-dynamics stream: " << _stream <<
":" << endl << e.what();
191 ROS_ERROR_STREAM(std::string(
"Could not initialize rc-dynamics stream: ") + _stream +
": " + e.what());
195 receiver->setTimeout(timeoutMillis);
196 ROS_INFO_STREAM(
"rc_visard_driver: rc-dynamics stream ready: " << _stream);
200 shared_ptr<roboception::msgs::Frame> protoFrame;
201 while (!_stop && someoneListens)
206 protoFrame = receiver->receive<roboception::msgs::Frame>();
209 catch (std::exception& e)
211 ROS_ERROR_STREAM(
"Caught exception during receiving " << _stream <<
": " << e.what());
219 ROS_WARN_STREAM(
"Did not receive any " << _stream <<
" message within the last " << timeoutMillis <<
" ms. " 220 <<
"Either rc_visard stopped streaming or is turned off, " 221 <<
"or you seem to have serious network/connection problems!");
228 protoFrame->set_parent(_tfPrefix + protoFrame->parent());
229 protoFrame->set_name(_tfPrefix + protoFrame->name());
238 tf_pub.sendTransform(transform);
245 ROS_INFO_STREAM(
"rc_visard_driver: Disabled rc-dynamics stream: " << _stream);
252 bool DynamicsStream::startReceivingAndPublishingAsRos()
254 unsigned int timeoutMillis = 500;
258 bool publishVisualizationMarkers =
false;
259 ros::NodeHandle(
"~").param(
"enable_visualization_markers", publishVisualizationMarkers, publishVisualizationMarkers);
260 publishVisualizationMarkers = publishVisualizationMarkers && (_stream ==
"dynamics");
264 ros::Publisher pub_odom = _nh.advertise<nav_msgs::Odometry>(_stream, 1000);
267 if (publishVisualizationMarkers)
269 pub_markers = _nh.advertise<visualization_msgs::Marker>(
"dynamics_visualization_markers", 1000);
272 unsigned int cntNoListener = 0;
275 while (!_stop && !failed)
277 bool someoneListens =
278 pub_odom.getNumSubscribers() > 0 || (publishVisualizationMarkers && pub_markers.getNumSubscribers() > 0);
285 if (++cntNoListener > 200)
295 ROS_INFO_STREAM(
"rc_visard_driver: Enabled rc-dynamics stream: " << _stream);
299 rcd::DataReceiver::Ptr receiver;
302 receiver = _rcdyn->createReceiverForStream(_stream);
307 msg <<
"Could not initialize rc-dynamics stream: " << _stream <<
":" << endl << e.what();
313 ROS_ERROR_STREAM(std::string(
"Could not initialize rc-dynamics stream: ") + _stream +
": " + e.what());
317 receiver->setTimeout(timeoutMillis);
318 ROS_INFO_STREAM(
"rc_visard_driver: rc-dynamics stream ready: " << _stream);
322 shared_ptr<roboception::msgs::Dynamics> protoMsg;
323 while (!_stop && someoneListens)
328 protoMsg = receiver->receive<roboception::msgs::Dynamics>();
331 catch (std::exception& e)
333 ROS_ERROR_STREAM(
"Caught exception during receiving " << _stream <<
": " << e.what());
341 ROS_WARN_STREAM(
"Did not receive any " << _stream <<
" message within the last " << timeoutMillis <<
" ms. " 342 <<
"Either rc_visard stopped streaming or is turned off, " 343 <<
"or you seem to have serious network/connection problems!");
350 protoMsg->set_pose_frame(_tfPrefix + protoMsg->pose_frame());
351 protoMsg->set_linear_velocity_frame(_tfPrefix + protoMsg->linear_velocity_frame());
352 protoMsg->set_angular_velocity_frame(_tfPrefix + protoMsg->angular_velocity_frame());
353 protoMsg->set_linear_acceleration_frame(_tfPrefix + protoMsg->linear_acceleration_frame());
354 protoMsg->mutable_cam2imu_transform()->set_name(_tfPrefix + protoMsg->cam2imu_transform().name());
355 protoMsg->mutable_cam2imu_transform()->set_parent(_tfPrefix + protoMsg->cam2imu_transform().parent());
365 auto imu2cam =
tf::StampedTransform(cam2imu.inverse(), msgStamp, cam2imu.child_frame_id_, cam2imu.frame_id_);
373 auto odom = boost::make_shared<nav_msgs::Odometry>();
374 odom->header.frame_id = protoMsg->pose_frame();
375 odom->header.stamp = msgStamp;
376 odom->child_frame_id = protoMsg->linear_acceleration_frame();
377 odom->pose.pose = *
toRosPose(protoMsg->pose());
379 auto lin_vel = protoMsg->linear_velocity();
380 auto lin_vel_transformed = imu2world_rot_only.inverse() *
tf::Vector3(lin_vel.x(), lin_vel.y(), lin_vel.z());
381 odom->twist.twist.linear.
x = lin_vel_transformed.x();
382 odom->twist.twist.linear.y = lin_vel_transformed.y();
383 odom->twist.twist.linear.z = lin_vel_transformed.z();
384 auto ang_vel = protoMsg->angular_velocity();
385 odom->twist.twist.angular.x = ang_vel.x();
386 odom->twist.twist.angular.y = ang_vel.y();
387 odom->twist.twist.angular.z = ang_vel.z();
388 pub_odom.publish(odom);
391 if (publishVisualizationMarkers)
393 geometry_msgs::Point start, end;
394 auto protoPosePosition = protoMsg->pose().position();
395 start.x = protoPosePosition.x();
396 start.y = protoPosePosition.y();
397 start.z = protoPosePosition.z();
399 visualization_msgs::Marker lin_vel_marker;
400 lin_vel_marker.header.stamp = msgStamp;
401 lin_vel_marker.header.frame_id = protoMsg->linear_velocity_frame();
402 lin_vel_marker.ns = _tfPrefix;
403 lin_vel_marker.id = 0;
404 lin_vel_marker.type = visualization_msgs::Marker::ARROW;
405 lin_vel_marker.action = visualization_msgs::Marker::MODIFY;
406 lin_vel_marker.frame_locked =
true;
407 end.x = start.x + lin_vel.x();
408 end.y = start.y + lin_vel.y();
409 end.z = start.z + lin_vel.z();
410 lin_vel_marker.points.push_back(start);
411 lin_vel_marker.points.push_back(end);
412 lin_vel_marker.scale.x = 0.005;
413 lin_vel_marker.scale.y = 0.01;
414 lin_vel_marker.color.a = 1;
415 lin_vel_marker.color.g = lin_vel_marker.color.b = 1.0;
416 pub_markers.publish(lin_vel_marker);
418 visualization_msgs::Marker ang_vel_marker;
419 ang_vel_marker.header.stamp = msgStamp;
420 ang_vel_marker.header.frame_id = protoMsg->pose_frame();
421 ang_vel_marker.ns = _tfPrefix;
422 ang_vel_marker.id = 1;
423 ang_vel_marker.type = visualization_msgs::Marker::ARROW;
424 ang_vel_marker.action = visualization_msgs::Marker::MODIFY;
425 ang_vel_marker.frame_locked =
true;
426 auto ang_vel_transformed = imu2world_rot_only *
tf::Vector3(ang_vel.x(), ang_vel.y(), ang_vel.z());
427 end.
x = start.x + ang_vel_transformed.x();
428 end.y = start.y + ang_vel_transformed.y();
429 end.z = start.z + ang_vel_transformed.z();
430 ang_vel_marker.points.push_back(start);
431 ang_vel_marker.points.push_back(end);
432 ang_vel_marker.scale.x = 0.005;
433 ang_vel_marker.scale.y = 0.01;
434 ang_vel_marker.color.a = 1;
435 ang_vel_marker.color.r = ang_vel_marker.color.b = 1.0;
436 pub_markers.publish(ang_vel_marker);
438 visualization_msgs::Marker lin_accel_marker;
439 lin_accel_marker.header.stamp = msgStamp;
440 lin_accel_marker.header.frame_id = protoMsg->pose_frame();
441 lin_accel_marker.ns = _tfPrefix;
442 lin_accel_marker.id = 2;
443 lin_accel_marker.type = visualization_msgs::Marker::ARROW;
444 lin_accel_marker.action = visualization_msgs::Marker::MODIFY;
445 lin_accel_marker.frame_locked =
true;
446 auto lin_accel = protoMsg->linear_acceleration();
447 auto lin_accel_transformed = imu2world_rot_only *
tf::Vector3(lin_accel.x(), lin_accel.y(), lin_accel.z());
448 end.
x = start.x + lin_accel_transformed.x();
449 end.y = start.y + lin_accel_transformed.y();
450 end.z = start.z + lin_accel_transformed.z();
451 lin_accel_marker.points.push_back(start);
452 lin_accel_marker.points.push_back(end);
453 lin_accel_marker.scale.x = 0.005;
454 lin_accel_marker.scale.y = 0.01;
455 lin_accel_marker.color.a = 1;
456 lin_accel_marker.color.r = lin_accel_marker.color.g = 1.0;
457 pub_markers.publish(lin_accel_marker);
462 pub_odom.getNumSubscribers() > 0 || (publishVisualizationMarkers && pub_markers.getNumSubscribers() > 0);
465 ROS_INFO_STREAM(
"rc_visard_driver: Disabled rc-dynamics stream: " << _stream);
geometry_msgs::PoseStampedPtr toRosPoseStamped(const roboception::msgs::Frame &frame)
#define ROS_WARN_STREAM_THROTTLE(period, args)
void publish(const boost::shared_ptr< M > &message) const
tf::Transform toRosTfTransform(const roboception::msgs::Pose &pose)
Generic implementation for publishing protobuf messages to ros.
#define ROS_DEBUG_STREAM_THROTTLE(period, args)
virtual void publish(std::shared_ptr<::google::protobuf::Message > pbMsg)
Publish the generic protobuf message as a corresponding Ros Message.
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define ROS_WARN_STREAM(args)
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
#define ROS_ERROR_STREAM(args)
geometry_msgs::PosePtr toRosPose(const roboception::msgs::Pose &pose)
ros::Time toRosTime(const roboception::msgs::Time &time)
virtual bool used()
Returns true if there are subscribers to the topic.
tf::StampedTransform toRosTfStampedTransform(const roboception::msgs::Frame &frame)