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