Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
file_logger.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
3  * 2019 Tom van Dijk <tomvand@users.noreply.github.com>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
28 #include "file_logger.h"
29 
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include "std.h"
35 
36 #include "mcu_periph/sys_time.h"
37 #include "state.h"
38 #include "generated/airframe.h"
39 #ifdef COMMAND_THRUST
41 #else
44 #endif
45 
46 
48 #ifndef FILE_LOGGER_PATH
49 #define FILE_LOGGER_PATH /data/video/usb
50 #endif
51 
53 static FILE *file_logger = NULL;
54 
55 
64 static void file_logger_write_header(FILE *file) {
65  fprintf(file, "time,");
66  fprintf(file, "pos_x,pos_y,pos_z,");
67  fprintf(file, "vel_x,vel_y,vel_z,");
68  fprintf(file, "att_phi,att_theta,att_psi,");
69  fprintf(file, "rate_p,rate_q,rate_r,");
70 #ifdef COMMAND_THRUST
71  fprintf(file, "cmd_thrust,cmd_roll,cmd_pitch,cmd_yaw\n");
72 #else
73  fprintf(file, "h_ctl_aileron_setpoint,h_ctl_elevator_setpoint\n");
74 #endif
75 }
76 
83 static void file_logger_write_row(FILE *file) {
84  struct NedCoor_f *pos = stateGetPositionNed_f();
85  struct NedCoor_f *vel = stateGetSpeedNed_f();
86  struct FloatEulers *att = stateGetNedToBodyEulers_f();
87  struct FloatRates *rates = stateGetBodyRates_f();
88 
89  fprintf(file, "%f,", get_sys_time_float());
90  fprintf(file, "%f,%f,%f,", pos->x, pos->y, pos->z);
91  fprintf(file, "%f,%f,%f,", vel->x, vel->y, vel->z);
92  fprintf(file, "%f,%f,%f,", att->phi, att->theta, att->psi);
93  fprintf(file, "%f,%f,%f,", rates->p, rates->q, rates->r);
94 #ifdef COMMAND_THRUST
95  fprintf(file, "%d,%d,%d,%d\n",
96  stabilization_cmd[COMMAND_THRUST], stabilization_cmd[COMMAND_ROLL],
97  stabilization_cmd[COMMAND_PITCH], stabilization_cmd[COMMAND_YAW]);
98 #else
99  fprintf(file, "%d,%d\n", h_ctl_aileron_setpoint, h_ctl_elevator_setpoint);
100 #endif
101 }
102 
103 
106 {
107  // Create output folder if necessary
108  if (access(STRINGIFY(FILE_LOGGER_PATH), F_OK)) {
109  char save_dir_cmd[256];
110  sprintf(save_dir_cmd, "mkdir -p %s", STRINGIFY(FILE_LOGGER_PATH));
111  if (system(save_dir_cmd) != 0) {
112  printf("[file_logger] Could not create log file directory %s.\n", STRINGIFY(FILE_LOGGER_PATH));
113  return;
114  }
115  }
116 
117  // Get current date/time for filename
118  char date_time[80];
119  time_t now = time(0);
120  struct tm tstruct;
121  tstruct = *localtime(&now);
122  strftime(date_time, sizeof(date_time), "%Y%m%d-%H%M%S", &tstruct);
123 
124  uint32_t counter = 0;
125  char filename[512];
126 
127  // Check for available files
128  sprintf(filename, "%s/%s.csv", STRINGIFY(FILE_LOGGER_PATH), date_time);
129  while ((file_logger = fopen(filename, "r"))) {
130  fclose(file_logger);
131 
132  sprintf(filename, "%s/%s_%05d.csv", STRINGIFY(FILE_LOGGER_PATH), date_time, counter);
133  counter++;
134  }
135 
136  file_logger = fopen(filename, "w");
137  if(!file_logger) {
138  printf("[file_logger] ERROR opening log file %s!\n", filename);
139  return;
140  }
141 
142  printf("[file_logger] Start logging to %s...\n", filename);
143 
145 }
146 
149 {
150  if (file_logger != NULL) {
151  fclose(file_logger);
152  file_logger = NULL;
153  }
154 }
155 
158 {
159  if (file_logger == NULL) {
160  return;
161  }
163 }
NedCoor_f
vector in North East Down coordinates Units: meters
Definition: pprz_geodetic_float.h:63
stateGetPositionNed_f
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:710
NedCoor_f::z
float z
in meters
Definition: pprz_geodetic_float.h:66
FILE_LOGGER_PATH
#define FILE_LOGGER_PATH
Set the default File logger path to the USB drive.
Definition: file_logger.c:49
get_sys_time_float
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
file_logger_stop
void file_logger_stop(void)
Stop the logger an nicely close the file.
Definition: file_logger.c:148
stateGetNedToBodyEulers_f
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
stateGetBodyRates_f
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1200
uint32_t
unsigned long uint32_t
Definition: types.h:18
file_logger_start
void file_logger_start(void)
Start the file logger and open a new file.
Definition: file_logger.c:105
stabilization_attitude.h
counter
int32_t counter
Definition: avoid_navigation.c:55
FloatEulers::theta
float theta
in radians
Definition: pprz_algebra_float.h:86
file_logger_write_header
static void file_logger_write_header(FILE *file)
Logging functions.
Definition: file_logger.c:64
std.h
stateGetSpeedNed_f
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:908
file_logger.h
File logger for Linux based autopilots.
FloatEulers::phi
float phi
in radians
Definition: pprz_algebra_float.h:85
stabilization_adaptive.h
sys_time.h
Architecture independent timing functions.
FloatRates::r
float r
in rad/s
Definition: pprz_algebra_float.h:96
file_logger
static FILE * file_logger
The file pointer.
Definition: file_logger.c:53
FloatRates::q
float q
in rad/s
Definition: pprz_algebra_float.h:95
NedCoor_f::y
float y
in meters
Definition: pprz_geodetic_float.h:65
h_ctl_elevator_setpoint
pprz_t h_ctl_elevator_setpoint
Definition: stabilization_adaptive.c:178
file_logger_write_row
static void file_logger_write_row(FILE *file)
Write CSV row Write values at this timestamp to log file.
Definition: file_logger.c:83
file_logger_periodic
void file_logger_periodic(void)
Log the values to a csv file
Definition: file_logger.c:157
stabilization_cmd
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:32
NedCoor_f::x
float x
in meters
Definition: pprz_geodetic_float.h:64
FloatEulers
euler angles
Definition: pprz_algebra_float.h:84
h_ctl_aileron_setpoint
pprz_t h_ctl_aileron_setpoint
Definition: stabilization_adaptive.c:164
stabilization.h
state.h
FloatEulers::psi
float psi
in radians
Definition: pprz_algebra_float.h:87
FloatRates::p
float p
in rad/s
Definition: pprz_algebra_float.h:94
FloatRates
angular rates
Definition: pprz_algebra_float.h:93