Skip to content

Commit 9b04aea

Browse files
FranFinralph-lange
authored andcommitted
Add client/service tutorial in rcl
1 parent 787428c commit 9b04aea

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

  • _docs/tutorials/core/programming_rcl_rclc
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: Programming with rcl and rclc
3+
permalink: /docs/tutorials/core/programming_rcl_rclc/
4+
redirect_from: /docs/tutorials/core/
5+
redirect_from: /docs/tutorials/
6+
---
7+
8+
## Programming client/service with rcl
9+
10+
ROS 2 services is another communication mechanism between nodes. Services implement a client-server paradigms based on ROS 2 messages and types. Further information about ROS 2 services can be found [here](https://index.ros.org/doc/ros2/Tutorials/Services/Understanding-ROS2-Services/)
11+
12+
Ready to use code related to this tutorial can be found in `micro-ROS-demos/rcl/addtwoints_server` and `micro-ROS-demos/rcl/addtwoints_client` folders.
13+
14+
Starting from a code where RCL is init and a micro-ROS node is created, these steps are required in order to generate a service server:
15+
16+
```c
17+
// Creating service server and options
18+
rcl_service_options_t service_options = rcl_service_get_default_options();
19+
rcl_service_t server = rcl_get_zero_initialized_service();
20+
21+
// Initializing service server
22+
rcl_service_init(&server, &node, ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts), "addtwoints", &service_options);
23+
24+
// Init service server wait set
25+
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
26+
rcl_wait_set_init(&wait_set, 0, 0, 0, 0, 1, 0, &context, rcl_get_default_allocator());
27+
28+
```
29+
30+
On the other hand the service client initialization looks like that:
31+
32+
```c
33+
// Creating service client and options
34+
rcl_client_options_t client_options = rcl_client_get_default_options();
35+
rcl_client_t client = rcl_get_zero_initialized_client();
36+
37+
// Initializing service client
38+
rcl_client_init(&client, &node, ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts), "addtwoints", &client_options)
39+
40+
// Init service client wait set
41+
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
42+
rcl_wait_set_init(&wait_set, 0, 0, 0, 1, 0, 0, &context, rcl_get_default_allocator());
43+
```
44+
45+
First of all, seeing `AddTwoInts.srv` type definition it is possible to determine request and reply elements of the service. Service client will make a request with two integers and service server should send its sum as a response.
46+
47+
```
48+
int64 a
49+
int64 b
50+
---
51+
int64 sum
52+
```
53+
54+
Once service client and server are configured, service client can perform a request and wait for reply:
55+
56+
```c
57+
// Creating a service request
58+
int64_t seq;
59+
example_interfaces__srv__AddTwoInts_Request req;
60+
req.a = 24;
61+
req.b = 42;
62+
63+
// Sending the request
64+
rcl_send_request(&client, &req, &seq)
65+
printf("Send service request %d + %d. Seq %ld\n",(int)req.a, (int)req.b, (int)seq);
66+
67+
// Wait for response
68+
bool done = false;
69+
do {
70+
rcl_wait_set_clear(&wait_set);
71+
72+
size_t index;
73+
rcl_wait_set_add_client(&wait_set, &client, &index);
74+
75+
rcl_wait(&wait_set, RCL_MS_TO_NS(1));
76+
77+
// If wait set client element is not null, response is ready
78+
if (wait_set.clients[index]) {
79+
rmw_request_id_t req_id;
80+
81+
// Create a service response struct
82+
example_interfaces__srv__AddTwoInts_Response res;
83+
84+
// Take the response
85+
rcl_ret_t rc = rcl_take_response(&client, &req_id, &res);
86+
87+
if (RCL_RET_OK == rc) {
88+
printf("Received service response %d + %d = %d. Seq %d\n",(int)req.a, (int)req.b, (int)res.sum,req_id.sequence_number);
89+
done = true;
90+
}
91+
}
92+
} while ( !done );
93+
```
94+
95+
On service server side, the ROS 2 node should be waiting for service requests and generate service replies:
96+
97+
```c
98+
while(1){
99+
rcl_wait_set_clear(&wait_set);
100+
101+
size_t index;
102+
rcl_wait_set_add_service(&wait_set, &service, &index);
103+
104+
rcl_wait(&wait_set, RCL_MS_TO_NS(1));
105+
106+
// If wait set service element is not null, request is ready
107+
if (wait_set.services[index]) {
108+
rmw_request_id_t req_id;
109+
110+
// Create a service request struct
111+
example_interfaces__srv__AddTwoInts_Request req;
112+
113+
// Take the request
114+
rcl_take_request(&service, &req_id, &req);
115+
116+
printf("Service request value: %d + %d. Seq %d\n", (int)req.a, (int)req.b, (int)req_id.sequence_number);
117+
118+
// Create a service response, fill the result and send it
119+
example_interfaces__srv__AddTwoInts_Response res;
120+
res.sum = req.a + req.b;
121+
rcl_send_response(&service, &req_id,&res);
122+
}
123+
}
124+
```

0 commit comments

Comments
 (0)