Skip to content

Commit f76ded8

Browse files
committed
Add working example
1 parent d0533d4 commit f76ded8

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed

examples/micro-ros_publisher/micro-ros_publisher.ino

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,37 @@ rcl_timer_t timer;
1818

1919
#define LED_PIN 13
2020

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)){error_loop();}}
23-
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+
}
2437

25-
void error_loop(){
26-
while(1){
38+
void error_loop()
39+
{
40+
while (1)
41+
{
2742
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
2843
delay(100);
2944
}
3045
}
3146

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-
}
40-
41-
void setup() {
42-
47+
void setup()
48+
{
4349
pinMode(LED_PIN, OUTPUT);
44-
digitalWrite(LED_PIN, HIGH);
45-
50+
digitalWrite(LED_PIN, HIGH);
51+
4652
delay(2000);
4753

4854
allocator = rcl_get_default_allocator();
@@ -56,32 +62,16 @@ void setup() {
5662

5763
// create publisher
5864
RCCHECK(rclc_publisher_init_default(
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-
65+
&publisher,
66+
&node,
67+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
68+
"micro_ros_arduino_node_publisher"));
8169
msg.data = 0;
8270
}
8371

84-
void loop() {
72+
void loop()
73+
{
8574
delay(100);
86-
RCCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
75+
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
76+
msg.data++;
8777
}

examples/micro-ros_publisher/transport.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,7 @@ extern "C"
4444
// Place here your reading bytes platform code
4545
// Return number of bytes read (max bytes: len)
4646
(void)errcode;
47-
uint32_t start_time = micros() * 1000;
48-
size_t readed = 0;
49-
50-
while ((readed < len) && ((micros() * 1000) - start_time) < (uint32_t)timeout)
51-
{
52-
if (SerialUSB.available())
53-
{
54-
buf[readed++] = SerialUSB.read();
55-
}
56-
}
57-
58-
return readed;
47+
SerialUSB.setTimeout(timeout);
48+
return SerialUSB.readBytes((char *)buf, len);
5949
}
60-
}
50+
}

0 commit comments

Comments
 (0)