0e8ced8a63c6c93d3897d23647fa8cde17064b41
[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, 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         double          lat;
90         double          lon;
91         double          alt;
92 };
93
94 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
95 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
96 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
97 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
98 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
99 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
100 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
101 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
102
103 struct cc_gpssat {
104         double          time;
105         uint16_t        svid;
106         uint8_t         c_n;
107 };
108
109 struct cc_gpssats {
110         int                     nsat;
111         struct cc_gpssat        sat[12];
112 };
113
114 struct cc_gpsdata {
115         int                     num;
116         int                     size;
117         struct cc_gpselt        *data;
118         double                  time_offset;
119         int                     numsats;
120         int                     sizesats;
121         struct cc_gpssats       *sats;
122 };
123
124 /*
125  * For sequential data which are evenly spaced
126  */
127 struct cc_perioddata {
128         int             num;
129         double          start;
130         double          step;
131         double          *data;
132 };
133
134 enum ao_flight_state {
135         ao_flight_startup = 0,
136         ao_flight_idle = 1,
137         ao_flight_pad = 2,
138         ao_flight_boost = 3,
139         ao_flight_fast = 4,
140         ao_flight_coast = 5,
141         ao_flight_drogue = 6,
142         ao_flight_main = 7,
143         ao_flight_landed = 8,
144         ao_flight_invalid = 9
145 };
146
147 struct cc_flightraw {
148         int                     flight;
149         int                     serial;
150         double                  ground_accel;
151         double                  ground_pres;
152         struct cc_timedata      accel;
153         struct cc_timedata      pres;
154         struct cc_timedata      temp;
155         struct cc_timedata      volt;
156         struct cc_timedata      main;
157         struct cc_timedata      drogue;
158         struct cc_timedata      state;
159         struct cc_gpsdata       gps;
160 };
161
162 struct cc_flightraw *
163 cc_log_read(FILE *file);
164
165 void
166 cc_flightraw_free(struct cc_flightraw *raw);
167
168 struct cc_flightcooked {
169         double                  flight_start;
170         double                  flight_stop;
171
172         struct cc_perioddata    accel_accel;
173         struct cc_perioddata    accel_speed;
174         struct cc_perioddata    accel_pos;
175         struct cc_perioddata    pres_pos;
176         struct cc_perioddata    pres_speed;
177         struct cc_perioddata    pres_accel;
178         struct cc_perioddata    gps_lat;
179         struct cc_perioddata    gps_lon;
180         struct cc_perioddata    gps_alt;
181
182         /* unfiltered, but converted */
183         struct cc_timedata      pres;
184         struct cc_timedata      accel;
185         struct cc_timedata      state;
186 };
187
188 /*
189  * Telemetry data contents
190  */
191
192
193 struct cc_gps_time {
194         int year;
195         int month;
196         int day;
197         int hour;
198         int minute;
199         int second;
200 };
201
202 struct cc_gps {
203         int     nsat;
204         int     gps_locked;
205         int     gps_connected;
206         struct cc_gps_time gps_time;
207         double  lat;            /* degrees (+N -S) */
208         double  lon;            /* degrees (+E -W) */
209         int     alt;            /* m */
210
211         int     gps_extended;   /* has extra data */
212         double  ground_speed;   /* m/s */
213         int     course;         /* degrees */
214         double  climb_rate;     /* m/s */
215         double  hdop;           /* unitless? */
216         int     h_error;        /* m */
217         int     v_error;        /* m */
218 };
219
220 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
221 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
222 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
223 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
224 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
225 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
226 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
227 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
228
229 struct cc_gps_sat {
230         int     svid;
231         int     state;
232         int     c_n0;
233 };
234
235 struct cc_gps_tracking {
236         int                     channels;
237         struct cc_gps_sat       sats[12];
238 };
239
240 struct cc_telem {
241         char    callsign[16];
242         int     serial;
243         int     flight;
244         int     rssi;
245         char    state[16];
246         int     tick;
247         int     accel;
248         int     pres;
249         int     temp;
250         int     batt;
251         int     drogue;
252         int     main;
253         int     flight_accel;
254         int     ground_accel;
255         int     flight_vel;
256         int     flight_pres;
257         int     ground_pres;
258         int     accel_plus_g;
259         int     accel_minus_g;
260         struct cc_gps   gps;
261         struct cc_gps_tracking  gps_tracking;
262 };
263
264 int
265 cc_telem_parse(const char *input_line, struct cc_telem *telem);
266
267 #ifndef TRUE
268 #define TRUE 1
269 #define FALSE 0
270 #endif
271
272 /* Conversion functions */
273 double
274 cc_pressure_to_altitude(double pressure);
275
276 double
277 cc_altitude_to_pressure(double altitude);
278
279 double
280 cc_barometer_to_pressure(double baro);
281
282 double
283 cc_barometer_to_altitude(double baro);
284
285 double
286 cc_accelerometer_to_acceleration(double accel, double ground_accel);
287
288 double
289 cc_thermometer_to_temperature(double thermo);
290
291 double
292 cc_battery_to_voltage(double battery);
293
294 double
295 cc_ignitor_to_voltage(double ignite);
296
297 void
298 cc_great_circle (double start_lat, double start_lon,
299                  double end_lat, double end_lon,
300                  double *dist, double *bearing);
301
302 void
303 cc_timedata_limits(struct cc_timedata *d, double min_time, double max_time, int *start, int *stop);
304
305 int
306 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time);
307
308 int
309 cc_timedata_min_mag(struct cc_timedata *d, double min_time, double max_time);
310
311 int
312 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time);
313
314 int
315 cc_timedata_max_mag(struct cc_timedata *d, double min_time, double max_time);
316
317 double
318 cc_timedata_average(struct cc_timedata *d, double min_time, double max_time);
319
320 double
321 cc_timedata_average_mag(struct cc_timedata *d, double min_time, double max_time);
322
323 int
324 cc_perioddata_limits(struct cc_perioddata *d, double min_time, double max_time, int *start, int *stop);
325
326 int
327 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time);
328
329 int
330 cc_perioddata_min_mag(struct cc_perioddata *d, double min_time, double max_time);
331
332 int
333 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time);
334
335 int
336 cc_perioddata_max_mag(struct cc_perioddata *d, double min_time, double max_time);
337
338 double
339 cc_perioddata_average(struct cc_perioddata *d, double min_time, double max_time);
340
341 double
342 cc_perioddata_average_mag(struct cc_perioddata *d, double min_time, double max_time);
343
344 double *
345 cc_low_pass(double *data, int data_len, double omega_pass, double omega_stop, double error);
346
347 struct cc_perioddata *
348 cc_period_make(struct cc_timedata *td, double start_time, double stop_time);
349
350 struct cc_perioddata *
351 cc_period_low_pass(struct cc_perioddata *raw, double omega_pass, double omega_stop, double error);
352
353 struct cc_timedata *
354 cc_timedata_convert(struct cc_timedata *d, double (*f)(double v, double a), double a);
355
356 struct cc_timedata *
357 cc_timedata_integrate(struct cc_timedata *d, double min_time, double max_time);
358
359 struct cc_perioddata *
360 cc_perioddata_differentiate(struct cc_perioddata *i);
361
362 struct cc_flightcooked *
363 cc_flight_cook(struct cc_flightraw *raw);
364
365 void
366 cc_flightcooked_free(struct cc_flightcooked *cooked);
367
368 #endif /* _CC_H_ */