Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
file_logger.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
27 #include "file_logger.h"
28 
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <time.h>
32 #include "std.h"
33 
34 #include "subsystems/imu.h"
35 #ifdef COMMAND_THRUST
37 #else
40 #endif
41 
42 #include "state.h"
43 
45 #ifndef FILE_LOGGER_PATH
46 #define FILE_LOGGER_PATH /data/video/usb
47 #endif
48 
50 static FILE *file_logger = NULL;
51 
54 {
55  // check if log path exists
56  struct stat s;
57  int err = stat(STRINGIFY(FILE_LOGGER_PATH), &s);
58 
59  if(err < 0) {
60  // try to make the directory
61  mkdir(STRINGIFY(FILE_LOGGER_PATH), 0666);
62  }
63 
64  // Get current date/time, format is YYYY-MM-DD.HH:mm:ss
65  char date_time[80];
66  time_t now = time(0);
67  struct tm tstruct;
68  tstruct = *localtime(&now);
69  strftime(date_time, sizeof(date_time), "%Y-%m-%d_%X", &tstruct);
70 
71  uint32_t counter = 0;
72  char filename[512];
73 
74  // Check for available files
75  sprintf(filename, "%s/%s.csv", STRINGIFY(FILE_LOGGER_PATH), date_time);
76  while ((file_logger = fopen(filename, "r"))) {
77  fclose(file_logger);
78 
79  sprintf(filename, "%s/%s_%05d.csv", STRINGIFY(FILE_LOGGER_PATH), date_time, counter);
80  counter++;
81  }
82 
83  file_logger = fopen(filename, "w");
84 
85  if (file_logger != NULL) {
86  fprintf(
88 
89  //rotorcraft uses COMMAND_THRUST, fixedwing COMMAND_THROTTLE at this time
90 #ifdef COMMAND_THRUST
91  "counter,gyro_unscaled_p,gyro_unscaled_q,gyro_unscaled_r,accel_unscaled_x,accel_unscaled_y,accel_unscaled_z,mag_unscaled_x,mag_unscaled_y,mag_unscaled_z,COMMAND_THRUST,COMMAND_ROLL,COMMAND_PITCH,COMMAND_YAW,qi,qx,qy,qz\n"
92 #else
93  "counter,gyro_unscaled_p,gyro_unscaled_q,gyro_unscaled_r,accel_unscaled_x,accel_unscaled_y,accel_unscaled_z,mag_unscaled_x,mag_unscaled_y,mag_unscaled_z, h_ctl_aileron_setpoint, h_ctl_elevator_setpoint, qi,qx,qy,qz\n"
94 #endif
95  );
96  }
97 }
98 
101 {
102  if (file_logger != NULL) {
103  fclose(file_logger);
104  file_logger = NULL;
105  }
106 }
107 
111 {
112  if (file_logger == NULL) {
113  return;
114  }
115  static uint32_t counter;
116  struct Int32Quat *quat = stateGetNedToBodyQuat_i();
117 
118 #ifdef COMMAND_THRUST //For example rotorcraft
119  fprintf(file_logger, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
120  counter,
130  stabilization_cmd[COMMAND_THRUST],
131  stabilization_cmd[COMMAND_ROLL],
132  stabilization_cmd[COMMAND_PITCH],
133  stabilization_cmd[COMMAND_YAW],
134  quat->qi,
135  quat->qx,
136  quat->qy,
137  quat->qz
138  );
139 #else
140  fprintf(file_logger, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
141  counter,
153  quat->qi,
154  quat->qx,
155  quat->qy,
156  quat->qz
157  );
158 #endif
159 
160  counter++;
161 }
pprz_t h_ctl_elevator_setpoint
pprz_t h_ctl_aileron_setpoint
struct Imu imu
global IMU state
Definition: imu.c:108
static struct Int32Quat * stateGetNedToBodyQuat_i(void)
Get vehicle body attitude quaternion (int).
Definition: state.h:1113
void file_logger_periodic(void)
Log the values to a csv file.
Definition: file_logger.c:110
Fixed wing horizontal control.
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
int32_t r
in rad/s with INT32_RATE_FRAC
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
void file_logger_stop(void)
Stop the logger an nicely close the file.
Definition: file_logger.c:100
unsigned long uint32_t
Definition: types.h:18
int32_t counter
Inertial Measurement Unit interface.
API to get/set the generic vehicle states.
File logger for Linux based autopilots.
General stabilization interface for rotorcrafts.
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:32
#define FILE_LOGGER_PATH
Set the default File logger path to the USB drive.
Definition: file_logger.c:46
int32_t p
in rad/s with INT32_RATE_FRAC
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
static FILE * file_logger
The file pointer.
Definition: file_logger.c:50
int32_t q
in rad/s with INT32_RATE_FRAC
Rotation quaternion.
void file_logger_start(void)
Start the file logger and open a new file.
Definition: file_logger.c:53
Fixed wing horizontal adaptive control.