altos: Extend GPS altitudes to at least 24 bits everywhere
[fw/altos] / altoslib / AltosCSV.java
1 /*
2  * Copyright © 2010 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 package org.altusmetrum.altoslib_5;
19
20 import java.io.*;
21 import java.util.*;
22
23 public class AltosCSV implements AltosWriter {
24         File                    name;
25         PrintStream             out;
26         boolean                 header_written;
27         boolean                 seen_boost;
28         int                     boost_tick;
29         LinkedList<AltosState>  pad_states;
30         AltosState              state;
31
32         static boolean          has_basic;
33         static boolean          has_battery;
34         static boolean          has_flight_state;
35         static boolean          has_advanced;
36         static boolean          has_gps;
37         static boolean          has_gps_sat;
38         static boolean          has_companion;
39
40         static final int ALTOS_CSV_VERSION = 5;
41
42         /* Version 4 format:
43          *
44          * General info
45          *      version number
46          *      serial number
47          *      flight number
48          *      callsign
49          *      time (seconds since boost)
50          *      clock (tick count / 100)
51          *      rssi
52          *      link quality
53          *
54          * Flight status
55          *      state
56          *      state name
57          *
58          * Basic sensors
59          *      acceleration (m/s²)
60          *      pressure (mBar)
61          *      altitude (m)
62          *      height (m)
63          *      accelerometer speed (m/s)
64          *      barometer speed (m/s)
65          *      temp (°C)
66          *      drogue (V)
67          *      main (V)
68          *
69          * Battery
70          *      battery (V)
71          *
72          * Advanced sensors (if available)
73          *      accel_x (m/s²)
74          *      accel_y (m/s²)
75          *      accel_z (m/s²)
76          *      gyro_x (d/s)
77          *      gyro_y (d/s)
78          *      gyro_z (d/s)
79          *      mag_x (g)
80          *      mag_y (g)
81          *      mag_z (g)
82          *
83          * GPS data (if available)
84          *      connected (1/0)
85          *      locked (1/0)
86          *      nsat (used for solution)
87          *      latitude (°)
88          *      longitude (°)
89          *      altitude (m)
90          *      year (e.g. 2010)
91          *      month (1-12)
92          *      day (1-31)
93          *      hour (0-23)
94          *      minute (0-59)
95          *      second (0-59)
96          *      from_pad_dist (m)
97          *      from_pad_azimuth (deg true)
98          *      from_pad_range (m)
99          *      from_pad_elevation (deg from horizon)
100          *      pdop
101          *      hdop
102          *      vdop
103          *
104          * GPS Sat data
105          *      C/N0 data for all 32 valid SDIDs
106          *
107          * Companion data
108          *      companion_id (1-255. 10 is TeleScience)
109          *      time of last companion data (seconds since boost)
110          *      update_period (0.1-2.55 minimum telemetry interval)
111          *      channels (0-12)
112          *      channel data for all 12 possible channels
113          */
114
115         void write_general_header() {
116                 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
117         }
118
119         void write_general(AltosState state) {
120                 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
121                            ALTOS_CSV_VERSION, state.serial, state.flight, state.callsign,
122                            (double) state.time_since_boost(), (double) state.tick / 100.0,
123                            state.rssi,
124                            state.status & 0x7f);
125         }
126
127         void write_flight_header() {
128                 out.printf("state,state_name");
129         }
130
131         void write_flight(AltosState state) {
132                 out.printf("%d,%8s", state.state, state.state_name());
133         }
134
135         void write_basic_header() {
136                 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,drogue_voltage,main_voltage");
137         }
138
139         void write_basic(AltosState state) {
140                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f",
141                            state.acceleration(),
142                            state.pressure(),
143                            state.altitude(),
144                            state.height(),
145                            state.speed(),
146                            state.speed(),
147                            state.temperature,
148                            state.apogee_voltage,
149                            state.main_voltage);
150         }
151
152         void write_battery_header() {
153                 out.printf("battery_voltage");
154         }
155
156         void write_battery(AltosState state) {
157                 out.printf("%5.2f", state.battery_voltage);
158         }
159
160         void write_advanced_header() {
161                 out.printf("accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
162         }
163
164         void write_advanced(AltosState state) {
165                 AltosIMU        imu = state.imu;
166                 AltosMag        mag = state.mag;
167
168                 if (imu == null)
169                         imu = new AltosIMU();
170                 if (mag == null)
171                         mag = new AltosMag();
172                 out.printf("%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f",
173                            imu.accel_x, imu.accel_y, imu.accel_z,
174                            imu.gyro_x, imu.gyro_y, imu.gyro_z,
175                            mag.x, mag.y, mag.z);
176         }
177
178         void write_gps_header() {
179                 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,pdop,hdop,vdop");
180         }
181
182         void write_gps(AltosState state) {
183                 AltosGPS        gps = state.gps;
184                 if (gps == null)
185                         gps = new AltosGPS();
186
187                 AltosGreatCircle from_pad = state.from_pad;
188                 if (from_pad == null)
189                         from_pad = new AltosGreatCircle();
190
191                 out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%8.1f,%5d,%3d,%3d,%3d,%3d,%3d,%9.0f,%9.0f,%4.0f,%4.0f,%6.1f,%6.1f,%6.1f",
192                            gps.connected?1:0,
193                            gps.locked?1:0,
194                            gps.nsat,
195                            gps.lat,
196                            gps.lon,
197                            gps.alt,
198                            gps.year,
199                            gps.month,
200                            gps.day,
201                            gps.hour,
202                            gps.minute,
203                            gps.second,
204                            from_pad.distance,
205                            state.range,
206                            from_pad.bearing,
207                            state.elevation,
208                            gps.pdop,
209                            gps.hdop,
210                            gps.vdop);
211         }
212
213         void write_gps_sat_header() {
214                 for(int i = 1; i <= 32; i++) {
215                         out.printf("sat%02d", i);
216                         if (i != 32)
217                                 out.printf(",");
218                 }
219         }
220
221         void write_gps_sat(AltosState state) {
222                 AltosGPS        gps = state.gps;
223                 for(int i = 1; i <= 32; i++) {
224                         int     c_n0 = 0;
225                         if (gps != null && gps.cc_gps_sat != null) {
226                                 for(int j = 0; j < gps.cc_gps_sat.length; j++)
227                                         if (gps.cc_gps_sat[j].svid == i) {
228                                                 c_n0 = gps.cc_gps_sat[j].c_n0;
229                                                 break;
230                                         }
231                         }
232                         out.printf ("%3d", c_n0);
233                         if (i != 32)
234                                 out.printf(",");
235                 }
236         }
237
238         void write_companion_header() {
239                 out.printf("companion_id,companion_time,companion_update,companion_channels");
240                 for (int i = 0; i < 12; i++)
241                         out.printf(",companion_%02d", i);
242         }
243
244         void write_companion(AltosState state) {
245                 AltosCompanion companion = state.companion;
246
247                 int     channels_written = 0;
248                 if (companion == null) {
249                         out.printf("0,0,0,0");
250                 } else {
251                         out.printf("%3d,%5.2f,%5.2f,%2d",
252                                    companion.board_id,
253                                    (companion.tick - boost_tick) / 100.0,
254                                    companion.update_period / 100.0,
255                                    companion.channels);
256                         for (; channels_written < companion.channels; channels_written++)
257                                 out.printf(",%5d", companion.companion_data[channels_written]);
258                 }
259                 for (; channels_written < 12; channels_written++)
260                         out.printf(",0");
261         }
262
263         void write_header() {
264                 out.printf("#"); write_general_header();
265                 if (has_flight_state) {
266                         out.printf(",");
267                         write_flight_header();
268                 }
269                 if (has_basic) {
270                         out.printf(",");
271                         write_basic_header();
272                 }
273                 if (has_battery) {
274                         out.printf(",");
275                         write_battery_header();
276                 }
277                 if (has_advanced) {
278                         out.printf(",");
279                         write_advanced_header();
280                 }
281                 if (has_gps) {
282                         out.printf(",");
283                         write_gps_header();
284                 }
285                 if (has_gps_sat) {
286                         out.printf(",");
287                         write_gps_sat_header();
288                 }
289                 if (has_companion) {
290                         out.printf(",");
291                         write_companion_header();
292                 }
293                 out.printf ("\n");
294         }
295
296         void write_one(AltosState state) {
297                 write_general(state);
298                 if (has_flight_state) {
299                         out.printf(",");
300                         write_flight(state);
301                 }
302                 if (has_basic) {
303                         out.printf(",");
304                         write_basic(state);
305                 }
306                 if (has_battery) {
307                         out.printf(",");
308                         write_battery(state);
309                 }
310                 if (has_advanced) {
311                         out.printf(",");
312                         write_advanced(state);
313                 }
314                 if (has_gps) {
315                         out.printf(",");
316                         write_gps(state);
317                 }
318                 if (has_gps_sat) {
319                         out.printf(",");
320                         write_gps_sat(state);
321                 }
322                 if (has_companion) {
323                         out.printf(",");
324                         write_companion(state);
325                 }
326                 out.printf ("\n");
327         }
328
329         private void flush_pad() {
330                 while (!pad_states.isEmpty()) {
331                         write_one (pad_states.remove());
332                 }
333         }
334
335         private void write(AltosState state) {
336                 if (state.state == AltosLib.ao_flight_startup)
337                         return;
338                 if (!header_written) {
339                         write_header();
340                         header_written = true;
341                 }
342                 if (!seen_boost) {
343                         if (state.state >= AltosLib.ao_flight_boost) {
344                                 seen_boost = true;
345                                 boost_tick = state.tick;
346                                 flush_pad();
347                         }
348                 }
349                 if (seen_boost)
350                         write_one(state);
351                 else
352                         pad_states.add(state);
353         }
354
355         private PrintStream out() {
356                 return out;
357         }
358
359         public void close() {
360                 if (!pad_states.isEmpty()) {
361                         boost_tick = pad_states.element().tick;
362                         flush_pad();
363                 }
364                 out.close();
365         }
366
367         public void write(AltosStateIterable states) {
368                 states.write_comments(out());
369
370                 has_flight_state = false;
371                 has_basic = false;
372                 has_battery = false;
373                 has_advanced = false;
374                 has_gps = false;
375                 has_gps_sat = false;
376                 has_companion = false;
377                 for (AltosState state : states) {
378                         if (state.state != AltosLib.ao_flight_stateless && state.state != AltosLib.ao_flight_invalid && state.state != AltosLib.ao_flight_startup)
379                                 has_flight_state = true;
380                         if (state.acceleration() != AltosLib.MISSING || state.pressure() != AltosLib.MISSING)
381                                 has_basic = true;
382                         if (state.battery_voltage != AltosLib.MISSING)
383                                 has_battery = true;
384                         if (state.imu != null || state.mag != null)
385                                 has_advanced = true;
386                         if (state.gps != null) {
387                                 has_gps = true;
388                                 if (state.gps.cc_gps_sat != null)
389                                         has_gps_sat = true;
390                         }
391                         if (state.companion != null)
392                                 has_companion = true;
393                 }
394                 for (AltosState state : states)
395                         write(state);
396         }
397
398         public AltosCSV(PrintStream in_out, File in_name) {
399                 name = in_name;
400                 out = in_out;
401                 pad_states = new LinkedList<AltosState>();
402         }
403
404         public AltosCSV(File in_name) throws FileNotFoundException {
405                 this(new PrintStream(in_name), in_name);
406         }
407
408         public AltosCSV(String in_string) throws FileNotFoundException {
409                 this(new File(in_string));
410         }
411 }