altoslib: Missing headers for mag sensor in CSV output
[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,mag_x,mag_y,mag_z");
162         }
163
164         void write_advanced(AltosState state) {
165                 out.printf("%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f",
166                            state.accel_along(), state.accel_across(), state.accel_through(),
167                            state.gyro_roll(), state.gyro_pitch(), state.gyro_yaw(),
168                            state.mag_along(), state.mag_across(), state.mag_through());
169         }
170
171         void write_gps_header() {
172                 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");
173         }
174
175         void write_gps(AltosState state) {
176                 AltosGPS        gps = state.gps;
177                 if (gps == null)
178                         gps = new AltosGPS();
179
180                 AltosGreatCircle from_pad = state.from_pad;
181                 if (from_pad == null)
182                         from_pad = new AltosGreatCircle();
183
184                 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",
185                            gps.connected?1:0,
186                            gps.locked?1:0,
187                            gps.nsat,
188                            gps.lat,
189                            gps.lon,
190                            gps.alt,
191                            gps.year,
192                            gps.month,
193                            gps.day,
194                            gps.hour,
195                            gps.minute,
196                            gps.second,
197                            from_pad.distance,
198                            state.range,
199                            from_pad.bearing,
200                            state.elevation,
201                            gps.pdop,
202                            gps.hdop,
203                            gps.vdop);
204         }
205
206         void write_gps_sat_header() {
207                 for(int i = 1; i <= 32; i++) {
208                         out.printf("sat%02d", i);
209                         if (i != 32)
210                                 out.printf(",");
211                 }
212         }
213
214         void write_gps_sat(AltosState state) {
215                 AltosGPS        gps = state.gps;
216                 for(int i = 1; i <= 32; i++) {
217                         int     c_n0 = 0;
218                         if (gps != null && gps.cc_gps_sat != null) {
219                                 for(int j = 0; j < gps.cc_gps_sat.length; j++)
220                                         if (gps.cc_gps_sat[j].svid == i) {
221                                                 c_n0 = gps.cc_gps_sat[j].c_n0;
222                                                 break;
223                                         }
224                         }
225                         out.printf ("%3d", c_n0);
226                         if (i != 32)
227                                 out.printf(",");
228                 }
229         }
230
231         void write_companion_header() {
232                 out.printf("companion_id,companion_time,companion_update,companion_channels");
233                 for (int i = 0; i < 12; i++)
234                         out.printf(",companion_%02d", i);
235         }
236
237         void write_companion(AltosState state) {
238                 AltosCompanion companion = state.companion;
239
240                 int     channels_written = 0;
241                 if (companion == null) {
242                         out.printf("0,0,0,0");
243                 } else {
244                         out.printf("%3d,%5.2f,%5.2f,%2d",
245                                    companion.board_id,
246                                    (companion.tick - boost_tick) / 100.0,
247                                    companion.update_period / 100.0,
248                                    companion.channels);
249                         for (; channels_written < companion.channels; channels_written++)
250                                 out.printf(",%5d", companion.companion_data[channels_written]);
251                 }
252                 for (; channels_written < 12; channels_written++)
253                         out.printf(",0");
254         }
255
256         void write_header() {
257                 out.printf("#"); write_general_header();
258                 if (has_flight_state) {
259                         out.printf(",");
260                         write_flight_header();
261                 }
262                 if (has_basic) {
263                         out.printf(",");
264                         write_basic_header();
265                 }
266                 if (has_battery) {
267                         out.printf(",");
268                         write_battery_header();
269                 }
270                 if (has_advanced) {
271                         out.printf(",");
272                         write_advanced_header();
273                 }
274                 if (has_gps) {
275                         out.printf(",");
276                         write_gps_header();
277                 }
278                 if (has_gps_sat) {
279                         out.printf(",");
280                         write_gps_sat_header();
281                 }
282                 if (has_companion) {
283                         out.printf(",");
284                         write_companion_header();
285                 }
286                 out.printf ("\n");
287         }
288
289         void write_one(AltosState state) {
290                 write_general(state);
291                 if (has_flight_state) {
292                         out.printf(",");
293                         write_flight(state);
294                 }
295                 if (has_basic) {
296                         out.printf(",");
297                         write_basic(state);
298                 }
299                 if (has_battery) {
300                         out.printf(",");
301                         write_battery(state);
302                 }
303                 if (has_advanced) {
304                         out.printf(",");
305                         write_advanced(state);
306                 }
307                 if (has_gps) {
308                         out.printf(",");
309                         write_gps(state);
310                 }
311                 if (has_gps_sat) {
312                         out.printf(",");
313                         write_gps_sat(state);
314                 }
315                 if (has_companion) {
316                         out.printf(",");
317                         write_companion(state);
318                 }
319                 out.printf ("\n");
320         }
321
322         private void flush_pad() {
323                 while (!pad_states.isEmpty()) {
324                         write_one (pad_states.remove());
325                 }
326         }
327
328         private void write(AltosState state) {
329                 if (state.state == AltosLib.ao_flight_startup)
330                         return;
331                 if (!header_written) {
332                         write_header();
333                         header_written = true;
334                 }
335                 if (!seen_boost) {
336                         if (state.state >= AltosLib.ao_flight_boost) {
337                                 seen_boost = true;
338                                 boost_tick = state.tick;
339                                 flush_pad();
340                         }
341                 }
342                 if (seen_boost)
343                         write_one(state);
344                 else
345                         pad_states.add(state);
346         }
347
348         private PrintStream out() {
349                 return out;
350         }
351
352         public void close() {
353                 if (!pad_states.isEmpty()) {
354                         boost_tick = pad_states.element().tick;
355                         flush_pad();
356                 }
357                 out.close();
358         }
359
360         public void write(AltosStateIterable states) {
361                 states.write_comments(out());
362
363                 has_flight_state = false;
364                 has_basic = false;
365                 has_battery = false;
366                 has_advanced = false;
367                 has_gps = false;
368                 has_gps_sat = false;
369                 has_companion = false;
370                 for (AltosState state : states) {
371                         if (state.state != AltosLib.ao_flight_stateless && state.state != AltosLib.ao_flight_invalid && state.state != AltosLib.ao_flight_startup)
372                                 has_flight_state = true;
373                         if (state.acceleration() != AltosLib.MISSING || state.pressure() != AltosLib.MISSING)
374                                 has_basic = true;
375                         if (state.battery_voltage != AltosLib.MISSING)
376                                 has_battery = true;
377                         if (state.accel_across() != AltosLib.MISSING)
378                                 has_advanced = true;
379                         if (state.gps != null) {
380                                 has_gps = true;
381                                 if (state.gps.cc_gps_sat != null)
382                                         has_gps_sat = true;
383                         }
384                         if (state.companion != null)
385                                 has_companion = true;
386                 }
387                 for (AltosState state : states)
388                         write(state);
389         }
390
391         public AltosCSV(PrintStream in_out, File in_name) {
392                 name = in_name;
393                 out = in_out;
394                 pad_states = new LinkedList<AltosState>();
395         }
396
397         public AltosCSV(File in_name) throws FileNotFoundException {
398                 this(new PrintStream(in_name), in_name);
399         }
400
401         public AltosCSV(String in_string) throws FileNotFoundException {
402                 this(new File(in_string));
403         }
404 }