Go to the documentation of this file.00001 #include "statepublisher_rostopic.h"
00002 #include "PlotJuggler/any.hpp"
00003 #include "../shape_shifter_factory.hpp"
00004 #include "../qnodedialog.h"
00005 #include "timestamp_injector.h"
00006 #include <rosbag/bag.h>
00007
00008 TopicPublisherROS::TopicPublisherROS():
00009 enabled_(false ),
00010 _node(nullptr)
00011 {
00012
00013 }
00014
00015 TopicPublisherROS::~TopicPublisherROS()
00016 {
00017 enabled_ = false;
00018 }
00019
00020 void TopicPublisherROS::setParentMenu(QMenu *menu)
00021 {
00022 _menu = menu;
00023 _current_time = new QAction(QString("use current time in std_msg/Header "), _menu);
00024 _current_time->setCheckable(true);
00025 _current_time->setChecked(true);
00026 _menu->addAction( _current_time );
00027 }
00028
00029 void TopicPublisherROS::setEnabled(bool to_enable)
00030 {
00031 if( !_node )
00032 {
00033 _node = RosManager::getNode();
00034 }
00035 enabled_ = (to_enable && _node);
00036 }
00037
00038
00039 void TopicPublisherROS::updateState(PlotDataMap *datamap, double current_time)
00040 {
00041 if(!enabled_ || !_node) return;
00042
00043 const ros::Time ros_time =ros::Time::now();
00044
00045 for(const auto& data_it: datamap->user_defined )
00046 {
00047 const std::string& topic_name = data_it.first;
00048
00049 const RosIntrospection::ShapeShifter* registered_shapeshifted_msg = RosIntrospectionFactory::get().getShapeShifter( topic_name );
00050 if( ! registered_shapeshifted_msg )
00051 {
00052
00053 continue;
00054 }
00055
00056 RosIntrospection::ShapeShifter shapeshifted_msg = *registered_shapeshifted_msg;
00057 const PlotDataAnyPtr& plot_any = data_it.second;
00058
00059 nonstd::optional<nonstd::any> any_value = plot_any->getYfromX( current_time );
00060
00061 if(!any_value)
00062 {
00063
00064 continue;
00065 }
00066 const bool isRawBuffer = any_value->type() == typeid( std::vector<uint8_t>);
00067 const bool isRosbagMessage = any_value->type() == typeid(rosbag::MessageInstance);
00068
00069 std::vector<uint8_t> raw_buffer;
00070
00071 if( isRawBuffer){
00072 raw_buffer = nonstd::any_cast<std::vector<uint8_t>>( any_value.value() );
00073 }
00074 else if( isRosbagMessage ){
00075 const rosbag::MessageInstance& msg_instance = nonstd::any_cast<rosbag::MessageInstance>( any_value.value() );
00076 raw_buffer.resize( msg_instance.size() );
00077 ros::serialization::OStream stream(raw_buffer.data(), raw_buffer.size());
00078 msg_instance.write(stream);
00079 }
00080 else{
00081 continue;
00082 }
00083
00084 if( _current_time->isChecked())
00085 {
00086 auto typelist = RosIntrospectionFactory::get().getRosTypeList( topic_name );
00087 if(typelist)
00088 {
00089 injectTime(*typelist, shapeshifted_msg.getDataType(), raw_buffer.data(), ros_time);
00090 }
00091 }
00092
00093 ros::serialization::IStream stream( raw_buffer.data(), raw_buffer.size() );
00094 shapeshifted_msg.read( stream );
00095
00096 auto publisher_it = publishers_.find( topic_name );
00097 if( publisher_it == publishers_.end())
00098 {
00099 auto res = publishers_.insert( std::make_pair(topic_name,
00100 shapeshifted_msg.advertise( *_node, topic_name, 10, true) ) );
00101 publisher_it = res.first;
00102 }
00103
00104 const ros::Publisher& publisher = publisher_it->second;
00105 publisher.publish( shapeshifted_msg );
00106 }
00107
00108 }