feat(clock_source): Create new system clock source parameter in order to select the main clock source

This commit is contained in:
danielbuleandra authored and Alexander Lerach committed 2026-08-07 09:55:32 +02:00
1 parent 52cf24a954
commit 4bb943bf6d
13 files changed
+114 -13

No files matched your search

+5 -1
View File
@@ -56,6 +56,7 @@
#include <px4_platform_common/log.h>
#include <px4_platform_common/time.h>
#include <lib/systemlib/mavlink_log.h>
#include <lib/systemlib/system_time_source.h>
#include <uORB/topics/rtcm_data.h>
#include <uORB/topics/sensor_gps.h>
@@ -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);
}
}
+5 -1
View File
@@ -71,6 +71,7 @@
#include <lib/failure_injection/FailureInjection.hpp>
#include <lib/gnss/correction_framer.h>
#include <systemlib/system_time_source.h>
#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<long>(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
+12 -6
View File
@@ -45,6 +45,7 @@
#include <drivers/drv_hrt.h>
#include <systemlib/err.h>
#include <systemlib/system_time_source.h>
#include <mathlib/mathlib.h>
#include <matrix/math.hpp>
#include <lib/parameters/param.h>
@@ -560,15 +561,20 @@ void UavcanGnssBridge::process_fixx(const uavcan::ReceivedDataStructure<FixType>
// 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;
}
+1
View File
@@ -36,6 +36,7 @@ set(SRCS
mavlink_log.cpp
mavlink_log.h
px4_macros.h
system_time_source.h
)
if(${PX4_PLATFORM} STREQUAL "nuttx")
+15
View File
@@ -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
+49
View File
@@ -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 <cstdint>
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,
};
+7 -1
View File
@@ -43,6 +43,9 @@
#include <stdlib.h>
#include <lib/parameters/param.h>
#include <systemlib/system_time_source.h>
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;
@@ -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 {
@@ -51,6 +51,7 @@
#include <lib/drivers/rangefinder/PX4Rangefinder.hpp>
#include <lib/drivers/barometer/PX4Barometer.hpp>
#include <lib/geo/geo.h>
#include <systemlib/system_time_source.h>
#include <uORB/PublicationMulti.hpp>
#include <uORB/Subscription.hpp>
@@ -206,6 +207,7 @@ private:
(ParamInt<px4::params::SIM_GZ_EN_ODOM>) _sim_gz_en_odom,
(ParamInt<px4::params::SIM_GZ_EN_GPS>) _sim_gz_en_gps,
(ParamInt<px4::params::SIM_GZ_EN_IMU>) _sim_gz_en_imu,
(ParamInt<px4::params::SIM_GZ_EN_MAG>) _sim_gz_en_mag
(ParamInt<px4::params::SIM_GZ_EN_MAG>) _sim_gz_en_mag,
(ParamInt<px4::params::SYS_TIME_SRC>) _param_sys_time_src
)
};
+1 -1
View File
@@ -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;
@@ -37,6 +37,7 @@
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <px4_platform_common/time.h>
#include <systemlib/system_time_source.h>
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<px4::params::SYS_TIME_SRC>) _param_sys_time_src
)
};
@@ -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;
@@ -45,6 +45,7 @@
#include <lib/timesync/Timesync.hpp>
#include <lib/perf/perf_counter.h>
#include <systemlib/system_time_source.h>
#if defined(CONFIG_NET) || defined(__PX4_POSIX)
# define UXRCE_DDS_CLIENT_UDP 1
@@ -216,6 +217,7 @@ private:
(ParamInt<px4::params::UXRCE_DDS_SYNCT>) _param_uxrce_dds_synct,
(ParamInt<px4::params::UXRCE_DDS_TX_TO>) _param_uxrce_dds_tx_to,
(ParamInt<px4::params::UXRCE_DDS_RX_TO>) _param_uxrce_dds_rx_to,
(ParamInt<px4::params::UXRCE_DDS_FLCTRL>) _param_uxrce_dds_flctrl
(ParamInt<px4::params::UXRCE_DDS_FLCTRL>) _param_uxrce_dds_flctrl,
(ParamInt<px4::params::SYS_TIME_SRC>) _param_sys_time_src
)
};