-
Notifications
You must be signed in to change notification settings - Fork 0
/
Opener.cpp
304 lines (274 loc) · 7.54 KB
/
Opener.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @file Opener/Opener.cpp
* @brief ハイブリッドロケットの開放判定を行うライブラリ
* @details ハイブリッドロケットの開放判定を行うライブラリ
**/
#include <Arduino.h>
#include "Opener.h"
int arraycmp(const void *p1, const void *p2)
{
return *(const float *)p1 > *(const float *)p2;
}
bool is_odd_number(int n)
{
return n & 0x1;
}
float get_median(int n, float a[])
{
int k = n / 2;
qsort(a, n, sizeof(float), arraycmp);
if (is_odd_number(n))
{
return a[k];
}
else
{
return (float)(a[k - 1] + a[k]) / 2;
}
}
void OPENER::switch_parameter(SETTING setting)
{
if (setting == FM)
{
lift_off_threshold_altitude_m = fm_lift_off_threshold_altitude_m;
lift_off_threshold_ac_mss = fm_lift_off_threshold_ac_mss;
ALT_oversampling_count = fm_ALT_oversampling_count;
ALT_threshold_count = fm_ALT_threshold_count;
}
else if (setting == SHINSASYO)
{
lift_off_threshold_altitude_m = shinsasyo_lift_off_threshold_altitude_m;
lift_off_threshold_ac_mss = shinsasyo_lift_off_threshold_ac_mss;
ALT_oversampling_count = shinsasyo_ALT_oversampling_count;
ALT_threshold_count = shinsasyo_ALT_threshold_count;
}
}
void OPENER::init()
{
goCHECK();
}
uint32_t OPENER::get_time_ms()
{
// for raspberry pi pico SDK
// return to_ms_since_boot(get_absolute_time());
// for Arduino
return millis();
}
/*!
* @brief 100Hzで呼び出す必要がある関数
* @param acceleration_mss 機軸方向の加速度[m/s^2](ランチャ上で重力加速度がかかって正の値をとる方向)
* @param altitude_m 高度[m](基準高度は任意)
* @return 判定が更新されたか場合trueを返す.更新がない場合はfalseを返す.
*/
bool OPENER::opener_100Hz(float acceleration_mss, float altitude_m)
{
before_100Hz_time = get_time_ms();
ACC_buf_mss[count_100Hz] = acceleration_mss;
ALT_buf_mss[count_100Hz] = altitude_m;
// 10Hz
count_100Hz++;
if (count_100Hz >= 10)
{
count_100Hz = 0;
opener_10Hz();
return true;
}
return false;
}
/*!
* @brief 加速度・高度の中央値を計算し,離床判定・開放判定を行う
*/
void OPENER::opener_10Hz()
{
bool normal_datarate = get_time_ms() - before_10Hz_time < period_10Hz_ms;
before_10Hz_time = get_time_ms();
float acceleration_median_mss = get_median(10, ACC_buf_mss);
float altitude_median_m = get_median(10, ALT_buf_mss);
ALT_count++;
if (ALT_count == 1)
{
bool climbing = altitude_median_m > before_altitude_m + lift_off_threshold_altitude_m;
if (climbing && normal_datarate)
{
if (lift_off_altitude_count < ALT_threshold_count){
lift_off_altitude_count++;
}
}
else
{
lift_off_altitude_count = 0;
}
bool descending = altitude_median_m < before_altitude_m - open_threshold_altitude_m;
if (descending && normal_datarate)
{
if (apogee_altitude_count < ALT_threshold_count){
apogee_altitude_count++;
}
}
else
{
apogee_altitude_count = 0;
}
before_altitude_m = altitude_median_m;
}
if (ALT_count >= ALT_oversampling_count)
{
ALT_count = 0;
}
bool accelerating = acceleration_median_mss > lift_off_threshold_ac_mss;
if (accelerating)
{
if (lift_off_acc_count <= ACC_threshold_count){
lift_off_acc_count++;
}
}
else
{
lift_off_acc_count = 0;
}
bool freefall = acceleration_median_mss < open_threshold_ac_mss;
if (freefall)
{
if (meco_acc_count <= ACC_threshold_count){
meco_acc_count++;
}
}
else
{
meco_acc_count = 0;
}
if (mode == READY)
{
if (lift_off_altitude_count >= ALT_threshold_count)
{
goFLIGHT();
lift_off_judge = ALTSEN;
}
if (lift_off_acc_count >= ACC_threshold_count)
{
goFLIGHT();
lift_off_judge = ACCSEN;
}
}
if (mode == FLIGHT)
{
open_judge.meco_time = get_time_ms() - lift_off_time_ms >= meco_threshold_time_ms;
open_judge.meco_acc = meco_acc_count >= ACC_threshold_count;
bool meco = open_judge.meco_time && open_judge.meco_acc;
open_judge.apogee_time = get_time_ms() - lift_off_time_ms >= open_threshold_time_ms;
open_judge.apogee_descending = apogee_altitude_count >= ALT_threshold_count;
bool apogee = open_judge.apogee_time || open_judge.apogee_descending;
if (!open_judge.prohibitOpen && meco && apogee)
{
goOPENED();
}
}
}
/*!
* @brief ::CHECKモードへ移行する.変数の初期化し,::prohibitOpenを有効化する.
*/
void OPENER::goCHECK()
{
mode = CHECK;
open_judge.isOpend = false;
open_judge.prohibitOpen = true;
open_judge.meco_time = false;
open_judge.meco_acc = false;
open_judge.apogee_time = false;
open_judge.apogee_descending = false;
lift_off_judge = NONE;
lift_off_time_ms = 0;
count_100Hz = 0;
// oversampling
ALT_count = 0;
// altitude->velocity
before_altitude_m = 0;
// sensor_judgement
lift_off_altitude_count = 0;
lift_off_acc_count = 0;
apogee_altitude_count = 0;
meco_acc_count = 0;
close();
}
/*!
* @brief goCHECK()を実行して変数を初期化してから, prohibitOpen を無効化したうえで READY モードへ移行する.
*/
void OPENER::goREADY()
{
goCHECK();
open_judge.prohibitOpen = false;
mode = READY;
}
/*!
* @brief FLIGHT モードに移行し,離床時刻を記録する.
*/
void OPENER::goFLIGHT()
{
mode = FLIGHT;
lift_off_time_ms = get_time_ms() - flight_judgement_duration_ms;
}
/*!
* @brief OPENED モードに移行し,開放時刻を記録する.
*/
void OPENER::goOPENED()
{
mode = OPENED;
open_judge.isOpend = true;
open_time_ms = get_time_ms();
open();
}
/*!
* @brief prohibitOpen を無効化する.
*/
void OPENER::clear_prohibitOpen()
{
open_judge.prohibitOpen = false;
}
/*!
* @brief prohibitOpen を有効化する.
*/
void OPENER::prohibitOpen()
{
open_judge.prohibitOpen = true;
}
/*!
* @brief prohibitOpen が無効になっていることを確認し,手動で開放機構を開く.
*/
void OPENER::manualOpen()
{
if (!open_judge.prohibitOpen)
{
open();
}
}
/*!
* @brief 手動で開放機構を閉じ, prohibitOpen を有効化する.
*/
void OPENER::manualClose()
{
close();
open_judge.prohibitOpen = true;
}
/*!
* @brief 開放までのタイマーの秒数を設定する.
* @param _open_threshold_time_ms 開放機構の動作にかかる時間を引いた,離床から開放までの時間のシム値[ms]
*/
void OPENER::set_open_threshold_time_ms(uint32_t _open_threshold_time_ms)
{
open_threshold_time_ms = _open_threshold_time_ms;
}
/*!
* @brief 開放までのタイマーの秒数を取得する.
* @return 開放機構の動作にかかる時間を引いた,離床から開放までの時間のシム値[ms]
*/
uint32_t OPENER::get_open_threshold_time_ms(){
return open_threshold_time_ms;
}
/*!
* @brief 燃焼終了のタイマーの秒数を取得する.
* @return 離床判定後,燃焼中と判断し開放判定を行わない時間[ms]
*/
uint32_t OPENER::get_meco_time_ms()
{
return meco_threshold_time_ms;
}