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 <string>
27#include <unordered_map>
28#include <utility>
29
30namespace awviz_common
31{
36{
37public:
38 Display() = default;
39
40 virtual ~Display() = default;
41
47 virtual void initialize(
48 rclcpp::Node::SharedPtr node, std::shared_ptr<rerun::RecordingStream> stream) = 0;
49
55 virtual void set_property(
56 const std::string & topic,
57 const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) = 0;
58
62 virtual void start() = 0;
63
67 virtual void end() = 0;
68
73 virtual bool is_initialized() const { return is_initialized_; }
74
75protected:
76 rclcpp::Node::SharedPtr node_;
77 std::shared_ptr<rerun::RecordingStream> stream_;
79};
80
84template <typename MsgType>
86{
87public:
92 {
93 const auto msg_type = rosidl_generator_traits::name<MsgType>();
94 property_.set_type(msg_type);
95 }
96
101
108 rclcpp::Node::SharedPtr node, std::shared_ptr<rerun::RecordingStream> stream) override
109 {
110 node_ = std::move(node);
111 stream_ = std::move(stream);
112 is_initialized_ = true;
113 };
114
121 const std::string & topic,
122 const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) override
123 {
124 property_.set_topic(topic);
125 property_.set_entity_roots(entity_roots);
126 }
127
131 void start() override { subscribe(); }
132
136 void end() override { unsubscribe(); }
137
138 bool is_initialized() const override { return is_initialized_ && property_.is_initialized(); }
139
140protected:
141 static constexpr const char * TIMELINE_NAME = "timestamp";
142
147 virtual void subscribe()
148 {
149 if (!is_initialized()) {
150 return;
151 }
152
153 // TODO(ktro2828): QoS setting
154 subscription_ = node_->create_subscription<MsgType>(
155 property_.topic(), rclcpp::SensorDataQoS{},
156 [this](const typename MsgType::ConstSharedPtr msg) { log_message(msg); });
157 };
158
162 virtual void unsubscribe() { subscription_.reset(); }
163
169 virtual void log_message(typename MsgType::ConstSharedPtr msg) = 0;
170
171protected:
172 typename rclcpp::Subscription<MsgType>::SharedPtr subscription_;
174};
175} // namespace awviz_common
176#endif // AWVIZ_COMMON__DISPLAY_HPP_
Intermediate class for display items.
Definition: display.hpp:36
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:73
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:76
bool is_initialized_
Whether the object has been initialized.
Definition: display.hpp:78
virtual ~Display()=default
std::shared_ptr< rerun::RecordingStream > stream_
RecordingStream shared pointer.
Definition: display.hpp:77
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.
Inherited plugin class to display ROS topics.
Definition: display.hpp:86
static constexpr const char * TIMELINE_NAME
Entity name of timeline record.
Definition: display.hpp:141
virtual void subscribe()
Start to subscribing the specified topic.
Definition: display.hpp:147
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:120
rclcpp::Subscription< MsgType >::SharedPtr subscription_
Subscription of the topic.
Definition: display.hpp:172
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:107
RosTopicDisplay()
Construct an instance.
Definition: display.hpp:91
~RosTopicDisplay() override
Destruct an instance.
Definition: display.hpp:100
virtual void unsubscribe()
End to subscribing the topic.
Definition: display.hpp:162
void end() override
End to display.
Definition: display.hpp:136
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:138
void start() override
Start to display.
Definition: display.hpp:131
RosTopicProperty property_
Topic property.
Definition: display.hpp:173
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
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
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:31