micropoint: Add MicroDataPoint
[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                 for (MicroDataPoint point : data.points()) {
43                         landed_height = point.height;
44                         landed_time = point.time;
45                 }
46
47                 boolean above = false;
48                 for (MicroDataPoint point : data.points()) {
49                         if (point.height > landed_height + 10) {
50                                 above = true;
51                         } else {
52                                 if (above && point.height < landed_height + 2) {
53                                         above = false;
54                                         landed_time = point.time;
55                                 }
56                         }
57                 }
58         }
59
60         void find_apogee() {
61                 apogee_height = 0;
62                 apogee_time = 0;
63                 
64                 for (MicroDataPoint point : data.points()) {
65                         if (point.height > apogee_height) {
66                                 apogee_height = point.height;
67                                 apogee_time = point.time;
68                         }
69                 }
70         }
71
72         void find_coast() {
73                 coast_height = 0;
74                 coast_time = 0;
75
76                 for (MicroDataPoint point : data.points()) {
77                         if (point.accel < -9.8)
78                                 break;
79                         coast_time = point.time;
80                         coast_height = point.height;
81                 }
82         }
83
84         void find_max_speed() {
85                 max_speed = 0;
86                 for (MicroDataPoint point : data.points()) {
87                         if (point.time > apogee_time)
88                                 break;
89                         if (point.speed > max_speed)
90                                 max_speed = point.speed;
91                 }
92         }
93
94         void find_max_accel() {
95                 max_accel = 0;
96                 for (MicroDataPoint point : data.points()) {
97                         if (point.time > apogee_time)
98                                 break;
99                         if (point.accel > max_accel)
100                                 max_accel = point.accel;
101                 }
102         }
103
104         double boost_duration() {
105                 return coast_time;
106         }
107
108         double boost_height() {
109                 return coast_height;
110         }
111
112         double  boost_speed() {
113                 return coast_height / coast_time;
114         }
115
116         double boost_accel() {
117                 return boost_speed() / boost_duration();
118         }
119
120         double coast_duration() {
121                 return apogee_time - coast_time;
122         }
123
124         double coast_height() {
125                 return apogee_height - coast_height;
126         }
127
128         double coast_speed() {
129                 return coast_height() / coast_duration();
130         }
131
132         double coast_accel() {
133                 return coast_speed() / coast_duration();
134         }
135
136         double descent_duration() {
137                 return landed_time - apogee_time;
138         }
139
140         double descent_height() {
141                 return apogee_height - landed_height;
142         }
143
144         double descent_speed() {
145                 return descent_height() / descent_duration();
146         }
147
148         public MicroStats(MicroData data) {
149
150                 this.data = data;
151
152                 find_coast();
153                 find_apogee();
154                 find_landing();
155                 find_max_speed();
156                 find_max_accel();
157         }
158
159         public MicroStats() {
160                 this(new MicroData());
161         }
162 }