ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to write a BT node that subscribe a topic (navigation2).

asked 2021-02-16 23:10:58 -0600

Kazunari Tanaka gravatar image

I want to write a BT node that recieve state of a bumper.

But my code can not catch messages of a bumper.

How can I write a BT Node that works properly with subscription?

Thank you!

#include <iostream>
#include <memory>
#include <string>

#include <rclcpp/qos.hpp>
#include <rclcpp/rclcpp.hpp>

#include "behaviortree_cpp_v3/condition_node.h"
#include "gazebo_msgs/msg/contacts_state.hpp"
#include "geometry_msgs/msg/pose_stamped.hpp"

namespace nav2_behavior_tree {

class BumperCollisionCondition : public BT::ConditionNode {
  public:
    BumperCollisionCondition(const std::string &condition_name, const BT::NodeConfiguration &conf);

    BumperCollisionCondition() = delete;

    BT::NodeStatus tick() override;

    static BT::PortsList providedPorts() {
        return {};
    }

  private:
    rclcpp::Subscription<gazebo_msgs::msg::ContactsState>::SharedPtr contact_state_;
    rclcpp::Node::SharedPtr node_;
    rclcpp::Time lasttime_;
};

} // namespace nav2_behavior_tree

namespace nav2_behavior_tree {

BumperCollisionCondition::BumperCollisionCondition(
    const std::string &condition_name,
    const BT::NodeConfiguration &conf)
    : BT::ConditionNode(condition_name, conf) {
  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
  auto qos = rclcpp::SystemDefaultsQoS();
  qos.best_effort();
  contact_state_ = node_->create_subscription<gazebo_msgs::msg::ContactsState>(
    "bumper_front",
    qos,
    [this](const gazebo_msgs::msg::ContactsState::SharedPtr msg) -> void {
      RCLCPP_INFO(node_->get_logger(), "TEST"); // "TEST" never print.
      auto states = msg->states;
      lasttime_ = rclcpp::Time(msg->header.stamp);
    });
}

BT::NodeStatus BumperCollisionCondition::tick() {
    RCLCPP_INFO(node_->get_logger(), "test %d", lasttime_.nanoseconds()); // lasttime_ never update.
    return BT::NodeStatus::FAILURE;
}

} // namespace nav2_behavior_tree

#include "behaviortree_cpp_v3/bt_factory.h"
BT_REGISTER_NODES(factory) {
    factory.registerNodeType<nav2_behavior_tree::BumperCollisionCondition>("BumperCollision");
}
edit retag flag offensive close merge delete

Comments

while you provided the code for the node, can you provide the overall tree you are trying to use this BT node in? I wonder if this condition node is ever being checked.

I want to ask to make sure that the condition has an appropriate parent, for example ReactiveFallbackFallback Nodes

vinny gravatar image vinny  ( 2021-02-17 10:00:47 -0600 )edit

Thank you your response! My bt is here.

<root main_tree_to_execute="MainTree">
    <BehaviorTree ID="MainTree">
      <RetryUntilSuccesful num_attempts="-1">
        <BumperCollision/>
      </RetryUntilSuccesful>
    </BehaviorTree>
</root>

I observed that tick method is called in every frame.

Kazunari Tanaka gravatar image Kazunari Tanaka  ( 2021-02-17 21:21:29 -0600 )edit
1

I changed the bt tree to following. Then the callback function was called. I seem that the point is lifetime of BT node. correct?

<root main_tree_to_execute="MainTree">
    <BehaviorTree ID="MainTree">
      <ReactiveFallback name="BumperFallback">
        <BumperCollision/>
        <Wait wait_duration="20000"/>
      </ReactiveFallback>
    </BehaviorTree>
</root>
Kazunari Tanaka gravatar image Kazunari Tanaka  ( 2021-02-17 22:10:33 -0600 )edit

Yeah, I think simply ticking the tree like you did in the RetryUntilSuccessful does not allow the node to enter to the callback where as ReactiveFallback will allow it to.

vinny gravatar image vinny  ( 2021-02-18 12:50:42 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-02-18 12:51:37 -0600

vinny gravatar image

Using a ReactiveFallback parent node rather than RetryUntilSuccessful allowed the custom condition node to work.

<root main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
  <ReactiveFallback name="BumperFallback">
    <BumperCollision/>
    <Wait wait_duration="20000"/>
  </ReactiveFallback>
</BehaviorTree>

</root>

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-02-16 23:02:18 -0600

Seen: 419 times

Last updated: Feb 18 '21