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