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.
24 import org.altusmetrum.AltosLib.*;
26 public class AltosCSV implements AltosWriter {
29 boolean header_written;
32 LinkedList<AltosRecord> pad_records;
35 static final int ALTOS_CSV_VERSION = 5;
44 * time (seconds since boost)
45 * clock (tick count / 100)
58 * accelerometer speed (m/s)
59 * barometer speed (m/s)
65 * Advanced sensors (if available)
76 * GPS data (if available)
79 * nsat (used for solution)
90 * from_pad_azimuth (deg true)
92 * from_pad_elevation (deg from horizon)
96 * C/N0 data for all 32 valid SDIDs
99 * companion_id (1-255. 10 is TeleScience)
100 * time of last companion data (seconds since boost)
101 * update_period (0.1-2.55 minimum telemetry interval)
103 * channel data for all 12 possible channels
106 void write_general_header() {
107 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
110 void write_general(AltosRecord record) {
111 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
112 ALTOS_CSV_VERSION, record.serial, record.flight, record.callsign,
113 (double) record.time, (double) record.tick / 100.0,
115 record.status & 0x7f);
118 void write_flight_header() {
119 out.printf("state,state_name");
122 void write_flight(AltosRecord record) {
123 out.printf("%d,%8s", record.state, record.state());
126 void write_basic_header() {
127 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
130 void write_basic(AltosRecord record) {
131 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
132 record.acceleration(),
133 record.raw_pressure(),
134 record.raw_altitude(),
136 record.accel_speed(),
138 record.temperature(),
139 record.battery_voltage(),
140 record.drogue_voltage(),
141 record.main_voltage());
144 void write_advanced_header() {
145 out.printf("accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
148 void write_advanced(AltosRecord record) {
149 AltosIMU imu = record.imu();
150 AltosMag mag = record.mag();
153 imu = new AltosIMU();
155 mag = new AltosMag();
156 out.printf("%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d",
157 imu.accel_x, imu.accel_y, imu.accel_z,
158 imu.gyro_x, imu.gyro_y, imu.gyro_z,
159 mag.x, mag.y, mag.z);
162 void write_gps_header() {
163 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
166 void write_gps(AltosRecord record) {
167 AltosGPS gps = record.gps;
169 gps = new AltosGPS();
171 AltosGreatCircle from_pad = state.from_pad;
172 if (from_pad == null)
173 from_pad = new AltosGreatCircle();
175 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",
195 void write_gps_sat_header() {
196 for(int i = 1; i <= 32; i++) {
197 out.printf("sat%02d", i);
203 void write_gps_sat(AltosRecord record) {
204 AltosGPS gps = record.gps;
205 for(int i = 1; i <= 32; i++) {
207 if (gps != null && gps.cc_gps_sat != null) {
208 for(int j = 0; j < gps.cc_gps_sat.length; j++)
209 if (gps.cc_gps_sat[j].svid == i) {
210 c_n0 = gps.cc_gps_sat[j].c_n0;
214 out.printf ("%3d", c_n0);
220 void write_companion_header() {
221 out.printf("companion_id,companion_time,companion_update,companion_channels");
222 for (int i = 0; i < 12; i++)
223 out.printf(",companion_%02d", i);
226 void write_companion(AltosRecord record) {
227 AltosRecordCompanion companion = record.companion;
229 int channels_written = 0;
230 if (companion == null) {
231 out.printf("0,0,0,0");
233 out.printf("%3d,%5.2f,%5.2f,%2d",
235 (companion.tick - boost_tick) / 100.0,
236 companion.update_period / 100.0,
238 for (; channels_written < companion.channels; channels_written++)
239 out.printf(",%5d", companion.companion_data[channels_written]);
241 for (; channels_written < 12; channels_written++)
245 void write_header(boolean advanced, boolean gps, boolean companion) {
246 out.printf("#"); write_general_header();
247 out.printf(","); write_flight_header();
248 out.printf(","); write_basic_header();
250 out.printf(","); write_advanced_header();
252 out.printf(","); write_gps_header();
253 out.printf(","); write_gps_sat_header();
256 out.printf(","); write_companion_header();
261 void write_one(AltosRecord record) {
262 state = new AltosState(record, state);
263 write_general(record); out.printf(",");
264 write_flight(record); out.printf(",");
265 write_basic(record); out.printf(",");
266 if (record.imu() != null || record.mag() != null)
267 write_advanced(record);
268 if (record.gps != null) {
270 write_gps(record); out.printf(",");
271 write_gps_sat(record);
273 if (record.companion != null) {
275 write_companion(record);
281 while (!pad_records.isEmpty()) {
282 write_one (pad_records.remove());
286 public void write(AltosRecord record) {
287 if (record.state == Altos.ao_flight_startup)
289 if (!header_written) {
290 write_header(record.imu() != null || record.mag() != null,
291 record.gps != null, record.companion != null);
292 header_written = true;
295 if (record.state >= Altos.ao_flight_boost) {
297 boost_tick = record.tick;
304 pad_records.add(record);
307 public PrintStream out() {
311 public void close() {
312 if (!pad_records.isEmpty()) {
313 boost_tick = pad_records.element().tick;
319 public void write(AltosRecordIterable iterable) {
320 iterable.write_comments(out());
321 for (AltosRecord r : iterable)
325 public AltosCSV(PrintStream in_out, File in_name) {
328 pad_records = new LinkedList<AltosRecord>();
331 public AltosCSV(File in_name) throws FileNotFoundException {
332 this(new PrintStream(in_name), in_name);
335 public AltosCSV(String in_string) throws FileNotFoundException {
336 this(new File(in_string));