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