Skip to content

Commit de5b65b

Browse files
committed
Merge pull request #2191 from PX4/sdlog2_gpstime
Give user a choice which time source to pick
2 parents 635b7fa + 937289a commit de5b65b

File tree

1 file changed

+69
-20
lines changed

1 file changed

+69
-20
lines changed

src/modules/sdlog2/sdlog2.c

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ PARAM_DEFINE_INT32(SDLOG_RATE, -1);
144144
*/
145145
PARAM_DEFINE_INT32(SDLOG_EXT, -1);
146146

147+
/**
148+
* Use timestamps only if GPS 3D fix is available
149+
*
150+
* A value of 1 constrains the log folder creation
151+
* to only use the time stamp if a 3D GPS lock is
152+
* present.
153+
*
154+
* @min 0
155+
* @max 1
156+
* @group SD Logging
157+
*/
158+
PARAM_DEFINE_INT32(SDLOG_GPSTIME, 0);
159+
147160
#define LOGBUFFER_WRITE_AND_COUNT(_msg) if (logbuffer_write(&lb, &log_msg, LOG_PACKET_SIZE(_msg))) { \
148161
log_msgs_written++; \
149162
} else { \
@@ -163,6 +176,7 @@ static const int MAX_WRITE_CHUNK = 512;
163176
static const int MIN_BYTES_TO_WRITE = 512;
164177

165178
static bool _extended_logging = false;
179+
static bool _gpstime_only = false;
166180

167181
#define MOUNTPOINT "/fs/microsd"
168182
static const char *mountpoint = MOUNTPOINT;
@@ -272,6 +286,11 @@ static void handle_status(struct vehicle_status_s *cmd);
272286
*/
273287
static int create_log_dir(void);
274288

289+
/**
290+
* Get the time struct from the currently preferred time source
291+
*/
292+
static bool get_log_time_utc_tt(struct tm *tt, bool boot_time);
293+
275294
/**
276295
* Select first free log file name and open it.
277296
*/
@@ -372,20 +391,41 @@ int sdlog2_main(int argc, char *argv[])
372391
exit(1);
373392
}
374393

394+
bool get_log_time_utc_tt(struct tm *tt, bool boot_time) {
395+
struct timespec ts;
396+
clock_gettime(CLOCK_REALTIME, &ts);
397+
/* use RTC time for log file naming, e.g. /fs/microsd/2014-01-19/19_37_52.px4log */
398+
time_t utc_time_sec;
399+
400+
if (_gpstime_only) {
401+
utc_time_sec = gps_time / 1e6;
402+
} else {
403+
utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9);
404+
}
405+
if (utc_time_sec > PX4_EPOCH_SECS) {
406+
407+
/* strip the time elapsed since boot */
408+
if (boot_time) {
409+
utc_time_sec -= hrt_absolute_time() / 1e6;
410+
}
411+
412+
struct tm *ttp = gmtime_r(&utc_time_sec, tt);
413+
return (ttp != NULL);
414+
} else {
415+
return false;
416+
}
417+
}
418+
375419
int create_log_dir()
376420
{
377421
/* create dir on sdcard if needed */
378422
uint16_t dir_number = 1; // start with dir sess001
379423
int mkdir_ret;
380424

381-
struct timespec ts;
382-
clock_gettime(CLOCK_REALTIME, &ts);
383-
/* use RTC time for log file naming, e.g. /fs/microsd/2014-01-19/19_37_52.px4log */
384-
time_t utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9);
385425
struct tm tt;
386-
struct tm *ttp = gmtime_r(&utc_time_sec, &tt);
426+
bool time_ok = get_log_time_utc_tt(&tt, true);
387427

388-
if (log_name_timestamp && ttp && (utc_time_sec > PX4_EPOCH_SECS)) {
428+
if (log_name_timestamp && time_ok) {
389429
int n = snprintf(log_dir, sizeof(log_dir), "%s/", log_root);
390430
strftime(log_dir + n, sizeof(log_dir) - n, "%Y-%m-%d", &tt);
391431
mkdir_ret = mkdir(log_dir, S_IRWXU | S_IRWXG | S_IRWXO);
@@ -434,15 +474,11 @@ int open_log_file()
434474
char log_file_name[32] = "";
435475
char log_file_path[64] = "";
436476

437-
struct timespec ts;
438-
clock_gettime(CLOCK_REALTIME, &ts);
439-
/* use RTC time for log file naming, e.g. /fs/microsd/2014-01-19/19_37_52.px4log */
440-
time_t utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9);
441477
struct tm tt;
442-
struct tm *ttp = gmtime_r(&utc_time_sec, &tt);
478+
bool time_ok = get_log_time_utc_tt(&tt, false);
443479

444480
/* start logging if we have a valid time and the time is not in the past */
445-
if (log_name_timestamp && ttp && (utc_time_sec > PX4_EPOCH_SECS)) {
481+
if (log_name_timestamp && time_ok) {
446482
strftime(log_file_name, sizeof(log_file_name), "%H_%M_%S.px4log", &tt);
447483
snprintf(log_file_path, sizeof(log_file_path), "%s/%s", log_dir, log_file_name);
448484

@@ -487,14 +523,10 @@ int open_perf_file(const char* str)
487523
char log_file_name[32] = "";
488524
char log_file_path[64] = "";
489525

490-
struct timespec ts;
491-
clock_gettime(CLOCK_REALTIME, &ts);
492-
/* use RTC time for log file naming, e.g. /fs/microsd/2014-01-19/19_37_52.txt */
493-
time_t utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9);
494526
struct tm tt;
495-
struct tm *ttp = gmtime_r(&utc_time_sec, &tt);
527+
bool time_ok = get_log_time_utc_tt(&tt, false);
496528

497-
if (log_name_timestamp && ttp && (utc_time_sec > PX4_EPOCH_SECS)) {
529+
if (log_name_timestamp && time_ok) {
498530
strftime(log_file_name, sizeof(log_file_name), "perf%H_%M_%S.txt", &tt);
499531
snprintf(log_file_path, sizeof(log_file_path), "%s/%s_%s", log_dir, str, log_file_name);
500532

@@ -966,6 +998,22 @@ int sdlog2_thread_main(int argc, char *argv[])
966998

967999
}
9681000

1001+
param_t log_gpstime_ph = param_find("SDLOG_GPSTIME");
1002+
1003+
if (log_gpstime_ph != PARAM_INVALID) {
1004+
1005+
int32_t param_log_gpstime;
1006+
param_get(log_gpstime_ph, &param_log_gpstime);
1007+
1008+
if (param_log_gpstime > 0) {
1009+
_gpstime_only = true;
1010+
} else if (param_log_gpstime == 0) {
1011+
_gpstime_only = false;
1012+
}
1013+
/* any other value means to ignore the parameter, so no else case */
1014+
1015+
}
1016+
9691017

9701018
if (check_free_space() != OK) {
9711019
errx(1, "ERR: MicroSD almost full");
@@ -1207,7 +1255,7 @@ int sdlog2_thread_main(int argc, char *argv[])
12071255
/* check GPS topic to get GPS time */
12081256
if (log_name_timestamp) {
12091257
if (!orb_copy(ORB_ID(vehicle_gps_position), subs.gps_pos_sub, &buf_gps_pos)) {
1210-
gps_time = buf_gps_pos.time_utc_usec;
1258+
gps_time = buf_gps_pos.time_utc_usec / 1e6;
12111259
}
12121260
}
12131261

@@ -1235,7 +1283,7 @@ int sdlog2_thread_main(int argc, char *argv[])
12351283
bool gps_pos_updated = copy_if_updated(ORB_ID(vehicle_gps_position), &subs.gps_pos_sub, &buf_gps_pos);
12361284

12371285
if (gps_pos_updated && log_name_timestamp) {
1238-
gps_time = buf_gps_pos.time_utc_usec;
1286+
gps_time = buf_gps_pos.time_utc_usec / 1e6;
12391287
}
12401288

12411289
if (!logging_enabled) {
@@ -1876,6 +1924,7 @@ int sdlog2_thread_main(int argc, char *argv[])
18761924
void sdlog2_status()
18771925
{
18781926
warnx("extended logging: %s", (_extended_logging) ? "ON" : "OFF");
1927+
warnx("time: gps: %u seconds", (unsigned)gps_time);
18791928
if (!logging_enabled) {
18801929
warnx("not logging");
18811930
} else {

0 commit comments

Comments
 (0)