Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / micropeak / MicroStats.java
1 /*
2  * Copyright © 2011 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  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.io.*;
22 import org.altusmetrum.altoslib_11.*;
23 import org.altusmetrum.altosuilib_11.*;
24
25 public class MicroStats {
26         double          coast_height;
27         double          coast_time;
28
29         double          apogee_height;
30         double          apogee_time;
31
32         double          landed_height;
33         double          landed_time;
34
35         double          max_speed;
36         double          max_accel;
37
38         MicroData       data;
39
40         void find_landing() {
41                 landed_height = 0;
42
43                 for (MicroDataPoint point : data.points()) {
44                         landed_height = point.height;
45                         landed_time = point.time;
46                 }
47
48                 boolean above = false;
49                 for (MicroDataPoint point : data.points()) {
50                         if (point.height > landed_height + 10) {
51                                 above = true;
52                         } else {
53                                 if (above && point.height < landed_height + 2) {
54                                         above = false;
55                                         landed_time = point.time;
56                                 }
57                         }
58                 }
59         }
60
61         void find_apogee() {
62                 apogee_height = data.apogee_height();
63                 double searched_apogee = 0;
64                 apogee_time = 0;
65                 
66                 /* This just finds the apogee time -- we've recorded the
67                  * peak altitude separately in eeprom, and that could
68                  * have occurred after the eeprom was full.
69                  */
70                 for (MicroDataPoint point : data.points()) {
71                         if (point.height > searched_apogee) {
72                                 searched_apogee = point.height;
73                                 apogee_time = point.time;
74                         }
75                 }
76         }
77
78         void find_coast() {
79                 coast_height = 0;
80                 coast_time = 0;
81
82                 for (MicroDataPoint point : data.points()) {
83                         if (point.accel < -9.8)
84                                 break;
85                         coast_time = point.time;
86                         coast_height = point.height;
87                 }
88         }
89
90         void find_max_speed() {
91                 max_speed = 0;
92                 for (MicroDataPoint point : data.points()) {
93                         if (point.time > apogee_time)
94                                 break;
95                         if (point.speed > max_speed)
96                                 max_speed = point.speed;
97                 }
98         }
99
100         void find_max_accel() {
101                 max_accel = 0;
102                 for (MicroDataPoint point : data.points()) {
103                         if (point.time > apogee_time)
104                                 break;
105                         if (point.accel > max_accel)
106                                 max_accel = point.accel;
107                 }
108         }
109
110         double boost_duration() {
111                 return coast_time;
112         }
113
114         double boost_height() {
115                 return coast_height;
116         }
117
118         double  boost_speed() {
119                 return coast_height / coast_time;
120         }
121
122         double boost_accel() {
123                 return boost_speed() / boost_duration();
124         }
125
126         double coast_duration() {
127                 return apogee_time - coast_time;
128         }
129
130         double coast_height() {
131                 return apogee_height - coast_height;
132         }
133
134         double coast_speed() {
135                 return coast_height() / coast_duration();
136         }
137
138         double coast_accel() {
139                 return coast_speed() / coast_duration();
140         }
141
142         double descent_duration() {
143                 return landed_time - apogee_time;
144         }
145
146         double descent_height() {
147                 return apogee_height - landed_height;
148         }
149
150         double descent_speed() {
151                 return descent_height() / descent_duration();
152         }
153
154         public static final int state_startup = -1;
155         public static final int state_pad = 0;
156         public static final int state_boost = 1;
157         public static final int state_coast = 2;
158         public static final int state_descent = 3;
159         public static final int state_landed = 4;
160
161         static final String state_names[] = {
162                 "pad",
163                 "boost",
164                 "coast",
165                 "descent",
166                 "landed"
167         };
168
169         public int state(double t) {
170                 if (t >= landed_time)
171                         return state_landed;
172                 if (t >= apogee_time)
173                         return state_descent;
174                 if (t >= coast_time)
175                         return state_coast;
176                 if (t >= 0)
177                         return state_boost;
178                 return state_pad;
179         }
180
181         public static String state_name(int state) {
182                 if (state < 0 || state > state_landed)
183                         return "unknown";
184                 return state_names[state];
185         }
186
187         public String state_name(double t) {
188                 return state_name(state(t));
189         }
190
191         public MicroStats(MicroData data) {
192
193                 this.data = data;
194
195                 find_coast();
196                 find_apogee();
197                 find_landing();
198                 find_max_speed();
199                 find_max_accel();
200         }
201
202         public MicroStats() {
203                 this(new MicroData());
204         }
205 }