Recovering UTC timestamps from ULog without sensor_gps (#25534)

* Added overloaded get_log_time util for fractional seconds

* Added write_info implementation for double info types

* Save boot time to .ulg via info message key boot_time_utc

* Changed time type from double to uint64_t, formatted

* Fixing get_log_time function doc
This commit is contained in:
Rowan Dempster
2025-09-15 16:18:26 -08:00
committed by GitHub
parent d3f912ad25
commit d205d11c3d
4 changed files with 38 additions and 9 deletions
+11
View File
@@ -1990,6 +1990,11 @@ void Logger::write_info(LogType type, const char *name, uint32_t value)
write_info_template<uint32_t>(type, name, value, "uint32_t");
}
void Logger::write_info(LogType type, const char *name, uint64_t value)
{
write_info_template<uint64_t>(type, name, value, "uint64_t");
}
template<typename T>
void Logger::write_info_template(LogType type, const char *name, T value, const char *type_str)
@@ -2122,6 +2127,12 @@ void Logger::write_version(LogType type)
write_info(type, "time_ref_utc", _param_sdlog_utc_offset.get() * 60);
uint64_t boot_time_utc_us;
if (util::get_log_time(boot_time_utc_us, _param_sdlog_utc_offset.get() * 60, true)) {
write_info(type, "boot_time_utc_us", boot_time_utc_us);
}
if (_replay_file_name) {
write_info(type, "replay", _replay_file_name);
}
+1
View File
@@ -267,6 +267,7 @@ private:
void write_info_multiple(LogType type, const char *name, int fd);
void write_info(LogType type, const char *name, int32_t value);
void write_info(LogType type, const char *name, uint32_t value);
void write_info(LogType type, const char *name, uint64_t value);
/** generic common template method for write_info variants */
template<typename T>
+16 -9
View File
@@ -72,20 +72,19 @@ bool file_exist(const char *filename)
return stat(filename, &buffer) == 0;
}
bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time)
bool get_log_time(uint64_t &utc_time_usec, int utc_offset_sec, bool boot_time)
{
uORB::Subscription vehicle_gps_position_sub{ORB_ID(vehicle_gps_position)};
time_t utc_time_sec;
bool use_clock_time = true;
/* Get the latest GPS publication */
sensor_gps_s gps_pos;
if (vehicle_gps_position_sub.copy(&gps_pos)) {
utc_time_sec = gps_pos.time_utc_usec / 1e6;
utc_time_usec = gps_pos.time_utc_usec;
if (gps_pos.fix_type >= 2 && utc_time_sec >= GPS_EPOCH_SECS) {
if (gps_pos.fix_type >= 2 && utc_time_usec >= (uint64_t) GPS_EPOCH_SECS * 1000000ULL) {
use_clock_time = false;
}
}
@@ -94,22 +93,30 @@ bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time)
/* take clock time if there's no fix (yet) */
struct timespec ts = {};
px4_clock_gettime(CLOCK_REALTIME, &ts);
utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9);
utc_time_usec = ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL;
if (utc_time_sec < GPS_EPOCH_SECS) {
if (utc_time_usec < (uint64_t) GPS_EPOCH_SECS * 1000000ULL) {
return false;
}
}
/* strip the time elapsed since boot */
if (boot_time) {
utc_time_sec -= hrt_absolute_time() / 1e6;
utc_time_usec -= hrt_absolute_time();
}
/* apply utc offset */
utc_time_sec += utc_offset_sec;
utc_time_usec += (int64_t) utc_offset_sec * 1000000LL;
return gmtime_r(&utc_time_sec, tt) != nullptr;
return true;
}
bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time)
{
uint64_t utc_time_usec;
bool result = get_log_time(utc_time_usec, utc_offset_sec, boot_time);
time_t utc_time_sec = static_cast<time_t>(utc_time_usec / 1000000ULL);
return result && gmtime_r(&utc_time_sec, tt) != nullptr;
}
int check_free_space(const char *log_root_dir, int32_t max_log_dirs_to_keep, orb_advert_t &mavlink_log_pub,
+10
View File
@@ -75,6 +75,16 @@ bool file_exist(const char *filename);
int check_free_space(const char *log_root_dir, int32_t max_log_dirs_to_keep, orb_advert_t &mavlink_log_pub,
int &sess_dir_index);
/**
* Utility for fetching UTC time in microseconds from sensor_gps or CLOCK_REALTIME
* @param utc_time_usec returned microseconds
* @param utc_offset_sec UTC time offset [s]
* @param boot_time use time when booted instead of current time
* @return true on success, false otherwise (eg. if no gps)
*/
bool get_log_time(uint64_t &utc_time_usec, int utc_offset_sec, bool boot_time);
/**
* Get the time for log file name
* @param tt returned time