Skip to content

Commit f9b6398

Browse files
author
Michael Jeronimo
committed
Add a SafeDistance condition node to check distance between poses
1 parent 61ae102 commit f9b6398

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

ros2_behavior_tree/examples/minimal/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ static const char bt_xml[] =
5757
</Recovery>
5858
</Sequence>
5959
<Message msg="Getting robot poses..."/>
60-
<GetRobotPose transform_buffer="{tf_1}" pose="{leader_pose}"/>
61-
<GetRobotPose transform_buffer="{tf_2}" pose="{follower_pose}"/>
60+
<Forever>
61+
<Sequence>
62+
<GetRobotPose transform_buffer="{tf_1}" pose="{leader_pose}"/>
63+
<GetRobotPose transform_buffer="{tf_2}" pose="{follower_pose}"/>
64+
<SafeDistance distance="1.0" pose_1="{leader_pose>" pose_2="{follower_pose}"/>
65+
</Sequence>
66+
</Forever>
6267
</Sequence>
6368
</BehaviorTree>
6469
</root>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_

ros2_behavior_tree/src/node_registrar.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "ros2_behavior_tree/repeat_until_node.hpp"
2828
#include "ros2_behavior_tree/ros2_service_client_node.hpp"
2929
#include "ros2_behavior_tree/round_robin_node.hpp"
30+
#include "ros2_behavior_tree/safe_distance_node.hpp"
3031
#include "ros2_behavior_tree/throttle_tick_rate_node.hpp"
3132

3233
BT_REGISTER_NODES(factory)
@@ -41,13 +42,14 @@ void
4142
NodeRegistrar::RegisterNodes(BT::BehaviorTreeFactory & factory)
4243
{
4344
// Condition nodes
45+
factory.registerNodeType<ros2_behavior_tree::CanTransformNode>("CanTransform");
46+
factory.registerNodeType<ros2_behavior_tree::SafeDistanceNode>("SafeDistance");
4447

4548
// Action nodes
4649
factory.registerNodeType<ros2_behavior_tree::AsyncWaitNode>("AsyncWait");
4750
factory.registerNodeType<ros2_behavior_tree::CreateROS2Node>("CreateROS2Node");
4851
factory.registerNodeType<ros2_behavior_tree::CreateTransformBufferNode>("CreateTransformBuffer");
4952
factory.registerNodeType<ros2_behavior_tree::GetRobotPoseNode>("GetRobotPose");
50-
factory.registerNodeType<ros2_behavior_tree::CanTransformNode>("CanTransform");
5153

5254
const BT::PortsList message_params {BT::InputPort<std::string>("msg")};
5355
factory.registerSimpleAction("Message",

0 commit comments

Comments
 (0)