-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disabled result caching: Result gets erased before result callback is…
… registered ros2/rclcpp#2101 Signed-off-by: Tomoya Fujita <[email protected]>
- Loading branch information
1 parent
196683d
commit 16be352
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <functional> | ||
#include <memory> | ||
#include <thread> | ||
|
||
#include "action_tutorials_interfaces/action/fibonacci.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp_action/rclcpp_action.hpp" | ||
|
||
class FibonacciActionServer : public rclcpp::Node | ||
{ | ||
public: | ||
using Fibonacci = action_tutorials_interfaces::action::Fibonacci; | ||
using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle<Fibonacci>; | ||
|
||
explicit FibonacciActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions()) | ||
: Node("fibonacci_action_server", options) | ||
{ | ||
using namespace std::placeholders; | ||
|
||
auto serverOptions = rcl_action_server_get_default_options(); | ||
serverOptions.result_timeout.nanoseconds = 0; | ||
|
||
this->action_server_ = rclcpp_action::create_server<Fibonacci>( | ||
this, | ||
"fibonacci", | ||
std::bind(&FibonacciActionServer::handle_goal, this, _1, _2), | ||
std::bind(&FibonacciActionServer::handle_cancel, this, _1), | ||
std::bind(&FibonacciActionServer::handle_accepted, this, _1), | ||
serverOptions); | ||
} | ||
|
||
private: | ||
rclcpp_action::Server<Fibonacci>::SharedPtr action_server_; | ||
|
||
rclcpp_action::GoalResponse handle_goal( | ||
const rclcpp_action::GoalUUID & uuid, | ||
std::shared_ptr<const Fibonacci::Goal> goal) | ||
{ | ||
RCLCPP_INFO(this->get_logger(), "Received goal request with order %d", goal->order); | ||
(void)uuid; | ||
return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE; | ||
} | ||
|
||
rclcpp_action::CancelResponse handle_cancel( | ||
const std::shared_ptr<GoalHandleFibonacci> goal_handle) | ||
{ | ||
RCLCPP_INFO(this->get_logger(), "Received request to cancel goal"); | ||
(void)goal_handle; | ||
return rclcpp_action::CancelResponse::ACCEPT; | ||
} | ||
|
||
void handle_accepted(const std::shared_ptr<GoalHandleFibonacci> goal_handle) | ||
{ | ||
using namespace std::placeholders; | ||
// this needs to return quickly to avoid blocking the executor, so spin up a new thread | ||
std::thread{std::bind(&FibonacciActionServer::execute, this, _1), goal_handle}.detach(); | ||
} | ||
|
||
void execute(const std::shared_ptr<GoalHandleFibonacci> goal_handle) | ||
{ | ||
RCLCPP_INFO(this->get_logger(), "Executing goal"); | ||
const auto goal = goal_handle->get_goal(); | ||
auto result = std::make_shared<Fibonacci::Result>(); | ||
result->sequence.push_back(42); | ||
goal_handle->succeed(result); | ||
RCLCPP_INFO(this->get_logger(), "Executing done"); | ||
} | ||
}; // class FibonacciActionServer | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
auto node = std::make_shared<FibonacciActionServer>(); | ||
rclcpp::spin(node); | ||
rclcpp::shutdown(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import rclpy | ||
from rclpy.action import ActionClient | ||
from rclpy.node import Node | ||
|
||
from action_tutorials_interfaces.action import Fibonacci | ||
|
||
|
||
class FibonacciActionClient(Node): | ||
|
||
def __init__(self): | ||
super().__init__('fibonacci_action_client') | ||
self._action_client = ActionClient(self, Fibonacci, 'fibonacci') | ||
|
||
def send_goal(self, order): | ||
goal_msg = Fibonacci.Goal() | ||
goal_msg.order = order | ||
|
||
self._action_client.wait_for_server() | ||
|
||
self._send_goal_future = self._action_client.send_goal_async(goal_msg) | ||
self._send_goal_future.add_done_callback(self.goal_response_callback) | ||
|
||
def goal_response_callback(self, future): | ||
goal_handle = future.result() | ||
if not goal_handle.accepted: | ||
self.get_logger().info('Goal rejected :(') | ||
return | ||
|
||
self.get_logger().info('Goal accepted :)') | ||
|
||
self.get_logger().info("goal_response_callback: get_result_async") | ||
self._get_result_future = goal_handle.get_result_async() | ||
self.get_logger().info("goal_response_callback: add_done_callback(result_callback)") | ||
self._get_result_future.add_done_callback(self.get_result_callback) | ||
self.get_logger().info("goal_response_callback: done") | ||
|
||
def get_result_callback(self, future): | ||
result = future.result() | ||
self.get_logger().info('Result: status {}'.format(result.status)) | ||
self.get_logger().info('Result: {0}'.format(result.result.sequence)) | ||
rclpy.shutdown() | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
|
||
action_client = FibonacciActionClient() | ||
|
||
action_client.send_goal(10) | ||
|
||
rclpy.spin(action_client) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |