Automatically extract flight number for eeprom and telem filenames.
[fw/altos] / ao-tools / lib / cc.h
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #ifndef _CC_H_
19 #define _CC_H_
20
21 #include <stdio.h>
22 #include <stdint.h>
23
24 char *
25 cc_fullname (char *dir, char *file);
26
27 char *
28 cc_basename(char *file);
29
30 int
31 cc_mkdir(char *dir);
32
33 struct cc_usbdev {
34         char    *sys;
35         char    *tty;
36         char    *manufacturer;
37         char    *product;
38         int     serial; /* AltOS always uses simple integer serial numbers */
39         int     idProduct;
40         int     idVendor;
41 };
42
43 struct cc_usbdevs {
44         struct cc_usbdev        **dev;
45         int                     ndev;
46 };
47
48 void
49 cc_usbdevs_free(struct cc_usbdevs *usbdevs);
50
51 struct cc_usbdevs *
52 cc_usbdevs_scan(void);
53
54 char *
55 cc_usbdevs_find_by_arg(char *arg, char *default_product);
56
57 void
58 cc_set_log_dir(char *dir);
59
60 char *
61 cc_get_log_dir(void);
62
63 char *
64 cc_make_filename(int serial, int flight, char *ext);
65
66 /*
67  * For sequential data which are not evenly spaced
68  */
69
70 struct cc_timedataelt {
71         double  time;
72         double  value;
73 };
74
75 struct cc_timedata {
76         int                     num;
77         int                     size;
78         struct cc_timedataelt   *data;
79         double                  time_offset;
80 };
81
82
83 /*
84  * For GPS data
85  */
86
87 struct cc_gpselt {
88         double          time;
89         int             hour;
90         int             minute;
91         int             second;
92         int             flags;
93         double          lat;
94         double          lon;
95         double          alt;
96 };
97
98 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
99 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
100 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
101 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
102 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
103 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
104 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
105 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
106
107 struct cc_gpssat {
108         double          time;
109         uint16_t        svid;
110         uint8_t         c_n;
111 };
112
113 struct cc_gpssats {
114         int                     nsat;
115         struct cc_gpssat        sat[12];
116 };
117
118 struct cc_gpsdata {
119         int                     num;
120         int                     size;
121         struct cc_gpselt        *data;
122         double                  time_offset;
123         int                     numsats;
124         int                     sizesats;
125         struct cc_gpssats       *sats;
126 };
127
128 /*
129  * For sequential data which are evenly spaced
130  */
131 struct cc_perioddata {
132         int             num;
133         double          start;
134         double          step;
135         double          *data;
136 };
137
138 enum ao_flight_state {
139         ao_flight_startup = 0,
140         ao_flight_idle = 1,
141         ao_flight_pad = 2,
142         ao_flight_boost = 3,
143         ao_flight_fast = 4,
144         ao_flight_coast = 5,
145         ao_flight_drogue = 6,
146         ao_flight_main = 7,
147         ao_flight_landed = 8,
148         ao_flight_invalid = 9
149 };
150
151 struct cc_flightraw {
152         int                     flight;
153         int                     serial;
154         double                  ground_accel;
155         double                  ground_pres;
156         int                     year, month, day;
157         struct cc_timedata      accel;
158         struct cc_timedata      pres;
159         struct cc_timedata      temp;
160         struct cc_timedata      volt;
161         struct cc_timedata      main;
162         struct cc_timedata      drogue;
163         struct cc_timedata      state;
164         struct cc_gpsdata       gps;
165 };
166
167 struct cc_flightraw *
168 cc_log_read(FILE *file);
169
170 void
171 cc_flightraw_free(struct cc_flightraw *raw);
172
173 struct cc_flightcooked {
174         double                  flight_start;
175         double                  flight_stop;
176
177         struct cc_perioddata    accel_accel;
178         struct cc_perioddata    accel_speed;
179         struct cc_perioddata    accel_pos;
180         struct cc_perioddata    pres_pos;
181         struct cc_perioddata    pres_speed;
182         struct cc_perioddata    pres_accel;
183         struct cc_perioddata    gps_lat;
184         struct cc_perioddata    gps_lon;
185         struct cc_perioddata    gps_alt;
186
187         /* unfiltered, but converted */
188         struct cc_timedata      pres;
189         struct cc_timedata      accel;
190         struct cc_timedata      state;
191 };
192
193 /*
194  * Telemetry data contents
195  */
196
197
198 struct cc_gps_time {
199         int year;
200         int month;
201         int day;
202         int hour;
203         int minute;
204         int second;
205 };
206
207 struct cc_gps {
208         int     nsat;
209         int     gps_locked;
210         int     gps_connected;
211         struct cc_gps_time gps_time;
212         double  lat;            /* degrees (+N -S) */
213         double  lon;            /* degrees (+E -W) */
214         int     alt;            /* m */
215
216         int     gps_extended;   /* has extra data */
217         double  ground_speed;   /* m/s */
218         int     course;         /* degrees */
219         double  climb_rate;     /* m/s */
220         double  hdop;           /* unitless? */
221         int     h_error;        /* m */
222         int     v_error;        /* m */
223 };
224
225 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
226 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
227 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
228 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
229 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
230 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
231 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
232 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
233
234 struct cc_gps_sat {
235         int     svid;
236         int     c_n0;
237 };
238
239 struct cc_gps_tracking {
240         int                     channels;
241         struct cc_gps_sat       sats[12];
242 };
243
244 struct cc_telem {
245         char    callsign[16];
246         int     serial;
247         int     flight;
248         int     rssi;
249         char    state[16];
250         int     tick;
251         int     accel;
252         int     pres;
253         int     temp;
254         int     batt;
255         int     drogue;
256         int     main;
257         int     flight_accel;
258         int     ground_accel;
259         int     flight_vel;
260         int     flight_pres;
261         int     ground_pres;
262         int     accel_plus_g;
263         int     accel_minus_g;
264         struct cc_gps   gps;
265         struct cc_gps_tracking  gps_tracking;
266 };
267
268 int
269 cc_telem_parse(const char *input_line, struct cc_telem *telem);
270
271 #ifndef TRUE
272 #define TRUE 1
273 #define FALSE 0
274 #endif
275
276 /* Conversion functions */
277 double
278 cc_pressure_to_altitude(double pressure);
279
280 double
281 cc_altitude_to_pressure(double altitude);
282
283 double
284 cc_barometer_to_pressure(double baro);
285
286 double
287 cc_barometer_to_altitude(double baro);
288
289 double
290 cc_accelerometer_to_acceleration(double accel, double ground_accel);
291
292 double
293 cc_thermometer_to_temperature(double thermo);
294
295 double
296 cc_battery_to_voltage(double battery);
297
298 double
299 cc_ignitor_to_voltage(double ignite);
300
301 void
302 cc_great_circle (double start_lat, double start_lon,
303                  double end_lat, double end_lon,
304                  double *dist, double *bearing);
305
306 void
307 cc_timedata_limits(struct cc_timedata *d, double min_time, double max_time, int *start, int *stop);
308
309 int
310 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time);
311
312 int
313 cc_timedata_min_mag(struct cc_timedata *d, double min_time, double max_time);
314
315 int
316 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time);
317
318 int
319 cc_timedata_max_mag(struct cc_timedata *d, double min_time, double max_time);
320
321 double
322 cc_timedata_average(struct cc_timedata *d, double min_time, double max_time);
323
324 double
325 cc_timedata_average_mag(struct cc_timedata *d, double min_time, double max_time);
326
327 int
328 cc_perioddata_limits(struct cc_perioddata *d, double min_time, double max_time, int *start, int *stop);
329
330 int
331 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time);
332
333 int
334 cc_perioddata_min_mag(struct cc_perioddata *d, double min_time, double max_time);
335
336 int
337 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time);
338
339 int
340 cc_perioddata_max_mag(struct cc_perioddata *d, double min_time, double max_time);
341
342 double
343 cc_perioddata_average(struct cc_perioddata *d, double min_time, double max_time);
344
345 double
346 cc_perioddata_average_mag(struct cc_perioddata *d, double min_time, double max_time);
347
348 double *
349 cc_low_pass(double *data, int data_len, double omega_pass, double omega_stop, double error);
350
351 struct cc_perioddata *
352 cc_period_make(struct cc_timedata *td, double start_time, double stop_time);
353
354 struct cc_perioddata *
355 cc_period_low_pass(struct cc_perioddata *raw, double omega_pass, double omega_stop, double error);
356
357 struct cc_timedata *
358 cc_timedata_convert(struct cc_timedata *d, double (*f)(double v, double a), double a);
359
360 struct cc_timedata *
361 cc_timedata_integrate(struct cc_timedata *d, double min_time, double max_time);
362
363 struct cc_perioddata *
364 cc_perioddata_differentiate(struct cc_perioddata *i);
365
366 struct cc_flightcooked *
367 cc_flight_cook(struct cc_flightraw *raw);
368
369 void
370 cc_flightcooked_free(struct cc_flightcooked *cooked);
371
372 #endif /* _CC_H_ */