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 = 5;
43 * time (seconds since boost)
44 * clock (tick count / 100)
57 * accelerometer speed (m/s)
58 * barometer speed (m/s)
64 * Advanced sensors (if available)
75 * GPS data (if available)
78 * nsat (used for solution)
89 * from_pad_azimuth (deg true)
91 * from_pad_elevation (deg from horizon)
95 * C/N0 data for all 32 valid SDIDs
98 * companion_id (1-255. 10 is TeleScience)
99 * time of last companion data (seconds since boost)
100 * update_period (0.1-2.55 minimum telemetry interval)
102 * channel data for all 12 possible channels
105 void write_general_header() {
106 out.printf("version,serial,flight,call,time,clock,rssi,lqi");
109 void write_general(AltosRecord record) {
110 out.printf("%s, %d, %d, %s, %8.2f, %8.2f, %4d, %3d",
111 ALTOS_CSV_VERSION, record.serial, record.flight, record.callsign,
112 (double) record.time, (double) record.tick / 100.0,
114 record.status & 0x7f);
117 void write_flight_header() {
118 out.printf("state,state_name");
121 void write_flight(AltosRecord record) {
122 out.printf("%d,%8s", record.state, record.state());
125 void write_basic_header() {
126 out.printf("acceleration,pressure,altitude,height,accel_speed,baro_speed,temperature,battery_voltage,drogue_voltage,main_voltage");
129 void write_basic(AltosRecord record) {
130 out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
131 record.acceleration(),
132 record.raw_pressure(),
133 record.raw_altitude(),
135 record.accel_speed(),
137 record.temperature(),
138 record.battery_voltage(),
139 record.drogue_voltage(),
140 record.main_voltage());
143 void write_advanced_header() {
144 out.printf("accel_x,accel_y,accel_z,gyro_x,gyro_y,gyro_z");
147 void write_advanced(AltosRecord record) {
148 AltosIMU imu = record.imu;
149 AltosMag mag = record.mag;
152 imu = new AltosIMU();
154 mag = new AltosMag();
155 out.printf("%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d",
156 imu.accel_x, imu.accel_y, imu.accel_z,
157 imu.gyro_x, imu.gyro_y, imu.gyro_z,
158 mag.x, mag.y, mag.z);
161 void write_gps_header() {
162 out.printf("connected,locked,nsat,latitude,longitude,altitude,year,month,day,hour,minute,second,pad_dist,pad_range,pad_az,pad_el,hdop");
165 void write_gps(AltosRecord record) {
166 AltosGPS gps = record.gps;
168 gps = new AltosGPS();
170 AltosGreatCircle from_pad = state.from_pad;
171 if (from_pad == null)
172 from_pad = new AltosGreatCircle();
174 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",
194 void write_gps_sat_header() {
195 for(int i = 1; i <= 32; i++) {
196 out.printf("sat%02d", i);
202 void write_gps_sat(AltosRecord record) {
203 AltosGPS gps = record.gps;
204 for(int i = 1; i <= 32; i++) {
206 if (gps != null && gps.cc_gps_sat != null) {
207 for(int j = 0; j < gps.cc_gps_sat.length; j++)
208 if (gps.cc_gps_sat[j].svid == i) {
209 c_n0 = gps.cc_gps_sat[j].c_n0;
213 out.printf ("%3d", c_n0);
219 void write_companion_header() {
220 out.printf("companion_id,companion_time,companion_update,companion_channels");
221 for (int i = 0; i < 12; i++)
222 out.printf(",companion_%02d", i);
225 void write_companion(AltosRecord record) {
226 AltosRecordCompanion companion = record.companion;
228 int channels_written = 0;
229 if (companion == null) {
230 out.printf("0,0,0,0");
232 out.printf("%3d,%5.2f,%5.2f,%2d",
234 (companion.tick - boost_tick) / 100.0,
235 companion.update_period / 100.0,
237 for (; channels_written < companion.channels; channels_written++)
238 out.printf(",%5d", companion.companion_data[channels_written]);
240 for (; channels_written < 12; channels_written++)
244 void write_header(boolean advanced, boolean gps, boolean companion) {
245 out.printf("#"); write_general_header();
246 out.printf(","); write_flight_header();
247 out.printf(","); write_basic_header();
249 out.printf(","); write_advanced_header();
251 out.printf(","); write_gps_header();
252 out.printf(","); write_gps_sat_header();
255 out.printf(","); write_companion_header();
260 void write_one(AltosRecord record) {
261 state = new AltosState(record, state);
262 write_general(record); out.printf(",");
263 write_flight(record); out.printf(",");
264 write_basic(record); out.printf(",");
265 if (record.imu != null || record.mag != null)
266 write_advanced(record);
267 if (record.gps != null) {
269 write_gps(record); out.printf(",");
270 write_gps_sat(record);
272 if (record.companion != null) {
274 write_companion(record);
280 while (!pad_records.isEmpty()) {
281 write_one (pad_records.remove());
285 public void write(AltosRecord record) {
286 if (record.state == Altos.ao_flight_startup)
288 if (!header_written) {
289 write_header(record.imu != null || record.mag != null,
290 record.gps != null, record.companion != null);
291 header_written = true;
294 if (record.state >= Altos.ao_flight_boost) {
296 boost_tick = record.tick;
303 pad_records.add(record);
306 public PrintStream out() {
310 public void close() {
311 if (!pad_records.isEmpty()) {
312 boost_tick = pad_records.element().tick;
318 public void write(AltosRecordIterable iterable) {
319 iterable.write_comments(out());
320 for (AltosRecord r : iterable)
324 public AltosCSV(PrintStream in_out, File in_name) {
327 pad_records = new LinkedList<AltosRecord>();
330 public AltosCSV(File in_name) throws FileNotFoundException {
331 this(new PrintStream(in_name), in_name);
334 public AltosCSV(String in_string) throws FileNotFoundException {
335 this(new File(in_string));