Skip to content

Commit 19a0bb6

Browse files
committed
AP_RTC: allow time to shift forward when disarmed
1 parent cf7b01d commit 19a0bb6

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
@@ -5,6 +5,12 @@
55
#include <GCS_MAVLink/GCS.h>
66
#include <time.h>
77

8+
#define DEBUG_RTC_SHIFT 1
9+
10+
#if DEBUG_RTC_SHIFT
11+
#include <AP_Logger/AP_Logger.h>
12+
#endif
13+
814
extern const AP_HAL::HAL& hal;
915

1016
AP_RTC::AP_RTC()
@@ -43,9 +49,19 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
4349
{
4450
const uint64_t oldest_acceptable_date_us = 1640995200ULL*1000*1000; // 2022-01-01 0:00
4551

46-
if (type >= rtc_source_type) {
47-
// e.g. system-time message when we've been set by the GPS
48-
return;
52+
// only allow time to be moved forward from the same sourcetype
53+
// while the vehicle is disarmed:
54+
if (hal.util->get_soft_armed()) {
55+
if (type >= rtc_source_type) {
56+
// e.g. system-time message when we've been set by the GPS
57+
return;
58+
}
59+
} else {
60+
// vehicle is disarmed; accept (e.g.) GPS time source updates
61+
if (type > rtc_source_type) {
62+
// e.g. system-time message when we've been set by the GPS
63+
return;
64+
}
4965
}
5066

5167
// check it's from an allowed sources:
@@ -66,6 +82,11 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
6682
}
6783
WITH_SEMAPHORE(rsem);
6884

85+
#if DEBUG_RTC_SHIFT
86+
uint64_t old_utc = 0;
87+
UNUSED_RESULT(get_utc_usec(old_utc));
88+
#endif
89+
6990
rtc_shift = tmp;
7091

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

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

0 commit comments

Comments
 (0)