AWViz-ROS
Loading...
Searching...
No Matches
display.hpp
Go to the documentation of this file.
1// Copyright 2024 Kotaro Uetake.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef AWVIZ_COMMON__DISPLAY_HPP_
16#define AWVIZ_COMMON__DISPLAY_HPP_
17
19
20#include <rclcpp/qos.hpp>
21#include <rclcpp/rclcpp.hpp>
22#include <rerun.hpp>
23#include <rosidl_runtime_cpp/traits.hpp>
24
25#include <memory>
26#include <optional>
27#include <string>
28#include <unordered_map>
29#include <utility>
30
31namespace awviz_common
32{
36enum class DisplayState : uint8_t {
37 kCreated = 0,
38 kInitialized = 1,
39 kConfigured = 2,
40 kRunning = 3,
41};
42
55{
56public:
57 Display() = default;
58
59 virtual ~Display() = default;
60
66 virtual void initialize(
67 rclcpp::Node::SharedPtr node, std::shared_ptr<rerun::RecordingStream> stream) = 0;
68
74 virtual void set_property(
75 const std::string & topic,
76 const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) = 0;
77
81 virtual void start() = 0;
82
86 virtual void end() = 0;
87
93 virtual bool is_initialized() const { return state_ != DisplayState::kCreated; }
94
95protected:
97 DisplayState state() const { return state_; }
98
99 rclcpp::Node::SharedPtr node_;
100 std::shared_ptr<rerun::RecordingStream> stream_;
102};
103
107template <typename MsgType>
109{
110public:
115 {
116 const auto msg_type = rosidl_generator_traits::name<MsgType>();
117 property_.set_type(msg_type);
118 }
119
124
131 rclcpp::Node::SharedPtr node, std::shared_ptr<rerun::RecordingStream> stream) override
132 {
133 node_ = std::move(node);
134 stream_ = std::move(stream);
136 };
137
144 const std::string & topic,
145 const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) override
146 {
147 property_.set_topic(topic);
148 property_.set_entity_roots(entity_roots);
150 }
151
155 void start() override
156 {
157 // Only transition to running when the display is properly initialized/configured.
158 if (!is_initialized()) {
159 return;
160 }
161 subscribe();
163 }
164
168 void end() override
169 {
170 unsubscribe();
172 }
173
174 bool is_initialized() const override
175 {
178 }
179
180protected:
181 static constexpr const char * TIMELINE_NAME = "timestamp";
182
187 void log_timestamp(const rclcpp::Time & stamp)
188 {
189 if (!stream_) {
190 return;
191 }
192 stream_->set_time_duration_secs(TIMELINE_NAME, stamp.seconds());
193 }
194
201 std::optional<std::string> resolve_entity_path(
202 const std::string & frame_id, bool include_topic = true) const
203 {
204 return include_topic ? property_.entity(frame_id) : property_.entity_without_topic(frame_id);
205 }
206
211 void log_warning_for_missing_entity(const std::string & frame_id) const
212 {
213 if (!stream_) {
214 return;
215 }
216
217 std::string message = "There is no corresponding entity path";
218
219 if (!frame_id.empty()) {
220 message += " for frame_id: " + frame_id;
221 }
222
223 const auto & topic = property_.topic();
224 if (!topic.empty()) {
225 message += " on topic: " + topic;
226 }
227
228 stream_->log(topic, rerun::TextLog(message));
229 }
230
235 void log_warning_text(const std::string & message) const
236 {
237 if (!stream_) {
238 return;
239 }
240
241 stream_->log(property_.topic(), rerun::TextLog(message));
242 }
243
248 virtual void subscribe()
249 {
250 if (!is_initialized()) {
251 return;
252 }
253
254 // TODO(ktro2828): QoS setting
255 subscription_ = node_->create_subscription<MsgType>(
256 property_.topic(), rclcpp::SensorDataQoS{},
257 [this](const typename MsgType::ConstSharedPtr msg) { log_message(msg); });
258 };
259
263 virtual void unsubscribe() { subscription_.reset(); }
264
270 virtual void log_message(typename MsgType::ConstSharedPtr msg) = 0;
271
272protected:
273 typename rclcpp::Subscription<MsgType>::SharedPtr subscription_;
275};
276} // namespace awviz_common
277#endif // AWVIZ_COMMON__DISPLAY_HPP_
Intermediate class for display items.
Definition: display.hpp:55
void set_state(DisplayState state)
Definition: display.hpp:96
virtual void start()=0
Start to display.
virtual void end()=0
End to display.
virtual bool is_initialized() const
Return true if the initialization is completed.
Definition: display.hpp:93
virtual void initialize(rclcpp::Node::SharedPtr node, std::shared_ptr< rerun::RecordingStream > stream)=0
Initialize attributes.
rclcpp::Node::SharedPtr node_
Node shared pointer.
Definition: display.hpp:99
virtual ~Display()=default
DisplayState state() const
Definition: display.hpp:97
std::shared_ptr< rerun::RecordingStream > stream_
RecordingStream shared pointer.
Definition: display.hpp:100
virtual void set_property(const std::string &topic, const std::shared_ptr< std::unordered_map< std::string, std::string > > entity_roots)=0
Set attributes of property.
DisplayState state_
Lifecycle state of the display.
Definition: display.hpp:101
Inherited plugin class to display ROS topics.
Definition: display.hpp:109
static constexpr const char * TIMELINE_NAME
Entity name of timeline record.
Definition: display.hpp:181
void log_warning_text(const std::string &message) const
Log a standard warning for missing entity paths.
Definition: display.hpp:235
virtual void subscribe()
Start to subscribing the specified topic.
Definition: display.hpp:248
void set_property(const std::string &topic, const std::shared_ptr< std::unordered_map< std::string, std::string > > entity_roots) override
Set status of attributes.
Definition: display.hpp:143
rclcpp::Subscription< MsgType >::SharedPtr subscription_
Subscription of the topic.
Definition: display.hpp:273
void initialize(rclcpp::Node::SharedPtr node, std::shared_ptr< rerun::RecordingStream > stream) override
Initialize the instance specifying the root ROS node and recording stream.
Definition: display.hpp:130
void log_warning_for_missing_entity(const std::string &frame_id) const
Log a standard warning for missing entity paths.
Definition: display.hpp:211
std::optional< std::string > resolve_entity_path(const std::string &frame_id, bool include_topic=true) const
Resolve an entity path from the given frame ID.
Definition: display.hpp:201
RosTopicDisplay()
Construct an instance.
Definition: display.hpp:114
~RosTopicDisplay() override
Destruct an instance.
Definition: display.hpp:123
virtual void unsubscribe()
End to subscribing the topic.
Definition: display.hpp:263
void end() override
End to display.
Definition: display.hpp:168
virtual void log_message(typename MsgType::ConstSharedPtr msg)=0
Log subscribed ROS message to recording stream.
bool is_initialized() const override
Return true if the initialization is completed.
Definition: display.hpp:174
void log_timestamp(const rclcpp::Time &stamp)
Set the timeline time for this message.
Definition: display.hpp:187
void start() override
Start to display.
Definition: display.hpp:155
RosTopicProperty property_
Topic property.
Definition: display.hpp:274
Struct representing a property of ROS msg display instance.
Definition: property.hpp:29
const std::string & topic() const noexcept
Get ROS topic name.
Definition: property.hpp:78
const std::string & entity() const noexcept
Return the entity path using topic name.
Definition: property.hpp:84
void set_entity_roots(const std::shared_ptr< std::unordered_map< std::string, std::string > > entity_roots)
Set entity path of record.
Definition: property.hpp:62
void set_type(const std::string &type)
Set ROS message type name.
Definition: property.hpp:50
std::optional< std::string > entity_without_topic(const std::string &frame_id) const noexcept
Return the entity path using the corresponding root and its frame ID without its topic.
Definition: property.hpp:109
bool is_initialized() const
Definition: property.hpp:118
void set_topic(const std::string &topic)
Set ROS topic name.
Definition: property.hpp:56
Definition: display.hpp:32
DisplayState
Lifecycle state of a Display instance.
Definition: display.hpp:36