|
1 | | -#include <rcl/rcl.h> |
2 | | -#include <rcl_action/rcl_action.h> |
3 | | -#include <rcl/error_handling.h> |
4 | | -#include <example_interfaces/action/fibonacci.h> |
5 | | - |
6 | 1 | #include <stdio.h> |
7 | 2 | #include <unistd.h> |
8 | 3 | #include <pthread.h> |
9 | 4 |
|
10 | | -#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc); return 1;}} |
11 | | -#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}} |
12 | | - |
13 | | -int main(int argc, const char * const * argv) |
14 | | -{ |
15 | | - rcl_init_options_t options = rcl_get_zero_initialized_init_options(); |
16 | | - RCCHECK(rcl_init_options_init(&options, rcl_get_default_allocator())) |
17 | | - |
18 | | - rcl_context_t context = rcl_get_zero_initialized_context(); |
19 | | - RCCHECK(rcl_init(argc, argv, &options, &context)) |
20 | | - |
21 | | - rcl_node_options_t node_ops = rcl_node_get_default_options(); |
22 | | - rcl_node_t node = rcl_get_zero_initialized_node(); |
23 | | - RCCHECK(rcl_node_init(&node, "fibonacci_action_client_rcl", "", &context, &node_ops)) |
24 | | - |
25 | | - const char * action_name = "fibonacci"; |
26 | | - const rosidl_action_type_support_t * action_type_support = ROSIDL_GET_ACTION_TYPE_SUPPORT(example_interfaces, Fibonacci); |
27 | | - rcl_action_client_t action_client = rcl_action_get_zero_initialized_client(); |
28 | | - rcl_action_client_options_t action_client_ops = rcl_action_client_get_default_options(); |
29 | | - |
30 | | - RCCHECK(rcl_action_client_init(&action_client, &node, action_type_support, action_name, &action_client_ops)) |
| 5 | +#include <rcl/rcl.h> |
| 6 | +#include <rcl/error_handling.h> |
| 7 | +#include <rclc/rclc.h> |
| 8 | +#include <rclc/executor.h> |
31 | 9 |
|
32 | | - size_t num_subscriptions, num_guard_conditions, num_timers, num_clients, num_services; |
| 10 | +#include <example_interfaces/action/fibonacci.h> |
33 | 11 |
|
34 | | - RCCHECK(rcl_action_client_wait_set_get_num_entities(&action_client, &num_subscriptions, &num_guard_conditions, &num_timers, &num_clients, &num_services)) |
35 | | - rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); |
36 | | - RCCHECK(rcl_wait_set_init(&wait_set, num_subscriptions, num_guard_conditions, num_timers, num_clients, num_services, 0, &context, rcl_get_default_allocator())) |
| 12 | +#define RCCHECK(fn) {rcl_ret_t temp_rc = fn; if ((temp_rc != RCL_RET_OK)) {printf( \ |
| 13 | + "Failed status on line %d: %d. Aborting.\n", __LINE__, (int)temp_rc); return 1;}} |
| 14 | +#define RCSOFTCHECK(fn) {rcl_ret_t temp_rc = fn; if ((temp_rc != RCL_RET_OK)) {printf( \ |
| 15 | + "Failed status on line %d: %d. Continuing.\n", __LINE__, (int)temp_rc);}} |
37 | 16 |
|
38 | | - int64_t goal_sequence_number; |
39 | | - int64_t result_sequence_number; |
| 17 | +#define MAX_FIBONACCI_ORDER 500 |
| 18 | +bool goal_completed = false; |
| 19 | +int goal_order = 10; |
40 | 20 |
|
41 | | - bool done = false; |
42 | | - bool goal_accepted = false; |
43 | | - int order = 10; |
| 21 | +void goal_request_callback(rclc_action_goal_handle_t * goal_handle, bool accepted, void * context) |
| 22 | +{ |
| 23 | + (void) context; |
| 24 | + |
| 25 | + example_interfaces__action__Fibonacci_SendGoal_Request * request = |
| 26 | + (example_interfaces__action__Fibonacci_SendGoal_Request *) goal_handle->ros_goal_request; |
| 27 | + printf( |
| 28 | + "Goal request (order: %d): %s\n", |
| 29 | + request->goal.order, |
| 30 | + accepted ? "Accepted" : "Rejected" |
| 31 | + ); |
| 32 | + |
| 33 | + if (!accepted) { |
| 34 | + goal_completed = true; |
| 35 | + } |
| 36 | +} |
44 | 37 |
|
45 | | - sleep(2); // Sleep a while to ensure DDS matching before sending request |
| 38 | +void feedback_callback(rclc_action_goal_handle_t * goal_handle, void * ros_feedback, void * context) |
| 39 | +{ |
| 40 | + (void) context; |
46 | 41 |
|
47 | | - printf("Sending goal request\n"); |
48 | | - example_interfaces__action__Fibonacci_SendGoal_Request ros_goal_request; |
49 | | - ros_goal_request.goal.order = order; |
50 | | - RCCHECK(rcl_action_send_goal_request(&action_client, &ros_goal_request, &goal_sequence_number)) |
51 | | - |
| 42 | + example_interfaces__action__Fibonacci_SendGoal_Request * request = |
| 43 | + (example_interfaces__action__Fibonacci_SendGoal_Request *) goal_handle->ros_goal_request; |
52 | 44 |
|
53 | | - do { |
54 | | - RCSOFTCHECK(rcl_wait_set_clear(&wait_set)) |
55 | | - |
56 | | - size_t client_index, subscription_index; |
57 | | - RCSOFTCHECK(rcl_action_wait_set_add_action_client(&wait_set, &action_client, &client_index, &subscription_index)) |
| 45 | + printf( |
| 46 | + "Goal Feedback (order: %d) [", |
| 47 | + request->goal.order |
| 48 | + ); |
58 | 49 |
|
59 | | - rcl_ret_t rc = rcl_wait(&wait_set, RCL_MS_TO_NS(50)); |
| 50 | + example_interfaces__action__Fibonacci_FeedbackMessage * feedback = |
| 51 | + (example_interfaces__action__Fibonacci_FeedbackMessage *) ros_feedback; |
60 | 52 |
|
61 | | - bool is_feedback_ready = false; |
62 | | - bool is_status_ready = false; |
63 | | - bool is_goal_response_ready = false; |
64 | | - bool is_cancel_response_ready = false; |
65 | | - bool is_result_response_ready = false; |
| 53 | + for (size_t i = 0; i < feedback->feedback.sequence.size; i++) { |
| 54 | + printf("%d ", feedback->feedback.sequence.data[i]); |
| 55 | + } |
| 56 | + printf("\b]\n"); |
| 57 | +} |
66 | 58 |
|
67 | | - RCSOFTCHECK(rcl_action_client_wait_set_get_entities_ready(&wait_set, &action_client, &is_feedback_ready, &is_status_ready, &is_goal_response_ready, &is_cancel_response_ready, &is_result_response_ready)) |
| 59 | +void result_request_callback( |
| 60 | + rclc_action_goal_handle_t * goal_handle, void * ros_result_response, |
| 61 | + void * context) |
| 62 | +{ |
| 63 | + (void) context; |
68 | 64 |
|
69 | | - if (is_goal_response_ready) { |
70 | | - printf("Goal response ready\n"); |
71 | | - example_interfaces__action__Fibonacci_SendGoal_Response ros_goal_response; |
72 | | - rmw_request_id_t response_header; |
| 65 | + static size_t goal_count = 1; |
73 | 66 |
|
74 | | - RCSOFTCHECK(rcl_action_take_goal_response(&action_client, &response_header, &ros_goal_response)) |
| 67 | + example_interfaces__action__Fibonacci_SendGoal_Request * request = |
| 68 | + (example_interfaces__action__Fibonacci_SendGoal_Request *) goal_handle->ros_goal_request; |
75 | 69 |
|
76 | | - if (ros_goal_response.accepted) { |
77 | | - printf("Goal request accepted\n"); |
78 | | - printf("Requesting response...\n"); |
| 70 | + printf( |
| 71 | + "Goal Result (order: %d) [", |
| 72 | + request->goal.order |
| 73 | + ); |
79 | 74 |
|
80 | | - goal_accepted = true; |
| 75 | + example_interfaces__action__Fibonacci_GetResult_Response * result = |
| 76 | + (example_interfaces__action__Fibonacci_GetResult_Response *) ros_result_response; |
81 | 77 |
|
82 | | - example_interfaces__action__Fibonacci_GetResult_Request ros_result_request; |
83 | | - RCSOFTCHECK(rcl_action_send_result_request(&action_client, &ros_result_request, &result_sequence_number)) |
84 | | - } |
| 78 | + if (result->status == GOAL_STATE_SUCCEEDED) { |
| 79 | + for (size_t i = 0; i < result->result.sequence.size; i++) { |
| 80 | + printf("%d ", result->result.sequence.data[i]); |
85 | 81 | } |
86 | | - |
87 | | - if (is_feedback_ready) { |
88 | | - example_interfaces__action__Fibonacci_FeedbackMessage ros_feedback; |
89 | | - |
90 | | - ros_feedback.feedback.sequence.data = (int32_t*) malloc(order * sizeof(int32_t)); |
91 | | - ros_feedback.feedback.sequence.capacity = order; |
| 82 | + } else if (result->status == GOAL_STATE_CANCELED) { |
| 83 | + printf("CANCELED "); |
| 84 | + } else { |
| 85 | + printf("ABORTED "); |
| 86 | + } |
92 | 87 |
|
93 | | - RCSOFTCHECK(rcl_action_take_feedback(&action_client, &ros_feedback)) |
| 88 | + printf("\b]\n"); |
94 | 89 |
|
95 | | - printf("Feedback: ["); |
96 | | - for (size_t i = 0; i < ros_feedback.feedback.sequence.size ; i++) { |
97 | | - printf("%d, ", ros_feedback.feedback.sequence.data[i]); |
98 | | - } |
99 | | - printf("]\n"); |
| 90 | + goal_completed = true; |
| 91 | +} |
100 | 92 |
|
101 | | - free(ros_feedback.feedback.sequence.data); |
102 | | - } |
103 | | - |
104 | | - if (is_result_response_ready) { |
105 | | - example_interfaces__action__Fibonacci_GetResult_Response ros_result_response; |
106 | | - rmw_request_id_t response_header; |
| 93 | +void cancel_request_callback( |
| 94 | + rclc_action_goal_handle_t * goal_handle, bool cancelled, |
| 95 | + void * context) |
| 96 | +{ |
| 97 | + (void) context; |
107 | 98 |
|
108 | | - ros_result_response.result.sequence.data = (int32_t*) malloc(order * sizeof(int32_t)); |
109 | | - ros_result_response.result.sequence.capacity = order; |
| 99 | + example_interfaces__action__Fibonacci_SendGoal_Request * request = |
| 100 | + (example_interfaces__action__Fibonacci_SendGoal_Request *) goal_handle->ros_goal_request; |
110 | 101 |
|
111 | | - RCSOFTCHECK(rcl_action_take_result_response(&action_client, &response_header, &ros_result_response)) |
| 102 | + printf( |
| 103 | + "Goal cancel request (order: %d): %s\n", |
| 104 | + request->goal.order, |
| 105 | + cancelled ? "Accepted" : "Rejected"); |
112 | 106 |
|
113 | | - printf("Response: ["); |
114 | | - for (size_t i = 0; i < ros_result_response.result.sequence.size ; i++) { |
115 | | - printf("%d, ", ros_result_response.result.sequence.data[i]); |
116 | | - } |
117 | | - printf("]\n"); |
| 107 | + if (cancelled) { |
| 108 | + goal_completed = true; |
| 109 | + } |
| 110 | +} |
118 | 111 |
|
119 | | - done = true; |
120 | | - free(ros_result_response.result.sequence.data); |
121 | | - } |
122 | | - } while ( !done ); |
| 112 | +int main(int argc, const char * const * argv) |
| 113 | +{ |
| 114 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 115 | + rclc_support_t support; |
| 116 | + |
| 117 | + // create init_options |
| 118 | + RCCHECK(rclc_support_init(&support, 0, NULL, &allocator)); |
| 119 | + |
| 120 | + // create node |
| 121 | + rcl_node_t node; |
| 122 | + RCCHECK(rclc_node_init_default(&node, "fibonacci_action_client_rcl", "", &support)); |
| 123 | + |
| 124 | + // Create action client |
| 125 | + rclc_action_client_t action_client; |
| 126 | + RCCHECK( |
| 127 | + rclc_action_client_init_default( |
| 128 | + &action_client, |
| 129 | + &node, |
| 130 | + ROSIDL_GET_ACTION_TYPE_SUPPORT(example_interfaces, Fibonacci), |
| 131 | + "fibonacci" |
| 132 | + )); |
| 133 | + |
| 134 | + // Create executor |
| 135 | + rclc_executor_t executor; |
| 136 | + rclc_executor_init(&executor, &support.context, 1, &allocator); |
| 137 | + |
| 138 | + example_interfaces__action__Fibonacci_FeedbackMessage ros_feedback; |
| 139 | + example_interfaces__action__Fibonacci_GetResult_Response ros_result_response; |
| 140 | + example_interfaces__action__Fibonacci_SendGoal_Request ros_goal_request; |
123 | 141 |
|
124 | | - RCCHECK(rcl_action_client_fini(&action_client, &node)) |
| 142 | + // Allocate msg memory |
| 143 | + ros_feedback.feedback.sequence.capacity = MAX_FIBONACCI_ORDER; |
| 144 | + ros_feedback.feedback.sequence.size = 0; |
| 145 | + ros_feedback.feedback.sequence.data = (int32_t *) malloc( |
| 146 | + ros_feedback.feedback.sequence.capacity * sizeof(int32_t)); |
| 147 | + |
| 148 | + ros_result_response.result.sequence.capacity = MAX_FIBONACCI_ORDER; |
| 149 | + ros_result_response.result.sequence.size = 0; |
| 150 | + ros_result_response.result.sequence.data = (int32_t *) malloc( |
| 151 | + ros_result_response.result.sequence.capacity * sizeof(int32_t)); |
| 152 | + |
| 153 | + RCCHECK( |
| 154 | + rclc_executor_add_action_client( |
| 155 | + &executor, |
| 156 | + &action_client, |
| 157 | + 10, |
| 158 | + &ros_result_response, |
| 159 | + &ros_feedback, |
| 160 | + goal_request_callback, |
| 161 | + feedback_callback, |
| 162 | + result_request_callback, |
| 163 | + cancel_request_callback, |
| 164 | + (void *) &action_client |
| 165 | + )); |
| 166 | + |
| 167 | + sleep(2); |
| 168 | + |
| 169 | + // Send goal request |
| 170 | + ros_goal_request.goal.order = goal_order; |
| 171 | + RCCHECK(rclc_action_send_goal_request(&action_client, &ros_goal_request, NULL)); |
| 172 | + |
| 173 | + while (!goal_completed) { |
| 174 | + rclc_executor_spin_some(&executor, RCL_MS_TO_NS(10)); |
| 175 | + usleep(100000); |
| 176 | + } |
| 177 | + |
| 178 | + RCCHECK(rclc_action_client_fini(&action_client, &node)) |
125 | 179 | RCCHECK(rcl_node_fini(&node)) |
126 | 180 |
|
127 | 181 | return 0; |
128 | 182 | } |
129 | | - |
|
0 commit comments