|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +#include <rcl/rcl.h> |
| 4 | +#include <rcl/error_handling.h> |
| 5 | +#include <rclc/rclc.h> |
| 6 | +#include <rclc/executor.h> |
| 7 | +#include <std_msgs/msg/int32.h> |
| 8 | +#include <rmw_microros/rmw_microros.h> |
| 9 | + |
| 10 | +#include "pico/stdlib.h" |
| 11 | +#include "pico_uart_transports.h" |
| 12 | + |
| 13 | +const uint LED_PIN = 25; |
| 14 | + |
| 15 | +rcl_publisher_t publisher; |
| 16 | +std_msgs__msg__Int32 msg; |
| 17 | + |
| 18 | +void timer_callback(rcl_timer_t *timer, int64_t last_call_time) |
| 19 | +{ |
| 20 | + rcl_ret_t ret = rcl_publish(&publisher, &msg, NULL); |
| 21 | + msg.data++; |
| 22 | +} |
| 23 | + |
| 24 | +int main() |
| 25 | +{ |
| 26 | + rmw_uros_set_custom_transport( |
| 27 | + true, |
| 28 | + NULL, |
| 29 | + pico_serial_transport_open, |
| 30 | + pico_serial_transport_close, |
| 31 | + pico_serial_transport_write, |
| 32 | + pico_serial_transport_read |
| 33 | + ); |
| 34 | + |
| 35 | + gpio_init(LED_PIN); |
| 36 | + gpio_set_dir(LED_PIN, GPIO_OUT); |
| 37 | + |
| 38 | + rcl_timer_t timer; |
| 39 | + rcl_node_t node; |
| 40 | + rcl_allocator_t allocator; |
| 41 | + rclc_support_t support; |
| 42 | + rclc_executor_t executor; |
| 43 | + |
| 44 | + allocator = rcl_get_default_allocator(); |
| 45 | + |
| 46 | + // Wait for agent successful ping for 2 minutes. |
| 47 | + const int timeout_ms = 1000; |
| 48 | + const uint8_t attempts = 120; |
| 49 | + |
| 50 | + rcl_ret_t ret = rmw_uros_ping_agent(timeout_ms, attempts); |
| 51 | + |
| 52 | + if (ret != RCL_RET_OK) |
| 53 | + { |
| 54 | + // Unreachable agent, exiting program. |
| 55 | + return ret; |
| 56 | + } |
| 57 | + |
| 58 | + rclc_support_init(&support, 0, NULL, &allocator); |
| 59 | + |
| 60 | + rclc_node_init_default(&node, "pico_node", "", &support); |
| 61 | + rclc_publisher_init_default( |
| 62 | + &publisher, |
| 63 | + &node, |
| 64 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
| 65 | + "pico_publisher"); |
| 66 | + |
| 67 | + rclc_timer_init_default( |
| 68 | + &timer, |
| 69 | + &support, |
| 70 | + RCL_MS_TO_NS(1000), |
| 71 | + timer_callback); |
| 72 | + |
| 73 | + rclc_executor_init(&executor, &support.context, 1, &allocator); |
| 74 | + rclc_executor_add_timer(&executor, &timer); |
| 75 | + |
| 76 | + gpio_put(LED_PIN, 1); |
| 77 | + |
| 78 | + msg.data = 0; |
| 79 | + while (true) |
| 80 | + { |
| 81 | + rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)); |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
0 commit comments