rosout_publisher.cpp
Go to the documentation of this file.
1 #include "rosout_publisher.h"
2 #include "PlotJuggler/any.hpp"
3 #include "../shape_shifter_factory.hpp"
4 #include "../qnodedialog.h"
5 #include <QSettings>
6 #include <rosbag/bag.h>
7 
9  _enabled(false ),
10  _tablemodel(nullptr),
11  _log_window(nullptr)
12 {
13 
14 }
15 
17 {
18 
19 }
20 
21 
22 void RosoutPublisher::setEnabled(bool to_enable)
23 {
24  _enabled = to_enable;
25 
26  if( enabled())
27  {
28  _minimum_time_usec = std::numeric_limits<int64_t>::max();
29  _maximum_time_usec = std::numeric_limits<int64_t>::min();
30 
31  if( _tablemodel == nullptr ){
32  _tablemodel = new LogsTableModel(this);
33  }
34 
35  _log_window = new RosoutWindow();
36 
37  auto logwidget = new rqt_console_plus::LogWidget( *_tablemodel, _log_window);
38  _log_window->setCentralWidget( logwidget );
39  Qt::WindowFlags flags = _log_window->windowFlags();
40  _log_window->setWindowFlags( flags | Qt::SubWindow );
41 
42  connect( this, &RosoutPublisher::timeRangeChanged,
44 
47 
48  QSettings settings;
49  _log_window->restoreGeometry(settings.value("RosoutPublisher.geometry").toByteArray());
50 
51  _log_window->show();
52  }
53  else{
54  if( _log_window )
55  {
56  _log_window->close();
57  }
58  }
59 
61 }
62 
64 {
65  QSettings settings;
66  settings.setValue("RosoutPublisher.geometry", _log_window->saveGeometry());
67 
68  if( _tablemodel ){
69  _tablemodel->deleteLater();
70  _tablemodel = nullptr;
71  }
72  if( _log_window ) {
73  _log_window->deleteLater();
74  _log_window = nullptr;
75  }
76  _enabled = false;
77 }
78 
79 
80 std::vector<const PlotDataAny *> RosoutPublisher::findRosoutTimeseries()
81 {
82  std::vector<const PlotDataAny*> logs_timeseries;
83 
84  for(const auto& data_it: _datamap->user_defined )
85  {
86  const std::string& topic_name = data_it.first;
87 
88  // check if I registered this message before
89  const RosIntrospection::ShapeShifter* registered_shapeshifted_msg = RosIntrospectionFactory::get().getShapeShifter( topic_name );
90  if( ! registered_shapeshifted_msg )
91  {
92  continue; // will not be able to use this anyway, just skip
93  }
94 
95  if(registered_shapeshifted_msg->getMD5Sum() != std::string(ros::message_traits::MD5Sum< rosgraph_msgs::Log >::value()) )
96  {
97  continue; // it is NOT a rosgraph_msgs::Log
98  }
99 
100  logs_timeseries.push_back( &data_it.second );
101  }
102 
103  return logs_timeseries;
104 }
105 
106 void RosoutPublisher::syncWithTableModel(const std::vector<const PlotDataAny*>& logs_timeseries)
107 {
108  const int64_t threshold_time = _maximum_time_usec;
109 
110  std::vector<rosgraph_msgs::LogConstPtr> logs;
111  logs.reserve(100);
112 
113  // most of the time we expect logs_timeseries to have just 1 element
114  for(const PlotDataAny* type_erased_logs: logs_timeseries )
115  {
116  const int first_index = type_erased_logs->getIndexFromX( threshold_time );
117 
118  if( first_index != -1)
119  {
120  for( int i=first_index; i< type_erased_logs->size(); i++)
121  {
122  const auto& any_msg = type_erased_logs->at(i);
123  const nonstd::any& any_value = any_msg.y;
124 
125  const bool isRawBuffer = any_value.type() == typeid( std::vector<uint8_t>);
126  const bool isRosbagMessage = any_value.type() == typeid( rosbag::MessageInstance );
127  std::vector<uint8_t> raw_buffer;
128 
129  if( isRawBuffer){
130  raw_buffer = nonstd::any_cast<std::vector<uint8_t>>( any_value );
131  }
132  else if( isRosbagMessage ){
133  const rosbag::MessageInstance& msg_instance = nonstd::any_cast<rosbag::MessageInstance>( any_value );
134  raw_buffer.resize( msg_instance.size() );
135  ros::serialization::OStream stream(raw_buffer.data(), raw_buffer.size());
136  msg_instance.write(stream);
137  }
138  else{
139  continue;
140  }
141 
142  rosgraph_msgs::LogPtr p(boost::make_shared<rosgraph_msgs::Log>());
143  ros::serialization::IStream stream(raw_buffer.data(), raw_buffer.size() );
145 
146  int64_t usec = p->header.stamp.toNSec() / 1000;
147  _minimum_time_usec = std::min( _minimum_time_usec, usec);
148  _maximum_time_usec = std::max( _maximum_time_usec, usec);
149 
150  if( usec >= threshold_time){
151  logs.push_back( p );
152  }
153  }
154  }
155  }
156  std::sort( logs.begin(), logs.end(),
157  [](const rosgraph_msgs::LogConstPtr& a, const rosgraph_msgs::LogConstPtr& b)
158  {
159  return a->header.stamp < b->header.stamp;
160  } );
161  _tablemodel->push_back( logs );
162 }
163 
164 void RosoutPublisher::updateState(double current_time)
165 {
166  if(!_enabled && !_tablemodel) return;
167 
168  std::vector<const PlotDataAny*> logs_timeseries = findRosoutTimeseries();
169 
170  syncWithTableModel(logs_timeseries);
171 
172  using namespace std::chrono;
173  TimePoint p_min = TimePoint() + microseconds(_minimum_time_usec);
174  //TimePoint p_max = TimePoint() + microseconds(_maximum_time_usec);
175  TimePoint p_curr = TimePoint() + microseconds( (int64_t)(current_time*1000000));
176 
177  emit timeRangeChanged(p_min, p_curr);
178 }
ValueType any_cast(any const &operand)
Definition: any.hpp:432
std::vector< const PlotDataAny * > findRosoutTimeseries()
std::chrono::high_resolution_clock::time_point TimePoint
RosoutWindow * _log_window
void timeRangeChanged(TimePoint time_min, TimePoint time_max)
static RosIntrospectionFactory & get()
virtual void updateState(double current_time) override
int64_t _minimum_time_usec
std::unordered_map< std::string, PlotDataAny > user_defined
Definition: plotdata.h:145
std::string const & getMD5Sum() const
uint32_t size() const
LogsTableModel * _tablemodel
virtual bool enabled() const override
virtual void setEnabled(bool enabled) override
const PlotDataMapRef * _datamap
int64_t _maximum_time_usec
void syncWithTableModel(const std::vector< const PlotDataAny * > &logs_timeseries)
virtual ~RosoutPublisher()
static RosIntrospection::ShapeShifter * getShapeShifter(const std::string &topic_name)
void push_back(const rosgraph_msgs::Log::ConstPtr &pushed_log)
uint64_t b
int i
void on_timeRangeChanged(TimePoint time_min, TimePoint time_max)
Definition: logwidget.cpp:144
const std::type_info & type() const any_noexcept
Definition: any.hpp:342
void deserialize(Stream &stream, T &t)
int flags
int a
void write(Stream &stream) const
virtual void setEnabled(bool enabled)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18