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