-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpong_received_bt_node.cpp
More file actions
62 lines (52 loc) · 1.96 KB
/
pong_received_bt_node.cpp
File metadata and controls
62 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "ros2_behavior_tree_example/plugins/pong_received_bt_node.hpp"
namespace polymath
{
namespace bt_ros_example
{
PongReceivedNode::PongReceivedNode(const std::string & condition_name, const BT::NodeConfiguration & conf)
: BT::ConditionNode(condition_name, conf),
sub_topic_("incoming_pong"),
pong_id_received_(-1)
{
node_ = conf.blackboard->get<rclcpp_lifecycle::LifecycleNode::SharedPtr>("node");
// Setup subscription on running this node
// Subscription will run when lifecycle node executor is called
sub_ = node_->create_subscription<std_msgs::msg::Int32>(sub_topic_,
rclcpp::SystemDefaultsQoS(),
std::bind(&PongReceivedNode::pong_callback, this, std::placeholders::_1));
bool force_pong;
getInput("force_pong", force_pong);
if(force_pong)
{
// assume we're transmitting the first pong
pong_id_received_=0;
}
return;
}
PongReceivedNode::~PongReceivedNode()
{
RCLCPP_INFO(node_->get_logger(), "SHUTTING DOWN PONG RECEIVED NODE");
sub_.reset();
}
BT::NodeStatus PongReceivedNode::tick()
{
if(pong_id_received_ <= -1)
{
// Return failiure if we haven't received any pongs
return BT::NodeStatus::FAILURE;
}
RCLCPP_INFO(node_->get_logger(), "TICK::PONG_NODE");
// record last pong id received so that other nodes could use it
setOutput("last_pong_id", pong_id_received_);
// reset pong id since we've received it
pong_id_received_ = -1;
return BT::NodeStatus::SUCCESS;
}
void PongReceivedNode::pong_callback(std_msgs::msg::Int32::SharedPtr msg)
{
RCLCPP_INFO(node_->get_logger(), "CALLBACK::RECEIVED PONG WITH ID %d", msg->data);
pong_id_received_ = msg->data;
return;
}
}
}