|
| 1 | +#include <rcl/rcl.h> |
| 2 | +#include <rcl/error_handling.h> |
| 3 | +#include <rclc/rclc.h> |
| 4 | +#include <rclc/executor.h> |
| 5 | + |
| 6 | +#include <std_msgs/msg/int32.h> |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#include <rmw_uros/options.h> |
| 12 | + |
| 13 | +#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;}} |
| 14 | +#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 | + |
| 16 | +rcl_publisher_t publisher; |
| 17 | +std_msgs__msg__Int32 msg; |
| 18 | + |
| 19 | +void timer_callback(rcl_timer_t * timer, int64_t last_call_time) |
| 20 | +{ |
| 21 | + (void) last_call_time; |
| 22 | + if (timer != NULL) { |
| 23 | + RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL)); |
| 24 | + printf("Sent: %d\n", msg.data); |
| 25 | + msg.data++; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +int main() |
| 30 | +{ |
| 31 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 32 | + rclc_support_t support; |
| 33 | + |
| 34 | + rcl_init_options_t init_options = rcl_get_zero_initialized_init_options(); |
| 35 | + RCCHECK(rcl_init_options_init(&init_options, allocator)); |
| 36 | + rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options); |
| 37 | + |
| 38 | + // Auto discover micro-ROS agent |
| 39 | + if(rmw_uros_discover_agent(rmw_options) != RCL_RET_OK){ |
| 40 | + printf("micro-ROS agent not found\n"); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + |
| 44 | + // create init_options |
| 45 | + RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator)); |
| 46 | + |
| 47 | + // create node |
| 48 | + rcl_node_t node = rcl_get_zero_initialized_node(); |
| 49 | + RCCHECK(rclc_node_init_default(&node, "int32_publisher_rclc", "", &support)); |
| 50 | + |
| 51 | + // create publisher |
| 52 | + RCCHECK(rclc_publisher_init_default( |
| 53 | + &publisher, |
| 54 | + &node, |
| 55 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
| 56 | + "std_msgs_msg_Int32")); |
| 57 | + |
| 58 | + // create timer, |
| 59 | + rcl_timer_t timer = rcl_get_zero_initialized_timer(); |
| 60 | + const unsigned int timer_timeout = 1000; |
| 61 | + RCCHECK(rclc_timer_init_default( |
| 62 | + &timer, |
| 63 | + &support, |
| 64 | + RCL_MS_TO_NS(timer_timeout), |
| 65 | + timer_callback)); |
| 66 | + |
| 67 | + // create executor |
| 68 | + rclc_executor_t executor = rclc_executor_get_zero_initialized_executor(); |
| 69 | + RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); |
| 70 | + |
| 71 | + unsigned int rcl_wait_timeout = 1000; // in ms |
| 72 | + RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout))); |
| 73 | + RCCHECK(rclc_executor_add_timer(&executor, &timer)); |
| 74 | + |
| 75 | + msg.data = 0; |
| 76 | + |
| 77 | + rclc_executor_spin(&executor); |
| 78 | + |
| 79 | + RCCHECK(rcl_publisher_fini(&publisher, &node)); |
| 80 | + RCCHECK(rcl_node_fini(&node)); |
| 81 | +} |
0 commit comments