AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
send_vehicle_command.hpp
1// Copyright 2024 Robin Müller
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#pragma once
16
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "auto_apms_behavior_tree_core/node.hpp"
25#include "auto_apms_behavior_tree_core/node/base/ros_action_node_base.hpp"
26#include "px4_msgs/msg/vehicle_command.hpp"
27#include "px4_msgs/msg/vehicle_command_ack.hpp"
28#include "rclcpp/rclcpp.hpp"
29
30namespace auto_apms_px4
31{
32
61class SendVehicleCommand : public auto_apms_behavior_tree::core::RosActionNodeBase
62{
63public:
64 static constexpr auto PORT_KEY_COMMAND = "command";
65 static constexpr auto PORT_KEY_PARAM1 = "param1";
66 static constexpr auto PORT_KEY_PARAM2 = "param2";
67 static constexpr auto PORT_KEY_PARAM3 = "param3";
68 static constexpr auto PORT_KEY_PARAM4 = "param4";
69 static constexpr auto PORT_KEY_PARAM5 = "param5";
70 static constexpr auto PORT_KEY_PARAM6 = "param6";
71 static constexpr auto PORT_KEY_PARAM7 = "param7";
72 static constexpr auto PORT_KEY_CONFIRMATION = "confirmation";
73 static constexpr auto PORT_KEY_TARGET_SYSTEM = "target_system";
74 static constexpr auto PORT_KEY_TARGET_COMPONENT = "target_component";
75
76 SendVehicleCommand(const std::string & instance_name, const Config & config, const Context & context);
77
78 ~SendVehicleCommand() override;
79
80 static BT::PortsList providedPorts();
81
82 BT::NodeStatus tick() override;
83
84 void halt() override;
85
92 virtual bool setMessage(px4_msgs::msg::VehicleCommand & msg);
93
94protected:
100 uint16_t resolveSourceComponent();
101
102private:
111 struct AckSubscription
112 {
113 using Ack = px4_msgs::msg::VehicleCommandAck;
114
115 AckSubscription(
116 rclcpp::Node::SharedPtr node, rclcpp::CallbackGroup::SharedPtr group, const std::string & topic_name);
117
118 void addListener(const void * owner, std::function<void(const Ack::SharedPtr &)> callback);
119 void removeListener(const void * owner);
120 void broadcast(const Ack::SharedPtr & msg);
121
122 rclcpp::Subscription<Ack>::SharedPtr subscription;
123 std::vector<std::pair<const void *, std::function<void(const Ack::SharedPtr &)>>> listeners;
124 std::string name;
125 };
126
134 std::string resolveCommandTopicName();
135
137 rclcpp::Publisher<px4_msgs::msg::VehicleCommand>::SharedPtr command_pub_;
139 std::shared_ptr<AckSubscription> ack_sub_;
140
142 px4_msgs::msg::VehicleCommand tx_msg_;
144 std::shared_ptr<px4_msgs::msg::VehicleCommandAck> last_ack_;
145 bool waiting_for_ack_ = false;
146 rclcpp::Time send_time_;
147 double ack_timeout_s_;
148};
149
150} // namespace auto_apms_px4
ROS-aware wrapper for the basic BT::ActionNodeBase (generic asynchronous action).
uint16_t resolveSourceComponent()
Transitively resolve the in-charge mode executor's source component from the global blackboard.
virtual bool setMessage(px4_msgs::msg::VehicleCommand &msg)
Populate the VehicleCommand message to publish.
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.