Hax0r graphing to support telem/eeprom files
[fw/altos] / ao-tools / 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 import altosui.AltosRecord;
26
27 public class AltosCSV {
28         File                    name;
29         PrintStream             out;
30         boolean                 header_written;
31         boolean                 seen_boost;
32         int                     boost_tick;
33         LinkedList<AltosRecord> pad_records;
34         AltosState              state;
35
36         static final int ALTOS_CSV_VERSION = 1;
37
38         /* Version 1 format:
39          *
40          * General info
41          *      version number
42          *      serial number
43          *      flight number
44          *      callsign
45          *      time (seconds since boost)
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          * GPS data
66          *      connected (1/0)
67          *      locked (1/0)
68          *      nsat (used for solution)
69          *      latitude (°)
70          *      longitude (°)
71          *      altitude (m)
72          *      year (e.g. 2010)
73          *      month (1-12)
74          *      day (1-31)
75          *      hour (0-23)
76          *      minute (0-59)
77          *      second (0-59)
78          *      from_pad_dist (m)
79          *      from_pad_azimuth (deg true)
80          *      from_pad_range (m)
81          *      from_pad_elevation (deg from horizon)
82          *      hdop
83          *
84          * GPS Sat data
85          *      C/N0 data for all 32 valid SDIDs
86          */
87
88         void write_general_header() {
89                 out.printf("version,serial,flight,call,time,rssi,lqi");
90         }
91
92         void write_general(AltosRecord record) {
93                 out.printf("%s, %d, %d, %s, %8.2f, %4d, %3d",
94                            record.version, record.serial, record.flight, record.callsign,
95                            (double) record.time,
96                            record.rssi,
97                            record.status & 0x7f);
98         }
99
100         void write_flight_header() {
101                 out.printf("state,state_name");
102         }
103
104         void write_flight(AltosRecord record) {
105                 out.printf("%d,%8s", record.state, record.state());
106         }
107
108         void write_basic_header() {
109                 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
110         }
111
112         void write_basic(AltosRecord record) {
113                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
114                            record.acceleration(),
115                            record.raw_pressure(),
116                            record.raw_altitude(),
117                            record.raw_height(),
118                            record.accel_speed(),
119                            state.baro_speed,
120                            record.temperature(),
121                            record.battery_voltage(),
122                            record.drogue_voltage(),
123                            record.main_voltage());
124         }
125
126         void write_gps_header() {
127                 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
128         }
129
130         void write_gps(AltosRecord record) {
131                 AltosGPS        gps = record.gps;
132                 if (gps == null)
133                         gps = new AltosGPS();
134
135                 AltosGreatCircle from_pad = state.from_pad;
136                 if (from_pad == null)
137                         from_pad = new AltosGreatCircle();
138
139                 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",
140                            gps.connected?1:0,
141                            gps.locked?1:0,
142                            gps.nsat,
143                            gps.lat,
144                            gps.lon,
145                            gps.alt,
146                            gps.year,
147                            gps.month,
148                            gps.day,
149                            gps.hour,
150                            gps.minute,
151                            gps.second,
152                            from_pad.distance,
153                            state.range,
154                            from_pad.bearing,
155                            state.elevation,
156                            gps.hdop);
157         }
158
159         void write_gps_sat_header() {
160                 for(int i = 1; i <= 32; i++) {
161                         out.printf("sat%02d", i);
162                         if (i != 32)
163                                 out.printf(",");
164                 }
165         }
166
167         void write_gps_sat(AltosRecord record) {
168                 AltosGPS        gps = record.gps;
169                 for(int i = 1; i <= 32; i++) {
170                         int     c_n0 = 0;
171                         if (gps != null && gps.cc_gps_sat != null) {
172                                 for(int j = 0; j < gps.cc_gps_sat.length; j++)
173                                         if (gps.cc_gps_sat[j].svid == i) {
174                                                 c_n0 = gps.cc_gps_sat[j].c_n0;
175                                                 break;
176                                         }
177                         }
178                         out.printf ("%3d", c_n0);
179                         if (i != 32)
180                                 out.printf(",");
181                 }
182         }
183
184         void write_header() {
185                 out.printf("#"); write_general_header();
186                 out.printf(","); write_flight_header();
187                 out.printf(","); write_basic_header();
188                 out.printf(","); write_gps_header();
189                 out.printf(","); write_gps_sat_header();
190                 out.printf ("\n");
191         }
192
193         void write_one(AltosRecord record) {
194                 state = new AltosState(record, state);
195                 write_general(record); out.printf(",");
196                 write_flight(record); out.printf(",");
197                 write_basic(record); out.printf(",");
198                 write_gps(record); out.printf(",");
199                 write_gps_sat(record);
200                 out.printf ("\n");
201         }
202
203         void flush_pad() {
204                 while (!pad_records.isEmpty()) {
205                         write_one (pad_records.remove());
206                 }
207         }
208
209         public void write(AltosRecord record) {
210                 if (!header_written) {
211                         write_header();
212                         header_written = true;
213                 }
214                 if (!seen_boost) {
215                         if (record.state >= Altos.ao_flight_boost) {
216                                 seen_boost = true;
217                                 boost_tick = record.tick;
218                                 flush_pad();
219                         }
220                 }
221                 if (seen_boost)
222                         write_one(record);
223                 else
224                         pad_records.add(record);
225         }
226
227         public PrintStream out() {
228                 return out;
229         }
230
231         public void close() {
232                 if (!pad_records.isEmpty()) {
233                         boost_tick = pad_records.element().tick;
234                         flush_pad();
235                 }
236                 out.close();
237         }
238
239         public void write(AltosRecordIterable iterable) {
240                 iterable.write_comments(out());
241                 for (AltosRecord r : iterable)
242                         write(r);
243         }
244
245         public AltosCSV(File in_name) throws FileNotFoundException {
246                 name = in_name;
247                 out = new PrintStream(name);
248                 pad_records = new LinkedList<AltosRecord>();
249         }
250
251         public AltosCSV(String in_string) throws FileNotFoundException {
252                 this(new File(in_string));
253         }
254 }