Skip to content

Commit 0828d38

Browse files
amfernAcuadros95pablogs9
authored
timesync epoch helpers (#119)
* timesync epoch helpers Signed-off-by: Ilya Guterman <amfernusus@gmail.com> * Update test_rmw.cpp Fix millis to seconds conversion * Allow failure on non critital CI sections * Update rmw_uros_options.c Add session sync check on rmw_uros_epoch * Update rmw_uros_options.c Fix session variable typo * Update rmw_uros_options.c Fix uncrustify * Update options.h Update .h with return info Co-authored-by: Antonio Cuadros <49162117+Acuadros95@users.noreply.github.com> Co-authored-by: Pablo Garrido <pablogs9@gmail.com>
1 parent 19a8dd0 commit 0828d38

4 files changed

Lines changed: 70 additions & 14 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
find src test include \( -name "*.cpp" -o -name "*.c" -o -name "*.h" -o -name "*.hpp" \) -exec uncrustify -c uncrustify.cfg --check {} +
5757
5858
- name: Static memory
59+
continue-on-error: true
5960
if: github.event_name == 'pull_request'
6061
uses: machine-learning-apps/pr-comment@master
6162
env:

rmw_microxrcedds_c/include/rmw_uros/options.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,30 @@ typedef struct rmw_uxrce_transport_params_t
5757
uint32_t client_key;
5858
} rmw_uxrce_transport_params_t;
5959

60+
61+
/**
62+
* \brief Returns the epoch time in milliseconds taking into account the offset computed during the time synchronization.
63+
* \return epoch time in milliseconds.
64+
* \return 0 if session is not initialized.
65+
* \return -1 if session time is not synchronized.
66+
*/
67+
int64_t rmw_uros_epoch_millis();
68+
69+
/**
70+
* \brief Returns the epoch time in nanoseconds taking into account the offset computed during the time synchronization.
71+
* \return epoch time in nanoseconds.
72+
* \return 0 if session is not initialized.
73+
* \return -1 if session time is not synchronized.
74+
*/
75+
int64_t rmw_uros_epoch_nanos();
76+
6077
/**
6178
* \brief Synchronizes the session time using the NTP protocol.
62-
* \param[in, out] result Variable to update with micro-ROS Agent POSIX timestamp in nanoseconds.
6379
* \param[in] timeout_ms The waiting time in milliseconds.
6480
* \return RMW_RET_OK when success.
65-
* \return RMW_RET_INVALID_ARGUMENT If no session is running or the synchronization fails.
81+
* \return RMW_RET_ERROR If no session is running or the synchronization fails.
6682
*/
6783
rmw_ret_t rmw_uros_sync_session(
68-
int64_t* result,
6984
const int timeout_ms);
7085

7186
/**

rmw_microxrcedds_c/src/rmw_uros_options.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,49 @@
2828
#include <uxr/client/util/ping.h>
2929
#include <uxr/client/util/time.h>
3030

31+
int64_t rmw_uros_epoch_millis()
32+
{
33+
// Check session is initialized
34+
if (NULL == session_memory.allocateditems)
35+
{
36+
RMW_SET_ERROR_MSG("Uninitialized session.");
37+
return 0;
38+
}
39+
40+
rmw_uxrce_mempool_item_t* item = session_memory.allocateditems;
41+
rmw_context_impl_t* context = (rmw_context_impl_t*)item->data;
42+
43+
if (!context->session.synchronized)
44+
{
45+
RMW_SET_ERROR_MSG("Session time not synchronized");
46+
return -1;
47+
}
48+
49+
return uxr_epoch_millis(&context->session);
50+
}
51+
52+
int64_t rmw_uros_epoch_nanos()
53+
{
54+
// Check session is initialized
55+
if (NULL == session_memory.allocateditems)
56+
{
57+
RMW_SET_ERROR_MSG("Uninitialized session.");
58+
return 0;
59+
}
60+
61+
rmw_uxrce_mempool_item_t* item = session_memory.allocateditems;
62+
rmw_context_impl_t* context = (rmw_context_impl_t*)item->data;
63+
64+
if (!context->session.synchronized)
65+
{
66+
RMW_SET_ERROR_MSG("Session time not synchronized");
67+
return -1;
68+
}
69+
70+
return uxr_epoch_nanos(&context->session);
71+
}
72+
3173
rmw_ret_t rmw_uros_sync_session(
32-
int64_t* result,
3374
const int timeout_ms)
3475
{
3576
rmw_ret_t ret = RMW_RET_OK;
@@ -43,12 +84,9 @@ rmw_ret_t rmw_uros_sync_session(
4384

4485
rmw_uxrce_mempool_item_t* item = session_memory.allocateditems;
4586
rmw_context_impl_t* context = (rmw_context_impl_t*)item->data;
87+
context->session.synchronized = false;
4688

47-
if (uxr_sync_session(&context->session, timeout_ms))
48-
{
49-
*result = uxr_nanos() - context->session.time_offset;
50-
}
51-
else
89+
if (!uxr_sync_session(&context->session, timeout_ms))
5290
{
5391
RMW_SET_ERROR_MSG("Time synchronization failed.");
5492
return RMW_RET_ERROR;

rmw_microxrcedds_c/test/test_rmw.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ TEST(rmw_microxrcedds, sync_session)
7373
{
7474
rmw_context_t test_context = rmw_get_zero_initialized_context();
7575
rmw_init_options_t test_options = rmw_get_zero_initialized_init_options();
76-
int64_t result_ns = 0;
7776

7877
ASSERT_EQ(rmw_init_options_init(&test_options, rcutils_get_default_allocator()), RMW_RET_OK);
7978
ASSERT_EQ(rmw_init(&test_options, &test_context), RMW_RET_OK);
8079

8180
std::time_t timestamp_seconds = std::time(nullptr);
82-
ASSERT_EQ(rmw_uros_sync_session(&result_ns, 500), RMW_RET_OK);
81+
ASSERT_EQ(rmw_uros_sync_session(500), RMW_RET_OK);
8382

84-
int64_t time_diff = abs(result_ns / 1000000000 - timestamp_seconds);
85-
ASSERT_LE(time_diff, 5);
83+
int64_t time_diff_ns = abs(rmw_uros_epoch_nanos() / 1000000000 - timestamp_seconds);
84+
ASSERT_LE(time_diff_ns, 5);
85+
86+
int64_t time_diff_ms = abs(rmw_uros_epoch_millis() / 1000 - timestamp_seconds);
87+
ASSERT_LE(time_diff_ms, 5);
8688

8789
ASSERT_EQ(rmw_shutdown(&test_context), RMW_RET_OK);
88-
}
90+
}

0 commit comments

Comments
 (0)