1
- #include " lander_align.new.hpp"
1
+ #include " lander_align.new.hpp"
2
+
3
+ // LanderAlignActionServer member functions:
4
+ namespace mrover {
5
+
6
+ using LanderAlign = action::LanderAlign;
7
+ using GoalHandleLanderAlign = rclcpp_action::ServerGoalHandle<LanderAlign>;
8
+
9
+ LanderAlignNode::LanderAlignNode (const rclcpp::NodeOptions& options)
10
+ : Node(" lander_align" , options) {
11
+ // The constructor also instantiates a new action server
12
+ action_server_ = rclcpp_action::create_server<LanderAlign>(
13
+ this ,
14
+ " lander_align_action" ,
15
+ std::bind (&LanderAlignNode::handle_goal, this , std::placeholders::_1, std::placeholders::_2),
16
+ std::bind (&LanderAlignNode::handle_cancel, this , std::placeholders::_1),
17
+ std::bind (&LanderAlignNode::handle_accepted, this , std::placeholders::_1));
18
+ }
19
+
20
+ // the callback for handling new goals (this implementation just accepts all goals)
21
+ rclcpp_action::GoalResponse LanderAlignNode::handle_goal (const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const LanderAlign::Goal> goal) {
22
+ RCLCPP_INFO (get_logger (), " Received goal request with order %d" , goal->num );
23
+ (void )uuid;
24
+ return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
25
+ }
26
+
27
+ // the callback for dealing with cancellation (this implementation just tells the client that it accepted the cancellation)
28
+ rclcpp_action::CancelResponse LanderAlignNode::handle_cancel (const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
29
+ RCLCPP_INFO (get_logger (), " Received request to cancel goal" );
30
+ (void )goal_handle;
31
+ return rclcpp_action::CancelResponse::ACCEPT;
32
+ }
33
+
34
+ // the callback for accepting a new goal and processing it
35
+ void LanderAlignNode::handle_accepted (const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
36
+ using namespace std ::placeholders;
37
+ // this needs to return quickly to avoid blocking the executor, so spin up a new thread
38
+ std::thread{std::bind (&LanderAlignNode::execute, this , _1), goal_handle}.detach ();
39
+ }
40
+
41
+ // all further processing and updates are done in the execute method in the new thread
42
+ void LanderAlignNode::execute (const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
43
+ RCLCPP_INFO (this ->get_logger (), " Executing goal" );
44
+ auto result = std::make_shared<LanderAlign::Result>();
45
+ // Check if goal is done
46
+ if (rclcpp::ok ()) {
47
+ goal_handle->succeed (result);
48
+ RCLCPP_INFO (this ->get_logger (), " Goal succeeded" );
49
+ }
50
+ }
51
+ }
0 commit comments