|
1 | 1 | #include <rcl/rcl.h> |
2 | 2 | #include <rcl/error_handling.h> |
| 3 | +#include <rclc/rclc.h> |
| 4 | +#include <rclc/executor.h> |
| 5 | + |
3 | 6 | #include "example_interfaces/srv/add_two_ints.h" |
4 | 7 |
|
5 | 8 | #include <stdio.h> |
|
8 | 11 | #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;}} |
9 | 12 | #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);}} |
10 | 13 |
|
| 14 | +example_interfaces__srv__AddTwoInts_Request req; |
| 15 | +example_interfaces__srv__AddTwoInts_Response res; |
| 16 | + |
| 17 | +void client_callback(const void * msg, rmw_request_id_t * req_id){ |
| 18 | + example_interfaces__srv__AddTwoInts_Response * msgin = (example_interfaces__srv__AddTwoInts_Response * ) msg; |
| 19 | + printf("Received service response %ld + %ld = %ld. Seq %ld\n", req.a, req.b, msgin->sum, req_id->sequence_number); |
| 20 | +} |
| 21 | + |
11 | 22 | int main(int argc, const char * const * argv) |
12 | 23 | { |
13 | | - rcl_init_options_t options = rcl_get_zero_initialized_init_options(); |
14 | | - RCCHECK(rcl_init_options_init(&options, rcl_get_default_allocator())) |
| 24 | + RCLC_UNUSED(argc); |
| 25 | + RCLC_UNUSED(argv); |
| 26 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 27 | + rclc_support_t support; |
15 | 28 |
|
16 | | - rcl_context_t context = rcl_get_zero_initialized_context(); |
17 | | - RCCHECK(rcl_init(argc, argv, &options, &context)) |
| 29 | + // create init_options |
| 30 | + RCCHECK(rclc_support_init(&support, 0, NULL, &allocator)); |
18 | 31 |
|
19 | | - rcl_node_options_t node_ops = rcl_node_get_default_options(); |
20 | | - rcl_node_t node = rcl_get_zero_initialized_node(); |
21 | | - RCCHECK(rcl_node_init(&node, "addtowints_client_rcl", "", &context, &node_ops)) |
| 32 | + // create node |
| 33 | + rcl_node_t node = rcl_get_zero_initialized_node(); |
| 34 | + RCCHECK(rclc_node_init_default(&node, "add_twoints_client_rclc", "", &support)); |
22 | 35 |
|
23 | | - const char * client_name = "/addtwoints"; |
24 | | - |
25 | | - rcl_client_options_t client_options = rcl_client_get_default_options(); |
| 36 | + // create client |
26 | 37 | rcl_client_t client = rcl_get_zero_initialized_client(); |
27 | | - const rosidl_service_type_support_t * client_type_support = ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts); |
| 38 | + RCCHECK(rclc_client_init_default(&client, &node, ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts), "/addtwoints")); |
28 | 39 |
|
29 | | - RCCHECK(rcl_client_init(&client, &node, client_type_support, client_name, &client_options)) |
| 40 | + // create executor |
| 41 | + rclc_executor_t executor = rclc_executor_get_zero_initialized_executor(); |
| 42 | + RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); |
30 | 43 |
|
31 | | - rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); |
32 | | - RCCHECK(rcl_wait_set_init(&wait_set, 0, 0, 0, 1, 0, 0, &context, rcl_get_default_allocator())) |
| 44 | + unsigned int rcl_wait_timeout = 10; // in ms |
| 45 | + RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout))); |
| 46 | + RCCHECK(rclc_executor_add_client(&executor, &client, &res, client_callback)); |
33 | 47 |
|
34 | | - sleep(2); // Sleep a while to ensure DDS matching before sending request |
35 | | - |
36 | | - // Send request |
37 | 48 | int64_t seq; |
38 | | - example_interfaces__srv__AddTwoInts_Request req; |
39 | 49 | example_interfaces__srv__AddTwoInts_Request__init(&req); |
40 | 50 | req.a = 24; |
41 | 51 | req.b = 42; |
42 | 52 |
|
| 53 | + sleep(2); // Sleep a while to ensure DDS matching before sending request |
| 54 | + |
43 | 55 | RCCHECK(rcl_send_request(&client, &req, &seq)) |
44 | 56 | printf("Send service request %ld + %ld. Seq %ld\n",req.a, req.b, seq); |
| 57 | + |
| 58 | + rclc_executor_spin(&executor); |
45 | 59 |
|
46 | | - // Wait for response |
47 | | - bool done = false; |
48 | | - do { |
49 | | - RCSOFTCHECK(rcl_wait_set_clear(&wait_set)) |
50 | | - |
51 | | - size_t index; |
52 | | - RCSOFTCHECK(rcl_wait_set_add_client(&wait_set, &client, &index)) |
53 | | - |
54 | | - RCSOFTCHECK(rcl_wait(&wait_set, RCL_MS_TO_NS(1))) |
55 | | - |
56 | | - for (size_t i = 0; i < wait_set.size_of_clients; i++) { |
57 | | - if (wait_set.clients[0]) { |
58 | | - rmw_request_id_t req_id; |
59 | | - example_interfaces__srv__AddTwoInts_Response res; |
60 | | - example_interfaces__srv__AddTwoInts_Response__init(&res); |
61 | | - |
62 | | - rcl_ret_t rc = rcl_take_response(&client, &req_id, &res); |
63 | | - |
64 | | - if (RCL_RET_OK == rc) { |
65 | | - printf("Received service response %ld + %ld = %ld. Seq %ld\n",req.a, req.b, res.sum, req_id.sequence_number); |
66 | | - done = true; |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - } while ( !done ); |
71 | | - |
72 | | - RCCHECK(rcl_client_fini(&client,&node)) |
73 | | - RCCHECK(rcl_node_fini(&node)) |
74 | | - |
75 | | - return 0; |
| 60 | + RCCHECK(rcl_client_fini(&client, &node)); |
| 61 | + RCCHECK(rcl_node_fini(&node)); |
76 | 62 | } |
77 | | - |
0 commit comments