3975cf1b46d8c470cc4debe3670f78bc441278e4
[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 };
79
80
81 /*
82  * For GPS data
83  */
84
85 struct cc_gpselt {
86         double          time;
87         double          lat;
88         double          lon;
89         double          alt;
90 };
91
92 struct cc_gpsdata {
93         int                     num;
94         int                     size;
95         double                  time_offset;
96         struct cc_gpselt        *data;
97 };
98
99 /*
100  * For sequential data which are evenly spaced
101  */
102 struct cc_perioddata {
103         int             num;
104         double          start;
105         double          step;
106         double          *data;
107 };
108
109 enum ao_flight_state {
110         ao_flight_startup = 0,
111         ao_flight_idle = 1,
112         ao_flight_pad = 2,
113         ao_flight_boost = 3,
114         ao_flight_fast = 4,
115         ao_flight_coast = 5,
116         ao_flight_drogue = 6,
117         ao_flight_main = 7,
118         ao_flight_landed = 8,
119         ao_flight_invalid = 9
120 };
121
122 struct cc_flightraw {
123         int                     flight;
124         int                     serial;
125         double                  ground_accel;
126         double                  ground_pres;
127         struct cc_timedata      accel;
128         struct cc_timedata      pres;
129         struct cc_timedata      temp;
130         struct cc_timedata      volt;
131         struct cc_timedata      main;
132         struct cc_timedata      drogue;
133         struct cc_timedata      state;
134         struct cc_gpsdata       gps;
135 };
136
137 struct cc_flightraw *
138 cc_log_read(FILE *file);
139
140 void
141 cc_flightraw_free(struct cc_flightraw *raw);
142
143 struct cc_flightcooked {
144         struct cc_perioddata    accel_accel;
145         struct cc_perioddata    accel_speed;
146         struct cc_perioddata    accel_pos;
147         struct cc_perioddata    pres_pos;
148         struct cc_perioddata    pres_speed;
149         struct cc_perioddata    pres_accel;
150         struct cc_perioddata    gps_lat;
151         struct cc_perioddata    gps_lon;
152         struct cc_perioddata    gps_alt;
153         struct cc_timedata      state;
154 };
155
156 /*
157  * Telemetry data contents
158  */
159
160
161 struct cc_gps_time {
162         int hour;
163         int minute;
164         int second;
165 };
166
167 struct cc_gps {
168         int     nsat;
169         int     gps_locked;
170         int     gps_connected;
171         struct cc_gps_time gps_time;
172         double  lat;            /* degrees (+N -S) */
173         double  lon;            /* degrees (+E -W) */
174         int     alt;            /* m */
175
176         int     gps_extended;   /* has extra data */
177         double  ground_speed;   /* m/s */
178         int     course;         /* degrees */
179         double  climb_rate;     /* m/s */
180         double  hdop;           /* unitless? */
181         int     h_error;        /* m */
182         int     v_error;        /* m */
183 };
184
185 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
186 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
187 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
188 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
189 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
190 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
191 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
192 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
193
194 struct cc_gps_sat {
195         int     svid;
196         int     state;
197         int     c_n0;
198 };
199
200 struct cc_gps_tracking {
201         int                     channels;
202         struct cc_gps_sat       sats[12];
203 };
204
205 struct cc_telem {
206         char    callsign[16];
207         int     serial;
208         int     rssi;
209         char    state[16];
210         int     tick;
211         int     accel;
212         int     pres;
213         int     temp;
214         int     batt;
215         int     drogue;
216         int     main;
217         int     flight_accel;
218         int     ground_accel;
219         int     flight_vel;
220         int     flight_pres;
221         int     ground_pres;
222         struct cc_gps   gps;
223         struct cc_gps_tracking  gps_tracking;
224 };
225
226 int
227 cc_telem_parse(const char *input_line, struct cc_telem *telem);
228
229 #ifndef TRUE
230 #define TRUE 1
231 #define FALSE 0
232 #endif
233
234 /* Conversion functions */
235 double
236 cc_pressure_to_altitude(double pressure);
237
238 double
239 cc_altitude_to_pressure(double altitude);
240
241 double
242 cc_barometer_to_pressure(double baro);
243
244 double
245 cc_barometer_to_altitude(double baro);
246
247 double
248 cc_accelerometer_to_acceleration(double accel, double ground_accel);
249
250 double
251 cc_thermometer_to_temperature(double thermo);
252
253 double
254 cc_battery_to_voltage(double battery);
255
256 double
257 cc_ignitor_to_voltage(double ignite);
258
259 void
260 cc_great_circle (double start_lat, double start_lon,
261                  double end_lat, double end_lon,
262                  double *dist, double *bearing);
263
264 int
265 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time);
266
267 int
268 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time);
269
270 #endif /* _CC_H_ */