AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
get_robot_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// https://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 "auto_apms_behavior_tree_core/node.hpp"
16#include "pyrobosim_msgs/msg/robot_state.hpp"
17
18#define INPUT_KEY_ROBOT "robot"
19#define OUTPUT_KEY_LOCATION "location"
20#define OUTPUT_KEY_BATTERY "battery"
21
22namespace auto_apms_simulation
23{
24
25class GetRobotState : public auto_apms_behavior_tree::core::RosSubscriberNode<pyrobosim_msgs::msg::RobotState>
26{
27 bool first_message_received_ = false;
28 MessageType last_msg_;
29
30public:
31 GetRobotState(const std::string & instance_name, const Config & config, Context context)
32 : RosSubscriberNode(instance_name, config, context, rclcpp::SensorDataQoS())
33 {
34 }
35
36 static BT::PortsList providedPorts()
37 {
38 return {
39 BT::OutputPort<double>(OUTPUT_KEY_BATTERY, "{=}", "Current battery state [%]."),
40 BT::OutputPort<std::string>(OUTPUT_KEY_LOCATION, "{=}", "Current location name."),
41 BT::InputPort<std::string>(INPUT_KEY_ROBOT, "Name of the robot.")};
42 }
43
44 BT::NodeStatus onTick(const std::shared_ptr<MessageType> & last_msg_ptr) final
45 {
46 // Check if a new message was received
47 if (last_msg_ptr) {
48 first_message_received_ = true;
49 last_msg_ = *last_msg_ptr;
50 }
51
52 if (!first_message_received_) return BT::NodeStatus::FAILURE;
53
54 setOutput(OUTPUT_KEY_LOCATION, last_msg_.last_visited_location);
55 setOutput(OUTPUT_KEY_BATTERY, last_msg_.battery_level);
56 return BT::NodeStatus::SUCCESS;
57 }
58};
59
60} // namespace auto_apms_simulation
61
62AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_simulation::GetRobotState)
RosSubscriberNode(const std::string &instance_name, const Config &config, Context context, const rclcpp::QoS &qos={10})
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:40