topic_data.cpp
Go to the documentation of this file.
2 
3 namespace graph_rviz_plugin
4 {
5 
6 TopicData::TopicData(std::string topic_name,
7  std::string topic_type,
8  std::shared_ptr<ros::NodeHandle> nh,
9  QObject *parent) :
10  QObject(parent),
11  nh_(nh),
12  topic_name_(topic_name),
13  topic_type_(topic_type)
14 {
15 }
16 
18 {
19 }
20 
22 {
24 
25  if (!nh_)
26  throw std::runtime_error("Node handle not initialized!");
27 
28  if (topic_type_ == "std_msgs/Bool")
29  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::boolCallback, this);
30  else if (topic_type_ == "std_msgs/Duration")
31  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::durationCallback, this);
32  else if (topic_type_ == "std_msgs/Float32")
33  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::float32Callback, this);
34  else if (topic_type_ == "std_msgs/Float64")
35  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::float64Callback, this);
36  else if (topic_type_ == "std_msgs/Int8")
37  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::int8Callback, this);
38  else if (topic_type_ == "std_msgs/Int16")
39  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::int16Callback, this);
40  else if (topic_type_ == "std_msgs/Int32")
41  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::int32Callback, this);
42  else if (topic_type_ == "std_msgs/Int64")
43  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::int64Callback, this);
44  else if (topic_type_ == "std_msgs/UInt8")
45  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::uint8Callback, this);
46  else if (topic_type_ == "std_msgs/UInt16")
47  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::uint16Callback, this);
48  else if (topic_type_ == "std_msgs/UInt32")
49  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::uint32Callback, this);
50  else if (topic_type_ == "std_msgs/UInt64")
51  sub_ = nh_->subscribe(topic_name_, 1, &TopicData::uint64Callback, this);
52  else
53  {
54  ROS_ERROR_STREAM("Could not find callback for topic type " << topic_type_);
55  return;
56  }
57 }
58 
60 {
61  topic_data_.clear();
62  topic_time_.clear();
63 }
64 
66 {
67  sub_.shutdown();
68 }
69 
70 void TopicData::pushData(const double data, const ros::Time now)
71 {
72  double time = (now.toSec() - begin_.toSec());
73 
74  try
75  {
76  topic_time_.push_back(time);
77  topic_data_.push_back(data);
78  }
79  catch (const std::exception &e)
80  {
81  ROS_WARN_STREAM("Memory full, acquisition stop");
82  Q_EMIT displayMessageBox("Fatal Error", "Memory is full, acquisition is stopped.", "",
83  QMessageBox::Icon::Critical);
84  sub_.shutdown();
85  }
86 }
87 
88 void TopicData::boolCallback(const std_msgs::BoolConstPtr &msg)
89 {
90  std::lock_guard<std::mutex> guard(data_mutex_);
91  const double data(msg->data);
92  pushData(data, ros::Time::now());
93  data_update_ = true;
94 }
95 
96 void TopicData::durationCallback(const std_msgs::DurationConstPtr &msg)
97 {
98  std::lock_guard<std::mutex> guard(data_mutex_);
99  const double data = (msg->data).toSec();
100  pushData(data, ros::Time::now());
101  data_update_ = true;
102 }
103 
104 void TopicData::float32Callback(const std_msgs::Float32ConstPtr &msg)
105 {
106  std::lock_guard<std::mutex> guard(data_mutex_);
107  const double data = (double) (msg->data);
108  pushData(data, ros::Time::now());
109  data_update_ = true;
110 }
111 
112 void TopicData::float64Callback(const std_msgs::Float64ConstPtr &msg)
113 {
114  std::lock_guard<std::mutex> guard(data_mutex_);
115  const double data = (double) (msg->data);
116  pushData(data, ros::Time::now());
117  data_update_ = true;
118 }
119 
120 void TopicData::int8Callback(const std_msgs::Int8ConstPtr &msg)
121 {
122  std::lock_guard<std::mutex> guard(data_mutex_);
123  const double data = (double) (msg->data);
124  pushData(data, ros::Time::now());
125  data_update_ = true;
126 }
127 
128 void TopicData::int16Callback(const std_msgs::Int16ConstPtr &msg)
129 {
130  std::lock_guard<std::mutex> guard(data_mutex_);
131  double data = (double) (msg->data);
132  pushData(data, ros::Time::now());
133  data_update_ = true;
134 }
135 
136 void TopicData::int32Callback(const std_msgs::Int32ConstPtr &msg)
137 {
138  std::lock_guard<std::mutex> guard(data_mutex_);
139  double data = (double) (msg->data);
140  pushData(data, ros::Time::now());
141  data_update_ = true;
142 }
143 
144 void TopicData::int64Callback(const std_msgs::Int64ConstPtr &msg)
145 {
146  std::lock_guard<std::mutex> guard(data_mutex_);
147  double data = (double) (msg->data);
148  pushData(data, ros::Time::now());
149  data_update_ = true;
150 }
151 
152 void TopicData::timeCallback(const std_msgs::TimeConstPtr &msg)
153 {
154  std::lock_guard<std::mutex> guard(data_mutex_);
155  double data = (msg->data).toSec();
156  pushData(data, ros::Time::now());
157  data_update_ = true;
158 }
159 
160 void TopicData::uint8Callback(const std_msgs::UInt8ConstPtr &msg)
161 {
162  std::lock_guard<std::mutex> guard(data_mutex_);
163  const double data = (double) (msg->data);
164  pushData(data, ros::Time::now());
165  data_update_ = true;
166 }
167 
168 void TopicData::uint16Callback(const std_msgs::UInt16ConstPtr &msg)
169 {
170  std::lock_guard<std::mutex> guard(data_mutex_);
171  const double data = (double) (msg->data);
172  pushData(data, ros::Time::now());
173  data_update_ = true;
174 }
175 
176 void TopicData::uint32Callback(const std_msgs::UInt32ConstPtr &msg)
177 {
178  std::lock_guard<std::mutex> guard(data_mutex_);
179  const double data = (double) (msg->data);
180  pushData(data, ros::Time::now());
181  data_update_ = true;
182 }
183 
184 void TopicData::uint64Callback(const std_msgs::UInt64ConstPtr &msg)
185 {
186  std::lock_guard<std::mutex> guard(data_mutex_);
187  const double data = (double) (msg->data);
188  pushData(data, ros::Time::now());
189  data_update_ = true;
190 }
191 
192 QVector<double> TopicData::getTopicData()
193 {
194  std::lock_guard<std::mutex> guard(data_mutex_);
195  return topic_data_;
196 }
197 
198 QVector<double> TopicData::getTopicTime()
199 {
200  std::lock_guard<std::mutex> guard(data_mutex_);
201  return topic_time_;
202 }
203 
204 void TopicData::displayMessageBoxHandler(const QString title,
205  const QString message,
206  const QString info_msg,
207  const QMessageBox::Icon icon)
208 {
209  QMessageBox msg_box;
210  msg_box.setWindowTitle(title);
211  msg_box.setText(message);
212  msg_box.setInformativeText(info_msg);
213  msg_box.setIcon(icon);
214  msg_box.setStandardButtons(QMessageBox::Ok);
215  msg_box.exec();
216 }
217 
218 }
void int16Callback(const std_msgs::Int16ConstPtr &msg)
Definition: topic_data.cpp:128
void uint32Callback(const std_msgs::UInt32ConstPtr &msg)
Definition: topic_data.cpp:176
void durationCallback(const std_msgs::DurationConstPtr &msg)
Definition: topic_data.cpp:96
void uint16Callback(const std_msgs::UInt16ConstPtr &msg)
Definition: topic_data.cpp:168
std::shared_ptr< ros::NodeHandle > nh_
Definition: topic_data.hpp:42
QVector< double > topic_time_
Definition: topic_data.hpp:52
QVector< double > getTopicData()
Definition: topic_data.cpp:192
void uint8Callback(const std_msgs::UInt8ConstPtr &msg)
Definition: topic_data.cpp:160
TopicData(std::string topic_name, std::string topic_type, std::shared_ptr< ros::NodeHandle >, QObject *parent=nullptr)
Definition: topic_data.cpp:6
void pushData(const double Data, const ros::Time now)
Definition: topic_data.cpp:70
const std::string topic_name_
Definition: topic_data.hpp:43
QVector< double > getTopicTime()
Definition: topic_data.cpp:198
void timeCallback(const std_msgs::TimeConstPtr &msg)
Definition: topic_data.cpp:152
void float32Callback(const std_msgs::Float32ConstPtr &msg)
Definition: topic_data.cpp:104
#define ROS_WARN_STREAM(args)
QVector< double > topic_data_
Definition: topic_data.hpp:51
void boolCallback(const std_msgs::BoolConstPtr &msg)
Definition: topic_data.cpp:88
const std::string topic_type_
Definition: topic_data.hpp:44
void float64Callback(const std_msgs::Float64ConstPtr &msg)
Definition: topic_data.cpp:112
void int8Callback(const std_msgs::Int8ConstPtr &msg)
Definition: topic_data.cpp:120
void uint64Callback(const std_msgs::UInt64ConstPtr &msg)
Definition: topic_data.cpp:184
void displayMessageBoxHandler(const QString title, const QString text, const QString info="", const QMessageBox::Icon icon=QMessageBox::Icon::Information)
Definition: topic_data.cpp:204
void int64Callback(const std_msgs::Int64ConstPtr &msg)
Definition: topic_data.cpp:144
static Time now()
#define ROS_ERROR_STREAM(args)
void displayMessageBox(const QString, const QString, const QString, const QMessageBox::Icon)
void int32Callback(const std_msgs::Int32ConstPtr &msg)
Definition: topic_data.cpp:136


graph_rviz_plugin
Author(s): Édouard Pronier, Victor Lamoine - Institut Maupertuis
autogenerated on Mon Feb 28 2022 22:27:30