Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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 = 2;
35
36         /* Version 2 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
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
86         void write_general_header() {
87                 out.printf("version,serial,flight,call,time,rssi,lqi");
88         }
89
90         void write_general(AltosRecord record) {
91                 out.printf("%s, %d, %d, %s, %8.2f, %4d, %3d",
92                            ALTOS_CSV_VERSION, record.serial, record.flight, record.callsign,
93                            (double) record.time,
94                            record.rssi,
95                            record.status & 0x7f);
96         }
97
98         void write_flight_header() {
99                 out.printf("state,state_name");
100         }
101
102         void write_flight(AltosRecord record) {
103                 out.printf("%d,%8s", record.state, record.state());
104         }
105
106         void write_basic_header() {
107                 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
108         }
109
110         void write_basic(AltosRecord record) {
111                 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
112                            record.acceleration(),
113                            record.raw_pressure(),
114                            record.raw_altitude(),
115                            record.raw_height(),
116                            record.accel_speed(),
117                            state.baro_speed,
118                            record.temperature(),
119                            record.battery_voltage(),
120                            record.drogue_voltage(),
121                            record.main_voltage());
122         }
123
124         void write_gps_header() {
125                 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
126         }
127
128         void write_gps(AltosRecord record) {
129                 AltosGPS        gps = record.gps;
130                 if (gps == null)
131                         gps = new AltosGPS();
132
133                 AltosGreatCircle from_pad = state.from_pad;
134                 if (from_pad == null)
135                         from_pad = new AltosGreatCircle();
136
137                 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",
138                            gps.connected?1:0,
139                            gps.locked?1:0,
140                            gps.nsat,
141                            gps.lat,
142                            gps.lon,
143                            gps.alt,
144                            gps.year,
145                            gps.month,
146                            gps.day,
147                            gps.hour,
148                            gps.minute,
149                            gps.second,
150                            from_pad.distance,
151                            state.range,
152                            from_pad.bearing,
153                            state.elevation,
154                            gps.hdop);
155         }
156
157         void write_gps_sat_header() {
158                 for(int i = 1; i <= 32; i++) {
159                         out.printf("sat%02d", i);
160                         if (i != 32)
161                                 out.printf(",");
162                 }
163         }
164
165         void write_gps_sat(AltosRecord record) {
166                 AltosGPS        gps = record.gps;
167                 for(int i = 1; i <= 32; i++) {
168                         int     c_n0 = 0;
169                         if (gps != null && gps.cc_gps_sat != null) {
170                                 for(int j = 0; j < gps.cc_gps_sat.length; j++)
171                                         if (gps.cc_gps_sat[j].svid == i) {
172                                                 c_n0 = gps.cc_gps_sat[j].c_n0;
173                                                 break;
174                                         }
175                         }
176                         out.printf ("%3d", c_n0);
177                         if (i != 32)
178                                 out.printf(",");
179                 }
180         }
181
182         void write_header() {
183                 out.printf("#"); write_general_header();
184                 out.printf(","); write_flight_header();
185                 out.printf(","); write_basic_header();
186                 out.printf(","); write_gps_header();
187                 out.printf(","); write_gps_sat_header();
188                 out.printf ("\n");
189         }
190
191         void write_one(AltosRecord record) {
192                 state = new AltosState(record, state);
193                 write_general(record); out.printf(",");
194                 write_flight(record); out.printf(",");
195                 write_basic(record); out.printf(",");
196                 write_gps(record); out.printf(",");
197                 write_gps_sat(record);
198                 out.printf ("\n");
199         }
200
201         void flush_pad() {
202                 while (!pad_records.isEmpty()) {
203                         write_one (pad_records.remove());
204                 }
205         }
206
207         public void write(AltosRecord record) {
208                 if (!header_written) {
209                         write_header();
210                         header_written = true;
211                 }
212                 if (!seen_boost) {
213                         if (record.state >= Altos.ao_flight_boost) {
214                                 seen_boost = true;
215                                 boost_tick = record.tick;
216                                 flush_pad();
217                         }
218                 }
219                 if (seen_boost)
220                         write_one(record);
221                 else
222                         pad_records.add(record);
223         }
224
225         public PrintStream out() {
226                 return out;
227         }
228
229         public void close() {
230                 if (!pad_records.isEmpty()) {
231                         boost_tick = pad_records.element().tick;
232                         flush_pad();
233                 }
234                 out.close();
235         }
236
237         public void write(AltosRecordIterable iterable) {
238                 iterable.write_comments(out());
239                 for (AltosRecord r : iterable)
240                         write(r);
241         }
242
243         public AltosCSV(File in_name) throws FileNotFoundException {
244                 name = in_name;
245                 out = new PrintStream(name);
246                 pad_records = new LinkedList<AltosRecord>();
247         }
248
249         public AltosCSV(String in_string) throws FileNotFoundException {
250                 this(new File(in_string));
251         }
252 }