Skip to content

Commit 9f8cce3

Browse files
committed
feedback_callback support added
1 parent 384f077 commit 9f8cce3

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

simple_node/include/simple_node/actions/action_client.hpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <chrono>
66
#include <future>
7+
#include <memory>
78
#include <string>
89
#include <thread>
910

@@ -20,11 +21,15 @@ template <typename ActionT>
2021
class ActionClient : public rclcpp_action::Client<ActionT> {
2122

2223
using Goal = typename ActionT::Goal;
24+
using Feedback = typename ActionT::Feedback;
2325
using GoalHandle = rclcpp_action::ClientGoalHandle<ActionT>;
2426
using Result = typename GoalHandle::Result::SharedPtr;
27+
using FeedbackCallback = std::function<void(
28+
typename GoalHandle::SharedPtr, const std::shared_ptr<const Feedback>)>;
2529

2630
public:
27-
ActionClient(rclcpp::Node *node, std::string action_name)
31+
ActionClient(rclcpp::Node *node, std::string action_name,
32+
FeedbackCallback feedback_cb = nullptr)
2833
: rclcpp_action::Client<ActionT>(
2934
node->get_node_base_interface(), node->get_node_graph_interface(),
3035
node->get_node_logging_interface(), action_name) {
@@ -34,6 +39,7 @@ class ActionClient : public rclcpp_action::Client<ActionT> {
3439
this->goal_handle = nullptr;
3540
this->goal_thread = nullptr;
3641
this->result = nullptr;
42+
this->feedback_cb = feedback_cb;
3743
}
3844

3945
~ActionClient() { delete this->goal_thread; }
@@ -42,9 +48,17 @@ class ActionClient : public rclcpp_action::Client<ActionT> {
4248

4349
Result get_result() { return this->result; }
4450

45-
void send_goal(Goal goal) {
51+
void send_goal(Goal goal, FeedbackCallback feedback_cb = nullptr) {
52+
53+
FeedbackCallback _feedback_cb = this->feedback_cb;
54+
55+
if (feedback_cb != nullptr) {
56+
_feedback_cb = feedback_cb;
57+
}
58+
4659
this->set_status(action_msgs::msg::GoalStatus::STATUS_UNKNOWN);
47-
this->goal_thread = new std::thread(&ActionClient::_send_goal, this, goal);
60+
this->goal_thread =
61+
new std::thread(&ActionClient::_send_goal, this, goal, _feedback_cb);
4862
}
4963

5064
bool cancel_goal() {
@@ -102,17 +116,22 @@ class ActionClient : public rclcpp_action::Client<ActionT> {
102116
std::thread *goal_thread;
103117
Result result;
104118
std::shared_ptr<GoalHandle> goal_handle;
119+
FeedbackCallback feedback_cb;
105120

106121
void set_status(int8_t status) {
107122
const std::lock_guard<std::mutex> lock(this->status_mtx);
108123
this->status = status;
109124
}
110125

111-
void _send_goal(Goal goal) {
126+
void _send_goal(Goal goal, FeedbackCallback feedback_cb) {
112127

113128
this->result = nullptr;
114129

115-
auto send_goal_future = this->async_send_goal(goal);
130+
auto send_goal_options =
131+
typename rclcpp_action::Client<ActionT>::SendGoalOptions();
132+
send_goal_options.feedback_callback = feedback_cb;
133+
134+
auto send_goal_future = this->async_send_goal(goal, send_goal_options);
116135

117136
// wait for acceptance
118137
while (send_goal_future.wait_for(std::chrono::seconds(1)) !=

simple_node/include/simple_node/node.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ class Node : public rclcpp::Node {
2727
void join_spin();
2828

2929
template <typename ActionT>
30-
typename std::shared_ptr<actions::ActionClient<ActionT>>
31-
create_action_client(std::string action_name) {
30+
typename std::shared_ptr<actions::ActionClient<ActionT>> create_action_client(
31+
std::string action_name,
32+
std::function<
33+
void(typename rclcpp_action::ClientGoalHandle<ActionT>::SharedPtr,
34+
const std::shared_ptr<const typename ActionT::Feedback>)>
35+
feedback_cb = nullptr) {
3236

3337
rclcpp::CallbackGroup::SharedPtr group = nullptr;
3438

3539
std::shared_ptr<actions::ActionClient<ActionT>> action_client(
36-
new actions::ActionClient<ActionT>(this, action_name),
40+
new actions::ActionClient<ActionT>(this, action_name, feedback_cb),
3741
this->create_action_deleter<rclcpp_action::Client<ActionT>>(group));
3842

3943
this->get_node_waitables_interface()->add_waitable(action_client, group);

simple_node/simple_node/actions/client/action_client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from typing import Callable
23
import time
34
from threading import Thread, Lock
45
from action_msgs.msg import GoalStatus
@@ -9,12 +10,13 @@
910

1011
class ActionClient(ActionClient2):
1112

12-
def __init__(self, node: Node, action_type, action_name: str):
13+
def __init__(self, node: Node, action_type, action_name: str, feedback_cb: Callable = None):
1314
self._status = GoalStatus.STATUS_UNKNOWN
1415
self.__status_lock = Lock()
1516
self.__goal_handle = None
1617
self.__goal_thread = None
1718
self.__result = None
19+
self.feedback_cb = feedback_cb
1820
super().__init__(node, action_type, action_name,
1921
callback_group=ReentrantCallbackGroup())
2022

@@ -56,11 +58,12 @@ def wait_for_result(self):
5658
def get_result(self):
5759
return self.__result
5860

59-
def __send_goal(self, goal):
61+
def __send_goal(self, goal, feedback_cb: Callable = None):
6062

6163
self.__result = None
6264

63-
send_goal_future = self.send_goal_async(goal)
65+
send_goal_future = self.send_goal_async(
66+
goal, feedback_callback=feedback_cb)
6467

6568
# wait for acceptance
6669
while not send_goal_future.done():
@@ -100,8 +103,15 @@ def __send_goal(self, goal):
100103
self._set_status(get_result_future.result().status)
101104
self.__result = get_result_future.result().result
102105

103-
def send_goal(self, goal):
104-
self.__goal_thread = Thread(target=self.__send_goal, args=(goal,))
106+
def send_goal(self, goal, feedback_cb: Callable = None):
107+
108+
_feedback_cb = self.feedback_cb
109+
110+
if not feedback_cb is None:
111+
_feedback_cb = feedback_cb
112+
113+
self.__goal_thread = Thread(
114+
target=self.__send_goal, args=(goal, _feedback_cb))
105115
self._set_status(GoalStatus.STATUS_UNKNOWN)
106116
self.__goal_thread.start()
107117

simple_node/simple_node/node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def create_client(self, srv_type, srv_name: str) -> Client:
6565

6666
return super().create_client(srv_type, srv_name, callback_group=ReentrantCallbackGroup())
6767

68-
def create_action_client(self, action_type, action_name: str) -> ActionClient:
68+
def create_action_client(self, action_type, action_name: str, feedback_cb: Callable = None) -> ActionClient:
6969
""" create action client from node
7070
7171
Args:
@@ -76,7 +76,7 @@ def create_action_client(self, action_type, action_name: str) -> ActionClient:
7676
ActionClient: client created
7777
"""
7878

79-
return ActionClient(self, action_type, action_name)
79+
return ActionClient(self, action_type, action_name, feedback_cb)
8080

8181
def create_action_server(self,
8282
action_type,

0 commit comments

Comments
 (0)