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