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