6ae8a2b28a11101e78f6b076cb2d7808558a0345
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.micropeak;
19
20 import java.io.*;
21 import org.altusmetrum.AltosLib.*;
22 import org.altusmetrum.altosuilib.*;
23
24 public class MicroStats {
25         double          coast_height;
26         double          coast_time;
27
28         double          apogee_height;
29         double          apogee_time;
30
31         double          landed_height;
32         double          landed_time;
33
34         double          max_speed;
35         double          max_accel;
36
37         MicroData       data;
38
39         void find_landing() {
40                 landed_height = 0;
41
42                 int t = 0;
43                 for (double height : data.heights()) {
44                         landed_height = height;
45                         t++;
46                 }
47                 landed_time = data.time(t);
48
49                 t = 0;
50                 boolean above = false;
51                 for (double height : data.heights()) {
52                         if (height > landed_height + 10) {
53                                 above = true;
54                         } else {
55                                 if (above && height < landed_height + 2) {
56                                         above = false;
57                                         landed_time = data.time(t);
58                                 }
59                         }
60                         t++;
61                 }
62         }
63
64         void find_apogee() {
65                 apogee_height = 0;
66                 apogee_time = 0;
67                 
68                 int t = 0;
69                 for (double height : data.heights()) {
70                         if (height > apogee_height) {
71                                 apogee_height = height;
72                                 apogee_time = data.time(t);
73                         }
74                         t++;
75                 }
76         }
77
78         void find_coast() {
79                 coast_height = 0;
80                 coast_time = 0;
81
82                 int t = 0;
83                 for (double accel : data.accels()) {
84                         if (accel < -9.8)
85                                 break;
86                         t++;
87                 }
88                 coast_time = data.time(t);
89
90                 int coast_t = t;
91                 t = 0;
92                 for (double height : data.heights()) {
93                         if (t >= coast_t) {
94                                 coast_height = height;
95                                 break;
96                         }
97                         t++;
98                 }
99         }
100
101         void find_max_speed() {
102                 max_speed = 0;
103                 int     t = 0;
104                 for (double speed : data.speeds()) {
105                         if (data.time(t) > apogee_time)
106                                 break;
107                         if (speed > max_speed)
108                                 max_speed = speed;
109                         t++;
110                 }
111         }
112
113         void find_max_accel() {
114                 max_accel = 0;
115
116                 int t = 0;
117                 for (double accel : data.accels()) {
118                         if (data.time(t) > apogee_time)
119                                 break;
120                         if (accel > max_accel)
121                                 max_accel = accel;
122                         t++;
123                 }
124         }
125
126         double boost_duration() {
127                 return coast_time;
128         }
129
130         double boost_height() {
131                 return coast_height;
132         }
133
134         double  boost_speed() {
135                 return coast_height / coast_time;
136         }
137
138         double boost_accel() {
139                 return boost_speed() / boost_duration();
140         }
141
142         double coast_duration() {
143                 return apogee_time - coast_time;
144         }
145
146         double coast_height() {
147                 return apogee_height - coast_height;
148         }
149
150         double coast_speed() {
151                 return coast_height() / coast_duration();
152         }
153
154         double coast_accel() {
155                 return coast_speed() / coast_duration();
156         }
157
158         double descent_duration() {
159                 return landed_time - apogee_time;
160         }
161
162         double descent_height() {
163                 return apogee_height - landed_height;
164         }
165
166         double descent_speed() {
167                 return descent_height() / descent_duration();
168         }
169
170         public MicroStats(MicroData data) {
171
172                 this.data = data;
173
174                 find_coast();
175                 find_apogee();
176                 find_landing();
177                 find_max_speed();
178                 find_max_accel();
179         }
180
181         public MicroStats() {
182                 this(new MicroData());
183         }
184 }