Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
logger_file.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 "logger_file.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 #include "generated/modules.h"
47 
49 #ifndef LOGGER_FILE_PATH
50 #define LOGGER_FILE_PATH /data/video/usb
51 #endif
52 
54 static FILE *logger_file = NULL;
55 
56 
65 static void logger_file_write_header(FILE *file) {
66  fprintf(file, "time,");
67  fprintf(file, "pos_x,pos_y,pos_z,");
68  fprintf(file, "vel_x,vel_y,vel_z,");
69  fprintf(file, "att_phi,att_theta,att_psi,");
70  fprintf(file, "rate_p,rate_q,rate_r,");
71 #ifdef BOARD_BEBOP
72  fprintf(file, "rpm_obs_1,rpm_obs_2,rpm_obs_3,rpm_obs_4,");
73  fprintf(file, "rpm_ref_1,rpm_ref_2,rpm_ref_3,rpm_ref_4,");
74 #endif
75 #ifdef INS_EXT_POSE_H
77 #endif
78 #ifdef COMMAND_THRUST
79  fprintf(file, "cmd_thrust,cmd_roll,cmd_pitch,cmd_yaw\n");
80 #else
81  fprintf(file, "h_ctl_aileron_setpoint,h_ctl_elevator_setpoint\n");
82 #endif
83 }
84 
91 static void logger_file_write_row(FILE *file) {
92  struct NedCoor_f *pos = stateGetPositionNed_f();
93  struct NedCoor_f *vel = stateGetSpeedNed_f();
94  struct FloatEulers *att = stateGetNedToBodyEulers_f();
95  struct FloatRates *rates = stateGetBodyRates_f();
96 
97  fprintf(file, "%f,", get_sys_time_float());
98  fprintf(file, "%f,%f,%f,", pos->x, pos->y, pos->z);
99  fprintf(file, "%f,%f,%f,", vel->x, vel->y, vel->z);
100  fprintf(file, "%f,%f,%f,", att->phi, att->theta, att->psi);
101  fprintf(file, "%f,%f,%f,", rates->p, rates->q, rates->r);
102 #ifdef BOARD_BEBOP
105 #endif
106 #ifdef INS_EXT_POSE_H
107  ins_ext_pos_log_data(file);
108 #endif
109 #ifdef COMMAND_THRUST
110  fprintf(file, "%d,%d,%d,%d\n",
111  stabilization_cmd[COMMAND_THRUST], stabilization_cmd[COMMAND_ROLL],
112  stabilization_cmd[COMMAND_PITCH], stabilization_cmd[COMMAND_YAW]);
113 #else
114  fprintf(file, "%d,%d\n", h_ctl_aileron_setpoint, h_ctl_elevator_setpoint);
115 #endif
116 }
117 
118 
121 {
122  // Ensure that the module is running when started with this function
123  logger_file_logger_file_periodic_status = MODULES_RUN;
124 
125  // Create output folder if necessary
126  if (access(STRINGIFY(LOGGER_FILE_PATH), F_OK)) {
127  char save_dir_cmd[256];
128  sprintf(save_dir_cmd, "mkdir -p %s", STRINGIFY(LOGGER_FILE_PATH));
129  if (system(save_dir_cmd) != 0) {
130  printf("[logger_file] Could not create log file directory %s.\n", STRINGIFY(LOGGER_FILE_PATH));
131  return;
132  }
133  }
134 
135  // Get current date/time for filename
136  char date_time[80];
137  time_t now = time(0);
138  struct tm tstruct;
139  tstruct = *localtime(&now);
140  strftime(date_time, sizeof(date_time), "%Y%m%d-%H%M%S", &tstruct);
141 
142  uint32_t counter = 0;
143  char filename[512];
144 
145  // Check for available files
146  sprintf(filename, "%s/%s.csv", STRINGIFY(LOGGER_FILE_PATH), date_time);
147  while ((logger_file = fopen(filename, "r"))) {
148  fclose(logger_file);
149 
150  sprintf(filename, "%s/%s_%05d.csv", STRINGIFY(LOGGER_FILE_PATH), date_time, counter);
151  counter++;
152  }
153 
154  logger_file = fopen(filename, "w");
155  if(!logger_file) {
156  printf("[logger_file] ERROR opening log file %s!\n", filename);
157  return;
158  }
159 
160  printf("[logger_file] Start logging to %s...\n", filename);
161 
163 }
164 
167 {
168  if (logger_file != NULL) {
169  fclose(logger_file);
170  logger_file = NULL;
171  }
172 }
173 
176 {
177  if (logger_file == NULL) {
178  return;
179  }
181 }
struct ActuatorsBebop actuators_bebop
Definition: actuators.c:58
uint16_t rpm_ref[4]
Reference RPM.
Definition: actuators.h:50
uint16_t rpm_obs[4]
Observed RPM.
Definition: actuators.h:51
Fixed wing horizontal control.
float q
in rad/s
float phi
in radians
float p
in rad/s
float r
in rad/s
float theta
in radians
float psi
in radians
euler angles
angular rates
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:710
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1200
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:908
void ins_ext_pos_log_data(FILE *file)
void ins_ext_pos_log_header(FILE *file)
Logging.
uint32_t counter
Definition: ins_flow.c:192
static FILE * logger_file
The file pointer.
Definition: logger_file.c:54
void logger_file_stop(void)
Stop the logger an nicely close the file.
Definition: logger_file.c:166
void logger_file_periodic(void)
Log the values to a csv file
Definition: logger_file.c:175
static void logger_file_write_header(FILE *file)
Logging functions.
Definition: logger_file.c:65
static void logger_file_write_row(FILE *file)
Write CSV row Write values at this timestamp to log file.
Definition: logger_file.c:91
void logger_file_start(void)
Start the file logger and open a new file.
Definition: logger_file.c:120
#define LOGGER_FILE_PATH
Set the default File logger path to the USB drive.
Definition: logger_file.c:50
File logger for Linux based autopilots.
float z
in meters
float x
in meters
float y
in meters
vector in North East Down coordinates Units: meters
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:34
General stabilization interface for rotorcrafts.
pprz_t h_ctl_elevator_setpoint
pprz_t h_ctl_aileron_setpoint
Fixed wing horizontal adaptive control.
API to get/set the generic vehicle states.
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78