PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
subscription.hpp
1 /****************************************************************************
2  * Copyright (c) 2023 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <px4_ros2/common/context.hpp>
9 #include <px4_ros2/common/exception.hpp>
10 #include <px4_ros2/components/shared_subscription.hpp>
11 
12 using namespace std::chrono_literals; // NOLINT
13 
14 namespace px4_ros2
15 {
26 template<typename RosMessageType>
28 {
29  using UpdateCallback = std::function<void (const RosMessageType &)>;
30 
31 public:
32  Subscription(Context & context, const std::string & topic)
33  : _node(context.node())
34  {
35  const std::string namespaced_topic = context.topicNamespacePrefix() + topic;
36  _callback_instance = SharedSubscription<RosMessageType>::create(
37  _node, namespaced_topic,
38  [this](const typename RosMessageType::UniquePtr & msg) {
39  _last = *msg;
40  _last_message_time = _node.get_clock()->now();
41  for (const auto & callback : _callbacks) {
42  callback(_last);
43  }
44  });
45  }
46 
52  void onUpdate(const UpdateCallback & callback)
53  {
54  _callbacks.push_back(callback);
55  }
56 
63  const RosMessageType & last() const
64  {
65  if (!hasReceivedMessages()) {
66  throw Exception("No messages received.");
67  }
68  return _last;
69  }
70 
76  const rclcpp::Time & lastTime() const
77  {
78  return _last_message_time;
79  }
80 
88  template<typename DurationT = std::milli>
89  bool lastValid(const std::chrono::duration<int64_t, DurationT> max_delay = 500ms) const
90  {
91  return hasReceivedMessages() && _node.get_clock()->now() - _last_message_time < max_delay;
92  }
93 
94 protected:
95  rclcpp::Node & _node;
96 
97 private:
98  SharedSubscriptionCallbackInstance _callback_instance;
99 
100  RosMessageType _last;
101  rclcpp::Time _last_message_time;
102 
103  std::vector<std::function<void(const RosMessageType &)>> _callbacks{};
104 
105  bool hasReceivedMessages() const
106  {
107  return _last_message_time.seconds() != 0;
108  }
109 };
110 
112 } // namespace px4_ros2
Shared class for a ROS subscription.
Definition: shared_subscription.hpp:40
Definition: context.hpp:20
Definition: exception.hpp:16
Provides a subscription to arbitrary ROS topics.
Definition: subscription.hpp:28
bool lastValid(const std::chrono::duration< int64_t, DurationT > max_delay=500ms) const
Check whether the last message is still valid. To be valid, the message must have been received withi...
Definition: subscription.hpp:89
void onUpdate(const UpdateCallback &callback)
Add a callback to execute when receiving a new message.
Definition: subscription.hpp:52
const RosMessageType & last() const
Get the last-received message.
Definition: subscription.hpp:63
const rclcpp::Time & lastTime() const
Get the receive-time of the last message.
Definition: subscription.hpp:76