protobuf2ros_publisher.cc
Go to the documentation of this file.
00001 /* 
00002 * Roboception GmbH 
00003 * Munich, Germany 
00004 * www.roboception.com 
00005 * 
00006 * Copyright (c) 2017 Roboception GmbH 
00007 * All rights reserved 
00008 * 
00009 * Author: Christian Emmerich 
00010 */
00011 
00012 
00013 #include "protobuf2ros_publisher.h"
00014 
00015 #include "protobuf2ros_conversions.h"
00016 
00017 using namespace std;
00018 
00019 namespace rc
00020 {
00021 
00022 Protobuf2RosPublisher::Protobuf2RosPublisher(ros::NodeHandle &nh,
00023                                              const string &topic,
00024                                              const string &pbMsgType,
00025                                              const string &frame_id_prefix)
00026         : _tfPrefix(frame_id_prefix)
00027 {
00028   if (pbMsgType == "Imu")
00029   {
00030     pub = nh.advertise<sensor_msgs::Imu>(topic, 1000);
00031   } else if (pbMsgType == "Frame")
00032   {
00033     pub = nh.advertise<geometry_msgs::PoseStamped>(topic, 1000);
00034   } else
00035   {
00036     stringstream msg;
00037     msg << "Protobuf message type '" << pbMsgType << "' not supported!";
00038     throw runtime_error(msg.str());
00039   }
00040 }
00041 
00042 
00043 bool Protobuf2RosPublisher::used()
00044 {
00045   return pub.getNumSubscribers() > 0;
00046 }
00047 
00048 
00049 void Protobuf2RosPublisher::publish(shared_ptr<::google::protobuf::Message> pbMsg)
00050 {
00051   string msg_name = pbMsg->GetDescriptor()->name();
00052 
00053   if (msg_name == "Imu")
00054   {
00055     // dynamically cast to Imu type, check validity, and transform to ros
00056 
00057     shared_ptr <roboception::msgs::Imu> protoImu =
00058             dynamic_pointer_cast<roboception::msgs::Imu>(pbMsg);
00059     if (!protoImu)
00060     {
00061       throw runtime_error("Given protobuf message was not of type Imu!");
00062     }
00063 
00064     auto rosImu = toRosImu(protoImu);
00065     rosImu->header.frame_id = _tfPrefix + rosImu->header.frame_id;
00066     pub.publish(rosImu);
00067   }
00068   else if (msg_name == "Frame")
00069   {
00070     // dynamically cast to Frame type, check validity, and transform to ros
00071 
00072     shared_ptr <roboception::msgs::Frame> protoFrame =
00073             dynamic_pointer_cast<roboception::msgs::Frame>(pbMsg);
00074     if (!protoFrame)
00075     {
00076       throw runtime_error("Given protobuf message was not of type Frame!");
00077     }
00078 
00079     auto rosPose = toRosPoseStamped(protoFrame);
00080     rosPose->header.frame_id = _tfPrefix + rosPose->header.frame_id;
00081     pub.publish(rosPose);
00082   }
00083   else
00084   {
00085     stringstream msg;
00086     msg << "Protobuf message type '" << msg_name << "' not supported!";
00087     throw runtime_error(msg.str());
00088   }
00089 }
00090 
00091 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06