Skip to content

Commit 116c6a9

Browse files
pazeshunrhaschke
andauthored
Reset image displays when no new image has been received for a while (#1852)
Co-authored-by: Robert Haschke <[email protected]>
1 parent f40ed82 commit 116c6a9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/rviz/image/image_display_base.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
namespace rviz
4343
{
44-
ImageDisplayBase::ImageDisplayBase() : Display(), sub_(), tf_filter_(), messages_received_(0)
44+
ImageDisplayBase::ImageDisplayBase()
45+
: Display(), sub_(), tf_filter_(), messages_received_(0), last_received_(ros::Time())
4546
{
4647
topic_property_ = new RosTopicProperty(
4748
"Image Topic", "", QString::fromStdString(ros::message_traits::datatype<sensor_msgs::Image>()),
@@ -65,6 +66,16 @@ ImageDisplayBase::ImageDisplayBase() : Display(), sub_(), tf_filter_(), messages
6566

6667
unreliable_property_ = new BoolProperty("Unreliable", false, "Prefer UDP topic transport", this,
6768
&ImageDisplayBase::updateTopic);
69+
70+
timeout_property_ =
71+
new FloatProperty("Timeout", 1.0,
72+
"Seconds to wait before resetting when no new image has been received.\n"
73+
"Zero disables the feature.",
74+
this, &ImageDisplayBase::updateResetTimeout);
75+
76+
reset_timer_ = new QTimer(this);
77+
connect(reset_timer_, &QTimer::timeout, this, &ImageDisplayBase::onResetTimer);
78+
updateResetTimeout();
6879
}
6980

7081
ImageDisplayBase::~ImageDisplayBase()
@@ -113,6 +124,7 @@ void ImageDisplayBase::incomingMessage(const sensor_msgs::Image::ConstPtr& msg)
113124
setStatus(StatusProperty::Ok, "Image", QString::number(messages_received_) + " images received");
114125

115126
emitTimeSignal(msg->header.stamp);
127+
last_received_ = ros::Time::now();
116128

117129
processMessage(msg);
118130
}
@@ -137,6 +149,8 @@ void ImageDisplayBase::reset()
137149

138150
messages_received_ = 0;
139151
setStatus(StatusProperty::Warn, "Image", "No Image received");
152+
153+
last_received_ = ros::Time();
140154
}
141155

142156
void ImageDisplayBase::updateQueueSize()
@@ -261,6 +275,14 @@ void ImageDisplayBase::updateTopic()
261275
context_->queueRender();
262276
}
263277

278+
void ImageDisplayBase::updateResetTimeout()
279+
{
280+
if (timeout_property_->getFloat() > 0)
281+
reset_timer_->start(33); // frequently check for receive timeout
282+
else
283+
reset_timer_->stop();
284+
}
285+
264286
void ImageDisplayBase::fillTransportOptionList(EnumProperty* property)
265287
{
266288
property->clearOptions();
@@ -305,4 +327,11 @@ void ImageDisplayBase::fillTransportOptionList(EnumProperty* property)
305327
}
306328
}
307329

330+
void ImageDisplayBase::onResetTimer()
331+
{
332+
ros::Time last = last_received_;
333+
if (!last.isZero() && ros::Time::now() > last + ros::Duration(timeout_property_->getFloat()))
334+
reset();
335+
}
336+
308337
} // end namespace rviz

src/rviz/image/image_display_base.h

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

32+
#include <atomic>
3233
#include <QObject>
34+
#include <QTimer>
3335

3436
#ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829
3537
#include <message_filters/subscriber.h>
@@ -44,6 +46,7 @@
4446
#include "rviz/properties/ros_topic_property.h"
4547
#include "rviz/properties/enum_property.h"
4648
#include "rviz/properties/int_property.h"
49+
#include "rviz/properties/float_property.h"
4750

4851
#include "rviz/display.h"
4952
#include "rviz/rviz_export.h"
@@ -77,9 +80,15 @@ protected Q_SLOTS:
7780
/** @brief Update queue size of tf filter */
7881
virtual void updateQueueSize();
7982

83+
/** @brief Start or stop reset_timer_ */
84+
void updateResetTimeout();
85+
8086
/** @brief Fill list of available and working transport options */
8187
void fillTransportOptionList(EnumProperty* property);
8288

89+
/** @brief Check for timeout and reset() if necessary */
90+
void onResetTimer();
91+
8392
protected:
8493
void onInitialize() override;
8594

@@ -131,6 +140,10 @@ protected Q_SLOTS:
131140
std::set<std::string> transport_plugin_types_;
132141

133142
BoolProperty* unreliable_property_;
143+
144+
FloatProperty* timeout_property_;
145+
std::atomic<ros::Time> last_received_;
146+
QTimer* reset_timer_;
134147
};
135148

136149
} // end namespace rviz

0 commit comments

Comments
 (0)