|
| 1 | +/* |
| 2 | +* Copyright (c) 2023, Open Source Robotics Foundation, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* Redistribution and use in source and binary forms, with or without |
| 6 | +* modification, are permitted (subject to the limitations in the disclaimer |
| 7 | +* below) provided that the following conditions are met: |
| 8 | +* |
| 9 | +* * Redistributions of source code must retain the above copyright |
| 10 | +* notice, this list of conditions and the following disclaimer. |
| 11 | +* * Redistributions in binary form must reproduce the above copyright |
| 12 | +* notice, this list of conditions and the following disclaimer in the |
| 13 | +* documentation and/or other materials provided with the distribution. |
| 14 | +* * Neither the name of the copyright holder nor the names of its |
| 15 | +* contributors may be used to endorse or promote products derived from |
| 16 | +* this software without specific prior written permission. |
| 17 | +* |
| 18 | +* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS |
| 19 | +* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +* POSSIBILITY OF SUCH DAMAGE. |
| 30 | +*/ |
| 31 | + |
| 32 | +#ifndef RVIZ_DEFAULT_PLUGINS__DISPLAYS__POINTCLOUD__POINT_CLOUD_TRANSPORT_DISPLAY_HPP_ |
| 33 | +#define RVIZ_DEFAULT_PLUGINS__DISPLAYS__POINTCLOUD__POINT_CLOUD_TRANSPORT_DISPLAY_HPP_ |
| 34 | + |
| 35 | +#include <memory> |
| 36 | + |
| 37 | +#include "get_transport_from_topic.hpp" |
| 38 | +#include "point_cloud_transport/point_cloud_transport.hpp" |
| 39 | +#include "point_cloud_transport/subscriber_filter.hpp" |
| 40 | +#include "rviz_common/ros_topic_display.hpp" |
| 41 | + |
| 42 | +namespace rviz_default_plugins |
| 43 | +{ |
| 44 | +namespace displays |
| 45 | +{ |
| 46 | + |
| 47 | +template<class MessageType> |
| 48 | +class PointCloud2TransportDisplay : public rviz_common::_RosTopicDisplay |
| 49 | +{ |
| 50 | +// No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class. |
| 51 | + |
| 52 | +public: |
| 53 | +/// Convenience typedef so subclasses don't have to use |
| 54 | +/// the long templated class name to refer to their super class. |
| 55 | + typedef PointCloud2TransportDisplay<MessageType> PC2RDClass; |
| 56 | + |
| 57 | + PointCloud2TransportDisplay() |
| 58 | + : messages_received_(0) |
| 59 | + { |
| 60 | + QString message_type = QString::fromStdString(rosidl_generator_traits::name<MessageType>()); |
| 61 | + topic_property_->setMessageType(message_type); |
| 62 | + topic_property_->setDescription(message_type + " topic to subscribe to."); |
| 63 | + } |
| 64 | + |
| 65 | +/** |
| 66 | +* When overriding this method, the onInitialize() method of this superclass has to be called. |
| 67 | +* Otherwise, the ros node will not be initialized. |
| 68 | +*/ |
| 69 | + void onInitialize() override |
| 70 | + { |
| 71 | + _RosTopicDisplay::onInitialize(); |
| 72 | + } |
| 73 | + |
| 74 | + ~PointCloud2TransportDisplay() override |
| 75 | + { |
| 76 | + unsubscribe(); |
| 77 | + } |
| 78 | + |
| 79 | + void reset() override |
| 80 | + { |
| 81 | + Display::reset(); |
| 82 | + messages_received_ = 0; |
| 83 | + } |
| 84 | + |
| 85 | + void setTopic(const QString & topic, const QString & datatype) override |
| 86 | + { |
| 87 | + (void) datatype; |
| 88 | + topic_property_->setString(topic); |
| 89 | + } |
| 90 | + |
| 91 | +protected: |
| 92 | + void updateTopic() override |
| 93 | + { |
| 94 | + resetSubscription(); |
| 95 | + } |
| 96 | + |
| 97 | + virtual void subscribe() |
| 98 | + { |
| 99 | + if (!isEnabled()) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + if (topic_property_->isEmpty()) { |
| 104 | + setStatus( |
| 105 | + rviz_common::properties::StatusProperty::Error, "Topic", |
| 106 | + QString("Error subscribing: Empty topic name")); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + subscription_ = std::make_shared<point_cloud_transport::SubscriberFilter>(); |
| 112 | + subscription_->subscribe( |
| 113 | + rviz_ros_node_.lock()->get_raw_node(), |
| 114 | + getPointCloud2BaseTopicFromTopic(topic_property_->getTopicStd()), |
| 115 | + getPointCloud2TransportFromTopic(topic_property_->getTopicStd()), |
| 116 | + qos_profile.get_rmw_qos_profile()); |
| 117 | + subscription_start_time_ = rviz_ros_node_.lock()->get_raw_node()->now(); |
| 118 | + subscription_callback_ = subscription_->registerCallback( |
| 119 | + std::bind( |
| 120 | + &PointCloud2TransportDisplay<MessageType>::incomingMessage, this, std::placeholders::_1)); |
| 121 | + setStatus(rviz_common::properties::StatusProperty::Ok, "Topic", "OK"); |
| 122 | + } catch (rclcpp::exceptions::InvalidTopicNameError & e) { |
| 123 | + setStatus( |
| 124 | + rviz_common::properties::StatusProperty::Error, "Topic", |
| 125 | + QString("Error subscribing: ") + e.what()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + void transformerChangedCallback() override |
| 130 | + { |
| 131 | + resetSubscription(); |
| 132 | + } |
| 133 | + |
| 134 | + void resetSubscription() |
| 135 | + { |
| 136 | + unsubscribe(); |
| 137 | + reset(); |
| 138 | + subscribe(); |
| 139 | + context_->queueRender(); |
| 140 | + } |
| 141 | + |
| 142 | + virtual void unsubscribe() |
| 143 | + { |
| 144 | + subscription_.reset(); |
| 145 | + } |
| 146 | + |
| 147 | + void onEnable() override |
| 148 | + { |
| 149 | + subscribe(); |
| 150 | + } |
| 151 | + |
| 152 | + void onDisable() override |
| 153 | + { |
| 154 | + unsubscribe(); |
| 155 | + reset(); |
| 156 | + } |
| 157 | + |
| 158 | +/// Incoming message callback. |
| 159 | +/** |
| 160 | +* Checks if the message pointer |
| 161 | +* is valid, increments messages_received_, then calls |
| 162 | +* processMessage(). |
| 163 | +*/ |
| 164 | + void incomingMessage(const typename MessageType::ConstSharedPtr msg) |
| 165 | + { |
| 166 | + if (!msg) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + ++messages_received_; |
| 171 | + QString topic_str = QString::number(messages_received_) + " messages received"; |
| 172 | + // Append topic subscription frequency if we can lock rviz_ros_node_. |
| 173 | + std::shared_ptr<rviz_common::ros_integration::RosNodeAbstractionIface> node_interface = |
| 174 | + rviz_ros_node_.lock(); |
| 175 | + if (node_interface != nullptr) { |
| 176 | + const double duration = |
| 177 | + (node_interface->get_raw_node()->now() - subscription_start_time_).seconds(); |
| 178 | + const double subscription_frequency = |
| 179 | + static_cast<double>(messages_received_) / duration; |
| 180 | + topic_str += " at " + QString::number(subscription_frequency, 'f', 1) + " hz."; |
| 181 | + } |
| 182 | + setStatus( |
| 183 | + rviz_common::properties::StatusProperty::Ok, |
| 184 | + "Topic", |
| 185 | + topic_str); |
| 186 | + |
| 187 | + processMessage(msg); |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +/// Implement this to process the contents of a message. |
| 192 | +/** |
| 193 | +* This is called by incomingMessage(). |
| 194 | +*/ |
| 195 | + virtual void processMessage(typename MessageType::ConstSharedPtr msg) = 0; |
| 196 | + |
| 197 | + uint32_t messages_received_; |
| 198 | + rclcpp::Time subscription_start_time_; |
| 199 | + |
| 200 | + std::shared_ptr<point_cloud_transport::SubscriberFilter> subscription_; |
| 201 | + message_filters::Connection subscription_callback_; |
| 202 | +}; |
| 203 | + |
| 204 | +} // end namespace displays |
| 205 | +} // end namespace rviz_default_plugins |
| 206 | + |
| 207 | + |
| 208 | +#endif // RVIZ_DEFAULT_PLUGINS__DISPLAYS__POINTCLOUD__POINT_CLOUD_TRANSPORT_DISPLAY_HPP_ |
0 commit comments