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