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