Skip to content

Commit 28c9af7

Browse files
committed
Simplify code
1 parent adeb2bf commit 28c9af7

File tree

2 files changed

+24
-45
lines changed

2 files changed

+24
-45
lines changed

src/rviz/image/image_display_base.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
namespace rviz
4343
{
44-
ImageDisplayBase::ImageDisplayBase() : Display(), sub_(), tf_filter_(), messages_received_(0), timeout_tm_(0), is_img_up_(false)
44+
ImageDisplayBase::ImageDisplayBase() : Display(), sub_(), tf_filter_(), messages_received_(0)
4545
{
4646
topic_property_ = new RosTopicProperty(
4747
"Image Topic", "", QString::fromStdString(ros::message_traits::datatype<sensor_msgs::Image>()),
@@ -66,17 +66,15 @@ ImageDisplayBase::ImageDisplayBase() : Display(), sub_(), tf_filter_(), messages
6666
unreliable_property_ = new BoolProperty("Unreliable", false, "Prefer UDP topic transport", this,
6767
&ImageDisplayBase::updateTopic);
6868

69-
reset_to_property_ = new BoolProperty("Reset When Timed Out", false,
70-
"Reset when new image has not been received for Timeout [sec].",
71-
this, &ImageDisplayBase::updateResetTO);
72-
7369
timeout_property_ =
7470
new FloatProperty("Timeout", 1.0,
75-
"Seconds to wait before resetting when new image has not been received.",
76-
this, &ImageDisplayBase::updateResetTO);
71+
"Seconds to wait before resetting when no new image has been received.\n"
72+
"Zero disables the feature.",
73+
this, &ImageDisplayBase::updateResetTimeout);
7774

78-
reset_to_timer_ = new QTimer(this);
79-
connect(reset_to_timer_, &QTimer::timeout, this, &ImageDisplayBase::onResetTOTimer);
75+
reset_timer_ = new QTimer(this);
76+
connect(reset_timer_, &QTimer::timeout, this, &ImageDisplayBase::onResetTimer);
77+
updateResetTimeout();
8078
}
8179

8280
ImageDisplayBase::~ImageDisplayBase()
@@ -125,10 +123,9 @@ void ImageDisplayBase::incomingMessage(const sensor_msgs::Image::ConstPtr& msg)
125123
setStatus(StatusProperty::Ok, "Image", QString::number(messages_received_) + " images received");
126124

127125
emitTimeSignal(msg->header.stamp);
126+
last_received_ = ros::Time::now();
128127

129128
processMessage(msg);
130-
131-
is_img_up_ = true;
132129
}
133130

134131

@@ -152,8 +149,7 @@ void ImageDisplayBase::reset()
152149
messages_received_ = 0;
153150
setStatus(StatusProperty::Warn, "Image", "No Image received");
154151

155-
timeout_tm_ = ros::Time(0);
156-
is_img_up_ = false;
152+
last_received_ = ros::Time();
157153
}
158154

159155
void ImageDisplayBase::updateQueueSize()
@@ -278,16 +274,12 @@ void ImageDisplayBase::updateTopic()
278274
context_->queueRender();
279275
}
280276

281-
void ImageDisplayBase::updateResetTO()
277+
void ImageDisplayBase::updateResetTimeout()
282278
{
283-
if (reset_to_property_->getBool())
284-
{
285-
reset_to_timer_->start(33);
286-
}
279+
if (timeout_property_->getFloat() > 0)
280+
reset_timer_->start(33); // frequently check for receive timeout
287281
else
288-
{
289-
reset_to_timer_->stop();
290-
}
282+
reset_timer_->stop();
291283
}
292284

293285
void ImageDisplayBase::fillTransportOptionList(EnumProperty* property)
@@ -334,20 +326,11 @@ void ImageDisplayBase::fillTransportOptionList(EnumProperty* property)
334326
}
335327
}
336328

337-
void ImageDisplayBase::onResetTOTimer()
329+
void ImageDisplayBase::onResetTimer()
338330
{
339-
if (is_img_up_)
340-
{
341-
is_img_up_ = false;
342-
timeout_tm_ = ros::Time::now() + ros::Duration(timeout_property_->getFloat());
343-
}
344-
else
345-
{
346-
if ((timeout_tm_ != ros::Time(0)) && (ros::Time::now() > timeout_tm_))
347-
{
348-
reset();
349-
}
350-
}
331+
ros::Time last = last_received_;
332+
if (!last.isZero() && ros::Time::now() > last + ros::Duration(timeout_property_->getFloat()))
333+
reset();
351334
}
352335

353336
} // end namespace rviz

src/rviz/image/image_display_base.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifndef IMAGE_DISPLAY_BASE_H
3030
#define IMAGE_DISPLAY_BASE_H
3131

32+
#include <atomic>
3233
#include <QObject>
3334
#include <QTimer>
3435

@@ -79,14 +80,14 @@ protected Q_SLOTS:
7980
/** @brief Update queue size of tf filter */
8081
virtual void updateQueueSize();
8182

82-
/** @brief Prepare for or stop resetting when timed out */
83-
virtual void updateResetTO();
83+
/** @brief Start or stop reset_timer_ */
84+
void updateResetTimeout();
8485

8586
/** @brief Fill list of available and working transport options */
8687
void fillTransportOptionList(EnumProperty* property);
8788

88-
/** @brief Timer function to reset when timed out */
89-
void onResetTOTimer();
89+
/** @brief Check for timeout and reset() if necessary */
90+
void onResetTimer();
9091

9192
protected:
9293
void onInitialize() override;
@@ -140,14 +141,9 @@ protected Q_SLOTS:
140141

141142
BoolProperty* unreliable_property_;
142143

143-
BoolProperty* reset_to_property_;
144-
145144
FloatProperty* timeout_property_;
146-
147-
ros::Time timeout_tm_;
148-
bool is_img_up_;
149-
150-
QTimer* reset_to_timer_;
145+
std::atomic<ros::Time> last_received_;
146+
QTimer* reset_timer_;
151147
};
152148

153149
} // end namespace rviz

0 commit comments

Comments
 (0)