topic_info.cpp
Go to the documentation of this file.
2 
3 namespace topics_rviz_plugin
4 {
5 
6 TopicInfo::TopicInfo(const std::string topic_name,
7  const std::string topic_type,
8  const ros::Duration refresh_duration) :
9  topic_name_(topic_name),
10  topic_type_(topic_type),
11  refresh_duration_(refresh_duration)
12 {
13  // Force refresh
15 
16  label_ = std::make_shared<QLabel>(QString::fromStdString(topic_name_));
17  label_->setToolTip(QString::fromStdString(topic_type_));
18 
19  if (topic_type_ == "std_msgs/Bool" ||
20  topic_type_ == "std_msgs/Duration" ||
21  topic_type_ == "std_msgs/String" ||
22  topic_type_ == "std_msgs/Time")
23  {
24  display_ = std::make_shared<QLabel>();
25  }
26  else if (topic_type_ == "std_msgs/Int8" ||
27  topic_type_ == "std_msgs/Int16" ||
28  topic_type_ == "std_msgs/Int32" ||
29  topic_type_ == "std_msgs/Int64" ||
30  topic_type_ == "std_msgs/UInt8" ||
31  topic_type_ == "std_msgs/UInt16" ||
32  topic_type_ == "std_msgs/UInt32" ||
33  topic_type_ == "std_msgs/UInt64" ||
34  topic_type_ == "std_msgs/Float32" ||
35  topic_type_ == "std_msgs/Float64")
36  {
37  display_ = std::make_shared<QLCDNumber>();
38  std::static_pointer_cast<QLCDNumber>(display_)->setFrameShape(QFrame::Shape::NoFrame);
39  }
40  else
41  {
42  ROS_ERROR_STREAM("Unsupported built-in type, cannot display topic " << topic_name_ << " of type " << topic_type_);
43  display_ = std::make_shared<QLabel>("Unsupported topic type");
44  return;
45  };
46 
47  if (topic_type == "std_msgs/Bool")
49  else if (topic_type == "std_msgs/Duration")
51  else if (topic_type == "std_msgs/Float32")
53  else if (topic_type == "std_msgs/Float64")
55  else if (topic_type == "std_msgs/Int8")
57  else if (topic_type == "std_msgs/Int16")
59  else if (topic_type == "std_msgs/Int32")
61  else if (topic_type == "std_msgs/Int64")
63  else if (topic_type == "std_msgs/String")
65  else if (topic_type == "std_msgs/Time")
67  else if (topic_type == "std_msgs/UInt8")
69  else if (topic_type == "std_msgs/UInt16")
71  else if (topic_type == "std_msgs/UInt32")
73  else if (topic_type == "std_msgs/UInt64")
75  else
76  {
77  ROS_ERROR_STREAM("Could not find callback for topic type " << topic_type_);
78  return;
79  }
80 
82 }
83 
85 {
86  nh_.shutdown();
87 }
88 
89 void
91 {
93  last_ = ros::Time::now() - refresh_duration_; // Force update of the value
94 }
95 
98 {
99  return refresh_duration_;
100 }
101 
102 bool
104 {
105  ros::Time now(ros::Time::now());
106 
108  return true;
109 
110  if ((now - refresh_duration_) >= last_)
111  {
112  last_ = now;
113  return true;
114  }
115  else
116  return false;
117 }
118 
119 void
120 TopicInfo::adjustLCDNumberOfDigitsHandler(const long unsigned number)
121 {
122  std::shared_ptr<QLCDNumber> lcd(std::dynamic_pointer_cast<QLCDNumber>(display_));
123  if (!lcd)
124  return;
125 
126  if (number < 100)
127  lcd->setDigitCount(5);
128  else if (number < 10000)
129  lcd->setDigitCount(7);
130  else if (number < 1000000)
131  lcd->setDigitCount(9);
132  else if (number < 100000000)
133  lcd->setDigitCount(11);
134  else
135  lcd->setDigitCount(20);
136 }
137 
138 void
139 TopicInfo::boolCallback(const std_msgs::BoolConstPtr &msg)
140 {
141  if (!refresh())
142  return;
143 
144  QString label("False");
145  if (msg->data)
146  label = "True";
147 
148  std::static_pointer_cast<QLabel>(display_)->setText(label);
149 }
150 
151 void
152 TopicInfo::durationCallback(const std_msgs::DurationConstPtr &msg)
153 {
154  if (!refresh())
155  return;
156 
157  QTime t(0, 0, 0, 0);
158  t = t.addSecs(msg->data.sec);
159  t = t.addMSecs(msg->data.nsec / 1e6);
160  std::static_pointer_cast<QLabel>(display_)->setText(t.toString("hh:mm:ss:zzz"));
161 }
162 
163 void
164 TopicInfo::float32Callback(const std_msgs::Float32ConstPtr &msg)
165 {
166  if (!refresh())
167  return;
168 
169  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
170  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
171  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
172 }
173 
174 void
175 TopicInfo::float64Callback(const std_msgs::Float64ConstPtr &msg)
176 {
177  if (!refresh())
178  return;
179 
180  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
181  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
182  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
183 }
184 
185 void
186 TopicInfo::int8Callback(const std_msgs::Int8ConstPtr &msg)
187 {
188  if (!refresh())
189  return;
190 
191  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
192  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
193  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
194 }
195 
196 void
197 TopicInfo::int16Callback(const std_msgs::Int16ConstPtr &msg)
198 {
199  if (!refresh())
200  return;
201 
202  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
203  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
204  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
205 }
206 
207 void
208 TopicInfo::int32Callback(const std_msgs::Int32ConstPtr &msg)
209 {
210  if (!refresh())
211  return;
212 
213  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
214  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
215  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
216 }
217 
218 void
219 TopicInfo::int64Callback(const std_msgs::Int64ConstPtr &msg)
220 {
221  if (!refresh())
222  return;
223 
224  long unsigned digits_counting(msg->data > 0 ? msg->data : -msg->data);
225  Q_EMIT adjustLCDNumberOfDigits(digits_counting);
226  int32_t casted(msg->data);
227  std::static_pointer_cast<QLCDNumber>(display_)->display(casted);
228 }
229 
230 void
231 TopicInfo::stringCallback(const std_msgs::StringConstPtr &msg)
232 {
233  if (!refresh())
234  return;
235 
236  std::static_pointer_cast<QLabel>(display_)->setText(QString::fromStdString(msg->data));
237 }
238 
239 void
240 TopicInfo::timeCallback(const std_msgs::TimeConstPtr &msg)
241 {
242  if (!refresh())
243  return;
244 
245  QDateTime time;
246  time.setMSecsSinceEpoch(msg->data.sec * 1e3 + msg->data.nsec / 1e6);
247  std::static_pointer_cast<QLabel>(display_)->setText(time.toString(Qt::DateFormat::ISODate));
248 }
249 
250 void
251 TopicInfo::uint8Callback(const std_msgs::UInt8ConstPtr &msg)
252 {
253  if (!refresh())
254  return;
255 
256  Q_EMIT adjustLCDNumberOfDigits(msg->data);
257  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
258 }
259 
260 void
261 TopicInfo::uint16Callback(const std_msgs::UInt16ConstPtr &msg)
262 {
263  if (!refresh())
264  return;
265 
266  Q_EMIT adjustLCDNumberOfDigits(msg->data);
267  std::static_pointer_cast<QLCDNumber>(display_)->display(msg->data);
268 }
269 
270 void
271 TopicInfo::uint32Callback(const std_msgs::UInt32ConstPtr &msg)
272 {
273  if (!refresh())
274  return;
275 
276  uint16_t casted(msg->data);
277  Q_EMIT adjustLCDNumberOfDigits(casted);
278  std::static_pointer_cast<QLCDNumber>(display_)->display(casted);
279 }
280 
281 void
282 TopicInfo::uint64Callback(const std_msgs::UInt64ConstPtr &msg)
283 {
284  if (!refresh())
285  return;
286 
287  uint16_t casted(msg->data);
288  Q_EMIT adjustLCDNumberOfDigits(casted);
289  std::static_pointer_cast<QLCDNumber>(display_)->display(casted);
290 }
291 
292 }
void setMaximumRefreshRate(const ros::Duration d)
Definition: topic_info.cpp:90
void uint64Callback(const std_msgs::UInt64ConstPtr &msg)
Definition: topic_info.cpp:282
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void int8Callback(const std_msgs::Int8ConstPtr &msg)
Definition: topic_info.cpp:186
TopicInfo(const std::string topic_name, const std::string topic_type, const ros::Duration refresh_duration=ros::Duration(0))
Definition: topic_info.cpp:6
void adjustLCDNumberOfDigitsHandler(const long unsigned number)
Definition: topic_info.cpp:120
const std::string topic_type_
Definition: topic_info.hpp:41
void float32Callback(const std_msgs::Float32ConstPtr &msg)
Definition: topic_info.cpp:164
void durationCallback(const std_msgs::DurationConstPtr &msg)
Definition: topic_info.cpp:152
geometry_msgs::TransformStamped t
void stringCallback(const std_msgs::StringConstPtr &msg)
Definition: topic_info.cpp:231
void float64Callback(const std_msgs::Float64ConstPtr &msg)
Definition: topic_info.cpp:175
const std::string topic_name_
Definition: topic_info.hpp:40
void timeCallback(const std_msgs::TimeConstPtr &msg)
Definition: topic_info.cpp:240
std::shared_ptr< QLabel > label_
Definition: topic_info.hpp:42
ros::Duration maximumRefreshRate()
Definition: topic_info.cpp:97
void int16Callback(const std_msgs::Int16ConstPtr &msg)
Definition: topic_info.cpp:197
static Time now()
std::shared_ptr< QWidget > display_
Definition: topic_info.hpp:43
void uint8Callback(const std_msgs::UInt8ConstPtr &msg)
Definition: topic_info.cpp:251
#define ROS_ERROR_STREAM(args)
void uint32Callback(const std_msgs::UInt32ConstPtr &msg)
Definition: topic_info.cpp:271
void int64Callback(const std_msgs::Int64ConstPtr &msg)
Definition: topic_info.cpp:219
void int32Callback(const std_msgs::Int32ConstPtr &msg)
Definition: topic_info.cpp:208
void uint16Callback(const std_msgs::UInt16ConstPtr &msg)
Definition: topic_info.cpp:261
void adjustLCDNumberOfDigits(const long unsigned number)
void boolCallback(const std_msgs::BoolConstPtr &msg)
Definition: topic_info.cpp:139


topics_rviz_plugin
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Mon Feb 28 2022 23:53:11