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 = 4;
43 * time (seconds since boost)
44 * clock (tick count / 100)
57 * accelerometer speed (m/s)
58 * barometer speed (m/s)
64 * GPS data (if available)
67 * nsat (used for solution)
78 * from_pad_azimuth (deg true)
80 * from_pad_elevation (deg from horizon)
84 * C/N0 data for all 32 valid SDIDs
87 * companion_id (1-255. 10 is TeleScience)
88 * time of last companion data (seconds since boost)
89 * update_period (0.1-2.55 minimum telemetry interval)
91 * channel data for all 12 possible channels
94 void write_general_header() {
95 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
98 void write_general(AltosRecord record) {
99 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
100 ALTOS_CSV_VERSION, record.serial, record.flight, record.callsign,
101 (double) record.time, (double) record.tick / 100.0,
103 record.status & 0x7f);
106 void write_flight_header() {
107 out.printf("state,state_name");
110 void write_flight(AltosRecord record) {
111 out.printf("%d,%8s", record.state, record.state());
114 void write_basic_header() {
115 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
118 void write_basic(AltosRecord record) {
119 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
120 record.acceleration(),
121 record.raw_pressure(),
122 record.raw_altitude(),
124 record.accel_speed(),
126 record.temperature(),
127 record.battery_voltage(),
128 record.drogue_voltage(),
129 record.main_voltage());
132 void write_gps_header() {
133 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
136 void write_gps(AltosRecord record) {
137 AltosGPS gps = record.gps;
139 gps = new AltosGPS();
141 AltosGreatCircle from_pad = state.from_pad;
142 if (from_pad == null)
143 from_pad = new AltosGreatCircle();
145 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",
165 void write_gps_sat_header() {
166 for(int i = 1; i <= 32; i++) {
167 out.printf("sat%02d", i);
173 void write_gps_sat(AltosRecord record) {
174 AltosGPS gps = record.gps;
175 for(int i = 1; i <= 32; i++) {
177 if (gps != null && gps.cc_gps_sat != null) {
178 for(int j = 0; j < gps.cc_gps_sat.length; j++)
179 if (gps.cc_gps_sat[j].svid == i) {
180 c_n0 = gps.cc_gps_sat[j].c_n0;
184 out.printf ("%3d", c_n0);
190 void write_companion_header() {
191 out.printf("companion_id,companion_time,companion_update,companion_channels");
192 for (int i = 0; i < 12; i++)
193 out.printf(",companion_%02d", i);
196 void write_companion(AltosRecord record) {
197 AltosRecordCompanion companion = record.companion;
199 int channels_written = 0;
200 if (companion == null) {
201 out.printf("0,0,0,0");
203 out.printf("%3d,%5.2f,%5.2f,%2d",
205 (companion.tick - boost_tick) / 100.0,
206 companion.update_period / 100.0,
208 for (; channels_written < companion.channels; channels_written++)
209 out.printf(",%5d", companion.companion_data[channels_written]);
211 for (; channels_written < 12; channels_written++)
215 void write_header(boolean gps, boolean companion) {
216 out.printf("#"); write_general_header();
217 out.printf(","); write_flight_header();
218 out.printf(","); write_basic_header();
220 out.printf(","); write_gps_header();
221 out.printf(","); write_gps_sat_header();
224 out.printf(","); write_companion_header();
229 void write_one(AltosRecord record) {
230 state = new AltosState(record, state);
231 write_general(record); out.printf(",");
232 write_flight(record); out.printf(",");
234 if (record.gps != null) {
236 write_gps(record); out.printf(",");
237 write_gps_sat(record);
239 if (record.companion != null) {
241 write_companion(record);
247 while (!pad_records.isEmpty()) {
248 write_one (pad_records.remove());
252 public void write(AltosRecord record) {
253 if (record.state == Altos.ao_flight_startup)
255 if (!header_written) {
256 write_header(record.gps != null, record.companion != null);
257 header_written = true;
260 if (record.state >= Altos.ao_flight_boost) {
262 boost_tick = record.tick;
269 pad_records.add(record);
272 public PrintStream out() {
276 public void close() {
277 if (!pad_records.isEmpty()) {
278 boost_tick = pad_records.element().tick;
284 public void write(AltosRecordIterable iterable) {
285 iterable.write_comments(out());
286 for (AltosRecord r : iterable)
290 public AltosCSV(PrintStream in_out, File in_name) {
293 pad_records = new LinkedList<AltosRecord>();
296 public AltosCSV(File in_name) throws FileNotFoundException {
297 this(new PrintStream(in_name), in_name);
300 public AltosCSV(String in_string) throws FileNotFoundException {
301 this(new File(in_string));