2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
25 public class AltosCSV implements AltosWriter {
28 boolean header_written;
31 LinkedList<AltosRecord> pad_records;
34 static final int ALTOS_CSV_VERSION = 3;
43 * time (seconds since boost)
56 * accelerometer speed (m/s)
57 * barometer speed (m/s)
63 * GPS data (if available)
66 * nsat (used for solution)
77 * from_pad_azimuth (deg true)
79 * from_pad_elevation (deg from horizon)
83 * C/N0 data for all 32 valid SDIDs
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)
90 * channel data for all 12 possible channels
93 void write_general_header() {
94 out.printf("version,serial,flight,call,time,rssi,lqi");
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,
102 record.status & 0x7f);
105 void write_flight_header() {
106 out.printf("state,state_name");
109 void write_flight(AltosRecord record) {
110 out.printf("%d,%8s", record.state, record.state());
113 void write_basic_header() {
114 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
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(),
123 record.accel_speed(),
125 record.temperature(),
126 record.battery_voltage(),
127 record.drogue_voltage(),
128 record.main_voltage());
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");
135 void write_gps(AltosRecord record) {
136 AltosGPS gps = record.gps;
138 gps = new AltosGPS();
140 AltosGreatCircle from_pad = state.from_pad;
141 if (from_pad == null)
142 from_pad = new AltosGreatCircle();
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",
164 void write_gps_sat_header() {
165 for(int i = 1; i <= 32; i++) {
166 out.printf("sat%02d", i);
172 void write_gps_sat(AltosRecord record) {
173 AltosGPS gps = record.gps;
174 for(int i = 1; i <= 32; i++) {
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;
183 out.printf ("%3d", c_n0);
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);
195 void write_companion(AltosRecord record) {
196 AltosRecordCompanion companion = record.companion;
198 int channels_written = 0;
199 if (companion == null) {
200 out.printf("0,0,0,0");
202 out.printf("%3d,%5.2f,%5.2f,%2d",
204 (companion.tick - boost_tick) / 100.0,
205 companion.update_period / 100.0,
207 for (; channels_written < companion.channels; channels_written++)
208 out.printf(",%5d", companion.companion_data[channels_written]);
210 for (; channels_written < 12; channels_written++)
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();
219 out.printf(","); write_gps_header();
220 out.printf(","); write_gps_sat_header();
223 out.printf(","); write_companion_header();
228 void write_one(AltosRecord record) {
229 state = new AltosState(record, state);
230 write_general(record); out.printf(",");
231 write_flight(record); out.printf(",");
233 if (record.gps != null) {
235 write_gps(record); out.printf(",");
236 write_gps_sat(record);
238 if (record.companion != null) {
240 write_companion(record);
246 while (!pad_records.isEmpty()) {
247 write_one (pad_records.remove());
251 public void write(AltosRecord record) {
252 if (record.state == Altos.ao_flight_startup)
254 if (!header_written) {
255 write_header(record.gps != null, record.companion != null);
256 header_written = true;
259 if (record.state >= Altos.ao_flight_boost) {
261 boost_tick = record.tick;
268 pad_records.add(record);
271 public PrintStream out() {
275 public void close() {
276 if (!pad_records.isEmpty()) {
277 boost_tick = pad_records.element().tick;
283 public void write(AltosRecordIterable iterable) {
284 iterable.write_comments(out());
285 for (AltosRecord r : iterable)
289 public AltosCSV(PrintStream in_out, File in_name) {
292 pad_records = new LinkedList<AltosRecord>();
295 public AltosCSV(File in_name) throws FileNotFoundException {
296 this(new PrintStream(in_name), in_name);
299 public AltosCSV(String in_string) throws FileNotFoundException {
300 this(new File(in_string));