]> git.gag.com Git - fw/altos/blob - altoslib/AltosFlightSeries.java
altos/easytimer-v2: Fix up ao_pins.h
[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_14;
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 = 1.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                 for (int e = 0; e < series.size(); e++) {
122                         if (s.compareTo(series.get(e)) < 0){
123                                 series.add(e, s);
124                                 return;
125                         }
126                 }
127                 series.add(s);
128         }
129
130         public AltosTimeSeries add_series(String label, AltosUnits units) {
131                 AltosTimeSeries s = make_series(label, units);
132                 add_series(s);
133                 return s;
134         }
135
136         public void remove_series(AltosTimeSeries s) {
137                 series.remove(s);
138         }
139
140         public boolean has_series(String label) {
141                 for (AltosTimeSeries s : series)
142                         if (s.label.equals(label))
143                                 return true;
144                 return false;
145         }
146
147         public AltosTimeSeries state_series;
148
149         public static final String state_name = "State";
150
151         public void set_state(int state) {
152
153                 if (state != AltosLib.ao_flight_pad && state != AltosLib.MISSING && state != AltosLib.ao_flight_stateless) {
154                         if (state_series == null)
155                                 state_series = add_series(state_name, AltosConvert.state_name);
156                         if (this.state() != state)
157                                 state_series.add(time(), state);
158                 }
159                 super.set_state(state);
160         }
161
162         public AltosTimeSeries  accel_series;
163         public boolean          accel_computed;
164
165         public static final String accel_name = "Accel";
166
167         public AltosTimeSeries  vert_accel_series;
168
169         public static final String vert_accel_name = "Vertical Accel";
170
171         public void set_acceleration(double acceleration) {
172                 if (acceleration == AltosLib.MISSING)
173                         return;
174                 if (accel_series == null)
175                         accel_series = add_series(accel_name, AltosConvert.accel);
176
177                 accel_series.add(time(), acceleration);
178                 accel_computed = false;
179         }
180
181         private AltosTimeSeries compute_accel() {
182                 AltosTimeSeries new_accel_series = null;
183
184                 if (speed_series != null) {
185                         AltosTimeSeries temp_series;
186                         if (accel_filter_width > 0) {
187                                 temp_series = make_series(speed_name, AltosConvert.speed);
188                                 speed_series.filter(temp_series, accel_filter_width);
189                         } else
190                                 temp_series = speed_series;
191
192                         new_accel_series = make_series(accel_name, AltosConvert.accel);
193                         temp_series.differentiate(new_accel_series);
194                 }
195                 return new_accel_series;
196         }
197
198         public void set_filter(double speed_filter, double accel_filter) {
199                 this.speed_filter_width = speed_filter;
200                 this.accel_filter_width = accel_filter;
201
202                 AltosTimeSeries new_speed_series = compute_speed();
203
204                 if (new_speed_series != null) {
205                         speed_series.erase_values();
206                         for (AltosTimeValue tv : new_speed_series)
207                                 speed_series.add(tv);
208                 }
209                 if (accel_computed) {
210                         AltosTimeSeries new_accel_series = compute_accel();
211                         if (new_accel_series != null) {
212                                 accel_series.erase_values();
213                                 for (AltosTimeValue tv : new_accel_series)
214                                         accel_series.add(tv);
215                         }
216                 }
217         }
218
219         public void set_received_time(long received_time) {
220         }
221
222         public AltosTimeSeries tick_series;
223
224         public static final String tick_name = "Tick";
225
226         public void set_tick(int tick) {
227                 super.set_tick(tick);
228                 if (tick_series == null)
229                         tick_series = add_series(tick_name, null);
230                 tick_series.add(time(), tick);
231         }
232
233         public AltosTimeSeries rssi_series;
234
235         public static final String rssi_name = "RSSI";
236
237         public AltosTimeSeries status_series;
238
239         public static final String status_name = "Radio Status";
240
241         public void set_rssi(int rssi, int status) {
242                 if (rssi_series == null) {
243                         rssi_series = add_series(rssi_name, null);
244                         status_series = add_series(status_name, null);
245                 }
246                 rssi_series.add(time(), rssi);
247                 status_series.add(time(), status);
248         }
249
250         public AltosTimeSeries pressure_series;
251
252         public static final String pressure_name = "Pressure";
253
254         public AltosTimeSeries altitude_series;
255
256         public static final String altitude_name = "Altitude";
257
258         public AltosTimeSeries height_series;
259
260         public double max_height = AltosLib.MISSING;
261
262         public  void set_min_pressure(double pa) {
263                 double ground_altitude = cal_data().ground_altitude;
264                 if (ground_altitude != AltosLib.MISSING)
265                         max_height = AltosConvert.pressure_to_altitude(pa) -
266                                 ground_altitude;
267         }
268
269         public static final String height_name = "Height";
270
271         public  void set_pressure(double pa) {
272                 if (pa == AltosLib.MISSING)
273                         return;
274
275                 if (pressure_series == null)
276                         pressure_series = add_series(pressure_name, AltosConvert.pressure);
277                 pressure_series.add(time(), pa);
278                 if (altitude_series == null)
279                         altitude_series = add_series(altitude_name, AltosConvert.height);
280
281                 if (cal_data().ground_pressure == AltosLib.MISSING)
282                         cal_data().set_ground_pressure(pa);
283
284                 double altitude = AltosConvert.pressure_to_altitude(pa);
285                 altitude_series.add(time(), altitude);
286         }
287
288         private void compute_height() {
289                 if (height_series == null) {
290                         double ground_altitude = cal_data().ground_altitude;
291                         if (ground_altitude != AltosLib.MISSING && altitude_series != null) {
292                                 height_series = add_series(height_name, AltosConvert.height);
293                                 for (AltosTimeValue alt : altitude_series)
294                                         height_series.add(alt.time, alt.value - ground_altitude);
295                         } else if (speed_series != null) {
296                                 height_series = add_series(height_name, AltosConvert.height);
297                                 speed_series.integrate(height_series);
298                         }
299                 }
300
301                 if (gps_height == null && cal_data().gps_pad != null && cal_data().gps_pad.alt != AltosLib.MISSING && gps_altitude != null) {
302                         double gps_ground_altitude = cal_data().gps_pad.alt;
303                         gps_height = add_series(gps_height_name, AltosConvert.height);
304                         for (AltosTimeValue gps_alt : gps_altitude)
305                                 gps_height.add(gps_alt.time, gps_alt.value - gps_ground_altitude);
306                 }
307         }
308
309         public AltosTimeSeries speed_series;
310
311         public static final String speed_name = "Speed";
312
313         private AltosTimeSeries compute_speed() {
314                 AltosTimeSeries new_speed_series = null;
315                 AltosTimeSeries alt_speed_series = null;
316                 AltosTimeSeries accel_speed_series = null;
317
318                 if (altitude_series != null) {
319                         AltosTimeSeries temp_series;
320
321                         if (speed_filter_width > 0) {
322                                 temp_series = make_series(speed_name, AltosConvert.height);
323                                 altitude_series.filter(temp_series, speed_filter_width);
324                         } else
325                                 temp_series = altitude_series;
326
327                         alt_speed_series = make_series(speed_name, AltosConvert.speed);
328                         temp_series.differentiate(alt_speed_series);
329                 }
330                 if (accel_series != null && !accel_computed) {
331
332                         if (orient_series != null) {
333                                 vert_accel_series = add_series(vert_accel_name, AltosConvert.accel);
334
335                                 for (AltosTimeValue a : accel_series) {
336                                         double  orient = orient_series.value(a.time);
337                                         double  a_abs = a.value + AltosConvert.gravity;
338                                         double  v_a = a_abs * Math.cos(AltosConvert.degrees_to_radians(orient)) - AltosConvert.gravity;
339
340                                         vert_accel_series.add(a.time, v_a);
341                                 }
342                         }
343
344                         AltosTimeSeries temp_series = make_series(speed_name, AltosConvert.speed);
345
346                         if (vert_accel_series != null)
347                                 vert_accel_series.integrate(temp_series);
348                         else
349                                 accel_series.integrate(temp_series);
350
351                         AltosTimeSeries clip_series = make_series(speed_name, AltosConvert.speed);
352
353                         temp_series.clip(clip_series, 0, Double.POSITIVE_INFINITY);
354
355                         accel_speed_series = make_series(speed_name, AltosConvert.speed);
356                         clip_series.filter(accel_speed_series, 0.1);
357                 }
358
359                 if (alt_speed_series != null && accel_speed_series != null) {
360                         double  apogee_time = AltosLib.MISSING;
361                         if (state_series != null) {
362                                 for (AltosTimeValue d : state_series) {
363                                         if (d.value >= AltosLib.ao_flight_drogue){
364                                                 apogee_time = d.time;
365                                                 break;
366                                         }
367                                 }
368                         }
369                         if (apogee_time == AltosLib.MISSING) {
370                                 new_speed_series = alt_speed_series;
371                         } else {
372                                 new_speed_series = make_series(speed_name, AltosConvert.speed);
373                                 for (AltosTimeValue d : accel_speed_series) {
374                                         if (d.time <= apogee_time)
375                                                 new_speed_series.add(d);
376                                 }
377                                 for (AltosTimeValue d : alt_speed_series) {
378                                         if (d.time > apogee_time)
379                                                 new_speed_series.add(d);
380                                 }
381
382                         }
383                 } else if (alt_speed_series != null) {
384                         new_speed_series = alt_speed_series;
385                 } else if (accel_speed_series != null) {
386                         new_speed_series = accel_speed_series;
387                 }
388                 return new_speed_series;
389         }
390
391         public AltosTimeSeries orient_series;
392         public AltosTimeSeries azimuth_series;
393
394         public static final String orient_name = "Tilt Angle";
395         public static final String azimuth_name = "Azimuth Angle";
396
397         private void compute_orient() {
398
399                 if (orient_series != null)
400                         return;
401
402                 if (accel_ground_across == AltosLib.MISSING)
403                         return;
404
405                 AltosCalData cal_data = cal_data();
406
407                 if (cal_data.pad_orientation == AltosLib.MISSING)
408                         return;
409
410                 if (cal_data.accel_zero_across == AltosLib.MISSING)
411                         return;
412
413                 if (cal_data.gyro_zero_roll == AltosLib.MISSING)
414                         return;
415
416                 AltosRotation rotation = new AltosRotation(accel_ground_across,
417                                                            accel_ground_through,
418                                                            accel_ground_along,
419                                                            cal_data.pad_orientation);
420                 double prev_time = ground_time;
421
422                 orient_series = add_series(orient_name, AltosConvert.orient);
423                 orient_series.add(ground_time, rotation.tilt());
424
425                 azimuth_series = add_series(azimuth_name, AltosConvert.orient);
426                 azimuth_series.add(ground_time, rotation.azimuth());
427
428                 for (AltosTimeValue roll_v : gyro_roll) {
429                         double  time = roll_v.time;
430                         double  dt = time - prev_time;
431
432                         if (dt > 0) {
433                                 double  roll = AltosConvert.degrees_to_radians(roll_v.value) * dt;
434                                 double  pitch = AltosConvert.degrees_to_radians(gyro_pitch.value(time)) * dt;
435                                 double  yaw = AltosConvert.degrees_to_radians(gyro_yaw.value(time)) * dt;
436
437                                 rotation.rotate(pitch, yaw, roll);
438                                 orient_series.add(time, rotation.tilt());
439                                 azimuth_series.add(time, rotation.azimuth());
440                         }
441                         prev_time = time;
442                 }
443         }
444
445         public AltosTimeSeries  kalman_height_series, kalman_speed_series, kalman_accel_series;
446
447         public static final String kalman_height_name = "Kalman Height";
448         public static final String kalman_speed_name = "Kalman Speed";
449         public static final String kalman_accel_name = "Kalman Accel";
450
451         public void set_kalman(double height, double speed, double acceleration) {
452                 if (kalman_height_series == null) {
453                         kalman_height_series = add_series(kalman_height_name, AltosConvert.height);
454                         kalman_speed_series = add_series(kalman_speed_name, AltosConvert.speed);
455                         kalman_accel_series = add_series(kalman_accel_name, AltosConvert.accel);
456                 }
457                 kalman_height_series.add(time(), height);
458                 kalman_speed_series.add(time(), speed);
459                 kalman_accel_series.add(time(), acceleration);
460         }
461
462         public AltosTimeSeries thrust_series;
463
464         public static final String thrust_name = "Thrust";
465
466         public  void set_thrust(double N) {
467                 if (thrust_series == null)
468                         thrust_series = add_series(thrust_name, AltosConvert.force);
469                 thrust_series.add(time(), N);
470         }
471
472         public AltosTimeSeries temperature_series;
473
474         public static final String temperature_name = "Temperature";
475
476         public  void set_temperature(double deg_c) {
477                 if (temperature_series == null)
478                         temperature_series = add_series(temperature_name, AltosConvert.temperature);
479                 temperature_series.add(time(), deg_c);
480         }
481
482         public AltosTimeSeries battery_voltage_series;
483
484         public static final String battery_voltage_name = "Battery Voltage";
485
486         public void set_battery_voltage(double volts) {
487                 if (volts == AltosLib.MISSING)
488                         return;
489                 if (battery_voltage_series == null)
490                         battery_voltage_series = add_series(battery_voltage_name, AltosConvert.voltage);
491                 battery_voltage_series.add(time(), volts);
492         }
493
494         public AltosTimeSeries apogee_voltage_series;
495
496         public static final String apogee_voltage_name = "Apogee Voltage";
497
498         public void set_apogee_voltage(double volts) {
499                 if (volts == AltosLib.MISSING)
500                         return;
501                 if (apogee_voltage_series == null)
502                         apogee_voltage_series = add_series(apogee_voltage_name, AltosConvert.voltage);
503                 apogee_voltage_series.add(time(), volts);
504         }
505
506         public AltosTimeSeries main_voltage_series;
507
508         public static final String main_voltage_name = "Main Voltage";
509
510         public void set_main_voltage(double volts) {
511                 if (volts == AltosLib.MISSING)
512                         return;
513                 if (main_voltage_series == null)
514                         main_voltage_series = add_series(main_voltage_name, AltosConvert.voltage);
515                 main_voltage_series.add(time(), volts);
516         }
517
518         public ArrayList<AltosGPSTimeValue> gps_series;
519
520         public AltosGPS gps_before(double time) {
521                 AltosGPSTimeValue nearest = null;
522                 for (AltosGPSTimeValue gtv : gps_series) {
523                         if (nearest == null)
524                                 nearest = gtv;
525                         else {
526                                 if (gtv.time <= time) {
527                                         if (nearest.time <= time && gtv.time > nearest.time)
528                                                 nearest = gtv;
529                                 } else {
530                                         if (nearest.time > time && gtv.time < nearest.time)
531                                                 nearest = gtv;
532                                 }
533                         }
534                 }
535                 if (nearest != null)
536                         return nearest.gps;
537                 else
538                         return null;
539         }
540
541         public AltosTimeSeries  sats_in_view;
542         public AltosTimeSeries sats_in_soln;
543         public AltosTimeSeries gps_altitude;
544         public AltosTimeSeries gps_height;
545         public AltosTimeSeries gps_ground_speed;
546         public AltosTimeSeries gps_ascent_rate;
547         public AltosTimeSeries gps_course;
548         public AltosTimeSeries gps_speed;
549         public AltosTimeSeries gps_pdop, gps_vdop, gps_hdop;
550
551         public static final String sats_in_view_name = "Satellites in view";
552         public static final String sats_in_soln_name = "Satellites in solution";
553         public static final String gps_altitude_name = "GPS Altitude";
554         public static final String gps_height_name = "GPS Height";
555         public static final String gps_ground_speed_name = "GPS Ground Speed";
556         public static final String gps_ascent_rate_name = "GPS Ascent Rate";
557         public static final String gps_course_name = "GPS Course";
558         public static final String gps_speed_name = "GPS Speed";
559         public static final String gps_pdop_name = "GPS Dilution of Precision";
560         public static final String gps_vdop_name = "GPS Vertical Dilution of Precision";
561         public static final String gps_hdop_name = "GPS Horizontal Dilution of Precision";
562
563         public void set_gps(AltosGPS gps, boolean new_location, boolean new_sats) {
564                 super.set_gps(gps, new_location, new_sats);
565                 AltosCalData cal_data = cal_data();
566                 if (gps_series == null)
567                         gps_series = new ArrayList<AltosGPSTimeValue>();
568                 gps_series.add(new AltosGPSTimeValue(time(), gps));
569
570                 if (new_location) {
571                         if (sats_in_soln == null) {
572                                 sats_in_soln = add_series(sats_in_soln_name, null);
573                         }
574                         sats_in_soln.add(time(), gps.nsat);
575                         if (gps.pdop != AltosLib.MISSING) {
576                                 if (gps_pdop == null)
577                                         gps_pdop = add_series(gps_pdop_name, null);
578                                 gps_pdop.add(time(), gps.pdop);
579                         }
580                         if (gps.hdop != AltosLib.MISSING) {
581                                 if (gps_hdop == null)
582                                         gps_hdop = add_series(gps_hdop_name, null);
583                                 gps_hdop.add(time(), gps.hdop);
584                         }
585                         if (gps.vdop != AltosLib.MISSING) {
586                                 if (gps_vdop == null)
587                                         gps_vdop = add_series(gps_vdop_name, null);
588                                 gps_vdop.add(time(), gps.vdop);
589                         }
590                         if (gps.locked) {
591                                 if (gps.alt != AltosLib.MISSING) {
592                                         if (gps_altitude == null)
593                                                 gps_altitude = add_series(gps_altitude_name, AltosConvert.height);
594                                         gps_altitude.add(time(), gps.alt);
595                                 }
596                                 if (gps.ground_speed != AltosLib.MISSING) {
597                                         if (gps_ground_speed == null)
598                                                 gps_ground_speed = add_series(gps_ground_speed_name, AltosConvert.speed);
599                                         gps_ground_speed.add(time(), gps.ground_speed);
600                                 }
601                                 if (gps.climb_rate != AltosLib.MISSING) {
602                                         if (gps_ascent_rate == null)
603                                                 gps_ascent_rate = add_series(gps_ascent_rate_name, AltosConvert.speed);
604                                         gps_ascent_rate.add(time(), gps.climb_rate);
605                                 }
606                                 if (gps.course != AltosLib.MISSING) {
607                                         if (gps_course == null)
608                                                 gps_course = add_series(gps_course_name, null);
609                                         gps_course.add(time(), gps.course);
610                                 }
611                                 if (gps.ground_speed != AltosLib.MISSING && gps.climb_rate != AltosLib.MISSING) {
612                                         if (gps_speed == null)
613                                                 gps_speed = add_series(gps_speed_name, null);
614                                         gps_speed.add(time(), Math.sqrt(gps.ground_speed * gps.ground_speed +
615                                                                         gps.climb_rate * gps.climb_rate));
616                                 }
617                         }
618                 }
619                 if (new_sats) {
620                         if (gps.cc_gps_sat != null) {
621                                 if (sats_in_view == null)
622                                         sats_in_view = add_series(sats_in_view_name, null);
623                                 sats_in_view.add(time(), gps.cc_gps_sat.length);
624                         }
625                 }
626         }
627
628         public static final String accel_along_name = "Accel Along";
629         public static final String accel_across_name = "Accel Across";
630         public static final String accel_through_name = "Accel Through";
631
632         public AltosTimeSeries accel_along, accel_across, accel_through;
633
634         public static final String gyro_roll_name = "Roll Rate";
635         public static final String gyro_pitch_name = "Pitch Rate";
636         public static final String gyro_yaw_name = "Yaw Rate";
637
638         public AltosTimeSeries gyro_roll, gyro_pitch, gyro_yaw;
639
640         public static final String mag_along_name = "Magnetic Field Along";
641         public static final String mag_across_name = "Magnetic Field Across";
642         public static final String mag_through_name = "Magnetic Field Through";
643         public static final String mag_total_name = "Magnetic Field Strength";
644         public static final String compass_name = "Compass";
645
646         public AltosTimeSeries mag_along, mag_across, mag_through, mag_total, compass;
647
648         public  void set_accel(double along, double across, double through) {
649                 if (accel_along == null) {
650                         accel_along = add_series(accel_along_name, AltosConvert.accel);
651                         accel_across = add_series(accel_across_name, AltosConvert.accel);
652                         accel_through = add_series(accel_through_name, AltosConvert.accel);
653                 }
654                 accel_along.add(time(), along);
655                 accel_across.add(time(), across);
656                 accel_through.add(time(), through);
657         }
658
659         private double  accel_ground_along = AltosLib.MISSING;
660         private double  accel_ground_across = AltosLib.MISSING;
661         private double  accel_ground_through = AltosLib.MISSING;
662
663         private double          ground_time;
664
665         public  void set_accel_ground(double along, double across, double through) {
666                 accel_ground_along = along;
667                 accel_ground_across = across;
668                 accel_ground_through = through;
669                 ground_time = time();
670         }
671
672         public  void set_gyro(double roll, double pitch, double yaw) {
673                 if (gyro_roll == null) {
674                         gyro_roll = add_series(gyro_roll_name, AltosConvert.rotation_rate);
675                         gyro_pitch = add_series(gyro_pitch_name, AltosConvert.rotation_rate);
676                         gyro_yaw = add_series(gyro_yaw_name, AltosConvert.rotation_rate);
677                 }
678                 gyro_roll.add(time(), roll);
679                 gyro_pitch.add(time(), pitch);
680                 gyro_yaw.add(time(), yaw);
681         }
682
683         public  void set_mag(double along, double across, double through) {
684                 if (mag_along == null) {
685                         mag_along = add_series(mag_along_name, AltosConvert.magnetic_field);
686                         mag_across = add_series(mag_across_name, AltosConvert.magnetic_field);
687                         mag_through = add_series(mag_through_name, AltosConvert.magnetic_field);
688                         mag_total = add_series(mag_total_name, AltosConvert.magnetic_field);
689                         compass = add_series(compass_name, AltosConvert.orient);
690                 }
691                 mag_along.add(time(), along);
692                 mag_across.add(time(), across);
693                 mag_through.add(time(), through);
694                 mag_total.add(time(), Math.sqrt(along * along + across * across + through *through));
695                 compass.add(time(), Math.atan2(across, through) * 180 / Math.PI);
696         }
697
698         public void set_orient(double orient) {
699                 if (orient_series == null)
700                         orient_series = add_series(orient_name, AltosConvert.orient);
701                 orient_series.add(time(), orient);
702         }
703
704         public static final String pyro_voltage_name = "Pyro Voltage";
705
706         public AltosTimeSeries pyro_voltage;
707
708         public  void set_pyro_voltage(double volts) {
709                 if (pyro_voltage == null)
710                         pyro_voltage = add_series(pyro_voltage_name, AltosConvert.voltage);
711                 pyro_voltage.add(time(), volts);
712         }
713
714         private static String[] igniter_voltage_names;
715
716         public String igniter_voltage_name(int channel) {
717                 if (igniter_voltage_names == null || igniter_voltage_names.length <= channel) {
718                         String[] new_igniter_voltage_names = new String[channel + 1];
719                         int     i = 0;
720
721                         if (igniter_voltage_names != null) {
722                                 for (; i < igniter_voltage_names.length; i++)
723                                         new_igniter_voltage_names[i] = igniter_voltage_names[i];
724                         }
725                         for (; i < channel+1; i++)
726                                 new_igniter_voltage_names[i] = AltosLib.igniter_name(i);
727                         igniter_voltage_names = new_igniter_voltage_names;
728                 }
729                 return igniter_voltage_names[channel];
730         }
731
732         public AltosTimeSeries[] igniter_voltage;
733
734         public  void set_igniter_voltage(double[] voltage) {
735                 int channels = voltage.length;
736                 if (igniter_voltage == null || igniter_voltage.length <= channels) {
737                         AltosTimeSeries[]       new_igniter_voltage = new AltosTimeSeries[channels];
738                         int                     i = 0;
739
740                         if (igniter_voltage != null) {
741                                 for (; i < igniter_voltage.length; i++)
742                                         new_igniter_voltage[i] = igniter_voltage[i];
743                         }
744                         for (; i < channels; i++)
745                                 new_igniter_voltage[i] = add_series(igniter_voltage_name(i), AltosConvert.voltage);
746                         igniter_voltage = new_igniter_voltage;
747                 }
748                 for (int channel = 0; channel < voltage.length; channel++)
749                         igniter_voltage[channel].add(time(), voltage[channel]);
750         }
751
752         public static final String pyro_fired_name = "Pyro Channel State";
753
754         public AltosTimeSeries pyro_fired_series;
755
756         int     last_pyro_mask;
757
758         public  void set_pyro_fired(int pyro_mask) {
759                 if (pyro_fired_series == null)
760                         pyro_fired_series = add_series(pyro_fired_name, AltosConvert.pyro_name);
761                 for (int channel = 0; channel < 32; channel++) {
762                         if ((last_pyro_mask & (1 << channel)) == 0 &&
763                             (pyro_mask & (1 << channel)) != 0) {
764                                 pyro_fired_series.add(time(), channel);
765                         }
766                 }
767                 last_pyro_mask = pyro_mask;
768         }
769
770         public void set_companion(AltosCompanion companion) {
771         }
772
773         public static final String motor_pressure_name = "Motor Pressure";
774
775         public AltosTimeSeries motor_pressure_series;
776
777         public void set_motor_pressure(double motor_pressure) {
778                 if (motor_pressure_series == null)
779                         motor_pressure_series = add_series(motor_pressure_name, AltosConvert.pressure);
780                 motor_pressure_series.add(time(), motor_pressure);
781         }
782
783         public void finish() {
784                 compute_orient();
785                 if (speed_series == null) {
786                         speed_series = compute_speed();
787                         if (speed_series != null)
788                                 add_series(speed_series);
789                 }
790                 if (accel_series == null) {
791                         accel_series = compute_accel();
792                         if (accel_series != null) {
793                                 add_series(accel_series);
794                                 accel_computed = true;
795                         }
796                 }
797                 compute_height();
798         }
799
800         public AltosTimeSeries[] series() {
801                 finish();
802                 return series.toArray(new AltosTimeSeries[0]);
803         }
804
805         public AltosFlightSeries(AltosCalData cal_data) {
806                 super(cal_data);
807         }
808 }