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.
18 package org.altusmetrum.altoslib_5;
23 public class AltosCSV implements AltosWriter {
26 boolean header_written;
29 LinkedList<AltosState> pad_states;
32 static final int ALTOS_CSV_VERSION = 5;
41 * time (seconds since boost)
42 * clock (tick count / 100)
55 * accelerometer speed (m/s)
56 * barometer speed (m/s)
62 * Advanced sensors (if available)
73 * GPS data (if available)
76 * nsat (used for solution)
87 * from_pad_azimuth (deg true)
89 * from_pad_elevation (deg from horizon)
93 * C/N0 data for all 32 valid SDIDs
96 * companion_id (1-255. 10 is TeleScience)
97 * time of last companion data (seconds since boost)
98 * update_period (0.1-2.55 minimum telemetry interval)
100 * channel data for all 12 possible channels
103 void write_general_header() {
104 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
107 void write_general(AltosState state) {
108 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
109 ALTOS_CSV_VERSION, state.serial, state.flight, state.callsign,
110 (double) state.time, (double) state.tick / 100.0,
112 state.status & 0x7f);
115 void write_flight_header() {
116 out.printf("state,state_name");
119 void write_flight(AltosState state) {
120 out.printf("%d,%8s", state.state, state.state_name());
123 void write_basic_header() {
124 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
127 void write_basic(AltosState state) {
128 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
129 state.acceleration(),
136 state.battery_voltage,
137 state.apogee_voltage,
141 void write_advanced_header() {
142 out.printf("accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
145 void write_advanced(AltosState state) {
146 AltosIMU imu = state.imu;
147 AltosMag mag = state.mag;
150 imu = new AltosIMU();
152 mag = new AltosMag();
153 out.printf("%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d",
154 imu.accel_x, imu.accel_y, imu.accel_z,
155 imu.gyro_x, imu.gyro_y, imu.gyro_z,
156 mag.x, mag.y, mag.z);
159 void write_gps_header() {
160 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
163 void write_gps(AltosState state) {
164 AltosGPS gps = state.gps;
166 gps = new AltosGPS();
168 AltosGreatCircle from_pad = state.from_pad;
169 if (from_pad == null)
170 from_pad = new AltosGreatCircle();
172 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",
192 void write_gps_sat_header() {
193 for(int i = 1; i <= 32; i++) {
194 out.printf("sat%02d", i);
200 void write_gps_sat(AltosState state) {
201 AltosGPS gps = state.gps;
202 for(int i = 1; i <= 32; i++) {
204 if (gps != null && gps.cc_gps_sat != null) {
205 for(int j = 0; j < gps.cc_gps_sat.length; j++)
206 if (gps.cc_gps_sat[j].svid == i) {
207 c_n0 = gps.cc_gps_sat[j].c_n0;
211 out.printf ("%3d", c_n0);
217 void write_companion_header() {
218 out.printf("companion_id,companion_time,companion_update,companion_channels");
219 for (int i = 0; i < 12; i++)
220 out.printf(",companion_%02d", i);
223 void write_companion(AltosState state) {
224 AltosCompanion companion = state.companion;
226 int channels_written = 0;
227 if (companion == null) {
228 out.printf("0,0,0,0");
230 out.printf("%3d,%5.2f,%5.2f,%2d",
232 (companion.tick - boost_tick) / 100.0,
233 companion.update_period / 100.0,
235 for (; channels_written < companion.channels; channels_written++)
236 out.printf(",%5d", companion.companion_data[channels_written]);
238 for (; channels_written < 12; channels_written++)
242 void write_header(boolean advanced, boolean gps, boolean companion) {
243 out.printf("#"); write_general_header();
244 out.printf(","); write_flight_header();
245 out.printf(","); write_basic_header();
247 out.printf(","); write_advanced_header();
249 out.printf(","); write_gps_header();
250 out.printf(","); write_gps_sat_header();
253 out.printf(","); write_companion_header();
258 void write_one(AltosState state) {
259 write_general(state); out.printf(",");
260 write_flight(state); out.printf(",");
261 write_basic(state); out.printf(",");
262 if (state.imu != null || state.mag != null)
263 write_advanced(state);
264 if (state.gps != null) {
266 write_gps(state); out.printf(",");
267 write_gps_sat(state);
269 if (state.companion != null) {
271 write_companion(state);
277 while (!pad_states.isEmpty()) {
278 write_one (pad_states.remove());
282 public void write(AltosState state) {
283 if (state.state == AltosLib.ao_flight_startup)
285 if (!header_written) {
286 write_header(state.imu != null || state.mag != null,
287 state.gps != null, state.companion != null);
288 header_written = true;
291 if (state.state >= AltosLib.ao_flight_boost) {
293 boost_tick = state.tick;
300 pad_states.add(state);
303 public PrintStream out() {
307 public void close() {
308 if (!pad_states.isEmpty()) {
309 boost_tick = pad_states.element().tick;
315 public void write(AltosStateIterable states) {
316 states.write_comments(out());
317 for (AltosState state : states)
321 public AltosCSV(PrintStream in_out, File in_name) {
324 pad_states = new LinkedList<AltosState>();
327 public AltosCSV(File in_name) throws FileNotFoundException {
328 this(new PrintStream(in_name), in_name);
331 public AltosCSV(String in_string) throws FileNotFoundException {
332 this(new File(in_string));