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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_14;
24 public class AltosCSV implements AltosWriter {
27 boolean header_written;
38 boolean has_flight_state;
44 boolean has_companion;
45 boolean has_motor_pressure;
47 AltosFlightSeries series;
50 static final int ALTOS_CSV_VERSION = 6;
59 * time (seconds since boost)
61 * Radio info (if available)
74 * accelerometer speed (m/s)
75 * barometer speed (m/s)
83 * Advanced sensors (if available)
95 * Extra igniter voltages (if available)
102 * GPS data (if available)
105 * nsat (used for solution)
116 * from_pad_azimuth (deg true)
118 * from_pad_elevation (deg from horizon)
124 * C/N0 data for all 32 valid SDIDs
127 * companion_id (1-255. 10 is TeleScience)
128 * time of last companion data (seconds since boost)
129 * update_period (0.1-2.55 minimum telemetry interval)
131 * channel data for all 12 possible channels
134 void write_general_header() {
135 out.printf("version,serial,flight");
136 if (series.cal_data().callsign != null)
142 return series.time(indices);
145 void write_general() {
146 out.printf("%s, %d, %d",
148 series.cal_data().serial,
149 series.cal_data().flight);
150 if (series.cal_data().callsign != null)
151 out.printf(",%s", series.cal_data().callsign);
152 out.printf(", %8.2f", time());
155 void write_radio_header() {
156 out.printf("rssi,lqi");
160 return (int) series.value(AltosFlightSeries.rssi_name, indices);
164 return (int) series.value(AltosFlightSeries.status_name, indices);
168 out.printf("%4d, %3d",
169 rssi(), status() & 0x7f);
172 void write_flight_header() {
173 out.printf("state,state_name");
177 return (int) series.value(AltosFlightSeries.state_name, indices);
180 void write_flight() {
182 out.printf("%2d,%8s", state, AltosLib.state_name(state));
185 void write_basic_header() {
186 /* presence of thrust data means this is a test stand */
188 out.printf("pressure,thrust");
191 out.printf("acceleration,");
193 out.printf("pressure,altitude,");
194 out.printf("height,speed");
196 out.printf(",temperature");
198 out.printf(",drogue_voltage,main_voltage");
202 double pressure() { return series.value(AltosFlightSeries.pressure_name, indices); }
203 double thrust() { return series.value(AltosFlightSeries.thrust_name, indices); }
206 /* presence of thrust data means this is a test stand */
208 out.printf("%10.2f,%10.2f",
213 out.printf("%8.2f,", acceleration());
215 out.printf("%10.2f,%8.2f,",
216 pressure(), altitude());
217 out.printf("%8.2f,%8.2f",
220 out.printf(",%5.1f", temperature());
222 out.printf(",%5.2f,%5.2f",
228 void write_battery_header() {
229 out.printf("battery_voltage");
232 double battery_voltage() { return series.value(AltosFlightSeries.battery_voltage_name, indices); }
234 void write_battery() {
235 out.printf("%5.2f", battery_voltage());
238 void write_motor_pressure_header() {
239 out.printf("motor_pressure");
242 double motor_pressure() { return series.value(AltosFlightSeries.motor_pressure_name, indices); }
244 void write_motor_pressure() {
245 out.printf("%10.1f", motor_pressure());
248 void write_3d_accel_header() {
249 out.printf("accel_x,accel_y,accel_z");
252 double accel_along() { return series.value(AltosFlightSeries.accel_along_name, indices); }
253 double accel_across() { return series.value(AltosFlightSeries.accel_across_name, indices); }
254 double accel_through() { return series.value(AltosFlightSeries.accel_through_name, indices); }
256 void write_3d_accel() {
257 out.printf("%7.2f,%7.2f,%7.2f",
258 accel_along(), accel_across(), accel_through());
261 void write_imu_header() {
262 out.printf("gyro_roll,gyro_pitch,gyro_yaw,mag_x,mag_y,mag_z,tilt");
265 double gyro_roll() { return series.value(AltosFlightSeries.gyro_roll_name, indices); }
266 double gyro_pitch() { return series.value(AltosFlightSeries.gyro_pitch_name, indices); }
267 double gyro_yaw() { return series.value(AltosFlightSeries.gyro_yaw_name, indices); }
269 double mag_along() { return series.value(AltosFlightSeries.mag_along_name, indices); }
270 double mag_across() { return series.value(AltosFlightSeries.mag_across_name, indices); }
271 double mag_through() { return series.value(AltosFlightSeries.mag_through_name, indices); }
273 double tilt() { return series.value(AltosFlightSeries.orient_name, indices); }
276 out.printf("%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f",
277 gyro_roll(), gyro_pitch(), gyro_yaw(),
278 mag_along(), mag_across(), mag_through(),
282 void write_igniter_header() {
284 for (int i = 0; i < series.igniter_voltage.length; i++)
285 out.printf(",%s", AltosLib.igniter_short_name(i));
288 double pyro() { return series.value(AltosFlightSeries.pyro_voltage_name, indices); }
290 double igniter_value(int channel) { return series.value(series.igniter_voltage_name(channel), indices); }
292 void write_igniter() {
293 out.printf("%5.2f", pyro());
294 for (int i = 0; i < series.igniter_voltage.length; i++)
295 out.printf(",%5.2f", igniter_value(i));
298 void write_gps_header() {
299 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,pdop,hdop,vdop");
303 AltosGPS gps = series.gps_before(series.time(indices));
305 AltosGreatCircle from_pad;
307 if (series.cal_data().gps_pad != null && gps != null)
308 from_pad = new AltosGreatCircle(series.cal_data().gps_pad, gps);
310 from_pad = new AltosGreatCircle();
313 gps = new AltosGPS();
315 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,%6.1f,%6.1f",
337 void write_gps_sat_header() {
338 for(int i = 1; i <= 32; i++) {
339 out.printf("sat%02d", i);
345 void write_gps_sat() {
346 AltosGPS gps = series.gps_before(series.time(indices));
347 for(int i = 1; i <= 32; i++) {
349 if (gps != null && gps.cc_gps_sat != null) {
350 for(int j = 0; j < gps.cc_gps_sat.length; j++)
351 if (gps.cc_gps_sat[j].svid == i) {
352 c_n0 = gps.cc_gps_sat[j].c_n0;
356 out.printf ("%3d", c_n0);
362 void write_companion_header() {
364 out.printf("companion_id,companion_time,companion_update,companion_channels");
365 for (int i = 0; i < 12; i++)
366 out.printf(",companion_%02d", i);
370 void write_companion() {
372 AltosCompanion companion = state.companion;
374 int channels_written = 0;
375 if (companion == null) {
376 out.printf("0,0,0,0");
378 out.printf("%3d,%5.2f,%5.2f,%2d",
380 (companion.tick - boost_tick) / 100.0,
381 companion.update_period / 100.0,
383 for (; channels_written < companion.channels; channels_written++)
384 out.printf(",%5d", companion.companion_data[channels_written]);
386 for (; channels_written < 12; channels_written++)
391 void write_header() {
392 out.printf("#"); write_general_header();
395 write_radio_header();
397 if (has_flight_state) {
399 write_flight_header();
403 write_basic_header();
407 write_battery_header();
409 if (has_motor_pressure) {
411 write_motor_pressure_header();
415 write_3d_accel_header();
423 write_igniter_header();
431 write_gps_sat_header();
435 write_companion_header();
446 if (has_flight_state) {
458 if (has_motor_pressure) {
460 write_motor_pressure();
489 private void write() {
490 if (state() == AltosLib.ao_flight_startup)
492 if (!header_written) {
494 header_written = true;
499 private PrintStream out() {
503 public void close() {
507 public void write(AltosFlightSeries series) {
508 // series.write_comments(out());
510 this.series = series;
515 has_flight_state = false;
521 has_motor_pressure = false;
522 has_3d_accel = false;
527 has_companion = false;
529 if (series.has_series(AltosFlightSeries.rssi_name))
531 if (series.has_series(AltosFlightSeries.state_name))
532 has_flight_state = true;
533 if (series.has_series(AltosFlightSeries.accel_name)) {
538 if (series.has_series(AltosFlightSeries.thrust_name))
541 if (series.has_series(AltosFlightSeries.pressure_name)) {
545 if (series.has_series(AltosFlightSeries.apogee_voltage_name))
547 if (series.has_series(AltosFlightSeries.battery_voltage_name))
549 if (series.has_series(AltosFlightSeries.motor_pressure_name))
550 has_motor_pressure = true;
551 if (series.has_series(AltosFlightSeries.accel_across_name))
553 if (series.has_series(AltosFlightSeries.gyro_roll_name))
555 if (series.has_series(AltosFlightSeries.pyro_voltage_name))
558 if (series.gps_series != null)
560 if (series.sats_in_view != null)
563 if (state.companion != null)
564 has_companion = true;
567 indices = series.indices();
571 if (!series.step_indices(indices))
576 public AltosCSV(PrintStream in_out, File in_name) {
581 public AltosCSV(File in_name) throws FileNotFoundException {
582 this(new PrintStream(in_name), in_name);
585 public AltosCSV(String in_string) throws FileNotFoundException {
586 this(new File(in_string));