|
| 1 | +#include <rcl/rcl.h> |
| 2 | +#include <rcl/error_handling.h> |
| 3 | +#include <std_msgs/msg/string.h> |
| 4 | + |
| 5 | +#include <rmw_uros/options.h> |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | + |
| 9 | +#define ARRAY_LEN 4096 |
| 10 | + |
| 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;}} |
| 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);}} |
| 13 | + |
| 14 | +int main(int argc, const char * const * argv) |
| 15 | +{ |
| 16 | + rcl_init_options_t options = rcl_get_zero_initialized_init_options(); |
| 17 | + RCCHECK(rcl_init_options_init(&options, rcl_get_default_allocator())) |
| 18 | + |
| 19 | + rcl_context_t context = rcl_get_zero_initialized_context(); |
| 20 | + RCCHECK(rcl_init(argc, argv, &options, &context)) |
| 21 | + |
| 22 | + rcl_node_options_t node_ops = rcl_node_get_default_options(); |
| 23 | + rcl_node_t node = rcl_get_zero_initialized_node(); |
| 24 | + RCCHECK(rcl_node_init(&node, "char_long_sequence_subscriber_rcl", "", &context, &node_ops)) |
| 25 | + |
| 26 | + rcl_subscription_options_t subscription_ops = rcl_subscription_get_default_options(); |
| 27 | + rcl_subscription_t subscription = rcl_get_zero_initialized_subscription(); |
| 28 | + RCCHECK(rcl_subscription_init(&subscription, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "/char_long_sequence", &subscription_ops)) |
| 29 | + |
| 30 | + rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); |
| 31 | + RCCHECK(rcl_wait_set_init(&wait_set, 1, 0, 0, 0, 0, 0, &context, rcl_get_default_allocator())) |
| 32 | + |
| 33 | + std_msgs__msg__String msg; |
| 34 | + |
| 35 | + msg.data.data = (char * ) malloc(ARRAY_LEN * sizeof(char)); |
| 36 | + msg.data.size = 0; |
| 37 | + msg.data.capacity = ARRAY_LEN; |
| 38 | + |
| 39 | + char test_array[ARRAY_LEN]; |
| 40 | + memset(test_array,'z',ARRAY_LEN); |
| 41 | + |
| 42 | + do { |
| 43 | + RCSOFTCHECK(rcl_wait_set_clear(&wait_set)) |
| 44 | + |
| 45 | + size_t index; |
| 46 | + RCSOFTCHECK(rcl_wait_set_add_subscription(&wait_set, &subscription, &index)) |
| 47 | + |
| 48 | + RCSOFTCHECK(rcl_wait(&wait_set, RCL_MS_TO_NS(1))) |
| 49 | + |
| 50 | + if (wait_set.subscriptions[index]) { |
| 51 | + |
| 52 | + |
| 53 | + rcl_ret_t rc = rcl_take(wait_set.subscriptions[index], &msg, NULL, NULL); |
| 54 | + if (RCL_RET_OK == rc) { |
| 55 | + |
| 56 | + // Check if sequence items matches the know pattern |
| 57 | + bool pass_test = strncmp(test_array, msg.data.data, msg.data.size) == 0; |
| 58 | + |
| 59 | + printf("I received an %ld array. Test: [%s]\n", msg.data.size, (pass_test) ? "OK" : "FAIL"); |
| 60 | + } |
| 61 | + } |
| 62 | + } while ( true ); |
| 63 | + |
| 64 | + RCCHECK(rcl_subscription_fini(&subscription, &node)) |
| 65 | + RCCHECK(rcl_node_fini(&node)) |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments