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