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