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_12;
19 public class AltosFlightSeries extends AltosDataListener {
21 public ArrayList<AltosTimeSeries> series = new ArrayList<AltosTimeSeries>();
23 public double speed_filter_width = 4.0;
24 public double accel_filter_width = 1.0;
26 public int[] indices() {
27 int[] indices = new int[series.size()];
28 for (int i = 0; i < indices.length; i++)
30 step_indices(indices);
34 private double time(int id, int index) {
35 AltosTimeSeries s = series.get(id);
38 return Double.NEGATIVE_INFINITY;
40 if (index < s.values.size())
41 return s.values.get(index).time;
42 return Double.POSITIVE_INFINITY;
45 public boolean step_indices(int[] indices) {
46 double min_next = time(0, indices[0]+1);
48 for (int i = 1; i < indices.length; i++) {
49 double next = time(i, indices[i]+1);
54 if (min_next == Double.POSITIVE_INFINITY)
57 for (int i = 0; i < indices.length; i++) {
58 double t = time(i, indices[i] + 1);
66 public double time(int[] indices) {
67 double max = time(0, indices[0]);
69 for (int i = 1; i < indices.length; i++) {
70 double t = time(i, indices[i]);
77 public double value(String name, int[] indices) {
78 for (int i = 0; i < indices.length; i++) {
79 AltosTimeSeries s = series.get(i);
80 if (s.label.equals(name)) {
81 int index = indices[i];
84 if (index >= s.values.size())
85 index = s.values.size() - 1;
86 return s.values.get(index).value;
89 return AltosLib.MISSING;
92 public double value(String name, double time) {
93 for (AltosTimeSeries s : series) {
94 if (s.label.equals(name))
97 return AltosLib.MISSING;
100 public double value_before(String name, double time) {
101 for (AltosTimeSeries s : series) {
102 if (s.label.equals(name))
103 return s.value_before(time);
105 return AltosLib.MISSING;
108 public double value_after(String name, double time) {
109 for (AltosTimeSeries s : series) {
110 if (s.label.equals(name))
111 return s.value_after(time);
113 return AltosLib.MISSING;
116 public AltosTimeSeries make_series(String label, AltosUnits units) {
117 return new AltosTimeSeries(label, units);
120 public void add_series(AltosTimeSeries s) {
121 for (int e = 0; e < series.size(); e++) {
122 if (s.compareTo(series.get(e)) < 0){
130 public AltosTimeSeries add_series(String label, AltosUnits units) {
131 AltosTimeSeries s = make_series(label, units);
136 public void remove_series(AltosTimeSeries s) {
140 public boolean has_series(String label) {
141 for (AltosTimeSeries s : series)
142 if (s.label.equals(label))
147 public AltosTimeSeries state_series;
149 public static final String state_name = "State";
151 public void set_state(int state) {
153 if (state != AltosLib.ao_flight_pad && state != AltosLib.MISSING && state != AltosLib.ao_flight_stateless) {
154 if (state_series == null)
155 state_series = add_series(state_name, AltosConvert.state_name);
156 if (this.state() != state)
157 state_series.add(time(), state);
159 super.set_state(state);
162 public AltosTimeSeries accel_series;
163 public boolean accel_computed;
165 public static final String accel_name = "Accel";
167 public AltosTimeSeries vert_accel_series;
169 public static final String vert_accel_name = "Vertical Accel";
171 public void set_acceleration(double acceleration) {
172 if (acceleration == AltosLib.MISSING)
174 if (accel_series == null)
175 accel_series = add_series(accel_name, AltosConvert.accel);
177 accel_series.add(time(), acceleration);
178 accel_computed = false;
181 private AltosTimeSeries compute_accel() {
182 AltosTimeSeries new_accel_series = null;
184 if (speed_series != null) {
185 AltosTimeSeries temp_series;
186 if (accel_filter_width > 0) {
187 temp_series = make_series(speed_name, AltosConvert.speed);
188 speed_series.filter(temp_series, accel_filter_width);
190 temp_series = speed_series;
192 new_accel_series = make_series(accel_name, AltosConvert.accel);
193 temp_series.differentiate(new_accel_series);
195 return new_accel_series;
198 public void set_filter(double speed_filter, double accel_filter) {
199 this.speed_filter_width = speed_filter;
200 this.accel_filter_width = accel_filter;
202 AltosTimeSeries new_speed_series = compute_speed();
204 if (new_speed_series != null) {
205 speed_series.erase_values();
206 for (AltosTimeValue tv : new_speed_series)
207 speed_series.add(tv);
209 if (accel_computed) {
210 AltosTimeSeries new_accel_series = compute_accel();
211 if (new_accel_series != null) {
212 accel_series.erase_values();
213 for (AltosTimeValue tv : new_accel_series)
214 accel_series.add(tv);
219 public void set_received_time(long received_time) {
222 public AltosTimeSeries tick_series;
224 public static final String tick_name = "Tick";
226 public void set_tick(int tick) {
227 super.set_tick(tick);
228 if (tick_series == null)
229 tick_series = add_series(tick_name, null);
230 tick_series.add(time(), tick);
233 public AltosTimeSeries rssi_series;
235 public static final String rssi_name = "RSSI";
237 public AltosTimeSeries status_series;
239 public static final String status_name = "Radio Status";
241 public void set_rssi(int rssi, int status) {
242 if (rssi_series == null) {
243 rssi_series = add_series(rssi_name, null);
244 status_series = add_series(status_name, null);
246 rssi_series.add(time(), rssi);
247 status_series.add(time(), status);
250 public AltosTimeSeries pressure_series;
252 public static final String pressure_name = "Pressure";
254 public AltosTimeSeries altitude_series;
256 public static final String altitude_name = "Altitude";
258 public AltosTimeSeries height_series;
260 public double max_height = AltosLib.MISSING;
262 public void set_min_pressure(double pa) {
263 double ground_altitude = cal_data().ground_altitude;
264 if (ground_altitude != AltosLib.MISSING)
265 max_height = AltosConvert.pressure_to_altitude(pa) -
269 public static final String height_name = "Height";
271 public void set_pressure(double pa) {
272 if (pa == AltosLib.MISSING)
275 if (pressure_series == null)
276 pressure_series = add_series(pressure_name, AltosConvert.pressure);
277 pressure_series.add(time(), pa);
278 if (altitude_series == null)
279 altitude_series = add_series(altitude_name, AltosConvert.height);
281 if (cal_data().ground_pressure == AltosLib.MISSING)
282 cal_data().set_ground_pressure(pa);
284 double altitude = AltosConvert.pressure_to_altitude(pa);
285 altitude_series.add(time(), altitude);
288 private void compute_height() {
289 double ground_altitude = cal_data().ground_altitude;
290 if (height_series == null && ground_altitude != AltosLib.MISSING && altitude_series != null) {
291 height_series = add_series(height_name, AltosConvert.height);
292 for (AltosTimeValue alt : altitude_series)
293 height_series.add(alt.time, alt.value - ground_altitude);
296 if (gps_height == null && cal_data().gps_pad != null && cal_data().gps_pad.alt != AltosLib.MISSING && gps_altitude != null) {
297 double gps_ground_altitude = cal_data().gps_pad.alt;
298 gps_height = add_series(gps_height_name, AltosConvert.height);
299 for (AltosTimeValue gps_alt : gps_altitude)
300 gps_height.add(gps_alt.time, gps_alt.value - gps_ground_altitude);
304 public AltosTimeSeries speed_series;
306 public static final String speed_name = "Speed";
308 private AltosTimeSeries compute_speed() {
309 AltosTimeSeries new_speed_series = null;
310 AltosTimeSeries alt_speed_series = null;
311 AltosTimeSeries accel_speed_series = null;
313 if (altitude_series != null) {
314 AltosTimeSeries temp_series;
316 if (speed_filter_width > 0) {
317 temp_series = make_series(speed_name, AltosConvert.height);
318 altitude_series.filter(temp_series, speed_filter_width);
320 temp_series = altitude_series;
322 alt_speed_series = make_series(speed_name, AltosConvert.speed);
323 temp_series.differentiate(alt_speed_series);
325 if (accel_series != null && !accel_computed) {
327 if (orient_series != null) {
328 vert_accel_series = add_series(vert_accel_name, AltosConvert.accel);
330 for (AltosTimeValue a : accel_series) {
331 double orient = orient_series.value(a.time);
332 double a_abs = a.value + AltosConvert.gravity;
333 double v_a = a_abs * Math.cos(AltosConvert.degrees_to_radians(orient)) - AltosConvert.gravity;
335 vert_accel_series.add(a.time, v_a);
339 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
341 if (vert_accel_series != null)
342 vert_accel_series.integrate(temp_series);
344 accel_series.integrate(temp_series);
346 accel_speed_series = make_series(speed_name, AltosConvert.speed);
347 temp_series.filter(accel_speed_series, 0.1);
350 if (alt_speed_series != null && accel_speed_series != null) {
351 double apogee_time = AltosLib.MISSING;
352 if (state_series != null) {
353 for (AltosTimeValue d : state_series) {
354 if (d.value >= AltosLib.ao_flight_drogue){
355 apogee_time = d.time;
360 if (apogee_time == AltosLib.MISSING) {
361 new_speed_series = alt_speed_series;
363 new_speed_series = make_series(speed_name, AltosConvert.speed);
364 for (AltosTimeValue d : accel_speed_series) {
365 if (d.time <= apogee_time)
366 new_speed_series.add(d);
368 for (AltosTimeValue d : alt_speed_series) {
369 if (d.time > apogee_time)
370 new_speed_series.add(d);
374 } else if (alt_speed_series != null) {
375 new_speed_series = alt_speed_series;
376 } else if (accel_speed_series != null) {
377 new_speed_series = accel_speed_series;
379 return new_speed_series;
382 public AltosTimeSeries orient_series;
384 public static final String orient_name = "Tilt Angle";
386 private void compute_orient() {
388 if (orient_series != null)
391 if (accel_ground_across == AltosLib.MISSING)
394 if (cal_data().pad_orientation == AltosLib.MISSING)
397 if (cal_data().accel_zero_across == AltosLib.MISSING)
400 AltosRotation rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - cal_data().accel_zero_across),
401 AltosIMU.convert_accel(accel_ground_through - cal_data().accel_zero_through),
402 AltosIMU.convert_accel(accel_ground_along - cal_data().accel_zero_along),
403 cal_data().pad_orientation);
404 double prev_time = ground_time;
406 orient_series = add_series(orient_name, AltosConvert.orient);
407 orient_series.add(ground_time, rotation.tilt());
409 for (AltosTimeValue roll_v : gyro_roll) {
410 double time = roll_v.time;
411 double dt = time - prev_time;
414 double roll = AltosConvert.degrees_to_radians(roll_v.value) * dt;
415 double pitch = AltosConvert.degrees_to_radians(gyro_pitch.value(time)) * dt;
416 double yaw = AltosConvert.degrees_to_radians(gyro_yaw.value(time)) * dt;
418 rotation.rotate(pitch, yaw, roll);
419 orient_series.add(time, rotation.tilt());
425 public AltosTimeSeries kalman_height_series, kalman_speed_series, kalman_accel_series;
427 public static final String kalman_height_name = "Kalman Height";
428 public static final String kalman_speed_name = "Kalman Speed";
429 public static final String kalman_accel_name = "Kalman Accel";
431 public void set_kalman(double height, double speed, double acceleration) {
432 if (kalman_height_series == null) {
433 kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
434 kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
435 kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
437 kalman_height_series.add(time(), height);
438 kalman_speed_series.add(time(), speed);
439 kalman_accel_series.add(time(), acceleration);
442 public AltosTimeSeries thrust_series;
444 public static final String thrust_name = "Thrust";
446 public void set_thrust(double N) {
447 if (thrust_series == null)
448 thrust_series = add_series(thrust_name, AltosConvert.force);
449 thrust_series.add(time(), N);
452 public AltosTimeSeries temperature_series;
454 public static final String temperature_name = "Temperature";
456 public void set_temperature(double deg_c) {
457 if (temperature_series == null)
458 temperature_series = add_series(temperature_name, AltosConvert.temperature);
459 temperature_series.add(time(), deg_c);
462 public AltosTimeSeries battery_voltage_series;
464 public static final String battery_voltage_name = "Battery Voltage";
466 public void set_battery_voltage(double volts) {
467 if (volts == AltosLib.MISSING)
469 if (battery_voltage_series == null)
470 battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
471 battery_voltage_series.add(time(), volts);
474 public AltosTimeSeries apogee_voltage_series;
476 public static final String apogee_voltage_name = "Apogee Voltage";
478 public void set_apogee_voltage(double volts) {
479 if (volts == AltosLib.MISSING)
481 if (apogee_voltage_series == null)
482 apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
483 apogee_voltage_series.add(time(), volts);
486 public AltosTimeSeries main_voltage_series;
488 public static final String main_voltage_name = "Main Voltage";
490 public void set_main_voltage(double volts) {
491 if (volts == AltosLib.MISSING)
493 if (main_voltage_series == null)
494 main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
495 main_voltage_series.add(time(), volts);
498 public ArrayList<AltosGPSTimeValue> gps_series;
500 public AltosGPS gps_before(double time) {
501 AltosGPSTimeValue nearest = null;
502 for (AltosGPSTimeValue gtv : gps_series) {
506 if (gtv.time <= time) {
507 if (nearest.time <= time && gtv.time > nearest.time)
510 if (nearest.time > time && gtv.time < nearest.time)
521 public AltosTimeSeries sats_in_view;
522 public AltosTimeSeries sats_in_soln;
523 public AltosTimeSeries gps_altitude;
524 public AltosTimeSeries gps_height;
525 public AltosTimeSeries gps_ground_speed;
526 public AltosTimeSeries gps_ascent_rate;
527 public AltosTimeSeries gps_course;
528 public AltosTimeSeries gps_speed;
529 public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
531 public static final String sats_in_view_name = "Satellites in view";
532 public static final String sats_in_soln_name = "Satellites in solution";
533 public static final String gps_altitude_name = "GPS Altitude";
534 public static final String gps_height_name = "GPS Height";
535 public static final String gps_ground_speed_name = "GPS Ground Speed";
536 public static final String gps_ascent_rate_name = "GPS Ascent Rate";
537 public static final String gps_course_name = "GPS Course";
538 public static final String gps_speed_name = "GPS Speed";
539 public static final String gps_pdop_name = "GPS Dilution of Precision";
540 public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
541 public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
543 public void set_gps(AltosGPS gps) {
545 if (gps_series == null)
546 gps_series = new ArrayList<AltosGPSTimeValue>();
547 gps_series.add(new AltosGPSTimeValue(time(), gps));
549 if (sats_in_soln == null) {
550 sats_in_soln = add_series(sats_in_soln_name, null);
552 sats_in_soln.add(time(), gps.nsat);
553 if (gps.pdop != AltosLib.MISSING) {
554 if (gps_pdop == null)
555 gps_pdop = add_series(gps_pdop_name, null);
556 gps_pdop.add(time(), gps.pdop);
558 if (gps.hdop != AltosLib.MISSING) {
559 if (gps_hdop == null)
560 gps_hdop = add_series(gps_hdop_name, null);
561 gps_hdop.add(time(), gps.hdop);
563 if (gps.vdop != AltosLib.MISSING) {
564 if (gps_vdop == null)
565 gps_vdop = add_series(gps_vdop_name, null);
566 gps_vdop.add(time(), gps.vdop);
569 if (gps.alt != AltosLib.MISSING) {
570 if (gps_altitude == null)
571 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
572 gps_altitude.add(time(), gps.alt);
574 if (gps.ground_speed != AltosLib.MISSING) {
575 if (gps_ground_speed == null)
576 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
577 gps_ground_speed.add(time(), gps.ground_speed);
579 if (gps.climb_rate != AltosLib.MISSING) {
580 if (gps_ascent_rate == null)
581 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
582 gps_ascent_rate.add(time(), gps.climb_rate);
584 if (gps.course != AltosLib.MISSING) {
585 if (gps_course == null)
586 gps_course = add_series(gps_course_name, null);
587 gps_course.add(time(), gps.course);
589 if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING) {
590 if (gps_speed == null)
591 gps_speed = add_series(gps_speed_name, null);
592 gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
593 gps.climb_rate * gps.climb_rate));
596 if (gps.cc_gps_sat != null) {
597 if (sats_in_view == null)
598 sats_in_view = add_series(sats_in_view_name, null);
599 sats_in_view.add(time(), gps.cc_gps_sat.length);
603 public static final String accel_along_name = "Accel Along";
604 public static final String accel_across_name = "Accel Across";
605 public static final String accel_through_name = "Accel Through";
607 public AltosTimeSeries accel_along, accel_across, accel_through;
609 public static final String gyro_roll_name = "Roll Rate";
610 public static final String gyro_pitch_name = "Pitch Rate";
611 public static final String gyro_yaw_name = "Yaw Rate";
613 public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
615 public static final String mag_along_name = "Magnetic Field Along";
616 public static final String mag_across_name = "Magnetic Field Across";
617 public static final String mag_through_name = "Magnetic Field Through";
619 public AltosTimeSeries mag_along, mag_across, mag_through;
621 public void set_accel(double along, double across, double through) {
622 if (accel_along == null) {
623 accel_along = add_series(accel_along_name, AltosConvert.accel);
624 accel_across = add_series(accel_across_name, AltosConvert.accel);
625 accel_through = add_series(accel_through_name, AltosConvert.accel);
627 accel_along.add(time(), along);
628 accel_across.add(time(), across);
629 accel_through.add(time(), through);
632 private double accel_ground_along = AltosLib.MISSING;
633 private double accel_ground_across = AltosLib.MISSING;
634 private double accel_ground_through = AltosLib.MISSING;
636 private double ground_time;
638 public void set_accel_ground(double along, double across, double through) {
639 accel_ground_along = along;
640 accel_ground_across = across;
641 accel_ground_through = through;
642 ground_time = time();
645 public void set_gyro(double roll, double pitch, double yaw) {
646 if (gyro_roll == null) {
647 gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
648 gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
649 gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
651 gyro_roll.add(time(), roll);
652 gyro_pitch.add(time(), pitch);
653 gyro_yaw.add(time(), yaw);
656 public void set_mag(double along, double across, double through) {
657 if (mag_along == null) {
658 mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
659 mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
660 mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
662 mag_along.add(time(), along);
663 mag_across.add(time(), across);
664 mag_through.add(time(), through);
667 public void set_orient(double orient) {
668 if (orient_series == null)
669 orient_series = add_series(orient_name, AltosConvert.orient);
670 orient_series.add(time(), orient);
673 public static final String pyro_voltage_name = "Pyro Voltage";
675 public AltosTimeSeries pyro_voltage;
677 public void set_pyro_voltage(double volts) {
678 if (pyro_voltage == null)
679 pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
680 pyro_voltage.add(time(), volts);
683 private static String[] igniter_voltage_names;
685 public String igniter_voltage_name(int channel) {
686 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
687 String[] new_igniter_voltage_names = new String[channel + 1];
690 if (igniter_voltage_names != null) {
691 for (; i < igniter_voltage_names.length; i++)
692 new_igniter_voltage_names[i] = igniter_voltage_names[i];
694 for (; i < channel+1; i++)
695 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
696 igniter_voltage_names = new_igniter_voltage_names;
698 return igniter_voltage_names[channel];
701 public AltosTimeSeries[] igniter_voltage;
703 public void set_igniter_voltage(double[] voltage) {
704 int channels = voltage.length;
705 if (igniter_voltage == null || igniter_voltage.length <= channels) {
706 AltosTimeSeries[] new_igniter_voltage = new AltosTimeSeries[channels];
709 if (igniter_voltage != null) {
710 for (; i < igniter_voltage.length; i++)
711 new_igniter_voltage[i] = igniter_voltage[i];
713 for (; i < channels; i++)
714 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
715 igniter_voltage = new_igniter_voltage;
717 for (int channel = 0; channel < voltage.length; channel++)
718 igniter_voltage[channel].add(time(), voltage[channel]);
721 public static final String pyro_fired_name = "Pyro Channel State";
723 public AltosTimeSeries pyro_fired_series;
727 public void set_pyro_fired(int pyro_mask) {
728 if (pyro_fired_series == null)
729 pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
730 for (int channel = 0; channel < 32; channel++) {
731 if ((last_pyro_mask & (1 << channel)) == 0 &&
732 (pyro_mask & (1 << channel)) != 0) {
733 pyro_fired_series.add(time(), channel);
736 last_pyro_mask = pyro_mask;
739 public void set_companion(AltosCompanion companion) {
742 public void finish() {
744 if (speed_series == null) {
745 speed_series = compute_speed();
746 if (speed_series != null)
747 add_series(speed_series);
749 if (accel_series == null) {
750 accel_series = compute_accel();
751 if (accel_series != null) {
752 add_series(accel_series);
753 accel_computed = true;
759 public AltosTimeSeries[] series() {
761 return series.toArray(new AltosTimeSeries[0]);
764 public AltosFlightSeries(AltosCalData cal_data) {