2 * Copyright © 2017 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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 package org.altusmetrum.altoslib_11;
19 public class AltosFlightSeries extends AltosDataListener {
21 public ArrayList<AltosTimeSeries> series = new ArrayList<AltosTimeSeries>();
23 public int[] indices() {
24 int[] indices = new int[series.size()];
25 for (int i = 0; i < indices.length; i++)
27 step_indices(indices);
31 private double time(int id, int index) {
32 AltosTimeSeries s = series.get(id);
35 return Double.NEGATIVE_INFINITY;
37 if (index < s.values.size())
38 return s.values.get(index).time;
39 return Double.POSITIVE_INFINITY;
42 public boolean step_indices(int[] indices) {
43 double min_next = time(0, indices[0]+1);
45 for (int i = 1; i < indices.length; i++) {
46 double next = time(i, indices[i]+1);
51 if (min_next == Double.POSITIVE_INFINITY)
54 for (int i = 0; i < indices.length; i++) {
55 double t = time(i, indices[i] + 1);
63 public double time(int[] indices) {
64 double max = time(0, indices[0]);
66 for (int i = 1; i < indices.length; i++) {
67 double t = time(i, indices[i]);
74 public double value(String name, int[] indices) {
75 for (int i = 0; i < indices.length; i++) {
76 AltosTimeSeries s = series.get(i);
77 if (s.label.equals(name)) {
78 int index = indices[i];
81 if (index >= s.values.size())
82 index = s.values.size() - 1;
83 return s.values.get(index).value;
86 return AltosLib.MISSING;
89 public double value(String name, double time) {
90 for (AltosTimeSeries s : series) {
91 if (s.label.equals(name))
94 return AltosLib.MISSING;
97 public double value_before(String name, double time) {
98 for (AltosTimeSeries s : series) {
99 if (s.label.equals(name))
100 return s.value_before(time);
102 return AltosLib.MISSING;
105 public double value_after(String name, double time) {
106 for (AltosTimeSeries s : series) {
107 if (s.label.equals(name))
108 return s.value_after(time);
110 return AltosLib.MISSING;
113 public AltosTimeSeries make_series(String label, AltosUnits units) {
114 return new AltosTimeSeries(label, units);
117 public void add_series(AltosTimeSeries s) {
121 public AltosTimeSeries add_series(String label, AltosUnits units) {
122 AltosTimeSeries s = make_series(label, units);
127 public void remove_series(AltosTimeSeries s) {
131 public boolean has_series(String label) {
132 for (AltosTimeSeries s : series)
133 if (s.label.equals(label))
138 AltosTimeSeries state_series;
140 public static final String state_name = "State";
142 public void set_state(int state) {
143 if (state_series == null)
144 state_series = add_series(state_name, AltosConvert.state_name);
145 else if (this.state == state)
148 state_series.add(time(), state);
151 AltosTimeSeries accel_series;
153 public static final String accel_name = "Accel";
155 public void set_acceleration(double acceleration) {
156 if (accel_series == null) {
157 accel_series = add_series(accel_name, AltosConvert.accel);
159 accel_series.add(time(), acceleration);
162 private void compute_accel() {
163 if (accel_series != null)
166 if (speed_series != null) {
167 AltosTimeSeries temp_series = make_series(accel_name, AltosConvert.accel);
168 speed_series.differentiate(temp_series);
169 accel_series = add_series(accel_name, AltosConvert.accel);
170 temp_series.filter(accel_series, 0.25);
174 public void set_received_time(long received_time) {
177 AltosTimeSeries rssi_series;
179 public static final String rssi_name = "RSSI";
181 AltosTimeSeries status_series;
183 public static final String status_name = "Radio Status";
185 public void set_rssi(int rssi, int status) {
186 if (rssi_series == null) {
187 rssi_series = add_series(rssi_name, null);
188 status_series = add_series(status_name, null);
190 rssi_series.add(time(), rssi);
191 status_series.add(time(), status);
194 AltosTimeSeries pressure_series;
196 public static final String pressure_name = "Pressure";
198 AltosTimeSeries altitude_series;
200 public static final String altitude_name = "Altitude";
202 AltosTimeSeries height_series;
204 public static final String height_name = "Height";
206 public void set_pressure(double pa) {
207 if (pressure_series == null)
208 pressure_series = add_series(pressure_name, AltosConvert.pressure);
209 pressure_series.add(time(), pa);
210 if (altitude_series == null)
211 altitude_series = add_series(altitude_name, AltosConvert.height);
213 double altitude = AltosConvert.pressure_to_altitude(pa);
214 altitude_series.add(time(), altitude);
217 private void compute_height() {
218 double ground_altitude = cal_data.ground_altitude;
219 if (height_series == null && ground_altitude != AltosLib.MISSING && altitude_series != null) {
220 height_series = add_series(height_name, AltosConvert.height);
221 for (AltosTimeValue alt : altitude_series)
222 height_series.add(alt.time, alt.value - ground_altitude);
226 AltosTimeSeries speed_series;
228 public static final String speed_name = "Speed";
230 private void compute_speed() {
231 if (speed_series != null)
234 AltosTimeSeries alt_speed_series = null;
235 AltosTimeSeries accel_speed_series = null;
237 if (altitude_series != null) {
238 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
239 altitude_series.differentiate(temp_series);
241 alt_speed_series = make_series(speed_name, AltosConvert.speed);
242 temp_series.filter(alt_speed_series, 10.0);
244 if (accel_series != null) {
245 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
246 accel_series.integrate(temp_series);
248 accel_speed_series = make_series(speed_name, AltosConvert.speed);
249 temp_series.filter(accel_speed_series, 0.1);
252 if (alt_speed_series != null && accel_speed_series != null) {
253 double apogee_time = AltosLib.MISSING;
254 if (state_series != null) {
255 for (AltosTimeValue d : state_series) {
256 if (d.value >= AltosLib.ao_flight_drogue){
257 apogee_time = d.time;
262 if (apogee_time == AltosLib.MISSING) {
263 speed_series = alt_speed_series;
265 speed_series = make_series(speed_name, AltosConvert.speed);
266 for (AltosTimeValue d : accel_speed_series) {
267 if (d.time <= apogee_time)
270 for (AltosTimeValue d : alt_speed_series) {
271 if (d.time > apogee_time)
276 } else if (alt_speed_series != null) {
277 speed_series = alt_speed_series;
278 } else if (accel_speed_series != null) {
279 speed_series = accel_speed_series;
281 if (speed_series != null)
282 add_series(speed_series);
285 AltosTimeSeries kalman_height_series, kalman_speed_series, kalman_accel_series;
287 public static final String kalman_height_name = "Kalman Height";
288 public static final String kalman_speed_name = "Kalman Speed";
289 public static final String kalman_accel_name = "Kalman Accel";
291 public void set_kalman(double height, double speed, double acceleration) {
292 if (kalman_height_series == null) {
293 kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
294 kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
295 kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
297 kalman_height_series.add(time(), height);
298 kalman_speed_series.add(time(), speed);
299 kalman_accel_series.add(time(), acceleration);
302 AltosTimeSeries thrust_series;
304 public static final String thrust_name = "Thrust";
306 public void set_thrust(double N) {
307 if (thrust_series == null)
308 thrust_series = add_series(thrust_name, AltosConvert.force);
309 thrust_series.add(time(), N);
312 AltosTimeSeries temperature_series;
314 public static final String temperature_name = "Temperature";
316 public void set_temperature(double deg_c) {
317 if (temperature_series == null)
318 temperature_series = add_series(temperature_name, AltosConvert.temperature);
319 temperature_series.add(time(), deg_c);
322 AltosTimeSeries battery_voltage_series;
324 public static final String battery_voltage_name = "Battery Voltage";
326 public void set_battery_voltage(double volts) {
327 if (volts == AltosLib.MISSING)
329 if (battery_voltage_series == null)
330 battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
331 battery_voltage_series.add(time(), volts);
334 AltosTimeSeries apogee_voltage_series;
336 public static final String apogee_voltage_name = "Apogee Voltage";
338 public void set_apogee_voltage(double volts) {
339 if (volts == AltosLib.MISSING)
341 if (apogee_voltage_series == null)
342 apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
343 apogee_voltage_series.add(time(), volts);
346 AltosTimeSeries main_voltage_series;
348 public static final String main_voltage_name = "Main Voltage";
350 public void set_main_voltage(double volts) {
351 if (volts == AltosLib.MISSING)
353 if (main_voltage_series == null)
354 main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
355 main_voltage_series.add(time(), volts);
358 public ArrayList<AltosGPSTimeValue> gps_series;
360 public AltosGPS gps_before(double time) {
362 for (AltosGPSTimeValue gtv : gps_series)
363 if (gtv.time <= time)
370 AltosTimeSeries sats_in_view;
371 AltosTimeSeries sats_in_soln;
372 AltosTimeSeries gps_altitude;
373 AltosTimeSeries gps_height;
374 AltosTimeSeries gps_ground_speed;
375 AltosTimeSeries gps_ascent_rate;
376 AltosTimeSeries gps_course;
377 AltosTimeSeries gps_speed;
378 AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
380 public static final String sats_in_view_name = "Satellites in view";
381 public static final String sats_in_soln_name = "Satellites in solution";
382 public static final String gps_altitude_name = "GPS Altitude";
383 public static final String gps_height_name = "GPS Height";
384 public static final String gps_ground_speed_name = "GPS Ground Speed";
385 public static final String gps_ascent_rate_name = "GPS Ascent Rate";
386 public static final String gps_course_name = "GPS Course";
387 public static final String gps_speed_name = "GPS Speed";
388 public static final String gps_pdop_name = "GPS Dilution of Precision";
389 public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
390 public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
392 public void set_gps(AltosGPS gps) {
393 if (gps_series == null)
394 gps_series = new ArrayList<AltosGPSTimeValue>();
395 gps_series.add(new AltosGPSTimeValue(time(), gps));
397 if (sats_in_soln == null) {
398 sats_in_soln = add_series(sats_in_soln_name, null);
400 sats_in_soln.add(time(), gps.nsat);
401 if (gps.pdop != AltosLib.MISSING) {
402 if (gps_pdop == null) {
403 gps_pdop = add_series(gps_pdop_name, null);
404 gps_hdop = add_series(gps_hdop_name, null);
405 gps_vdop = add_series(gps_vdop_name, null);
407 gps_pdop.add(time(), gps.pdop);
408 gps_hdop.add(time(), gps.hdop);
409 gps_vdop.add(time(), gps.vdop);
412 if (gps_altitude == null) {
413 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
414 gps_height = add_series(gps_height_name, AltosConvert.height);
415 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
416 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
417 gps_course = add_series(gps_course_name, null);
418 gps_speed = add_series(gps_speed_name, null);
420 if (gps.alt != AltosLib.MISSING) {
421 gps_altitude.add(time(), gps.alt);
422 if (cal_data.gps_pad != null)
423 gps_height.add(time(), gps.alt - cal_data.gps_pad.alt);
425 if (gps.ground_speed != AltosLib.MISSING)
426 gps_ground_speed.add(time(), gps.ground_speed);
427 if (gps.climb_rate != AltosLib.MISSING)
428 gps_ascent_rate.add(time(), gps.climb_rate);
429 if (gps.course != AltosLib.MISSING)
430 gps_course.add(time(), gps.course);
431 if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING)
432 gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
433 gps.climb_rate * gps.climb_rate));
435 if (gps.cc_gps_sat != null) {
436 if (sats_in_view == null)
437 sats_in_view = add_series(sats_in_view_name, null);
438 sats_in_view.add(time(), gps.cc_gps_sat.length);
442 public static final String accel_along_name = "Accel Along";
443 public static final String accel_across_name = "Accel Across";
444 public static final String accel_through_name = "Accel Through";
446 AltosTimeSeries accel_along, accel_across, accel_through;
448 public static final String gyro_roll_name = "Roll Rate";
449 public static final String gyro_pitch_name = "Pitch Rate";
450 public static final String gyro_yaw_name = "Yaw Rate";
452 AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
454 public static final String mag_along_name = "Magnetic Field Along";
455 public static final String mag_across_name = "Magnetic Field Across";
456 public static final String mag_through_name = "Magnetic Field Through";
458 AltosTimeSeries mag_along, mag_across, mag_through;
460 public void set_accel(double along, double across, double through) {
461 if (accel_along == null) {
462 accel_along = add_series(accel_along_name, AltosConvert.accel);
463 accel_across = add_series(accel_across_name, AltosConvert.accel);
464 accel_through = add_series(accel_through_name, AltosConvert.accel);
466 accel_along.add(time(), along);
467 accel_across.add(time(), across);
468 accel_through.add(time(), through);
471 public void set_accel_ground(double along, double across, double through) {
474 public void set_gyro(double roll, double pitch, double yaw) {
475 if (gyro_roll == null) {
476 gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
477 gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
478 gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
480 gyro_roll.add(time(), roll);
481 gyro_pitch.add(time(), pitch);
482 gyro_yaw.add(time(), yaw);
485 public void set_mag(double along, double across, double through) {
486 if (mag_along == null) {
487 mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
488 mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
489 mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
491 mag_along.add(time(), along);
492 mag_across.add(time(), across);
493 mag_through.add(time(), through);
496 public static final String orient_name = "Tilt Angle";
498 AltosTimeSeries orient_series;
500 public void set_orient(double orient) {
501 if (orient_series == null)
502 orient_series = add_series(orient_name, AltosConvert.orient);
503 orient_series.add(time(), orient);
506 public static final String pyro_voltage_name = "Pyro Voltage";
508 AltosTimeSeries pyro_voltage;
510 public void set_pyro_voltage(double volts) {
511 if (pyro_voltage == null)
512 pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
513 pyro_voltage.add(time(), volts);
516 private static String[] igniter_voltage_names;
518 public String igniter_voltage_name(int channel) {
519 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
520 String[] new_igniter_voltage_names = new String[channel + 1];
523 if (igniter_voltage_names != null) {
524 for (; i < igniter_voltage_names.length; i++)
525 new_igniter_voltage_names[i] = igniter_voltage_names[i];
527 for (; i < channel+1; i++)
528 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
529 igniter_voltage_names = new_igniter_voltage_names;
531 return igniter_voltage_names[channel];
534 AltosTimeSeries[] igniter_voltage;
536 public void set_igniter_voltage(double[] voltage) {
537 int channels = voltage.length;
538 if (igniter_voltage == null || igniter_voltage.length <= channels) {
539 AltosTimeSeries[] new_igniter_voltage = new AltosTimeSeries[channels + 1];
542 if (igniter_voltage != null) {
543 for (; i < igniter_voltage.length; i++)
544 new_igniter_voltage[i] = igniter_voltage[i];
546 for (; i < channels; i++)
547 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
548 igniter_voltage = new_igniter_voltage;
550 for (int channel = 0; channel < voltage.length; channel++)
551 igniter_voltage[channel].add(time(), voltage[channel]);
554 public static final String pyro_fired_name = "Pyro Channel State";
556 AltosTimeSeries pyro_fired_series;
560 public void set_pyro_fired(int pyro_mask) {
561 if (pyro_fired_series == null)
562 pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
563 for (int channel = 0; channel < 32; channel++) {
564 if ((last_pyro_mask & (1 << channel)) == 0 &&
565 (pyro_mask & (1 << channel)) != 0) {
566 pyro_fired_series.add(time(), channel);
569 last_pyro_mask = pyro_mask;
572 public void set_companion(AltosCompanion companion) {
575 public void finish() {
581 public AltosTimeSeries[] series() {
583 return series.toArray(new AltosTimeSeries[0]);
586 public AltosFlightSeries(AltosCalData cal_data) {