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