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 = 4.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)
156 if (state_series == null)
157 state_series = add_series(state_name, AltosConvert.state_name);
158 else if (this.state == state)
161 state_series.add(time(), state);
164 public AltosTimeSeries accel_series;
166 public static final String accel_name = "Accel";
168 public AltosTimeSeries vert_accel_series;
170 public static final String vert_accel_name = "Vertical Accel";
172 public void set_acceleration(double acceleration) {
173 if (acceleration == AltosLib.MISSING)
175 if (accel_series == null)
176 accel_series = add_series(accel_name, AltosConvert.accel);
178 accel_series.add(time(), acceleration);
181 private void compute_accel() {
182 if (accel_series != null)
185 if (speed_series != null) {
186 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
187 speed_series.filter(temp_series, accel_filter_width);
188 accel_series = add_series(accel_name, AltosConvert.accel);
189 temp_series.differentiate(accel_series);
193 public void set_received_time(long received_time) {
196 public AltosTimeSeries rssi_series;
198 public static final String rssi_name = "RSSI";
200 public AltosTimeSeries status_series;
202 public static final String status_name = "Radio Status";
204 public void set_rssi(int rssi, int status) {
205 if (rssi_series == null) {
206 rssi_series = add_series(rssi_name, null);
207 status_series = add_series(status_name, null);
209 rssi_series.add(time(), rssi);
210 status_series.add(time(), status);
213 public AltosTimeSeries pressure_series;
215 public static final String pressure_name = "Pressure";
217 public AltosTimeSeries altitude_series;
219 public static final String altitude_name = "Altitude";
221 public AltosTimeSeries height_series;
223 public static final String height_name = "Height";
225 public void set_pressure(double pa) {
226 if (pa == AltosLib.MISSING)
229 if (pressure_series == null)
230 pressure_series = add_series(pressure_name, AltosConvert.pressure);
231 pressure_series.add(time(), pa);
232 if (altitude_series == null)
233 altitude_series = add_series(altitude_name, AltosConvert.height);
235 if (cal_data().ground_pressure == AltosLib.MISSING)
236 cal_data().set_ground_pressure(pa);
238 double altitude = AltosConvert.pressure_to_altitude(pa);
239 altitude_series.add(time(), altitude);
242 private void compute_height() {
243 double ground_altitude = cal_data().ground_altitude;
244 if (height_series == null && ground_altitude != AltosLib.MISSING && altitude_series != null) {
245 height_series = add_series(height_name, AltosConvert.height);
246 for (AltosTimeValue alt : altitude_series)
247 height_series.add(alt.time, alt.value - ground_altitude);
250 if (gps_height == null && cal_data().gps_pad != null && cal_data().gps_pad.alt != AltosLib.MISSING && gps_altitude != null) {
251 double gps_ground_altitude = cal_data().gps_pad.alt;
252 gps_height = add_series(gps_height_name, AltosConvert.height);
253 for (AltosTimeValue gps_alt : gps_altitude)
254 gps_height.add(gps_alt.time, gps_alt.value - gps_ground_altitude);
258 public AltosTimeSeries speed_series;
260 public static final String speed_name = "Speed";
262 private void compute_speed() {
263 if (speed_series != null)
266 AltosTimeSeries alt_speed_series = null;
267 AltosTimeSeries accel_speed_series = null;
269 if (altitude_series != null) {
270 AltosTimeSeries temp_series = make_series(altitude_name, AltosConvert.height);
271 altitude_series.filter(temp_series, speed_filter_width);
273 alt_speed_series = make_series(speed_name, AltosConvert.speed);
274 temp_series.differentiate(alt_speed_series);
276 if (accel_series != null) {
278 if (orient_series != null) {
279 vert_accel_series = add_series(vert_accel_name, AltosConvert.accel);
281 for (AltosTimeValue a : accel_series) {
282 double orient = orient_series.value(a.time);
283 double a_abs = a.value + AltosConvert.gravity;
284 double v_a = a_abs * Math.cos(AltosConvert.degrees_to_radians(orient)) - AltosConvert.gravity;
286 vert_accel_series.add(a.time, v_a);
290 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
292 if (vert_accel_series != null)
293 vert_accel_series.integrate(temp_series);
295 accel_series.integrate(temp_series);
297 accel_speed_series = make_series(speed_name, AltosConvert.speed);
298 temp_series.filter(accel_speed_series, 0.1);
301 if (alt_speed_series != null && accel_speed_series != null) {
302 double apogee_time = AltosLib.MISSING;
303 if (state_series != null) {
304 for (AltosTimeValue d : state_series) {
305 if (d.value >= AltosLib.ao_flight_drogue){
306 apogee_time = d.time;
311 if (apogee_time == AltosLib.MISSING) {
312 speed_series = alt_speed_series;
314 speed_series = make_series(speed_name, AltosConvert.speed);
315 for (AltosTimeValue d : accel_speed_series) {
316 if (d.time <= apogee_time)
319 for (AltosTimeValue d : alt_speed_series) {
320 if (d.time > apogee_time)
325 } else if (alt_speed_series != null) {
326 speed_series = alt_speed_series;
327 } else if (accel_speed_series != null) {
328 speed_series = accel_speed_series;
330 if (speed_series != null)
331 add_series(speed_series);
334 public AltosTimeSeries orient_series;
336 public static final String orient_name = "Tilt Angle";
338 private void compute_orient() {
340 if (orient_series != null)
343 if (accel_ground_across == AltosLib.MISSING)
346 if (cal_data().pad_orientation == AltosLib.MISSING)
349 if (cal_data().accel_zero_across == AltosLib.MISSING)
352 AltosRotation rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - cal_data().accel_zero_across),
353 AltosIMU.convert_accel(accel_ground_through - cal_data().accel_zero_through),
354 AltosIMU.convert_accel(accel_ground_along - cal_data().accel_zero_along),
355 cal_data().pad_orientation);
356 double prev_time = ground_time;
358 orient_series = add_series(orient_name, AltosConvert.orient);
359 orient_series.add(ground_time, rotation.tilt());
361 for (AltosTimeValue roll_v : gyro_roll) {
362 double time = roll_v.time;
363 double dt = time - prev_time;
366 double roll = AltosConvert.degrees_to_radians(roll_v.value) * dt;
367 double pitch = AltosConvert.degrees_to_radians(gyro_pitch.value(time)) * dt;
368 double yaw = AltosConvert.degrees_to_radians(gyro_yaw.value(time)) * dt;
370 rotation.rotate(pitch, yaw, roll);
371 orient_series.add(time, rotation.tilt());
377 public AltosTimeSeries kalman_height_series, kalman_speed_series, kalman_accel_series;
379 public static final String kalman_height_name = "Kalman Height";
380 public static final String kalman_speed_name = "Kalman Speed";
381 public static final String kalman_accel_name = "Kalman Accel";
383 public void set_kalman(double height, double speed, double acceleration) {
384 if (kalman_height_series == null) {
385 kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
386 kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
387 kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
389 kalman_height_series.add(time(), height);
390 kalman_speed_series.add(time(), speed);
391 kalman_accel_series.add(time(), acceleration);
394 public AltosTimeSeries thrust_series;
396 public static final String thrust_name = "Thrust";
398 public void set_thrust(double N) {
399 if (thrust_series == null)
400 thrust_series = add_series(thrust_name, AltosConvert.force);
401 thrust_series.add(time(), N);
404 public AltosTimeSeries temperature_series;
406 public static final String temperature_name = "Temperature";
408 public void set_temperature(double deg_c) {
409 if (temperature_series == null)
410 temperature_series = add_series(temperature_name, AltosConvert.temperature);
411 temperature_series.add(time(), deg_c);
414 public AltosTimeSeries battery_voltage_series;
416 public static final String battery_voltage_name = "Battery Voltage";
418 public void set_battery_voltage(double volts) {
419 if (volts == AltosLib.MISSING)
421 if (battery_voltage_series == null)
422 battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
423 battery_voltage_series.add(time(), volts);
426 public AltosTimeSeries apogee_voltage_series;
428 public static final String apogee_voltage_name = "Apogee Voltage";
430 public void set_apogee_voltage(double volts) {
431 if (volts == AltosLib.MISSING)
433 if (apogee_voltage_series == null)
434 apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
435 apogee_voltage_series.add(time(), volts);
438 public AltosTimeSeries main_voltage_series;
440 public static final String main_voltage_name = "Main Voltage";
442 public void set_main_voltage(double volts) {
443 if (volts == AltosLib.MISSING)
445 if (main_voltage_series == null)
446 main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
447 main_voltage_series.add(time(), volts);
450 public ArrayList<AltosGPSTimeValue> gps_series;
452 public AltosGPS gps_before(double time) {
454 for (AltosGPSTimeValue gtv : gps_series)
455 if (gtv.time <= time)
462 public AltosTimeSeries sats_in_view;
463 public AltosTimeSeries sats_in_soln;
464 public AltosTimeSeries gps_altitude;
465 public AltosTimeSeries gps_height;
466 public AltosTimeSeries gps_ground_speed;
467 public AltosTimeSeries gps_ascent_rate;
468 public AltosTimeSeries gps_course;
469 public AltosTimeSeries gps_speed;
470 public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
472 public static final String sats_in_view_name = "Satellites in view";
473 public static final String sats_in_soln_name = "Satellites in solution";
474 public static final String gps_altitude_name = "GPS Altitude";
475 public static final String gps_height_name = "GPS Height";
476 public static final String gps_ground_speed_name = "GPS Ground Speed";
477 public static final String gps_ascent_rate_name = "GPS Ascent Rate";
478 public static final String gps_course_name = "GPS Course";
479 public static final String gps_speed_name = "GPS Speed";
480 public static final String gps_pdop_name = "GPS Dilution of Precision";
481 public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
482 public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
484 public void set_gps(AltosGPS gps) {
485 if (gps_series == null)
486 gps_series = new ArrayList<AltosGPSTimeValue>();
487 gps_series.add(new AltosGPSTimeValue(time(), gps));
489 if (sats_in_soln == null) {
490 sats_in_soln = add_series(sats_in_soln_name, null);
492 sats_in_soln.add(time(), gps.nsat);
493 if (gps.pdop != AltosLib.MISSING) {
494 if (gps_pdop == null)
495 gps_pdop = add_series(gps_pdop_name, null);
496 gps_pdop.add(time(), gps.pdop);
498 if (gps.hdop != AltosLib.MISSING) {
499 if (gps_hdop == null)
500 gps_hdop = add_series(gps_hdop_name, null);
501 gps_hdop.add(time(), gps.hdop);
503 if (gps.vdop != AltosLib.MISSING) {
504 if (gps_vdop == null)
505 gps_vdop = add_series(gps_vdop_name, null);
506 gps_vdop.add(time(), gps.vdop);
509 if (gps.alt != AltosLib.MISSING) {
510 if (gps_altitude == null)
511 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
512 gps_altitude.add(time(), gps.alt);
514 if (gps.ground_speed != AltosLib.MISSING) {
515 if (gps_ground_speed == null)
516 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
517 gps_ground_speed.add(time(), gps.ground_speed);
519 if (gps.climb_rate != AltosLib.MISSING) {
520 if (gps_ascent_rate == null)
521 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
522 gps_ascent_rate.add(time(), gps.climb_rate);
524 if (gps.course != AltosLib.MISSING) {
525 if (gps_course == null)
526 gps_course = add_series(gps_course_name, null);
527 gps_course.add(time(), gps.course);
529 if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING) {
530 if (gps_speed == null)
531 gps_speed = add_series(gps_speed_name, null);
532 gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
533 gps.climb_rate * gps.climb_rate));
536 if (gps.cc_gps_sat != null) {
537 if (sats_in_view == null)
538 sats_in_view = add_series(sats_in_view_name, null);
539 sats_in_view.add(time(), gps.cc_gps_sat.length);
543 public static final String accel_along_name = "Accel Along";
544 public static final String accel_across_name = "Accel Across";
545 public static final String accel_through_name = "Accel Through";
547 public AltosTimeSeries accel_along, accel_across, accel_through;
549 public static final String gyro_roll_name = "Roll Rate";
550 public static final String gyro_pitch_name = "Pitch Rate";
551 public static final String gyro_yaw_name = "Yaw Rate";
553 public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
555 public static final String mag_along_name = "Magnetic Field Along";
556 public static final String mag_across_name = "Magnetic Field Across";
557 public static final String mag_through_name = "Magnetic Field Through";
559 public AltosTimeSeries mag_along, mag_across, mag_through;
561 public void set_accel(double along, double across, double through) {
562 if (accel_along == null) {
563 accel_along = add_series(accel_along_name, AltosConvert.accel);
564 accel_across = add_series(accel_across_name, AltosConvert.accel);
565 accel_through = add_series(accel_through_name, AltosConvert.accel);
567 accel_along.add(time(), along);
568 accel_across.add(time(), across);
569 accel_through.add(time(), through);
572 private double accel_ground_along = AltosLib.MISSING;
573 private double accel_ground_across = AltosLib.MISSING;
574 private double accel_ground_through = AltosLib.MISSING;
576 private double ground_time;
578 public void set_accel_ground(double along, double across, double through) {
579 accel_ground_along = along;
580 accel_ground_across = across;
581 accel_ground_through = through;
582 ground_time = time();
585 public void set_gyro(double roll, double pitch, double yaw) {
586 if (gyro_roll == null) {
587 gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
588 gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
589 gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
591 gyro_roll.add(time(), roll);
592 gyro_pitch.add(time(), pitch);
593 gyro_yaw.add(time(), yaw);
596 public void set_mag(double along, double across, double through) {
597 if (mag_along == null) {
598 mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
599 mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
600 mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
602 mag_along.add(time(), along);
603 mag_across.add(time(), across);
604 mag_through.add(time(), through);
607 public void set_orient(double orient) {
608 if (orient_series == null)
609 orient_series = add_series(orient_name, AltosConvert.orient);
610 orient_series.add(time(), orient);
613 public static final String pyro_voltage_name = "Pyro Voltage";
615 public AltosTimeSeries pyro_voltage;
617 public void set_pyro_voltage(double volts) {
618 if (pyro_voltage == null)
619 pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
620 pyro_voltage.add(time(), volts);
623 private static String[] igniter_voltage_names;
625 public String igniter_voltage_name(int channel) {
626 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
627 String[] new_igniter_voltage_names = new String[channel + 1];
630 if (igniter_voltage_names != null) {
631 for (; i < igniter_voltage_names.length; i++)
632 new_igniter_voltage_names[i] = igniter_voltage_names[i];
634 for (; i < channel+1; i++)
635 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
636 igniter_voltage_names = new_igniter_voltage_names;
638 return igniter_voltage_names[channel];
641 public AltosTimeSeries[] igniter_voltage;
643 public void set_igniter_voltage(double[] voltage) {
644 int channels = voltage.length;
645 if (igniter_voltage == null || igniter_voltage.length <= channels) {
646 AltosTimeSeries[] new_igniter_voltage = new AltosTimeSeries[channels + 1];
649 if (igniter_voltage != null) {
650 for (; i < igniter_voltage.length; i++)
651 new_igniter_voltage[i] = igniter_voltage[i];
653 for (; i < channels; i++)
654 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
655 igniter_voltage = new_igniter_voltage;
657 for (int channel = 0; channel < voltage.length; channel++)
658 igniter_voltage[channel].add(time(), voltage[channel]);
661 public static final String pyro_fired_name = "Pyro Channel State";
663 public AltosTimeSeries pyro_fired_series;
667 public void set_pyro_fired(int pyro_mask) {
668 if (pyro_fired_series == null)
669 pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
670 for (int channel = 0; channel < 32; channel++) {
671 if ((last_pyro_mask & (1 << channel)) == 0 &&
672 (pyro_mask & (1 << channel)) != 0) {
673 pyro_fired_series.add(time(), channel);
676 last_pyro_mask = pyro_mask;
679 public void set_companion(AltosCompanion companion) {
682 public void finish() {
689 public AltosTimeSeries[] series() {
691 return series.toArray(new AltosTimeSeries[0]);
694 public AltosFlightSeries(AltosCalData cal_data) {