altos/test: Adjust CRC error rate after FEC fix
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _CC_H_
20 #define _CC_H_
21
22 #include <stdio.h>
23 #include <stdint.h>
24 #include "cc-telemetry.h"
25
26 char *
27 cc_fullname (char *dir, char *file);
28
29 char *
30 cc_basename(char *file);
31
32 int
33 cc_mkdir(char *dir);
34
35 struct cc_usbdev {
36         char    *sys;
37         char    *tty;
38         char    *manufacturer;
39         char    *product;
40         int     serial; /* AltOS always uses simple integer serial numbers */
41         int     idProduct;
42         int     idVendor;
43 };
44
45 struct cc_usbdevs {
46         struct cc_usbdev        **dev;
47         int                     ndev;
48 };
49
50 void
51 cc_usbdevs_free(struct cc_usbdevs *usbdevs);
52
53 struct cc_usbdevs *
54 cc_usbdevs_scan(int non_tty);
55
56 char *
57 cc_usbdevs_find_by_arg(char *arg, char *default_product);
58
59 void
60 cc_set_log_dir(char *dir);
61
62 char *
63 cc_get_log_dir(void);
64
65 char *
66 cc_make_filename(int serial, int flight, char *ext);
67
68 /*
69  * For sequential data which are not evenly spaced
70  */
71
72 struct cc_timedataelt {
73         double  time;
74         double  value;
75 };
76
77 struct cc_timedata {
78         int                     num;
79         int                     size;
80         struct cc_timedataelt   *data;
81         double                  time_offset;
82 };
83
84
85 /*
86  * For GPS data
87  */
88
89 struct cc_gpselt {
90         double          time;
91         int             hour;
92         int             minute;
93         int             second;
94         int             flags;
95         double          lat;
96         double          lon;
97         double          alt;
98 };
99
100 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
101 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
102 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
103 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
104 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
105 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
106 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
107 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
108
109 struct cc_gpssat {
110         double          time;
111         uint16_t        svid;
112         uint8_t         c_n;
113 };
114
115 struct cc_gpssats {
116         int                     nsat;
117         struct cc_gpssat        sat[12];
118 };
119
120 struct cc_gpsdata {
121         int                     num;
122         int                     size;
123         struct cc_gpselt        *data;
124         double                  time_offset;
125         int                     numsats;
126         int                     sizesats;
127         struct cc_gpssats       *sats;
128 };
129
130 /*
131  * For sequential data which are evenly spaced
132  */
133 struct cc_perioddata {
134         int             num;
135         double          start;
136         double          step;
137         double          *data;
138 };
139
140 enum ao_flight_state {
141         ao_flight_startup = 0,
142         ao_flight_idle = 1,
143         ao_flight_pad = 2,
144         ao_flight_boost = 3,
145         ao_flight_fast = 4,
146         ao_flight_coast = 5,
147         ao_flight_drogue = 6,
148         ao_flight_main = 7,
149         ao_flight_landed = 8,
150         ao_flight_invalid = 9
151 };
152
153 struct cc_flightraw {
154         int                     flight;
155         int                     serial;
156         double                  ground_accel;
157         double                  ground_pres;
158         int                     year, month, day;
159         struct cc_timedata      accel;
160         struct cc_timedata      pres;
161         struct cc_timedata      temp;
162         struct cc_timedata      volt;
163         struct cc_timedata      main;
164         struct cc_timedata      drogue;
165         struct cc_timedata      state;
166         struct cc_gpsdata       gps;
167 };
168
169 struct cc_flightraw *
170 cc_log_read(FILE *file);
171
172 void
173 cc_flightraw_free(struct cc_flightraw *raw);
174
175 struct cc_flightcooked {
176         double                  flight_start;
177         double                  flight_stop;
178
179         struct cc_perioddata    accel_accel;
180         struct cc_perioddata    accel_speed;
181         struct cc_perioddata    accel_pos;
182         struct cc_perioddata    pres_pos;
183         struct cc_perioddata    pres_speed;
184         struct cc_perioddata    pres_accel;
185         struct cc_perioddata    gps_lat;
186         struct cc_perioddata    gps_lon;
187         struct cc_perioddata    gps_alt;
188
189         /* unfiltered, but converted */
190         struct cc_timedata      pres;
191         struct cc_timedata      accel;
192         struct cc_timedata      state;
193 };
194
195 /*
196  * Telemetry data contents
197  */
198
199
200 struct cc_gps_time {
201         int year;
202         int month;
203         int day;
204         int hour;
205         int minute;
206         int second;
207 };
208
209 struct cc_gps {
210         int     nsat;
211         int     gps_locked;
212         int     gps_connected;
213         struct cc_gps_time gps_time;
214         double  lat;            /* degrees (+N -S) */
215         double  lon;            /* degrees (+E -W) */
216         int     alt;            /* m */
217
218         int     gps_extended;   /* has extra data */
219         double  ground_speed;   /* m/s */
220         int     course;         /* degrees */
221         double  climb_rate;     /* m/s */
222         double  hdop;           /* unitless? */
223         int     h_error;        /* m */
224         int     v_error;        /* m */
225 };
226
227 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
228 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
229 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
230 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
231 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
232 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
233 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
234 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
235
236 struct cc_gps_sat {
237         int     svid;
238         int     c_n0;
239 };
240
241 struct cc_gps_tracking {
242         int                     channels;
243         struct cc_gps_sat       sats[12];
244 };
245
246 struct cc_telem {
247         char    callsign[16];
248         int     serial;
249         int     flight;
250         int     rssi;
251         char    state[16];
252         int     tick;
253         int     accel;
254         int     pres;
255         int     temp;
256         int     batt;
257         int     drogue;
258         int     main;
259         int     flight_accel;
260         int     ground_accel;
261         int     flight_vel;
262         int     flight_pres;
263         int     ground_pres;
264         int     accel_plus_g;
265         int     accel_minus_g;
266         struct cc_gps   gps;
267         struct cc_gps_tracking  gps_tracking;
268 };
269
270 int
271 cc_telem_parse(const char *input_line, struct cc_telem *telem);
272
273 struct ao_log_mega {
274         char                    type;                   /* 0 */
275         uint8_t                 is_config;              /* 1 */
276         uint16_t                tick;                   /* 2 */
277         union {                                         /* 4 */
278                 /* AO_LOG_FLIGHT */
279                 struct {
280                         uint16_t        flight;         /* 4 */
281                         int16_t         ground_accel;   /* 6 */
282                         uint32_t        ground_pres;    /* 8 */
283                 } flight;                               /* 12 */
284                 /* AO_LOG_STATE */
285                 struct {
286                         uint16_t        state;
287                         uint16_t        reason;
288                 } state;
289                 /* AO_LOG_SENSOR */
290                 struct {
291                         uint32_t        pres;           /* 4 */
292                         uint32_t        temp;           /* 8 */
293                         int16_t         accel_x;        /* 12 */
294                         int16_t         accel_y;        /* 14 */
295                         int16_t         accel_z;        /* 16 */
296                         int16_t         gyro_x;         /* 18 */
297                         int16_t         gyro_y;         /* 20 */
298                         int16_t         gyro_z;         /* 22 */
299                         int16_t         mag_x;          /* 24 */
300                         int16_t         mag_y;          /* 26 */
301                         int16_t         mag_z;          /* 28 */
302                         int16_t         accel;          /* 30 */
303                 } sensor;       /* 32 */
304                 /* AO_LOG_TEMP_VOLT */
305                 struct {
306                         int16_t         v_batt;         /* 4 */
307                         int16_t         v_pbatt;        /* 6 */
308                         int16_t         n_sense;        /* 8 */
309                         int16_t         sense[10];      /* 10 */
310                         uint16_t        pyro;           /* 30 */
311                 } volt;                                 /* 32 */
312                 /* AO_LOG_GPS_TIME */
313                 struct {
314                         int32_t         latitude;       /* 4 */
315                         int32_t         longitude;      /* 8 */
316                         int16_t         altitude;       /* 12 */
317                         uint8_t         hour;           /* 14 */
318                         uint8_t         minute;         /* 15 */
319                         uint8_t         second;         /* 16 */
320                         uint8_t         flags;          /* 17 */
321                         uint8_t         year;           /* 18 */
322                         uint8_t         month;          /* 19 */
323                         uint8_t         day;            /* 20 */
324                         uint8_t         pad;            /* 21 */
325                 } gps;  /* 22 */
326                 /* AO_LOG_GPS_SAT */
327                 struct {
328                         uint16_t        channels;       /* 4 */
329                         struct {
330                                 uint8_t svid;
331                                 uint8_t c_n;
332                         } sats[12];                     /* 6 */
333                 } gps_sat;                              /* 30 */
334
335                 struct {
336                         uint32_t                kind;
337                         int32_t                 data[6];
338                 } config_int;
339
340                 struct {
341                         uint32_t                kind;
342                         char                    string[24];
343                 } config_str;
344
345                 /* Raw bytes */
346                 uint8_t bytes[28];
347         } u;
348 };
349
350 struct ao_log_metrum {
351         char                    type;                   /* 0 */
352         uint8_t                 csum;                   /* 1 */
353         uint16_t                tick;                   /* 2 */
354         union {                                         /* 4 */
355                 /* AO_LOG_FLIGHT */
356                 struct {
357                         uint16_t        flight;         /* 4 */
358                         int16_t         ground_accel;   /* 6 */
359                         uint32_t        ground_pres;    /* 8 */
360                         uint32_t        ground_temp;    /* 12 */
361                 } flight;       /* 16 */
362                 /* AO_LOG_STATE */
363                 struct {
364                         uint16_t        state;          /* 4 */
365                         uint16_t        reason;         /* 6 */
366                 } state;        /* 8 */
367                 /* AO_LOG_SENSOR */
368                 struct {
369                         uint32_t        pres;           /* 4 */
370                         uint32_t        temp;           /* 8 */
371                         int16_t         accel;          /* 12 */
372                 } sensor;       /* 14 */
373                 /* AO_LOG_TEMP_VOLT */
374                 struct {
375                         int16_t         v_batt;         /* 4 */
376                         int16_t         sense_a;        /* 6 */
377                         int16_t         sense_m;        /* 8 */
378                 } volt;         /* 10 */
379                 /* AO_LOG_GPS_POS */
380                 struct {
381                         int32_t         latitude;       /* 4 */
382                         int32_t         longitude;      /* 8 */
383                         uint16_t        altitude_low;   /* 12 */
384                         int16_t         altitude_high;  /* 14 */
385                 } gps;          /* 16 */
386                 /* AO_LOG_GPS_TIME */
387                 struct {
388                         uint8_t         hour;           /* 4 */
389                         uint8_t         minute;         /* 5 */
390                         uint8_t         second;         /* 6 */
391                         uint8_t         flags;          /* 7 */
392                         uint8_t         year;           /* 8 */
393                         uint8_t         month;          /* 9 */
394                         uint8_t         day;            /* 10 */
395                         uint8_t         pdop;           /* 11 */
396                 } gps_time;     /* 12 */
397                 /* AO_LOG_GPS_SAT (up to three packets) */
398                 struct {
399                         uint8_t channels;               /* 4 */
400                         uint8_t more;                   /* 5 */
401                         struct {
402                                 uint8_t svid;
403                                 uint8_t c_n;
404                         } sats[4];                      /* 6 */
405                 } gps_sat;                              /* 14 */
406                 uint8_t         raw[12];                /* 4 */
407         } u;    /* 16 */
408 };
409
410 struct ao_log_mini {
411         char            type;                           /* 0 */
412         uint8_t         csum;                           /* 1 */
413         uint16_t        tick;                           /* 2 */
414         union {                                         /* 4 */
415                 /* AO_LOG_FLIGHT */
416                 struct {
417                         uint16_t        flight;         /* 4 */
418                         uint16_t        r6;
419                         uint32_t        ground_pres;    /* 8 */
420                 } flight;
421                 /* AO_LOG_STATE */
422                 struct {
423                         uint16_t        state;          /* 4 */
424                         uint16_t        reason;         /* 6 */
425                 } state;
426                 /* AO_LOG_SENSOR */
427                 struct {
428                         uint8_t         pres[3];        /* 4 */
429                         uint8_t         temp[3];        /* 7 */
430                         int16_t         sense_a;        /* 10 */
431                         int16_t         sense_m;        /* 12 */
432                         int16_t         v_batt;         /* 14 */
433                 } sensor;                               /* 16 */
434         } u;                                            /* 16 */
435 };                                                      /* 16 */
436
437 #define ao_log_pack24(dst,value) do {           \
438                 (dst)[0] = (value);             \
439                 (dst)[1] = (value) >> 8;        \
440                 (dst)[2] = (value) >> 16;       \
441         } while (0)
442
443 struct ao_log_gps {
444         char                    type;                   /* 0 */
445         uint8_t                 csum;                   /* 1 */
446         uint16_t                tick;                   /* 2 */
447         union {                                         /* 4 */
448                 /* AO_LOG_FLIGHT */
449                 struct {
450                         uint16_t        flight;                 /* 4 */
451                         int16_t         start_altitude;         /* 6 */
452                         int32_t         start_latitude;         /* 8 */
453                         int32_t         start_longitude;        /* 12 */
454                 } flight;                                       /* 16 */
455                 /* AO_LOG_GPS_TIME */
456                 struct {
457                         int32_t         latitude;       /* 4 */
458                         int32_t         longitude;      /* 8 */
459                         uint16_t        altitude_low;   /* 12 */
460                         uint8_t         hour;           /* 14 */
461                         uint8_t         minute;         /* 15 */
462                         uint8_t         second;         /* 16 */
463                         uint8_t         flags;          /* 17 */
464                         uint8_t         year;           /* 18 */
465                         uint8_t         month;          /* 19 */
466                         uint8_t         day;            /* 20 */
467                         uint8_t         course;         /* 21 */
468                         uint16_t        ground_speed;   /* 22 */
469                         int16_t         climb_rate;     /* 24 */
470                         uint8_t         pdop;           /* 26 */
471                         uint8_t         hdop;           /* 27 */
472                         uint8_t         vdop;           /* 28 */
473                         uint8_t         mode;           /* 29 */
474                         int16_t         altitude_high;  /* 30 */
475                 } gps;  /* 31 */
476                 /* AO_LOG_GPS_SAT */
477                 struct {
478                         uint16_t        channels;       /* 4 */
479                         struct {
480                                 uint8_t svid;
481                                 uint8_t c_n;
482                         } sats[12];                     /* 6 */
483                 } gps_sat;                              /* 30 */
484         } u;
485 };
486
487 #define AO_CONFIG_CONFIG                1
488 #define AO_CONFIG_MAIN                  2
489 #define AO_CONFIG_APOGEE                3
490 #define AO_CONFIG_LOCKOUT               4
491 #define AO_CONFIG_FREQUENCY             5
492 #define AO_CONFIG_RADIO_ENABLE          6
493 #define AO_CONFIG_ACCEL_CAL             7
494 #define AO_CONFIG_RADIO_CAL             8
495 #define AO_CONFIG_MAX_LOG               9
496 #define AO_CONFIG_IGNITE_MODE           10
497 #define AO_CONFIG_PAD_ORIENTATION       11
498 #define AO_CONFIG_SERIAL_NUMBER         12
499 #define AO_CONFIG_LOG_FORMAT            13
500 #define AO_CONFIG_MS5607_RESERVED       14
501 #define AO_CONFIG_MS5607_SENS           15
502 #define AO_CONFIG_MS5607_OFF            16
503 #define AO_CONFIG_MS5607_TCS            17
504 #define AO_CONFIG_MS5607_TCO            18
505 #define AO_CONFIG_MS5607_TREF           19
506 #define AO_CONFIG_MS5607_TEMPSENS       20
507 #define AO_CONFIG_MS5607_CRC            21
508
509
510 #define AO_LOG_FLIGHT           'F'
511 #define AO_LOG_SENSOR           'A'
512 #define AO_LOG_TEMP_VOLT        'T'
513 #define AO_LOG_DEPLOY           'D'
514 #define AO_LOG_STATE            'S'
515 #define AO_LOG_GPS_TIME         'G'
516 #define AO_LOG_GPS_LAT          'N'
517 #define AO_LOG_GPS_LON          'W'
518 #define AO_LOG_GPS_ALT          'H'
519 #define AO_LOG_GPS_SAT          'V'
520 #define AO_LOG_GPS_DATE         'Y'
521 #define AO_LOG_GPS_POS          'P'
522
523 #define AO_LOG_CONFIG           'c'
524
525 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
526 #define AO_GPS_NUM_SAT_SHIFT    (0)
527
528 #define AO_GPS_VALID            (1 << 4)
529 #define AO_GPS_RUNNING          (1 << 5)
530 #define AO_GPS_DATE_VALID       (1 << 6)
531 #define AO_GPS_COURSE_VALID     (1 << 7)
532
533 #define AO_LOG_FORMAT_UNKNOWN           0       /* unknown; altosui will have to guess */
534 #define AO_LOG_FORMAT_FULL              1       /* 8 byte typed log records */
535 #define AO_LOG_FORMAT_TINY              2       /* two byte state/baro records */
536 #define AO_LOG_FORMAT_TELEMETRY         3       /* 32 byte ao_telemetry records */
537 #define AO_LOG_FORMAT_TELESCIENCE       4       /* 32 byte typed telescience records */
538 #define AO_LOG_FORMAT_TELEMEGA          5       /* 32 byte typed telemega records */
539 #define AO_LOG_FORMAT_EASYMINI          6       /* 16-byte MS5607 baro only, 3.0V supply */
540 #define AO_LOG_FORMAT_TELEMETRUM        7       /* 16-byte typed telemetrum records */
541 #define AO_LOG_FORMAT_TELEMINI          8       /* 16-byte MS5607 baro only, 3.3V supply */
542 #define AO_LOG_FORMAT_TELEGPS           9       /* 32 byte telegps records */
543 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
544
545 int
546 cc_mega_parse(const char *input_line, struct ao_log_mega *l);
547
548 #ifndef TRUE
549 #define TRUE 1
550 #define FALSE 0
551 #endif
552
553 /* Conversion functions */
554 double
555 cc_pressure_to_altitude(double pressure);
556
557 double
558 cc_altitude_to_pressure(double altitude);
559
560 double
561 cc_altitude_to_temperature(double altitude);
562
563 double
564 cc_barometer_to_pressure(double baro);
565
566 double
567 cc_barometer_to_altitude(double baro);
568
569 double
570 cc_accelerometer_to_acceleration(double accel, double ground_accel);
571
572 double
573 cc_thermometer_to_temperature(double thermo);
574
575 double
576 cc_battery_to_voltage(double battery);
577
578 double
579 cc_ignitor_to_voltage(double ignite);
580
581 void
582 cc_great_circle (double start_lat, double start_lon,
583                  double end_lat, double end_lon,
584                  double *dist, double *bearing);
585
586 void
587 cc_timedata_limits(struct cc_timedata *d, double min_time, double max_time, int *start, int *stop);
588
589 int
590 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time);
591
592 int
593 cc_timedata_min_mag(struct cc_timedata *d, double min_time, double max_time);
594
595 int
596 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time);
597
598 int
599 cc_timedata_max_mag(struct cc_timedata *d, double min_time, double max_time);
600
601 double
602 cc_timedata_average(struct cc_timedata *d, double min_time, double max_time);
603
604 double
605 cc_timedata_average_mag(struct cc_timedata *d, double min_time, double max_time);
606
607 int
608 cc_perioddata_limits(struct cc_perioddata *d, double min_time, double max_time, int *start, int *stop);
609
610 int
611 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time);
612
613 int
614 cc_perioddata_min_mag(struct cc_perioddata *d, double min_time, double max_time);
615
616 int
617 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time);
618
619 int
620 cc_perioddata_max_mag(struct cc_perioddata *d, double min_time, double max_time);
621
622 double
623 cc_perioddata_average(struct cc_perioddata *d, double min_time, double max_time);
624
625 double
626 cc_perioddata_average_mag(struct cc_perioddata *d, double min_time, double max_time);
627
628 double *
629 cc_low_pass(double *data, int data_len, double omega_pass, double omega_stop, double error);
630
631 struct cc_perioddata *
632 cc_period_make(struct cc_timedata *td, double start_time, double stop_time);
633
634 struct cc_perioddata *
635 cc_period_low_pass(struct cc_perioddata *raw, double omega_pass, double omega_stop, double error);
636
637 struct cc_timedata *
638 cc_timedata_convert(struct cc_timedata *d, double (*f)(double v, double a), double a);
639
640 struct cc_timedata *
641 cc_timedata_integrate(struct cc_timedata *d, double min_time, double max_time);
642
643 struct cc_perioddata *
644 cc_perioddata_differentiate(struct cc_perioddata *i);
645
646 struct cc_flightcooked *
647 cc_flight_cook(struct cc_flightraw *raw);
648
649 void
650 cc_flightcooked_free(struct cc_flightcooked *cooked);
651
652 #endif /* _CC_H_ */