forked from ros2/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnot_composable.cpp
More file actions
91 lines (76 loc) · 3 KB
/
not_composable.cpp
File metadata and controls
91 lines (76 loc) · 3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <chrono>
#include <cinttypes>
#include "example_interfaces/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
// TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
#include "rclcpp_action/rclcpp_action.hpp"
using Fibonacci = example_interfaces::action::Fibonacci;
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("minimal_action_client");
auto action_client = rclcpp_action::create_client<Fibonacci>(node, "fibonacci");
if (!action_client->wait_for_action_server(std::chrono::seconds(20))) {
RCLCPP_ERROR(node->get_logger(), "Action server not available after waiting");
return 1;
}
// Populate a goal
auto goal_msg = Fibonacci::Goal();
goal_msg.order = 10;
RCLCPP_INFO(node->get_logger(), "Sending goal");
// Ask server to achieve some goal and wait until it's accepted
auto goal_handle_future = action_client->async_send_goal(goal_msg);
if (rclcpp::spin_until_complete(node, goal_handle_future) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(node->get_logger(), "send goal call failed :(");
return 1;
}
rclcpp_action::ClientGoalHandle<Fibonacci>::SharedPtr goal_handle = goal_handle_future.get();
if (!goal_handle) {
RCLCPP_ERROR(node->get_logger(), "Goal was rejected by server");
return 1;
}
// Wait for the server to be done with the goal
auto result_future = action_client->async_get_result(goal_handle);
RCLCPP_INFO(node->get_logger(), "Waiting for result");
if (rclcpp::spin_until_complete(node, result_future) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(node->get_logger(), "get result call failed :(");
return 1;
}
rclcpp_action::ClientGoalHandle<Fibonacci>::WrappedResult wrapped_result = result_future.get();
switch (wrapped_result.code) {
case rclcpp_action::ResultCode::SUCCEEDED:
break;
case rclcpp_action::ResultCode::ABORTED:
RCLCPP_ERROR(node->get_logger(), "Goal was aborted");
return 1;
case rclcpp_action::ResultCode::CANCELED:
RCLCPP_ERROR(node->get_logger(), "Goal was canceled");
return 1;
default:
RCLCPP_ERROR(node->get_logger(), "Unknown result code");
return 1;
}
RCLCPP_INFO(node->get_logger(), "result received");
for (auto number : wrapped_result.result->sequence) {
RCLCPP_INFO(node->get_logger(), "%" PRId32, number);
}
rclcpp::shutdown();
return 0;
}