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