@@ -18,37 +18,31 @@ rcl_timer_t timer;
18
18
19
19
#define LED_PIN 13
20
20
21
- #define RCCHECK (fn ) \
22
- { \
23
- rcl_ret_t temp_rc = fn; \
24
- if ((temp_rc != RCL_RET_OK)) \
25
- { \
26
- error_loop (); \
27
- } \
28
- }
29
- #define RCSOFTCHECK (fn ) \
30
- { \
31
- rcl_ret_t temp_rc = fn; \
32
- if ((temp_rc != RCL_RET_OK)) \
33
- { \
34
- error_loop (); \
35
- } \
36
- }
21
+ #define RCCHECK (fn ) { rcl_ret_t temp_rc = fn; if ((temp_rc != RCL_RET_OK)){error_loop ();}}
22
+ #define RCSOFTCHECK (fn ) { rcl_ret_t temp_rc = fn; if ((temp_rc != RCL_RET_OK)){}}
23
+
37
24
38
- void error_loop ()
39
- {
40
- while (1 )
41
- {
25
+ void error_loop (){
26
+ while (1 ){
42
27
digitalWrite (LED_PIN, !digitalRead (LED_PIN));
43
28
delay (100 );
44
29
}
45
30
}
46
31
47
- void setup ()
48
- {
49
- pinMode (LED_PIN, OUTPUT);
50
- digitalWrite (LED_PIN, HIGH);
32
+ void timer_callback (rcl_timer_t * timer, int64_t last_call_time)
33
+ {
34
+ RCLC_UNUSED (last_call_time);
35
+ if (timer != NULL ) {
36
+ RCSOFTCHECK (rcl_publish (&publisher, &msg, NULL ));
37
+ msg.data ++;
38
+ }
39
+ }
51
40
41
+ void setup () {
42
+
43
+ pinMode (LED_PIN, OUTPUT);
44
+ digitalWrite (LED_PIN, HIGH);
45
+
52
46
delay (2000 );
53
47
54
48
allocator = rcl_get_default_allocator ();
@@ -62,16 +56,32 @@ void setup()
62
56
63
57
// create publisher
64
58
RCCHECK (rclc_publisher_init_default (
65
- &publisher,
66
- &node,
67
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int32),
68
- " micro_ros_arduino_node_publisher" ));
59
+ &publisher,
60
+ &node,
61
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int32),
62
+ " micro_ros_arduino_node_publisher" ));
63
+
64
+ // create timer,
65
+ timer = rcl_get_zero_initialized_timer ();
66
+ const unsigned int timer_timeout = 1000 ;
67
+ RCCHECK (rclc_timer_init_default (
68
+ &timer,
69
+ &support,
70
+ RCL_MS_TO_NS (timer_timeout),
71
+ timer_callback));
72
+
73
+ // create executor
74
+ executor = rclc_executor_get_zero_initialized_executor ();
75
+ RCCHECK (rclc_executor_init (&executor, &support.context , 1 , &allocator));
76
+
77
+ unsigned int rcl_wait_timeout = 100 ; // in ms
78
+ RCCHECK (rclc_executor_set_timeout (&executor, RCL_MS_TO_NS (rcl_wait_timeout)));
79
+ RCCHECK (rclc_executor_add_timer (&executor, &timer));
80
+
69
81
msg.data = 0 ;
70
82
}
71
83
72
- void loop ()
73
- {
84
+ void loop () {
74
85
delay (100 );
75
- RCSOFTCHECK (rcl_publish (&publisher, &msg, NULL ));
76
- msg.data ++;
86
+ RCSOFTCHECK (rclc_executor_spin_some (&executor, RCL_MS_TO_NS (100 )));
77
87
}
0 commit comments