altoslib: Change how speed/accel are computed from pressure
[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 int[] indices() {
24                 int[] indices = new int[series.size()];
25                 for (int i = 0; i < indices.length; i++)
26                         indices[i] = -1;
27                 step_indices(indices);
28                 return indices;
29         }
30
31         private double time(int id, int index) {
32                 AltosTimeSeries         s = series.get(id);
33
34                 if (index < 0)
35                         return Double.NEGATIVE_INFINITY;
36
37                 if (index < s.values.size())
38                         return s.values.get(index).time;
39                 return Double.POSITIVE_INFINITY;
40         }
41
42         public boolean step_indices(int[] indices) {
43                 double  min_next = time(0, indices[0]+1);
44
45                 for (int i = 1; i < indices.length; i++) {
46                         double next = time(i, indices[i]+1);
47                         if (next < min_next)
48                                 min_next = next;
49                 }
50
51                 if (min_next == Double.POSITIVE_INFINITY)
52                         return false;
53
54                 for (int i = 0; i < indices.length; i++) {
55                         double  t = time(i, indices[i] + 1);
56
57                         if (t <= min_next)
58                                 indices[i]++;
59                 }
60                 return true;
61         }
62
63         public double time(int[] indices) {
64                 double max = time(0, indices[0]);
65
66                 for (int i = 1; i < indices.length; i++) {
67                         double t = time(i, indices[i]);
68                         if (t >= max)
69                                 max = t;
70                 }
71                 return max;
72         }
73
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];
79                                 if (index < 0)
80                                         index = 0;
81                                 if (index >= s.values.size())
82                                         index = s.values.size() - 1;
83                                 return s.values.get(index).value;
84                         }
85                 }
86                 return AltosLib.MISSING;
87         }
88
89         public double value(String name, double time) {
90                 for (AltosTimeSeries s : series) {
91                         if (s.label.equals(name))
92                                 return s.value(time);
93                 }
94                 return AltosLib.MISSING;
95         }
96
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);
101                 }
102                 return AltosLib.MISSING;
103         }
104
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);
109                 }
110                 return AltosLib.MISSING;
111         }
112
113         public AltosTimeSeries make_series(String label, AltosUnits units) {
114                 return new AltosTimeSeries(label, units);
115         }
116
117         public void add_series(AltosTimeSeries s) {
118                 series.add(s);
119         }
120
121         public AltosTimeSeries add_series(String label, AltosUnits units) {
122                 AltosTimeSeries s = make_series(label, units);
123                 add_series(s);
124                 return s;
125         }
126
127         public void remove_series(AltosTimeSeries s) {
128                 series.remove(s);
129         }
130
131         public boolean has_series(String label) {
132                 for (AltosTimeSeries s : series)
133                         if (s.label.equals(label))
134                                 return true;
135                 return false;
136         }
137
138         public AltosTimeSeries state_series;
139
140         public static final String state_name = "State";
141
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)
146                         return;
147                 this.state = state;
148                 state_series.add(time(), state);
149         }
150
151         public AltosTimeSeries  accel_series;
152
153         public static final String accel_name = "Accel";
154
155         public void set_acceleration(double acceleration) {
156                 if (accel_series == null) {
157                         accel_series = add_series(accel_name, AltosConvert.accel);
158                 }
159                 accel_series.add(time(), acceleration);
160         }
161
162         private void compute_accel() {
163                 if (accel_series != null)
164                         return;
165
166                 if (speed_series != null) {
167                         AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
168                         speed_series.filter(temp_series, 2.0);
169                         accel_series = add_series(accel_name, AltosConvert.accel);
170                         temp_series.differentiate(accel_series);
171                 }
172         }
173
174         public void set_received_time(long received_time) {
175         }
176
177         public AltosTimeSeries rssi_series;
178
179         public static final String rssi_name = "RSSI";
180
181         public AltosTimeSeries status_series;
182
183         public static final String status_name = "Radio Status";
184
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);
189                 }
190                 rssi_series.add(time(), rssi);
191                 status_series.add(time(), status);
192         }
193
194         public AltosTimeSeries pressure_series;
195
196         public static final String pressure_name = "Pressure";
197
198         public AltosTimeSeries altitude_series;
199
200         public static final String altitude_name = "Altitude";
201
202         public AltosTimeSeries height_series;
203
204         public static final String height_name = "Height";
205
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);
212
213                 double altitude = AltosConvert.pressure_to_altitude(pa);
214                 altitude_series.add(time(), altitude);
215         }
216
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);
223                 }
224         }
225
226         public AltosTimeSeries speed_series;
227
228         public static final String speed_name = "Speed";
229
230         private void compute_speed() {
231                 if (speed_series != null)
232                         return;
233
234                 AltosTimeSeries alt_speed_series = null;
235                 AltosTimeSeries accel_speed_series = null;
236
237                 if (altitude_series != null) {
238                         AltosTimeSeries temp_series = make_series(altitude_name, AltosConvert.height);
239                         altitude_series.filter(temp_series, 1.0);
240
241                         alt_speed_series = make_series(speed_name, AltosConvert.speed);
242                         temp_series.differentiate(alt_speed_series);
243                 }
244                 if (accel_series != null) {
245                         AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
246                         accel_series.integrate(temp_series);
247
248                         accel_speed_series = make_series(speed_name, AltosConvert.speed);
249                         temp_series.filter(accel_speed_series, 0.1);
250                 }
251
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;
258                                                 break;
259                                         }
260                                 }
261                         }
262                         if (apogee_time == AltosLib.MISSING) {
263                                 speed_series = alt_speed_series;
264                         } else {
265                                 speed_series = make_series(speed_name, AltosConvert.speed);
266                                 for (AltosTimeValue d : accel_speed_series) {
267                                         if (d.time <= apogee_time)
268                                                 speed_series.add(d);
269                                 }
270                                 for (AltosTimeValue d : alt_speed_series) {
271                                         if (d.time > apogee_time)
272                                                 speed_series.add(d);
273                                 }
274
275                         }
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;
280                 }
281                 if (speed_series != null)
282                         add_series(speed_series);
283         }
284
285         public AltosTimeSeries  kalman_height_series, kalman_speed_series, kalman_accel_series;
286
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";
290
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);
296                 }
297                 kalman_height_series.add(time(), height);
298                 kalman_speed_series.add(time(), speed);
299                 kalman_accel_series.add(time(), acceleration);
300         }
301
302         public AltosTimeSeries thrust_series;
303
304         public static final String thrust_name = "Thrust";
305
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);
310         }
311
312         public AltosTimeSeries temperature_series;
313
314         public static final String temperature_name = "Temperature";
315
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);
320         }
321
322         public AltosTimeSeries battery_voltage_series;
323
324         public static final String battery_voltage_name = "Battery Voltage";
325
326         public void set_battery_voltage(double volts) {
327                 if (volts == AltosLib.MISSING)
328                         return;
329                 if (battery_voltage_series == null)
330                         battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
331                 battery_voltage_series.add(time(), volts);
332         }
333
334         public AltosTimeSeries apogee_voltage_series;
335
336         public static final String apogee_voltage_name = "Apogee Voltage";
337
338         public void set_apogee_voltage(double volts) {
339                 if (volts == AltosLib.MISSING)
340                         return;
341                 if (apogee_voltage_series == null)
342                         apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
343                 apogee_voltage_series.add(time(), volts);
344         }
345
346         public AltosTimeSeries main_voltage_series;
347
348         public static final String main_voltage_name = "Main Voltage";
349
350         public void set_main_voltage(double volts) {
351                 if (volts == AltosLib.MISSING)
352                         return;
353                 if (main_voltage_series == null)
354                         main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
355                 main_voltage_series.add(time(), volts);
356         }
357
358         public ArrayList<AltosGPSTimeValue> gps_series;
359
360         public AltosGPS gps_before(double time) {
361                 AltosGPS gps = null;
362                 for (AltosGPSTimeValue gtv : gps_series)
363                         if (gtv.time <= time)
364                                 gps = gtv.gps;
365                         else
366                                 break;
367                 return gps;
368         }
369
370         public AltosTimeSeries  sats_in_view;
371         public AltosTimeSeries sats_in_soln;
372         public AltosTimeSeries gps_altitude;
373         public AltosTimeSeries gps_height;
374         public AltosTimeSeries gps_ground_speed;
375         public AltosTimeSeries gps_ascent_rate;
376         public AltosTimeSeries gps_course;
377         public AltosTimeSeries gps_speed;
378         public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
379
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";
391
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));
396
397                 if (sats_in_soln == null) {
398                         sats_in_soln = add_series(sats_in_soln_name, null);
399                 }
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);
406                         }
407                         gps_pdop.add(time(), gps.pdop);
408                         gps_hdop.add(time(), gps.hdop);
409                         gps_vdop.add(time(), gps.vdop);
410                 }
411                 if (gps.locked) {
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);
419                         }
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);
424                         }
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));
434                 }
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);
439                 }
440         }
441
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";
445
446         public AltosTimeSeries accel_along, accel_across, accel_through;
447
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";
451
452         public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
453
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";
457
458         public AltosTimeSeries mag_along, mag_across, mag_through;
459
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);
465                 }
466                 accel_along.add(time(), along);
467                 accel_across.add(time(), across);
468                 accel_through.add(time(), through);
469         }
470
471         public  void set_accel_ground(double along, double across, double through) {
472         }
473
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);
479                 }
480                 gyro_roll.add(time(), roll);
481                 gyro_pitch.add(time(), pitch);
482                 gyro_yaw.add(time(), yaw);
483         }
484
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);
490                 }
491                 mag_along.add(time(), along);
492                 mag_across.add(time(), across);
493                 mag_through.add(time(), through);
494         }
495
496         public static final String orient_name = "Tilt Angle";
497
498         public AltosTimeSeries orient_series;
499
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);
504         }
505
506         public static final String pyro_voltage_name = "Pyro Voltage";
507
508         public AltosTimeSeries pyro_voltage;
509
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);
514         }
515
516         private static String[] igniter_voltage_names;
517
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];
521                         int     i = 0;
522
523                         if (igniter_voltage_names != null) {
524                                 for (; i < igniter_voltage_names.length; i++)
525                                         new_igniter_voltage_names[i] = igniter_voltage_names[i];
526                         }
527                         for (; i < channel+1; i++)
528                                 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
529                         igniter_voltage_names = new_igniter_voltage_names;
530                 }
531                 return igniter_voltage_names[channel];
532         }
533
534         public AltosTimeSeries[] igniter_voltage;
535
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];
540                         int                     i = 0;
541
542                         if (igniter_voltage != null) {
543                                 for (; i < igniter_voltage.length; i++)
544                                         new_igniter_voltage[i] = igniter_voltage[i];
545                         }
546                         for (; i < channels; i++)
547                                 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
548                         igniter_voltage = new_igniter_voltage;
549                 }
550                 for (int channel = 0; channel < voltage.length; channel++)
551                         igniter_voltage[channel].add(time(), voltage[channel]);
552         }
553
554         public static final String pyro_fired_name = "Pyro Channel State";
555
556         public AltosTimeSeries pyro_fired_series;
557
558         int     last_pyro_mask;
559
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);
567                         }
568                 }
569                 last_pyro_mask = pyro_mask;
570         }
571
572         public void set_companion(AltosCompanion companion) {
573         }
574
575         public void finish() {
576                 compute_speed();
577                 compute_accel();
578                 compute_height();
579         }
580
581         public AltosTimeSeries[] series() {
582                 finish();
583                 return series.toArray(new AltosTimeSeries[0]);
584         }
585
586         public AltosFlightSeries(AltosCalData cal_data) {
587                 super(cal_data);
588         }
589 }