@@ -18,37 +18,31 @@ rcl_timer_t timer;
1818
1919#define LED_PIN 13
2020
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+
3724
38- void error_loop ()
39- {
40- while (1 )
41- {
25+ void error_loop (){
26+ while (1 ){
4227 digitalWrite (LED_PIN, !digitalRead (LED_PIN));
4328 delay (100 );
4429 }
4530}
4631
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+ }
5140
41+ void setup () {
42+
43+ pinMode (LED_PIN, OUTPUT);
44+ digitalWrite (LED_PIN, HIGH);
45+
5246 delay (2000 );
5347
5448 allocator = rcl_get_default_allocator ();
@@ -62,16 +56,32 @@ void setup()
6256
6357 // create publisher
6458 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+
6981 msg.data = 0 ;
7082}
7183
72- void loop ()
73- {
84+ void loop () {
7485 delay (100 );
75- RCSOFTCHECK (rcl_publish (&publisher, &msg, NULL ));
76- msg.data ++;
86+ RCSOFTCHECK (rclc_executor_spin_some (&executor, RCL_MS_TO_NS (100 )));
7787}
0 commit comments