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.
22 import org.altusmetrum.altoslib_2.*;
24 public class AltosCSV implements AltosWriter {
27 boolean header_written;
30 LinkedList<AltosState> pad_states;
33 static final int ALTOS_CSV_VERSION = 5;
42 * time (seconds since boost)
43 * clock (tick count / 100)
56 * accelerometer speed (m/s)
57 * barometer speed (m/s)
63 * Advanced sensors (if available)
74 * GPS data (if available)
77 * nsat (used for solution)
88 * from_pad_azimuth (deg true)
90 * from_pad_elevation (deg from horizon)
94 * C/N0 data for all 32 valid SDIDs
97 * companion_id (1-255. 10 is TeleScience)
98 * time of last companion data (seconds since boost)
99 * update_period (0.1-2.55 minimum telemetry interval)
101 * channel data for all 12 possible channels
104 void write_general_header() {
105 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
108 void write_general(AltosState state) {
109 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
110 ALTOS_CSV_VERSION, state.serial, state.flight, state.callsign,
111 (double) state.time, (double) state.tick / 100.0,
113 state.status & 0x7f);
116 void write_flight_header() {
117 out.printf("state,state_name");
120 void write_flight(AltosState state) {
121 out.printf("%d,%8s", state.state, state.state_name());
124 void write_basic_header() {
125 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
128 void write_basic(AltosState state) {
129 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
130 state.acceleration(),
137 state.battery_voltage,
138 state.apogee_voltage,
142 void write_advanced_header() {
143 out.printf("accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
146 void write_advanced(AltosState state) {
147 AltosIMU imu = state.imu;
148 AltosMag mag = state.mag;
151 imu = new AltosIMU();
153 mag = new AltosMag();
154 out.printf("%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d",
155 imu.accel_x, imu.accel_y, imu.accel_z,
156 imu.gyro_x, imu.gyro_y, imu.gyro_z,
157 mag.x, mag.y, mag.z);
160 void write_gps_header() {
161 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
164 void write_gps(AltosState state) {
165 AltosGPS gps = state.gps;
167 gps = new AltosGPS();
169 AltosGreatCircle from_pad = state.from_pad;
170 if (from_pad == null)
171 from_pad = new AltosGreatCircle();
173 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",
193 void write_gps_sat_header() {
194 for(int i = 1; i <= 32; i++) {
195 out.printf("sat%02d", i);
201 void write_gps_sat(AltosState state) {
202 AltosGPS gps = state.gps;
203 for(int i = 1; i <= 32; i++) {
205 if (gps != null && gps.cc_gps_sat != null) {
206 for(int j = 0; j < gps.cc_gps_sat.length; j++)
207 if (gps.cc_gps_sat[j].svid == i) {
208 c_n0 = gps.cc_gps_sat[j].c_n0;
212 out.printf ("%3d", c_n0);
218 void write_companion_header() {
219 out.printf("companion_id,companion_time,companion_update,companion_channels");
220 for (int i = 0; i < 12; i++)
221 out.printf(",companion_%02d", i);
224 void write_companion(AltosState state) {
225 AltosCompanion companion = state.companion;
227 int channels_written = 0;
228 if (companion == null) {
229 out.printf("0,0,0,0");
231 out.printf("%3d,%5.2f,%5.2f,%2d",
233 (companion.tick - boost_tick) / 100.0,
234 companion.update_period / 100.0,
236 for (; channels_written < companion.channels; channels_written++)
237 out.printf(",%5d", companion.companion_data[channels_written]);
239 for (; channels_written < 12; channels_written++)
243 void write_header(boolean advanced, boolean gps, boolean companion) {
244 out.printf("#"); write_general_header();
245 out.printf(","); write_flight_header();
246 out.printf(","); write_basic_header();
248 out.printf(","); write_advanced_header();
250 out.printf(","); write_gps_header();
251 out.printf(","); write_gps_sat_header();
254 out.printf(","); write_companion_header();
259 void write_one(AltosState state) {
260 write_general(state); out.printf(",");
261 write_flight(state); out.printf(",");
262 write_basic(state); out.printf(",");
263 if (state.imu != null || state.mag != null)
264 write_advanced(state);
265 if (state.gps != null) {
267 write_gps(state); out.printf(",");
268 write_gps_sat(state);
270 if (state.companion != null) {
272 write_companion(state);
278 while (!pad_states.isEmpty()) {
279 write_one (pad_states.remove());
283 public void write(AltosState state) {
284 if (state.state == Altos.ao_flight_startup)
286 if (!header_written) {
287 write_header(state.imu != null || state.mag != null,
288 state.gps != null, state.companion != null);
289 header_written = true;
292 if (state.state >= Altos.ao_flight_boost) {
294 boost_tick = state.tick;
301 pad_states.add(state);
304 public PrintStream out() {
308 public void close() {
309 if (!pad_states.isEmpty()) {
310 boost_tick = pad_states.element().tick;
316 public void write(AltosStateIterable states) {
317 states.write_comments(out());
318 for (AltosState state : states)
322 public AltosCSV(PrintStream in_out, File in_name) {
325 pad_states = new LinkedList<AltosState>();
328 public AltosCSV(File in_name) throws FileNotFoundException {
329 this(new PrintStream(in_name), in_name);
332 public AltosCSV(String in_string) throws FileNotFoundException {
333 this(new File(in_string));