Skip to content

Commit 4f29ff9

Browse files
Acuadros95pablogs9
andauthored
Add time sync example (#199)
* Add time sync example * Update ci.yml * Update examples/micro-ros_time_sync/micro-ros_time_sync.ino Co-authored-by: Pablo Garrido <[email protected]> * Update examples/micro-ros_time_sync/micro-ros_time_sync.ino Co-authored-by: Pablo Garrido <[email protected]> * Update micro-ros_time_sync.ino Include sync session on loop * Update ci.yml Change target board of time sync example * Update ci.yml Remove time lib install * Update micro-ros_time_sync.ino Cast intt64 as int for serial.println() Co-authored-by: Pablo Garrido <[email protected]>
1 parent 4545822 commit 4f29ff9

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
arduino-cli compile --fqbn OpenCR:OpenCR:OpenCR /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_subscriber -v
6262
arduino-cli compile --fqbn OpenCR:OpenCR:OpenCR /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_subscriber_twist -v
6363
arduino-cli compile --fqbn OpenCR:OpenCR:OpenCR /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_tf_publisher -v
64+
arduino-cli compile --fqbn teensy:avr:teensy41 /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_time_sync -v
6465
# Build one demo for each platform
6566
arduino-cli compile --fqbn teensy:avr:teensy31 /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_publisher -v
6667
arduino-cli compile --fqbn teensy:avr:teensy35 /github/home/Arduino/libraries/micro_ros_arduino/examples/micro-ros_publisher -v
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <micro_ros_arduino.h>
2+
3+
#include <stdio.h>
4+
#include <TimeLib.h>
5+
#include <rcl/rcl.h>
6+
#include <rcl/error_handling.h>
7+
#include <rclc/rclc.h>
8+
#include <rmw_uros/options.h>
9+
10+
rclc_support_t support;
11+
rcl_allocator_t allocator;
12+
13+
#define HWSERIAL Serial1
14+
15+
#define LED_PIN 13
16+
17+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
18+
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
19+
20+
const int timeout_ms = 1000;
21+
static int64_t time_ms;
22+
static time_t time_seconds;
23+
char time_str[25];
24+
25+
void error_loop(){
26+
while(1){
27+
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
28+
delay(100);
29+
}
30+
}
31+
32+
void setup() {
33+
set_microros_transports();
34+
HWSERIAL.begin(115200); // Configure debug serial
35+
pinMode(LED_PIN, OUTPUT);
36+
digitalWrite(LED_PIN, HIGH);
37+
38+
delay(2000);
39+
40+
allocator = rcl_get_default_allocator();
41+
42+
//create init_options
43+
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
44+
}
45+
46+
void loop() {
47+
// Synchronize time
48+
RCCHECK(rmw_uros_sync_session(timeout_ms));
49+
time_ms = rmw_uros_epoch_millis();
50+
51+
if (time_ms > 0)
52+
{
53+
time_seconds = time_ms/1000;
54+
setTime(time_seconds);
55+
sprintf(time_str, "%02d.%02d.%04d %02d:%02d:%02d.%03d", day(), month(), year(), hour(), minute(), second(), (uint) time_ms % 1000);
56+
57+
HWSERIAL.print("Agent date: ");
58+
HWSERIAL.println(time_str);
59+
}
60+
else
61+
{
62+
HWSERIAL.print("Session sync failed, error code: ");
63+
HWSERIAL.println((int) time_ms);
64+
}
65+
66+
delay(1001);
67+
}

0 commit comments

Comments
 (0)