Skip to content

Commit a5b51d8

Browse files
pablogs9jamoralp
andauthored
Rework demos (#42)
* Rework examples Acording to micro-ROS/rclc#58 * Rework add two ints service * Rework callback * fix callback * Update service * Remove rclc_executor_set_timeout * Update apps/add_two_ints_service/app.c Co-authored-by: Jose Antonio Moral <[email protected]> Co-authored-by: Jose Antonio Moral <[email protected]>
1 parent b81e2cf commit a5b51d8

File tree

5 files changed

+45
-79
lines changed

5 files changed

+45
-79
lines changed

apps/add_two_ints_service/app.c

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,44 @@
1212
#include <unistd.h>
1313
#include <pthread.h>
1414

15-
#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); vTaskDelete(NULL);;}}
16-
#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);}}
15+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printk("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);}}
16+
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printk("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}
1717

18-
// TODO(jamoralp): update using RCLC convenience functions once services are implemented there.
19-
void appMain(void *argument)
20-
{
21-
printf("Free heap pre uROS: %d bytes\n", xPortGetFreeHeapSize());
18+
void service_callback(const void * req, void * res){
19+
example_interfaces__srv__AddTwoInts_Request * req_in = (example_interfaces__srv__AddTwoInts_Request *) req;
20+
example_interfaces__srv__AddTwoInts_Response * res_in = (example_interfaces__srv__AddTwoInts_Response *) res;
2221

23-
rcl_init_options_t options = rcl_get_zero_initialized_init_options();
22+
printf("Service request value: %d + %d.\n", (int) req_in->a, (int) req_in->b);
2423

25-
RCCHECK(rcl_init_options_init(&options, rcl_get_default_allocator()))
24+
res_in->sum = req_in->a + req_in->b;
25+
}
2626

27-
// Optional RMW configuration
28-
rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&options);
29-
RCCHECK(rmw_uros_options_set_client_key(0xBA5EBA11, rmw_options))
27+
void main(void)
28+
{
29+
rcl_allocator_t allocator = rcl_get_default_allocator();
3030

31-
rcl_context_t context = rcl_get_zero_initialized_context();
32-
RCCHECK(rcl_init(0, NULL, &options, &context))
31+
// create init_options
32+
rclc_support_t support;
33+
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
3334

34-
rcl_node_options_t node_ops = rcl_node_get_default_options();
35+
// create node
36+
rcl_node_t node;
37+
RCCHECK(rclc_node_init_default(&node, "add_twoints_server_rclc", "", &support));
3538

36-
rcl_node_t node = rcl_get_zero_initialized_node();
37-
RCCHECK(rcl_node_init(&node, "olimex_node", "", &context, &node_ops))
39+
// create service
40+
rcl_service_t service;
41+
RCCHECK(rclc_service_init_default(&service, &node, ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts), "/addtwoints"));
3842

39-
const char * service_name = "addtwoints";
40-
rcl_service_options_t service_op = rcl_service_get_default_options();
41-
service_op.qos.reliability = RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT;
42-
rcl_service_t serv = rcl_get_zero_initialized_service();
43-
const rosidl_service_type_support_t * service_type_support =
44-
ROSIDL_GET_SRV_TYPE_SUPPORT(example_interfaces, srv, AddTwoInts);
43+
// create executor
44+
rclc_executor_t executor;
45+
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
4546

46-
RCCHECK(rcl_service_init(&serv, &node, service_type_support, service_name, &service_op))
47+
example_interfaces__srv__AddTwoInts_Response res;
48+
example_interfaces__srv__AddTwoInts_Request req;
49+
RCCHECK(rclc_executor_add_service(&executor, &service, &req, &res, service_callback));
4750

48-
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
49-
RCCHECK(rcl_wait_set_init(&wait_set, 0, 0, 0, 0, 1, 0, &context, rcl_get_default_allocator()))
51+
rclc_executor_spin(&executor);
5052

51-
printf("Free heap post uROS configuration: %d bytes\n", xPortGetFreeHeapSize());
52-
printf("uROS Used Memory %d bytes\n", usedMemory);
53-
printf("uROS Absolute Used Memory %d bytes\n", absoluteUsedMemory);
54-
55-
rcl_ret_t rc;
56-
do {
57-
RCSOFTCHECK(rcl_wait_set_clear(&wait_set))
58-
59-
size_t index_service;
60-
RCSOFTCHECK(rcl_wait_set_add_service(&wait_set, &serv, &index_service))
61-
62-
RCSOFTCHECK(rcl_wait(&wait_set, RCL_MS_TO_NS(100)))
63-
64-
if (wait_set.services[index_service]) {
65-
rmw_request_id_t req_id;
66-
example_interfaces__srv__AddTwoInts_Request req;
67-
example_interfaces__srv__AddTwoInts_Request__init(&req);
68-
RCSOFTCHECK(rcl_take_request(&serv,&req_id,&req))
69-
70-
printf("Service request value: %d + %d. Seq %d\n", (int)req.a, (int)req.b, (int) req_id.sequence_number);
71-
72-
example_interfaces__srv__AddTwoInts_Response res;
73-
example_interfaces__srv__AddTwoInts_Response__init(&res);
74-
75-
res.sum = req.a + req.b;
76-
77-
RCSOFTCHECK(rcl_send_response(&serv,&req_id,&res))
78-
}
79-
usleep(10000);
80-
} while (true);
81-
}
53+
RCCHECK(rcl_service_fini(&service, &node));
54+
RCCHECK(rcl_node_fini(&node));
55+
}

apps/crazyflie_position_publisher/app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void appMain(){
6565
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
6666

6767
// create node
68-
rcl_node_t node = rcl_get_zero_initialized_node();
68+
rcl_node_t node;
6969
RCCHECK(rclc_node_init_default(&node, "crazyflie_node", "", &support));
7070

7171
// create publishers

apps/int32_publisher/app.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void appMain(void * arg)
3737
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
3838

3939
// create node
40-
rcl_node_t node = rcl_get_zero_initialized_node();
40+
rcl_node_t node;
4141
RCCHECK(rclc_node_init_default(&node, "freertos_int32_publisher", "", &support));
4242

4343
// create publisher
@@ -48,7 +48,7 @@ void appMain(void * arg)
4848
"freertos_int32_publisher"));
4949

5050
// create timer,
51-
rcl_timer_t timer = rcl_get_zero_initialized_timer();
51+
rcl_timer_t timer;
5252
const unsigned int timer_timeout = 1000;
5353
RCCHECK(rclc_timer_init_default(
5454
&timer,
@@ -57,11 +57,8 @@ void appMain(void * arg)
5757
timer_callback));
5858

5959
// create executor
60-
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
60+
rclc_executor_t executor;
6161
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
62-
63-
unsigned int rcl_wait_timeout = 1000; // in ms
64-
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
6562
RCCHECK(rclc_executor_add_timer(&executor, &timer));
6663

6764
msg.data = 0;

apps/int32_subscriber/app.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void appMain(void * arg)
2828
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
2929

3030
// create node
31-
rcl_node_t node = rcl_get_zero_initialized_node();
31+
rcl_node_t node;
3232
RCCHECK(rclc_node_init_default(&node, "int32_subscriber_rclc", "", &support));
3333

3434
// create subscriber
@@ -39,20 +39,18 @@ void appMain(void * arg)
3939
"/microROS/int32_subscriber"));
4040

4141
// create executor
42-
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
42+
rclc_executor_t executor;
4343
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
44-
45-
unsigned int rcl_wait_timeout = 1000; // in ms
46-
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
4744
RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA));
4845

49-
while(1){
50-
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
51-
usleep(100000);
52-
}
53-
46+
while(1){
47+
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
48+
usleep(100000);
49+
}
5450

51+
// free resources
5552
RCCHECK(rcl_subscription_fini(&subscriber, &node));
5653
RCCHECK(rcl_node_fini(&node));
54+
5755
vTaskDelete(NULL);
5856
}

apps/ping_pong/app.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void appMain(void *argument)
8787
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
8888

8989
// create node
90-
rcl_node_t node = rcl_get_zero_initialized_node();
90+
rcl_node_t node;
9191
RCCHECK(rclc_node_init_default(&node, "pingpong_node", "", &support));
9292

9393
// Create a reliable ping publisher
@@ -108,16 +108,13 @@ void appMain(void *argument)
108108

109109

110110
// Create a 3 seconds ping timer timer,
111-
rcl_timer_t timer = rcl_get_zero_initialized_timer();
111+
rcl_timer_t timer;
112112
RCCHECK(rclc_timer_init_default(&timer, &support, RCL_MS_TO_NS(2000), ping_timer_callback));
113113

114114

115115
// Create executor
116-
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
116+
rclc_executor_t executor;
117117
RCCHECK(rclc_executor_init(&executor, &support.context, 3, &allocator));
118-
119-
unsigned int rcl_wait_timeout = 10; // in ms
120-
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
121118
RCCHECK(rclc_executor_add_timer(&executor, &timer));
122119
RCCHECK(rclc_executor_add_subscription(&executor, &ping_subscriber, &incoming_ping,
123120
&ping_subscription_callback, ON_NEW_DATA));

0 commit comments

Comments
 (0)