|
| 1 | +// Copyright (c) 2018 Intel Corporation |
| 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 | +#ifndef ROS2_BEHAVIOR_TREE__SAFE_DISTANCE_NODE_HPP_ |
| 16 | +#define ROS2_BEHAVIOR_TREE__SAFE_DISTANCE_NODE_HPP_ |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <memory> |
| 20 | + |
| 21 | +#include "behaviortree_cpp_v3/condition_node.h" |
| 22 | +#include "geometry_msgs/msg/pose_stamped.hpp" |
| 23 | +#include "rclcpp/rclcpp.hpp" |
| 24 | +#include "ros2_behavior_tree/bt_conversions.hpp" |
| 25 | +#include "tf2_geometry_msgs/tf2_geometry_msgs.h" |
| 26 | + |
| 27 | +namespace ros2_behavior_tree |
| 28 | +{ |
| 29 | + |
| 30 | +class SafeDistanceNode : public BT::ConditionNode |
| 31 | +{ |
| 32 | +public: |
| 33 | + SafeDistanceNode(const std::string & name, const BT::NodeConfiguration & config) |
| 34 | + : BT::ConditionNode(name, config) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + static BT::PortsList providedPorts() |
| 39 | + { |
| 40 | + return { |
| 41 | + BT::InputPort<double>("distance", "The distance threshold"), |
| 42 | + BT::InputPort<geometry_msgs::msg::PoseStamped>("pose_1", "The first pose"), |
| 43 | + BT::InputPort<geometry_msgs::msg::PoseStamped>("pose_2", "The second pose") |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + BT::NodeStatus tick() override |
| 48 | + { |
| 49 | + double distance; |
| 50 | + if (!getInput<double>("distance", distance)) { |
| 51 | + throw BT::RuntimeError("Missing parameter [distance] in SafeDistance node"); |
| 52 | + } |
| 53 | + |
| 54 | + geometry_msgs::msg::PoseStamped pose1 = geometry_msgs::msg::PoseStamped(); |
| 55 | + if (!getInput<geometry_msgs::msg::PoseStamped>("pose_1", pose1)) { |
| 56 | + throw BT::RuntimeError("Missing parameter [pose_1] in SafeDistance node"); |
| 57 | + } |
| 58 | + |
| 59 | + geometry_msgs::msg::PoseStamped pose2 = geometry_msgs::msg::PoseStamped(); |
| 60 | + if (!getInput<geometry_msgs::msg::PoseStamped>("pose_2", pose2)) { |
| 61 | + throw BT::RuntimeError("Missing parameter [pose_2] in SafeDistance node"); |
| 62 | + } |
| 63 | + |
| 64 | + double dx = pose1.pose.position.x - pose2.pose.position.x; |
| 65 | + double dy = pose1.pose.position.y - pose2.pose.position.y; |
| 66 | + |
| 67 | + return (dx * dx + dy * dy > distance * distance) ? BT::NodeStatus::SUCCESS : BT::NodeStatus::FAILURE; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +} // namespace ros2_behavior_tree |
| 72 | + |
| 73 | +#endif // ROS2_BEHAVIOR_TREE__SAFE_DISTANCE_NODE_HPP_ |
0 commit comments