Skip to content

Commit 789df02

Browse files
peterbarkertridge
authored andcommitted
AP_RTC: allow time to shift forward when disarmed
1 parent ed37ee8 commit 789df02

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

libraries/AP_RTC/AP_RTC.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#include <GCS_MAVLink/GCS.h>
1010
#include <AP_Common/time.h>
1111

12+
#define DEBUG_RTC_SHIFT 1
13+
14+
#if DEBUG_RTC_SHIFT
15+
#include <AP_Logger/AP_Logger.h>
16+
#endif
17+
1218
extern const AP_HAL::HAL& hal;
1319

1420
AP_RTC::AP_RTC()
@@ -47,9 +53,19 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
4753
{
4854
const uint64_t oldest_acceptable_date_us = 1640995200ULL*1000*1000; // 2022-01-01 0:00
4955

50-
if (type >= rtc_source_type) {
51-
// e.g. system-time message when we've been set by the GPS
52-
return;
56+
// only allow time to be moved forward from the same sourcetype
57+
// while the vehicle is disarmed:
58+
if (hal.util->get_soft_armed()) {
59+
if (type >= rtc_source_type) {
60+
// e.g. system-time message when we've been set by the GPS
61+
return;
62+
}
63+
} else {
64+
// vehicle is disarmed; accept (e.g.) GPS time source updates
65+
if (type > rtc_source_type) {
66+
// e.g. system-time message when we've been set by the GPS
67+
return;
68+
}
5369
}
5470

5571
// check it's from an allowed sources:
@@ -70,6 +86,11 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
7086
}
7187
WITH_SEMAPHORE(rsem);
7288

89+
#if DEBUG_RTC_SHIFT
90+
uint64_t old_utc = 0;
91+
UNUSED_RESULT(get_utc_usec(old_utc));
92+
#endif
93+
7394
rtc_shift = tmp;
7495

7596
// update hardware clock:
@@ -83,6 +104,32 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
83104
// update signing timestamp
84105
GCS_MAVLINK::update_signing_timestamp(time_utc_usec);
85106
#endif
107+
108+
#if DEBUG_RTC_SHIFT
109+
uint64_t new_utc = 0;
110+
UNUSED_RESULT(get_utc_usec(new_utc));
111+
if (old_utc != new_utc) {
112+
if (AP::logger().should_log(0xFFFF)){
113+
// log to AP_Logger
114+
// @LoggerMessage: RTC
115+
// @Description: Information about RTC clock resets
116+
// @Field: TimeUS: Time since system startup
117+
// @Field: old: old time
118+
// @Field: new: new time
119+
// @Field: in: new argument time
120+
AP::logger().WriteStreaming(
121+
"RTC",
122+
"TimeUS,old_utc,new_utc",
123+
"sss",
124+
"FFF",
125+
"QQQ",
126+
AP_HAL::micros64(),
127+
old_utc,
128+
new_utc
129+
);
130+
}
131+
}
132+
#endif
86133
}
87134

88135
bool AP_RTC::get_utc_usec(uint64_t &usec) const

0 commit comments

Comments
 (0)