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