Skip to content

Commit 8a402e9

Browse files
author
davide
committed
onHalt added and bug fixed
1 parent 7259649 commit 8a402e9

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

behaviortree_ros2/include/behaviortree_ros2/bt_action_node.hpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ enum ActionNodeErrorCode
3838
INVALID_GOAL
3939
};
4040

41+
inline const char* toStr(const ActionNodeErrorCode& err)
42+
{
43+
switch (err)
44+
{
45+
case SERVER_UNREACHABLE: return "SERVER_UNREACHABLE";
46+
case SEND_GOAL_TIMEOUT: return "SEND_GOAL_TIMEOUT";
47+
case GOAL_REJECTED_BY_SERVER: return "GOAL_REJECTED_BY_SERVER";
48+
case ACTION_ABORTED: return "ACTION_ABORTED";
49+
case ACTION_CANCELLED: return "ACTION_CANCELLED";
50+
case INVALID_GOAL: return "INVALID_GOAL";
51+
}
52+
return nullptr;
53+
}
54+
4155
/**
4256
* @brief Abstract class to wrap rclcpp_action::Client<>
4357
*
@@ -106,10 +120,9 @@ class RosActionNode : public BT::ActionNodeBase
106120
return providedBasicPorts({});
107121
}
108122

109-
NodeStatus tick() override final;
110-
111-
/// The default halt() implementation will call cancelGoal if necessary.
112-
void halt() override;
123+
/// @brief Callback executed when the node is halted. Note that cancelGoal()
124+
/// is done automatically.
125+
virtual void onHalt() {}
113126

114127
/** setGoal s a callback that allows the user to set
115128
* the goal message (ActionT::Goal).
@@ -146,6 +159,12 @@ class RosActionNode : public BT::ActionNodeBase
146159
/// Method used to send a request to the Action server to cancel the current goal
147160
void cancelGoal();
148161

162+
163+
/// The default halt() implementation will call cancelGoal if necessary.
164+
void halt() override final;
165+
166+
NodeStatus tick() override final;
167+
149168
protected:
150169

151170
std::shared_ptr<rclcpp::Node> node_;
@@ -311,9 +330,11 @@ template<class T> inline
311330
goal_options.result_callback =
312331
[this](const WrappedResult& result)
313332
{
314-
RCLCPP_DEBUG( node_->get_logger(), "result_callback" );
315-
result_ = result;
316-
emitWakeUpSignal();
333+
if (goal_handle_->get_goal_id() == result.goal_id) {
334+
RCLCPP_DEBUG( node_->get_logger(), "result_callback" );
335+
result_ = result;
336+
emitWakeUpSignal();
337+
}
317338
};
318339
//--------------------
319340
goal_options.goal_response_callback =
@@ -324,7 +345,7 @@ template<class T> inline
324345
{
325346
RCLCPP_ERROR(node_->get_logger(), "Goal was rejected by server");
326347
} else {
327-
RCLCPP_INFO(node_->get_logger(), "Goal accepted by server, waiting for result");
348+
RCLCPP_DEBUG(node_->get_logger(), "Goal accepted by server, waiting for result");
328349
}
329350
};
330351
//--------------------
@@ -396,9 +417,10 @@ template<class T> inline
396417
template<class T> inline
397418
void RosActionNode<T>::halt()
398419
{
399-
if( status() == NodeStatus::RUNNING )
420+
if(status() == BT::NodeStatus::RUNNING)
400421
{
401422
cancelGoal();
423+
onHalt();
402424
}
403425
}
404426

behaviortree_ros2/include/behaviortree_ros2/bt_service_node.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ enum ServiceNodeErrorCode
3434
SERVICE_ABORTED
3535
};
3636

37+
inline const char* toStr(const ServiceNodeErrorCode& err)
38+
{
39+
switch (err)
40+
{
41+
case SERVICE_UNREACHABLE: return "SERVICE_UNREACHABLE";
42+
case SERVICE_TIMEOUT: return "SERVICE_TIMEOUT";
43+
case INVALID_REQUEST: return "INVALID_REQUEST";
44+
case SERVICE_ABORTED: return "SERVICE_ABORTED";
45+
}
46+
return nullptr;
47+
}
48+
3749
/**
3850
* @brief Abstract class use to wrap rclcpp::Client<>
3951
*

btcpp_ros2_samples/src/sleep_action.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ bool SleepAction::setGoal(RosActionNode::Goal &goal)
1010

1111
NodeStatus SleepAction::onResultReceived(const RosActionNode::WrappedResult &wr)
1212
{
13-
RCLCPP_INFO( node_->get_logger(), "%s: onResultReceived %d", name().c_str(), wr.result->done );
13+
RCLCPP_INFO( node_->get_logger(), "%s: onResultReceived. Done = %s", name().c_str(),
14+
wr.result->done ? "true" : "false" );
15+
1416
return wr.result->done ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
1517
}
1618

1719
NodeStatus SleepAction::onFailure(ActionNodeErrorCode error)
1820
{
19-
RCLCPP_ERROR( node_->get_logger(), "%s: onFailure %d", name().c_str(), error );
21+
RCLCPP_ERROR( node_->get_logger(), "%s: onFailure with error: %s", name().c_str(), toStr(error) );
2022
return NodeStatus::FAILURE;
2123
}
2224

25+
void SleepAction::onHalt()
26+
{
27+
RCLCPP_INFO( node_->get_logger(), "%s: onHalt", name().c_str() );
28+
}
29+
2330
// Plugin registration.
2431
// The class SleepAction will self register with name "Sleep".
2532
CreateRosNodePlugin(SleepAction, "Sleep");

btcpp_ros2_samples/src/sleep_action.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class SleepAction: public RosActionNode<btcpp_ros2_interfaces::action::Sleep>
1919

2020
bool setGoal(Goal& goal) override;
2121

22+
void onHalt() override;
23+
2224
BT::NodeStatus onResultReceived(const WrappedResult& wr) override;
2325

2426
virtual BT::NodeStatus onFailure(ActionNodeErrorCode error) override;

btcpp_ros2_samples/src/sleep_client.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ int main(int argc, char **argv)
7979

8080
auto tree = factory.createTreeFromText(xml_text);
8181

82-
tree.tickWhileRunning();
82+
for(int i=0; i<5; i++){
83+
tree.tickWhileRunning();
84+
}
8385

8486
return 0;
8587
}

0 commit comments

Comments
 (0)