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


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Sat May 24 2025 02:24:01