AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
send_cmd_set_nav_state.cpp
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#include <cstdint>
16
17#include "auto_apms_px4/node/send_vehicle_command.hpp"
18
19#define INPUT_KEY_NAV_STATE "nav_state"
20
21namespace auto_apms_px4
22{
23
39class SendCmdSetNavState : public SendVehicleCommand
40{
41public:
42 using SendVehicleCommand::SendVehicleCommand;
43
44 static BT::PortsList providedPorts()
45 {
46 return {
47 BT::InputPort<int>(INPUT_KEY_NAV_STATE, "Target PX4 navigation state (mode id) to switch to."),
48 };
49 }
50
51 bool setMessage(px4_msgs::msg::VehicleCommand & msg) override final
52 {
53 const BT::Expected<int> expected_nav_state = getInput<int>(INPUT_KEY_NAV_STATE);
54 if (!expected_nav_state) {
55 RCLCPP_ERROR(
56 logger_, "%s - Missing required input '%s': %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
57 INPUT_KEY_NAV_STATE, expected_nav_state.error().c_str());
58 return false;
59 }
60
61 msg = px4_msgs::msg::VehicleCommand{};
62 msg.command = px4_msgs::msg::VehicleCommand::VEHICLE_CMD_SET_NAV_STATE;
63 msg.param1 = static_cast<float>(expected_nav_state.value());
64 msg.source_component = resolveSourceComponent();
65 msg.timestamp = 0; // Let PX4 set the timestamp.
66
67 RCLCPP_DEBUG(
68 logger_, "%s - Switching to nav_state %d (source_component %u).",
69 context_.getFullyQualifiedTreeNodeName(this).c_str(), expected_nav_state.value(), msg.source_component);
70 return true;
71 }
72};
73
74} // namespace auto_apms_px4
75
Behavior tree node that switches the active PX4 flight mode while respecting mode-executor ownership.
uint16_t resolveSourceComponent()
Transitively resolve the in-charge mode executor's source component from the global blackboard.
#define AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:50
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.