-
Notifications
You must be signed in to change notification settings - Fork 42
/
example_service_node.c
88 lines (70 loc) · 3.15 KB
/
example_service_node.c
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
// Copyright (c) 2020 - for information on the respective copyright owner
// see the NOTICE file and/or the repository https://github.com/ros2/rclc.
//
// 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 <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include "example_interfaces/srv/add_two_ints.h"
#include <stdio.h>
#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;}}
#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);}}
example_interfaces__srv__AddTwoInts_Request req;
example_interfaces__srv__AddTwoInts_Response res;
void service_callback(const void * req, void * res)
{
example_interfaces__srv__AddTwoInts_Request * req_in =
(example_interfaces__srv__AddTwoInts_Request *) req;
example_interfaces__srv__AddTwoInts_Response * res_in =
(example_interfaces__srv__AddTwoInts_Response *) res;
printf(
"Service request value: %d + %d\n", (int) req_in->a, (int) req_in->b);
res_in->sum = req_in->a + req_in->b;
}
int main(int argc, const char * const * argv)
{
RCLC_UNUSED(argc);
RCLC_UNUSED(argv);
rcl_allocator_t allocator = rcl_get_default_allocator();
rclc_support_t support;
// create init_options
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
// create node
rcl_node_t node;
RCCHECK(rclc_node_init_default(&node, "add_twoints_client_rclc", "", &support));
// create service
rcl_service_t service = rcl_get_zero_initialized_service();
RCCHECK(
rclc_service_init_default(
&service, &node,
ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts), "/addtwoints"));
// create executor
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
// Note:
// If you need more than the default number of publisher/subscribers, etc., you
// need to configure the micro-ROS middleware also!
// See documentation in the executor.h at the function rclc_executor_init()
// for more details.
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
unsigned int rcl_wait_timeout = 1000; // in ms
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
RCCHECK(rclc_executor_add_service(&executor, &service, &req, &res, service_callback));
// Optional prepare for avoiding allocations during spin
rclc_executor_prepare(&executor);
RCSOFTCHECK(rclc_executor_spin(&executor));
RCCHECK(rcl_service_fini(&service, &node));
RCCHECK(rcl_node_fini(&node));
}