|
| 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