altoslib: Use first pressure value if no ground pressure available
[fw/altos] / altoslib / AltosFlightSeries.java
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 package org.altusmetrum.altoslib_11;
16
17 import java.util.*;
18
19 public class AltosFlightSeries extends AltosDataListener {
20
21         public ArrayList<AltosTimeSeries> series = new ArrayList<AltosTimeSeries>();
22
23         public double   speed_filter_width = 4.0;
24         public double   accel_filter_width = 4.0;
25
26         public int[] indices() {
27                 int[] indices = new int[series.size()];
28                 for (int i = 0; i < indices.length; i++)
29                         indices[i] = -1;
30                 step_indices(indices);
31                 return indices;
32         }
33
34         private double time(int id, int index) {
35                 AltosTimeSeries         s = series.get(id);
36
37                 if (index < 0)
38                         return Double.NEGATIVE_INFINITY;
39
40                 if (index < s.values.size())
41                         return s.values.get(index).time;
42                 return Double.POSITIVE_INFINITY;
43         }
44
45         public boolean step_indices(int[] indices) {
46                 double  min_next = time(0, indices[0]+1);
47
48                 for (int i = 1; i < indices.length; i++) {
49                         double next = time(i, indices[i]+1);
50                         if (next < min_next)
51                                 min_next = next;
52                 }
53
54                 if (min_next == Double.POSITIVE_INFINITY)
55                         return false;
56
57                 for (int i = 0; i < indices.length; i++) {
58                         double  t = time(i, indices[i] + 1);
59
60                         if (t <= min_next)
61                                 indices[i]++;
62                 }
63                 return true;
64         }
65
66         public double time(int[] indices) {
67                 double max = time(0, indices[0]);
68
69                 for (int i = 1; i < indices.length; i++) {
70                         double t = time(i, indices[i]);
71                         if (t >= max)
72                                 max = t;
73                 }
74                 return max;
75         }
76
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];
82                                 if (index < 0)
83                                         index = 0;
84                                 if (index >= s.values.size())
85                                         index = s.values.size() - 1;
86                                 return s.values.get(index).value;
87                         }
88                 }
89                 return AltosLib.MISSING;
90         }
91
92         public double value(String name, double time) {
93                 for (AltosTimeSeries s : series) {
94                         if (s.label.equals(name))
95                                 return s.value(time);
96                 }
97                 return AltosLib.MISSING;
98         }
99
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);
104                 }
105                 return AltosLib.MISSING;
106         }
107
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);
112                 }
113                 return AltosLib.MISSING;
114         }
115
116         public AltosTimeSeries make_series(String label, AltosUnits units) {
117                 return new AltosTimeSeries(label, units);
118         }
119
120         public void add_series(AltosTimeSeries s) {
121                 series.add(s);
122         }
123
124         public AltosTimeSeries add_series(String label, AltosUnits units) {
125                 AltosTimeSeries s = make_series(label, units);
126                 add_series(s);
127                 return s;
128         }
129
130         public void remove_series(AltosTimeSeries s) {
131                 series.remove(s);
132         }
133
134         public boolean has_series(String label) {
135                 for (AltosTimeSeries s : series)
136                         if (s.label.equals(label))
137                                 return true;
138                 return false;
139         }
140
141         public AltosTimeSeries state_series;
142
143         public static final String state_name = "State";
144
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)
149                         return;
150                 this.state = state;
151                 state_series.add(time(), state);
152         }
153
154         public AltosTimeSeries  accel_series;
155
156         public static final String accel_name = "Accel";
157
158         public void set_acceleration(double acceleration) {
159                 if (accel_series == null) {
160                         accel_series = add_series(accel_name, AltosConvert.accel);
161                 }
162                 accel_series.add(time(), acceleration);
163         }
164
165         private void compute_accel() {
166                 if (accel_series != null)
167                         return;
168
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);
174                 }
175         }
176
177         public void set_received_time(long received_time) {
178         }
179
180         public AltosTimeSeries rssi_series;
181
182         public static final String rssi_name = "RSSI";
183
184         public AltosTimeSeries status_series;
185
186         public static final String status_name = "Radio Status";
187
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);
192                 }
193                 rssi_series.add(time(), rssi);
194                 status_series.add(time(), status);
195         }
196
197         public AltosTimeSeries pressure_series;
198
199         public static final String pressure_name = "Pressure";
200
201         public AltosTimeSeries altitude_series;
202
203         public static final String altitude_name = "Altitude";
204
205         public AltosTimeSeries height_series;
206
207         public static final String height_name = "Height";
208
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);
215
216                 if (cal_data.ground_pressure == AltosLib.MISSING)
217                         cal_data.set_ground_pressure(pa);
218
219                 double altitude = AltosConvert.pressure_to_altitude(pa);
220                 altitude_series.add(time(), altitude);
221         }
222
223         private void compute_height() {
224                 double ground_altitude = cal_data.ground_altitude;
225                 if (height_series == null && ground_altitude != AltosLib.MISSING && altitude_series != null) {
226                         height_series = add_series(height_name, AltosConvert.height);
227                         for (AltosTimeValue alt : altitude_series)
228                                 height_series.add(alt.time, alt.value - ground_altitude);
229                 }
230         }
231
232         public AltosTimeSeries speed_series;
233
234         public static final String speed_name = "Speed";
235
236         private void compute_speed() {
237                 if (speed_series != null)
238                         return;
239
240                 AltosTimeSeries alt_speed_series = null;
241                 AltosTimeSeries accel_speed_series = null;
242
243                 if (altitude_series != null) {
244                         AltosTimeSeries temp_series = make_series(altitude_name, AltosConvert.height);
245                         altitude_series.filter(temp_series, speed_filter_width);
246
247                         alt_speed_series = make_series(speed_name, AltosConvert.speed);
248                         temp_series.differentiate(alt_speed_series);
249                 }
250                 if (accel_series != null) {
251                         AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
252                         accel_series.integrate(temp_series);
253
254                         accel_speed_series = make_series(speed_name, AltosConvert.speed);
255                         temp_series.filter(accel_speed_series, 0.1);
256                 }
257
258                 if (alt_speed_series != null && accel_speed_series != null) {
259                         double  apogee_time = AltosLib.MISSING;
260                         if (state_series != null) {
261                                 for (AltosTimeValue d : state_series) {
262                                         if (d.value >= AltosLib.ao_flight_drogue){
263                                                 apogee_time = d.time;
264                                                 break;
265                                         }
266                                 }
267                         }
268                         if (apogee_time == AltosLib.MISSING) {
269                                 speed_series = alt_speed_series;
270                         } else {
271                                 speed_series = make_series(speed_name, AltosConvert.speed);
272                                 for (AltosTimeValue d : accel_speed_series) {
273                                         if (d.time <= apogee_time)
274                                                 speed_series.add(d);
275                                 }
276                                 for (AltosTimeValue d : alt_speed_series) {
277                                         if (d.time > apogee_time)
278                                                 speed_series.add(d);
279                                 }
280
281                         }
282                 } else if (alt_speed_series != null) {
283                         speed_series = alt_speed_series;
284                 } else if (accel_speed_series != null) {
285                         speed_series = accel_speed_series;
286                 }
287                 if (speed_series != null)
288                         add_series(speed_series);
289         }
290
291         public AltosTimeSeries  kalman_height_series, kalman_speed_series, kalman_accel_series;
292
293         public static final String kalman_height_name = "Kalman Height";
294         public static final String kalman_speed_name = "Kalman Speed";
295         public static final String kalman_accel_name = "Kalman Accel";
296
297         public void set_kalman(double height, double speed, double acceleration) {
298                 if (kalman_height_series == null) {
299                         kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
300                         kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
301                         kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
302                 }
303                 kalman_height_series.add(time(), height);
304                 kalman_speed_series.add(time(), speed);
305                 kalman_accel_series.add(time(), acceleration);
306         }
307
308         public AltosTimeSeries thrust_series;
309
310         public static final String thrust_name = "Thrust";
311
312         public  void set_thrust(double N) {
313                 if (thrust_series == null)
314                         thrust_series = add_series(thrust_name, AltosConvert.force);
315                 thrust_series.add(time(), N);
316         }
317
318         public AltosTimeSeries temperature_series;
319
320         public static final String temperature_name = "Temperature";
321
322         public  void set_temperature(double deg_c) {
323                 if (temperature_series == null)
324                         temperature_series = add_series(temperature_name, AltosConvert.temperature);
325                 temperature_series.add(time(), deg_c);
326         }
327
328         public AltosTimeSeries battery_voltage_series;
329
330         public static final String battery_voltage_name = "Battery Voltage";
331
332         public void set_battery_voltage(double volts) {
333                 if (volts == AltosLib.MISSING)
334                         return;
335                 if (battery_voltage_series == null)
336                         battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
337                 battery_voltage_series.add(time(), volts);
338         }
339
340         public AltosTimeSeries apogee_voltage_series;
341
342         public static final String apogee_voltage_name = "Apogee Voltage";
343
344         public void set_apogee_voltage(double volts) {
345                 if (volts == AltosLib.MISSING)
346                         return;
347                 if (apogee_voltage_series == null)
348                         apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
349                 apogee_voltage_series.add(time(), volts);
350         }
351
352         public AltosTimeSeries main_voltage_series;
353
354         public static final String main_voltage_name = "Main Voltage";
355
356         public void set_main_voltage(double volts) {
357                 if (volts == AltosLib.MISSING)
358                         return;
359                 if (main_voltage_series == null)
360                         main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
361                 main_voltage_series.add(time(), volts);
362         }
363
364         public ArrayList<AltosGPSTimeValue> gps_series;
365
366         public AltosGPS gps_before(double time) {
367                 AltosGPS gps = null;
368                 for (AltosGPSTimeValue gtv : gps_series)
369                         if (gtv.time <= time)
370                                 gps = gtv.gps;
371                         else
372                                 break;
373                 return gps;
374         }
375
376         public AltosTimeSeries  sats_in_view;
377         public AltosTimeSeries sats_in_soln;
378         public AltosTimeSeries gps_altitude;
379         public AltosTimeSeries gps_height;
380         public AltosTimeSeries gps_ground_speed;
381         public AltosTimeSeries gps_ascent_rate;
382         public AltosTimeSeries gps_course;
383         public AltosTimeSeries gps_speed;
384         public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
385
386         public static final String sats_in_view_name = "Satellites in view";
387         public static final String sats_in_soln_name = "Satellites in solution";
388         public static final String gps_altitude_name = "GPS Altitude";
389         public static final String gps_height_name = "GPS Height";
390         public static final String gps_ground_speed_name = "GPS Ground Speed";
391         public static final String gps_ascent_rate_name = "GPS Ascent Rate";
392         public static final String gps_course_name = "GPS Course";
393         public static final String gps_speed_name = "GPS Speed";
394         public static final String gps_pdop_name = "GPS Dilution of Precision";
395         public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
396         public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
397
398         public void set_gps(AltosGPS gps) {
399                 if (gps_series == null)
400                         gps_series = new ArrayList<AltosGPSTimeValue>();
401                 gps_series.add(new AltosGPSTimeValue(time(), gps));
402
403                 if (sats_in_soln == null) {
404                         sats_in_soln = add_series(sats_in_soln_name, null);
405                 }
406                 sats_in_soln.add(time(), gps.nsat);
407                 if (gps.pdop != AltosLib.MISSING) {
408                         if (gps_pdop == null)
409                                 gps_pdop = add_series(gps_pdop_name, null);
410                         gps_pdop.add(time(), gps.pdop);
411                 }
412                 if (gps.hdop != AltosLib.MISSING) {
413                         if (gps_hdop == null)
414                                 gps_hdop = add_series(gps_hdop_name, null);
415                         gps_hdop.add(time(), gps.hdop);
416                 }
417                 if (gps.vdop != AltosLib.MISSING) {
418                         if (gps_vdop == null)
419                                 gps_vdop = add_series(gps_vdop_name, null);
420                         gps_vdop.add(time(), gps.vdop);
421                 }
422                 if (gps.locked) {
423                         if (gps_altitude == null) {
424                                 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
425                                 gps_height = add_series(gps_height_name, AltosConvert.height);
426                                 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
427                                 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
428                                 gps_course = add_series(gps_course_name, null);
429                                 gps_speed = add_series(gps_speed_name, null);
430                         }
431                         if (gps.alt != AltosLib.MISSING) {
432                                 gps_altitude.add(time(), gps.alt);
433                                 if (cal_data.gps_pad != null)
434                                         gps_height.add(time(), gps.alt - cal_data.gps_pad.alt);
435                         }
436                         if (gps.ground_speed != AltosLib.MISSING)
437                                 gps_ground_speed.add(time(), gps.ground_speed);
438                         if (gps.climb_rate != AltosLib.MISSING)
439                                 gps_ascent_rate.add(time(), gps.climb_rate);
440                         if (gps.course != AltosLib.MISSING)
441                                 gps_course.add(time(), gps.course);
442                         if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING)
443                                 gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
444                                                                 gps.climb_rate * gps.climb_rate));
445                 }
446                 if (gps.cc_gps_sat != null) {
447                         if (sats_in_view == null)
448                                 sats_in_view = add_series(sats_in_view_name, null);
449                         sats_in_view.add(time(), gps.cc_gps_sat.length);
450                 }
451         }
452
453         public static final String accel_along_name = "Accel Along";
454         public static final String accel_across_name = "Accel Across";
455         public static final String accel_through_name = "Accel Through";
456
457         public AltosTimeSeries accel_along, accel_across, accel_through;
458
459         public static final String gyro_roll_name = "Roll Rate";
460         public static final String gyro_pitch_name = "Pitch Rate";
461         public static final String gyro_yaw_name = "Yaw Rate";
462
463         public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
464
465         public static final String mag_along_name = "Magnetic Field Along";
466         public static final String mag_across_name = "Magnetic Field Across";
467         public static final String mag_through_name = "Magnetic Field Through";
468
469         public AltosTimeSeries mag_along, mag_across, mag_through;
470
471         public  void set_accel(double along, double across, double through) {
472                 if (accel_along == null) {
473                         accel_along = add_series(accel_along_name, AltosConvert.accel);
474                         accel_across = add_series(accel_across_name, AltosConvert.accel);
475                         accel_through = add_series(accel_through_name, AltosConvert.accel);
476                 }
477                 accel_along.add(time(), along);
478                 accel_across.add(time(), across);
479                 accel_through.add(time(), through);
480         }
481
482         public  void set_accel_ground(double along, double across, double through) {
483         }
484
485         public  void set_gyro(double roll, double pitch, double yaw) {
486                 if (gyro_roll == null) {
487                         gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
488                         gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
489                         gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
490                 }
491                 gyro_roll.add(time(), roll);
492                 gyro_pitch.add(time(), pitch);
493                 gyro_yaw.add(time(), yaw);
494         }
495
496         public  void set_mag(double along, double across, double through) {
497                 if (mag_along == null) {
498                         mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
499                         mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
500                         mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
501                 }
502                 mag_along.add(time(), along);
503                 mag_across.add(time(), across);
504                 mag_through.add(time(), through);
505         }
506
507         public static final String orient_name = "Tilt Angle";
508
509         public AltosTimeSeries orient_series;
510
511         public void set_orient(double orient) {
512                 if (orient_series == null)
513                         orient_series = add_series(orient_name, AltosConvert.orient);
514                 orient_series.add(time(), orient);
515         }
516
517         public static final String pyro_voltage_name = "Pyro Voltage";
518
519         public AltosTimeSeries pyro_voltage;
520
521         public  void set_pyro_voltage(double volts) {
522                 if (pyro_voltage == null)
523                         pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
524                 pyro_voltage.add(time(), volts);
525         }
526
527         private static String[] igniter_voltage_names;
528
529         public String igniter_voltage_name(int channel) {
530                 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
531                         String[] new_igniter_voltage_names = new String[channel + 1];
532                         int     i = 0;
533
534                         if (igniter_voltage_names != null) {
535                                 for (; i < igniter_voltage_names.length; i++)
536                                         new_igniter_voltage_names[i] = igniter_voltage_names[i];
537                         }
538                         for (; i < channel+1; i++)
539                                 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
540                         igniter_voltage_names = new_igniter_voltage_names;
541                 }
542                 return igniter_voltage_names[channel];
543         }
544
545         public AltosTimeSeries[] igniter_voltage;
546
547         public  void set_igniter_voltage(double[] voltage) {
548                 int channels = voltage.length;
549                 if (igniter_voltage == null || igniter_voltage.length <= channels) {
550                         AltosTimeSeries[]       new_igniter_voltage = new AltosTimeSeries[channels + 1];
551                         int                     i = 0;
552
553                         if (igniter_voltage != null) {
554                                 for (; i < igniter_voltage.length; i++)
555                                         new_igniter_voltage[i] = igniter_voltage[i];
556                         }
557                         for (; i < channels; i++)
558                                 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
559                         igniter_voltage = new_igniter_voltage;
560                 }
561                 for (int channel = 0; channel < voltage.length; channel++)
562                         igniter_voltage[channel].add(time(), voltage[channel]);
563         }
564
565         public static final String pyro_fired_name = "Pyro Channel State";
566
567         public AltosTimeSeries pyro_fired_series;
568
569         int     last_pyro_mask;
570
571         public  void set_pyro_fired(int pyro_mask) {
572                 if (pyro_fired_series == null)
573                         pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
574                 for (int channel = 0; channel < 32; channel++) {
575                         if ((last_pyro_mask & (1 << channel)) == 0 &&
576                             (pyro_mask & (1 << channel)) != 0) {
577                                 pyro_fired_series.add(time(), channel);
578                         }
579                 }
580                 last_pyro_mask = pyro_mask;
581         }
582
583         public void set_companion(AltosCompanion companion) {
584         }
585
586         public void finish() {
587                 compute_speed();
588                 compute_accel();
589                 compute_height();
590         }
591
592         public AltosTimeSeries[] series() {
593                 finish();
594                 return series.toArray(new AltosTimeSeries[0]);
595         }
596
597         public AltosFlightSeries(AltosCalData cal_data) {
598                 super(cal_data);
599         }
600 }