From 4bb943bf6d8ba62557fec22618b5bd925feaabf5 Mon Sep 17 00:00:00 2001 From: danielbuleandra Date: Tue, 4 Aug 2026 17:16:56 +0200 Subject: [PATCH] feat(clock_source): Create new system clock source parameter in order to select the main clock source --- src/drivers/gnss/septentrio/septentrio.cpp | 6 ++- src/drivers/gps/gps.cpp | 6 ++- src/drivers/uavcan/sensors/gnss.cpp | 18 ++++--- src/lib/systemlib/CMakeLists.txt | 1 + src/lib/systemlib/system_params.yaml | 15 ++++++ src/lib/systemlib/system_time_source.h | 49 +++++++++++++++++++ src/modules/mavlink/mavlink_timesync.cpp | 8 ++- src/modules/simulation/gz_bridge/GZBridge.cpp | 5 +- src/modules/simulation/gz_bridge/GZBridge.hpp | 4 +- src/modules/time_persistor/TimePersistor.cpp | 2 +- src/modules/time_persistor/TimePersistor.hpp | 5 ++ .../uxrce_dds_client/uxrce_dds_client.cpp | 4 ++ .../uxrce_dds_client/uxrce_dds_client.h | 4 +- 13 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 src/lib/systemlib/system_time_source.h diff --git a/src/drivers/gnss/septentrio/septentrio.cpp b/src/drivers/gnss/septentrio/septentrio.cpp index 1144de5828e..df62c2fb198 100644 --- a/src/drivers/gnss/septentrio/septentrio.cpp +++ b/src/drivers/gnss/septentrio/septentrio.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include @@ -1893,7 +1894,10 @@ bool SeptentrioDriver::clock_needs_update(timespec real_time) void SeptentrioDriver::set_clock(timespec rtc_gps_time) { - if (clock_needs_update(rtc_gps_time)) { + int32_t sys_time_src = 0; + get_parameter("SYS_TIME_SRC", &sys_time_src); + + if (clock_needs_update(rtc_gps_time) && (sys_time_src & SYS_TIME_SRC_GPS)) { px4_clock_settime(CLOCK_REALTIME, &rtc_gps_time); } } diff --git a/src/drivers/gps/gps.cpp b/src/drivers/gps/gps.cpp index 313f444a09f..9e5e4d52da2 100644 --- a/src/drivers/gps/gps.cpp +++ b/src/drivers/gps/gps.cpp @@ -71,6 +71,7 @@ #include #include +#include #include "devices/src/gps_helper.h" @@ -569,7 +570,10 @@ int GPS::callback(GPSCallbackType type, void *data1, int data2, void *user) timespec rtc_gps_time = *(timespec *)data1; int drift_time = abs(static_cast(rtc_system_time.tv_sec - rtc_gps_time.tv_sec)); - if (drift_time >= SET_CLOCK_DRIFT_TIME_S) { + int32_t sys_time_src = 0; + param_get(param_find("SYS_TIME_SRC"), &sys_time_src); + + if (drift_time >= SET_CLOCK_DRIFT_TIME_S && (sys_time_src & SYS_TIME_SRC_GPS)) { // as of 2021 setting the time on Nuttx temporarily pauses interrupts // so only set the time if it is very wrong. // TODO: clock slewing of the RTC for small time differences diff --git a/src/drivers/uavcan/sensors/gnss.cpp b/src/drivers/uavcan/sensors/gnss.cpp index 45966903f27..0e5c75e00b8 100644 --- a/src/drivers/uavcan/sensors/gnss.cpp +++ b/src/drivers/uavcan/sensors/gnss.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -560,15 +561,20 @@ void UavcanGnssBridge::process_fixx(const uavcan::ReceivedDataStructure // If we haven't already done so, set the system clock using GPS data if (sensor_gps.time_utc_usec != 0 && (fix_type >= sensor_gps_s::FIX_TYPE_2D) && !_system_clock_set) { - timespec ts{}; + int32_t sys_time_src = 0; + param_get(param_find("SYS_TIME_SRC"), &sys_time_src); - // get the whole microseconds - ts.tv_sec = sensor_gps.time_utc_usec / 1000000ULL; + if (sys_time_src & SYS_TIME_SRC_GPS) { + timespec ts{}; - // get the remainder microseconds and convert to nanoseconds - ts.tv_nsec = (sensor_gps.time_utc_usec % 1000000ULL) * 1000; + // get the whole microseconds + ts.tv_sec = sensor_gps.time_utc_usec / 1000000ULL; - px4_clock_settime(CLOCK_REALTIME, &ts); + // get the remainder microseconds and convert to nanoseconds + ts.tv_nsec = (sensor_gps.time_utc_usec % 1000000ULL) * 1000; + + px4_clock_settime(CLOCK_REALTIME, &ts); + } _system_clock_set = true; } diff --git a/src/lib/systemlib/CMakeLists.txt b/src/lib/systemlib/CMakeLists.txt index 3e10c5e8920..d198ec7e259 100644 --- a/src/lib/systemlib/CMakeLists.txt +++ b/src/lib/systemlib/CMakeLists.txt @@ -36,6 +36,7 @@ set(SRCS mavlink_log.cpp mavlink_log.h px4_macros.h + system_time_source.h ) if(${PX4_PLATFORM} STREQUAL "nuttx") diff --git a/src/lib/systemlib/system_params.yaml b/src/lib/systemlib/system_params.yaml index e5fd9af18ce..21f983497f8 100644 --- a/src/lib/systemlib/system_params.yaml +++ b/src/lib/systemlib/system_params.yaml @@ -241,3 +241,18 @@ parameters: type: boolean default: 0 reboot_required: true + SYS_TIME_SRC: + description: + short: Select the source of the system time. + long: |- + This parameter selects the source allowed to set the system time. + The default value enables all sources. + type: bitmask + bit: + 0: GPS time + 1: MAVLink time + 2: Software RTC time + 3: DDS time + 4: Input simulation time + default: 31 + reboot_required: true diff --git a/src/lib/systemlib/system_time_source.h b/src/lib/systemlib/system_time_source.h new file mode 100644 index 00000000000..da8eefab65b --- /dev/null +++ b/src/lib/systemlib/system_time_source.h @@ -0,0 +1,49 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file system_time_source.h + * Bit values for the SYS_TIME_SRC bitmask parameter. + */ + +#pragma once + +#include + +enum SystemTimeSource : int32_t { + SYS_TIME_SRC_GPS = 1 << 0, + SYS_TIME_SRC_MAVLINK = 1 << 1, + SYS_TIME_SRC_SOFT_RTC = 1 << 2, + SYS_TIME_SRC_DDS = 1 << 3, + SYS_TIME_SRC_SIMULATOR = 1 << 4, +}; diff --git a/src/modules/mavlink/mavlink_timesync.cpp b/src/modules/mavlink/mavlink_timesync.cpp index 18746283dc0..820a4b7dacd 100644 --- a/src/modules/mavlink/mavlink_timesync.cpp +++ b/src/modules/mavlink/mavlink_timesync.cpp @@ -43,6 +43,9 @@ #include +#include +#include + MavlinkTimesync::MavlinkTimesync(Mavlink &mavlink) : _mavlink(mavlink) { @@ -94,7 +97,10 @@ MavlinkTimesync::handle_message(const mavlink_message_t *msg) time_t remote_time = time.time_unix_usec / 1000000; bool update = (remote_time > PX4_EPOCH_SECS) && (remote_time > local_time + seconds_behind); - if (update) { + int32_t sys_time_src = 0; + param_get(param_find("SYS_TIME_SRC"), &sys_time_src); + + if (update && (sys_time_src & SYS_TIME_SRC_MAVLINK)) { PX4_INFO("Setting system clock from SYSTEM_TIME sent by %d/%d", msg->sysid, msg->compid); tv.tv_sec = time.time_unix_usec / 1000000ULL; tv.tv_nsec = (time.time_unix_usec % 1000000ULL) * 1000ULL; diff --git a/src/modules/simulation/gz_bridge/GZBridge.cpp b/src/modules/simulation/gz_bridge/GZBridge.cpp index 21279f04a8a..8e917e4afc2 100644 --- a/src/modules/simulation/gz_bridge/GZBridge.cpp +++ b/src/modules/simulation/gz_bridge/GZBridge.cpp @@ -347,7 +347,10 @@ void GZBridge::clockCallback(const gz::msgs::Clock &msg) if (!_realtime_clock_set) { // Set initial real time clock at startup - px4_clock_settime(CLOCK_REALTIME, &ts); + if (_param_sys_time_src.get() & SYS_TIME_SRC_SIMULATOR) { + px4_clock_settime(CLOCK_REALTIME, &ts); + } + _realtime_clock_set = true; } else { diff --git a/src/modules/simulation/gz_bridge/GZBridge.hpp b/src/modules/simulation/gz_bridge/GZBridge.hpp index 93183ef1f79..31c892d539d 100644 --- a/src/modules/simulation/gz_bridge/GZBridge.hpp +++ b/src/modules/simulation/gz_bridge/GZBridge.hpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include @@ -206,6 +207,7 @@ private: (ParamInt) _sim_gz_en_odom, (ParamInt) _sim_gz_en_gps, (ParamInt) _sim_gz_en_imu, - (ParamInt) _sim_gz_en_mag + (ParamInt) _sim_gz_en_mag, + (ParamInt) _param_sys_time_src ) }; diff --git a/src/modules/time_persistor/TimePersistor.cpp b/src/modules/time_persistor/TimePersistor.cpp index 5136ed79ad1..92cb9e1ecaa 100644 --- a/src/modules/time_persistor/TimePersistor.cpp +++ b/src/modules/time_persistor/TimePersistor.cpp @@ -142,7 +142,7 @@ int TimePersistor::init() time_t t; - if (read_time(&t) == PX4_OK) { + if (read_time(&t) == PX4_OK && (_param_sys_time_src.get() & SYS_TIME_SRC_SOFT_RTC)) { struct timespec ts; ts.tv_sec = t; ts.tv_nsec = 0; diff --git a/src/modules/time_persistor/TimePersistor.hpp b/src/modules/time_persistor/TimePersistor.hpp index 273bb3f0acb..75cd91b183a 100644 --- a/src/modules/time_persistor/TimePersistor.hpp +++ b/src/modules/time_persistor/TimePersistor.hpp @@ -37,6 +37,7 @@ #include #include #include +#include static constexpr const auto TIME_FILE_PATH = PX4_STORAGEDIR "/time_save.bin"; @@ -70,4 +71,8 @@ private: int write_time(const time_t time); FILE *_file = 0; + + DEFINE_PARAMETERS( + (ParamInt) _param_sys_time_src + ) }; diff --git a/src/modules/uxrce_dds_client/uxrce_dds_client.cpp b/src/modules/uxrce_dds_client/uxrce_dds_client.cpp index 44e66a7c947..97f6254f6c2 100644 --- a/src/modules/uxrce_dds_client/uxrce_dds_client.cpp +++ b/src/modules/uxrce_dds_client/uxrce_dds_client.cpp @@ -609,6 +609,10 @@ void UxrceddsClient::syncSystemClock(uxrSession *session) return; } + if (!(_param_sys_time_src.get() & SYS_TIME_SRC_DDS)) { + return; + } + ts.tv_sec = agent_utc / 1_s; ts.tv_nsec = (agent_utc % 1_s) * 1000; diff --git a/src/modules/uxrce_dds_client/uxrce_dds_client.h b/src/modules/uxrce_dds_client/uxrce_dds_client.h index a7c1a5a13db..b5291a898f4 100644 --- a/src/modules/uxrce_dds_client/uxrce_dds_client.h +++ b/src/modules/uxrce_dds_client/uxrce_dds_client.h @@ -45,6 +45,7 @@ #include #include +#include #if defined(CONFIG_NET) || defined(__PX4_POSIX) # define UXRCE_DDS_CLIENT_UDP 1 @@ -216,6 +217,7 @@ private: (ParamInt) _param_uxrce_dds_synct, (ParamInt) _param_uxrce_dds_tx_to, (ParamInt) _param_uxrce_dds_rx_to, - (ParamInt) _param_uxrce_dds_flctrl + (ParamInt) _param_uxrce_dds_flctrl, + (ParamInt) _param_sys_time_src ) };