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 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) {
124 public AltosTimeSeries add_series(String label, AltosUnits units) {
125 AltosTimeSeries s = make_series(label, units);
130 public void remove_series(AltosTimeSeries s) {
134 public boolean has_series(String label) {
135 for (AltosTimeSeries s : series)
136 if (s.label.equals(label))
141 public AltosTimeSeries state_series;
143 public static final String state_name = "State";
145 public void set_state(int state) {
146 if (state_series == null)
147 state_series = add_series(state_name, AltosConvert.state_name);
148 else if (this.state == state)
151 state_series.add(time(), state);
154 public AltosTimeSeries accel_series;
156 public static final String accel_name = "Accel";
158 public void set_acceleration(double acceleration) {
159 if (accel_series == null) {
160 accel_series = add_series(accel_name, AltosConvert.accel);
162 accel_series.add(time(), acceleration);
165 private void compute_accel() {
166 if (accel_series != null)
169 if (speed_series != null) {
170 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
171 speed_series.filter(temp_series, accel_filter_width);
172 accel_series = add_series(accel_name, AltosConvert.accel);
173 temp_series.differentiate(accel_series);
177 public void set_received_time(long received_time) {
180 public AltosTimeSeries rssi_series;
182 public static final String rssi_name = "RSSI";
184 public AltosTimeSeries status_series;
186 public static final String status_name = "Radio Status";
188 public void set_rssi(int rssi, int status) {
189 if (rssi_series == null) {
190 rssi_series = add_series(rssi_name, null);
191 status_series = add_series(status_name, null);
193 rssi_series.add(time(), rssi);
194 status_series.add(time(), status);
197 public AltosTimeSeries pressure_series;
199 public static final String pressure_name = "Pressure";
201 public AltosTimeSeries altitude_series;
203 public static final String altitude_name = "Altitude";
205 public AltosTimeSeries height_series;
207 public static final String height_name = "Height";
209 public void set_pressure(double pa) {
210 if (pressure_series == null)
211 pressure_series = add_series(pressure_name, AltosConvert.pressure);
212 pressure_series.add(time(), pa);
213 if (altitude_series == null)
214 altitude_series = add_series(altitude_name, AltosConvert.height);
216 double altitude = AltosConvert.pressure_to_altitude(pa);
217 altitude_series.add(time(), altitude);
220 private void compute_height() {
221 double ground_altitude = cal_data.ground_altitude;
222 if (height_series == null && ground_altitude != AltosLib.MISSING && altitude_series != null) {
223 height_series = add_series(height_name, AltosConvert.height);
224 for (AltosTimeValue alt : altitude_series)
225 height_series.add(alt.time, alt.value - ground_altitude);
229 public AltosTimeSeries speed_series;
231 public static final String speed_name = "Speed";
233 private void compute_speed() {
234 if (speed_series != null)
237 AltosTimeSeries alt_speed_series = null;
238 AltosTimeSeries accel_speed_series = null;
240 if (altitude_series != null) {
241 AltosTimeSeries temp_series = make_series(altitude_name, AltosConvert.height);
242 altitude_series.filter(temp_series, speed_filter_width);
244 alt_speed_series = make_series(speed_name, AltosConvert.speed);
245 temp_series.differentiate(alt_speed_series);
247 if (accel_series != null) {
248 AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
249 accel_series.integrate(temp_series);
251 accel_speed_series = make_series(speed_name, AltosConvert.speed);
252 temp_series.filter(accel_speed_series, 0.1);
255 if (alt_speed_series != null && accel_speed_series != null) {
256 double apogee_time = AltosLib.MISSING;
257 if (state_series != null) {
258 for (AltosTimeValue d : state_series) {
259 if (d.value >= AltosLib.ao_flight_drogue){
260 apogee_time = d.time;
265 if (apogee_time == AltosLib.MISSING) {
266 speed_series = alt_speed_series;
268 speed_series = make_series(speed_name, AltosConvert.speed);
269 for (AltosTimeValue d : accel_speed_series) {
270 if (d.time <= apogee_time)
273 for (AltosTimeValue d : alt_speed_series) {
274 if (d.time > apogee_time)
279 } else if (alt_speed_series != null) {
280 speed_series = alt_speed_series;
281 } else if (accel_speed_series != null) {
282 speed_series = accel_speed_series;
284 if (speed_series != null)
285 add_series(speed_series);
288 public AltosTimeSeries kalman_height_series, kalman_speed_series, kalman_accel_series;
290 public static final String kalman_height_name = "Kalman Height";
291 public static final String kalman_speed_name = "Kalman Speed";
292 public static final String kalman_accel_name = "Kalman Accel";
294 public void set_kalman(double height, double speed, double acceleration) {
295 if (kalman_height_series == null) {
296 kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
297 kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
298 kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
300 kalman_height_series.add(time(), height);
301 kalman_speed_series.add(time(), speed);
302 kalman_accel_series.add(time(), acceleration);
305 public AltosTimeSeries thrust_series;
307 public static final String thrust_name = "Thrust";
309 public void set_thrust(double N) {
310 if (thrust_series == null)
311 thrust_series = add_series(thrust_name, AltosConvert.force);
312 thrust_series.add(time(), N);
315 public AltosTimeSeries temperature_series;
317 public static final String temperature_name = "Temperature";
319 public void set_temperature(double deg_c) {
320 if (temperature_series == null)
321 temperature_series = add_series(temperature_name, AltosConvert.temperature);
322 temperature_series.add(time(), deg_c);
325 public AltosTimeSeries battery_voltage_series;
327 public static final String battery_voltage_name = "Battery Voltage";
329 public void set_battery_voltage(double volts) {
330 if (volts == AltosLib.MISSING)
332 if (battery_voltage_series == null)
333 battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
334 battery_voltage_series.add(time(), volts);
337 public AltosTimeSeries apogee_voltage_series;
339 public static final String apogee_voltage_name = "Apogee Voltage";
341 public void set_apogee_voltage(double volts) {
342 if (volts == AltosLib.MISSING)
344 if (apogee_voltage_series == null)
345 apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
346 apogee_voltage_series.add(time(), volts);
349 public AltosTimeSeries main_voltage_series;
351 public static final String main_voltage_name = "Main Voltage";
353 public void set_main_voltage(double volts) {
354 if (volts == AltosLib.MISSING)
356 if (main_voltage_series == null)
357 main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
358 main_voltage_series.add(time(), volts);
361 public ArrayList<AltosGPSTimeValue> gps_series;
363 public AltosGPS gps_before(double time) {
365 for (AltosGPSTimeValue gtv : gps_series)
366 if (gtv.time <= time)
373 public AltosTimeSeries sats_in_view;
374 public AltosTimeSeries sats_in_soln;
375 public AltosTimeSeries gps_altitude;
376 public AltosTimeSeries gps_height;
377 public AltosTimeSeries gps_ground_speed;
378 public AltosTimeSeries gps_ascent_rate;
379 public AltosTimeSeries gps_course;
380 public AltosTimeSeries gps_speed;
381 public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
383 public static final String sats_in_view_name = "Satellites in view";
384 public static final String sats_in_soln_name = "Satellites in solution";
385 public static final String gps_altitude_name = "GPS Altitude";
386 public static final String gps_height_name = "GPS Height";
387 public static final String gps_ground_speed_name = "GPS Ground Speed";
388 public static final String gps_ascent_rate_name = "GPS Ascent Rate";
389 public static final String gps_course_name = "GPS Course";
390 public static final String gps_speed_name = "GPS Speed";
391 public static final String gps_pdop_name = "GPS Dilution of Precision";
392 public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
393 public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
395 public void set_gps(AltosGPS gps) {
396 if (gps_series == null)
397 gps_series = new ArrayList<AltosGPSTimeValue>();
398 gps_series.add(new AltosGPSTimeValue(time(), gps));
400 if (sats_in_soln == null) {
401 sats_in_soln = add_series(sats_in_soln_name, null);
403 sats_in_soln.add(time(), gps.nsat);
404 if (gps.pdop != AltosLib.MISSING) {
405 if (gps_pdop == null)
406 gps_pdop = add_series(gps_pdop_name, null);
407 gps_pdop.add(time(), gps.pdop);
409 if (gps.hdop != AltosLib.MISSING) {
410 if (gps_hdop == null)
411 gps_hdop = add_series(gps_hdop_name, null);
412 gps_hdop.add(time(), gps.hdop);
414 if (gps.vdop != AltosLib.MISSING) {
415 if (gps_vdop == null)
416 gps_vdop = add_series(gps_vdop_name, null);
417 gps_vdop.add(time(), gps.vdop);
420 if (gps_altitude == null) {
421 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
422 gps_height = add_series(gps_height_name, AltosConvert.height);
423 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
424 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
425 gps_course = add_series(gps_course_name, null);
426 gps_speed = add_series(gps_speed_name, null);
428 if (gps.alt != AltosLib.MISSING) {
429 gps_altitude.add(time(), gps.alt);
430 if (cal_data.gps_pad != null)
431 gps_height.add(time(), gps.alt - cal_data.gps_pad.alt);
433 if (gps.ground_speed != AltosLib.MISSING)
434 gps_ground_speed.add(time(), gps.ground_speed);
435 if (gps.climb_rate != AltosLib.MISSING)
436 gps_ascent_rate.add(time(), gps.climb_rate);
437 if (gps.course != AltosLib.MISSING)
438 gps_course.add(time(), gps.course);
439 if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING)
440 gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
441 gps.climb_rate * gps.climb_rate));
443 if (gps.cc_gps_sat != null) {
444 if (sats_in_view == null)
445 sats_in_view = add_series(sats_in_view_name, null);
446 sats_in_view.add(time(), gps.cc_gps_sat.length);
450 public static final String accel_along_name = "Accel Along";
451 public static final String accel_across_name = "Accel Across";
452 public static final String accel_through_name = "Accel Through";
454 public AltosTimeSeries accel_along, accel_across, accel_through;
456 public static final String gyro_roll_name = "Roll Rate";
457 public static final String gyro_pitch_name = "Pitch Rate";
458 public static final String gyro_yaw_name = "Yaw Rate";
460 public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
462 public static final String mag_along_name = "Magnetic Field Along";
463 public static final String mag_across_name = "Magnetic Field Across";
464 public static final String mag_through_name = "Magnetic Field Through";
466 public AltosTimeSeries mag_along, mag_across, mag_through;
468 public void set_accel(double along, double across, double through) {
469 if (accel_along == null) {
470 accel_along = add_series(accel_along_name, AltosConvert.accel);
471 accel_across = add_series(accel_across_name, AltosConvert.accel);
472 accel_through = add_series(accel_through_name, AltosConvert.accel);
474 accel_along.add(time(), along);
475 accel_across.add(time(), across);
476 accel_through.add(time(), through);
479 public void set_accel_ground(double along, double across, double through) {
482 public void set_gyro(double roll, double pitch, double yaw) {
483 if (gyro_roll == null) {
484 gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
485 gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
486 gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
488 gyro_roll.add(time(), roll);
489 gyro_pitch.add(time(), pitch);
490 gyro_yaw.add(time(), yaw);
493 public void set_mag(double along, double across, double through) {
494 if (mag_along == null) {
495 mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
496 mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
497 mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
499 mag_along.add(time(), along);
500 mag_across.add(time(), across);
501 mag_through.add(time(), through);
504 public static final String orient_name = "Tilt Angle";
506 public AltosTimeSeries orient_series;
508 public void set_orient(double orient) {
509 if (orient_series == null)
510 orient_series = add_series(orient_name, AltosConvert.orient);
511 orient_series.add(time(), orient);
514 public static final String pyro_voltage_name = "Pyro Voltage";
516 public AltosTimeSeries pyro_voltage;
518 public void set_pyro_voltage(double volts) {
519 if (pyro_voltage == null)
520 pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
521 pyro_voltage.add(time(), volts);
524 private static String[] igniter_voltage_names;
526 public String igniter_voltage_name(int channel) {
527 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
528 String[] new_igniter_voltage_names = new String[channel + 1];
531 if (igniter_voltage_names != null) {
532 for (; i < igniter_voltage_names.length; i++)
533 new_igniter_voltage_names[i] = igniter_voltage_names[i];
535 for (; i < channel+1; i++)
536 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
537 igniter_voltage_names = new_igniter_voltage_names;
539 return igniter_voltage_names[channel];
542 public AltosTimeSeries[] igniter_voltage;
544 public void set_igniter_voltage(double[] voltage) {
545 int channels = voltage.length;
546 if (igniter_voltage == null || igniter_voltage.length <= channels) {
547 AltosTimeSeries[] new_igniter_voltage = new AltosTimeSeries[channels + 1];
550 if (igniter_voltage != null) {
551 for (; i < igniter_voltage.length; i++)
552 new_igniter_voltage[i] = igniter_voltage[i];
554 for (; i < channels; i++)
555 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
556 igniter_voltage = new_igniter_voltage;
558 for (int channel = 0; channel < voltage.length; channel++)
559 igniter_voltage[channel].add(time(), voltage[channel]);
562 public static final String pyro_fired_name = "Pyro Channel State";
564 public AltosTimeSeries pyro_fired_series;
568 public void set_pyro_fired(int pyro_mask) {
569 if (pyro_fired_series == null)
570 pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
571 for (int channel = 0; channel < 32; channel++) {
572 if ((last_pyro_mask & (1 << channel)) == 0 &&
573 (pyro_mask & (1 << channel)) != 0) {
574 pyro_fired_series.add(time(), channel);
577 last_pyro_mask = pyro_mask;
580 public void set_companion(AltosCompanion companion) {
583 public void finish() {
589 public AltosTimeSeries[] series() {
591 return series.toArray(new AltosTimeSeries[0]);
594 public AltosFlightSeries(AltosCalData cal_data) {