micro-ROS / micro-ROS/micro_ros_stm32cubemx_utils

Cannot sync accurate time

Open
#131 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
275
Forks
100
PR merge metrics
No merged PRs in 30d

Description

# Detailed Description

I use `rmw_uros_sync_session()` to sync time with Micro-ROS-Agent. But when I use `rmw_uros_epoch_millis()` and `rmw_uros_epoch_nanos()` to get time, I got seconds starting from 0, not 170xxxxxx (Linux timestamp seconds) as expected.

# Thinking

This result was no surprise because I cannot find any API that can change local time of MCU, while there is an API to get time called `clock_gettime()` in `micro_ros_stm32cubemx_utils/extra_sources/microros_time.c`.

I want Micro-ROS to set local time that high accuracy hardware timer ( DWT, TIM or RTC ) holds, rather than FreeRTOS ticks.

it's said that `rmw_uros_sync_session()` uses NTP to sync time so it must has high accurate and less latency, but it cannot function properly, pity.

# Trials

My `clock_gettime()` is now modified to set RTC time. Micro-ROS subscribes `/time_reference` topic from Micro-ROS-Agent and transform timestamp to RTC Binary format. I use the following code to get current timestamp and it works well (superficially). This function seems to call `clock_gettime()` and get time from RTC. Actually this method introduces huge latency.

```c
rcutils_time_point_value_t current_time;
rcutils_ret_t ret;
ret = rcutils_system_time_now(¤t_time);
if(ret != RCUTILS_RET_OK)
printf("[ERROR]read systime failed!\r\n");
msg->header.stamp.sec = RCUTILS_NS_TO_S(current_time);
msg->header.stamp.nanosec = (uint32_t)current_time;
```

# Question
1. How does Micro-ROS set time?
2. Can maintainers expose some API to set local time?

Contributor guide

Open the contributing guide

Research direction

Start by reading micro_ros_stm32cubemx_utils/extra_sources/microros_time.c and the mentioned rmw_uros_sync_session(), rmw_uros_epoch_millis(), and rmw_uros_epoch_nanos() APIs. Compare their behavior with the reported RTC-based clock_gettime() approach and determine the intended time source and synchronization contract. Done would require a maintainer-approved way to synchronize accurate local MCU time without the reported latency.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.