protobuf2ros_publisher.cc
Go to the documentation of this file.
1 /*
2 * Roboception GmbH
3 * Munich, Germany
4 * www.roboception.com
5 *
6 * Copyright (c) 2017 Roboception GmbH
7 * All rights reserved
8 *
9 * Author: Christian Emmerich
10 */
11 
12 #include "protobuf2ros_publisher.h"
13 
15 
16 using namespace std;
17 
18 namespace rc
19 {
20 Protobuf2RosPublisher::Protobuf2RosPublisher(ros::NodeHandle& nh, const string& topic, const string& pbMsgType,
21  const string& frame_id_prefix)
22  : _tfPrefix(frame_id_prefix)
23 {
24  if (pbMsgType == "Imu")
25  {
26  pub = nh.advertise<sensor_msgs::Imu>(topic, 1000);
27  }
28  else if (pbMsgType == "Frame")
29  {
30  pub = nh.advertise<geometry_msgs::PoseStamped>(topic, 1000);
31  }
32  else
33  {
34  stringstream msg;
35  msg << "Protobuf message type '" << pbMsgType << "' not supported!";
36  throw runtime_error(msg.str());
37  }
38 }
39 
41 {
42  return pub.getNumSubscribers() > 0;
43 }
44 
45 void Protobuf2RosPublisher::publish(shared_ptr<::google::protobuf::Message> pbMsg)
46 {
47  string msg_name = pbMsg->GetDescriptor()->name();
48 
49  if (msg_name == "Imu")
50  {
51  // dynamically cast to Imu type, check validity, and transform to ros
52 
53  shared_ptr<roboception::msgs::Imu> protoImu = dynamic_pointer_cast<roboception::msgs::Imu>(pbMsg);
54  if (!protoImu)
55  {
56  throw runtime_error("Given protobuf message was not of type Imu!");
57  }
58 
59  auto rosImu = toRosImu(*protoImu);
60  rosImu->header.frame_id = _tfPrefix + rosImu->header.frame_id;
61  pub.publish(rosImu);
62  }
63  else if (msg_name == "Frame")
64  {
65  // dynamically cast to Frame type, check validity, and transform to ros
66 
67  shared_ptr<roboception::msgs::Frame> protoFrame = dynamic_pointer_cast<roboception::msgs::Frame>(pbMsg);
68  if (!protoFrame)
69  {
70  throw runtime_error("Given protobuf message was not of type Frame!");
71  }
72 
73  auto rosPose = toRosPoseStamped(*protoFrame);
74  rosPose->header.frame_id = _tfPrefix + rosPose->header.frame_id;
75  pub.publish(rosPose);
76  }
77  else
78  {
79  stringstream msg;
80  msg << "Protobuf message type '" << msg_name << "' not supported!";
81  throw runtime_error(msg.str());
82  }
83 }
84 }
msg
geometry_msgs::PoseStampedPtr toRosPoseStamped(const roboception::msgs::Frame &frame)
void publish(const boost::shared_ptr< M > &message) const
sensor_msgs::ImuPtr toRosImu(const roboception::msgs::Imu &imu)
virtual void publish(std::shared_ptr<::google::protobuf::Message > pbMsg)
Publish the generic protobuf message as a corresponding Ros Message.
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint32_t getNumSubscribers() const
virtual bool used()
Returns true if there are subscribers to the topic.


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Sat Feb 13 2021 03:42:55