-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyro.cpp
More file actions
281 lines (231 loc) · 9.58 KB
/
Pyro.cpp
File metadata and controls
281 lines (231 loc) · 9.58 KB
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
#include <cmath>
#include "sensors.h"
#include "pins.h"
#include "TCAL9538.h"
#include <rocket_state.h>
// Fire the pyros for this time during PYRO_TEST (ms)
#define PYRO_TEST_FIRE_TIME 100
#define MAXIMUM_TILT_ANGLE (M_PI/8) // 22.5 degrees
/**
* @brief Returns true if the error_code signals failure.
*/
bool error_is_failure(GpioError error_code) {
return error_code != GpioError::NoError;
}
/**
* @brief Determines if orientation is in an acceptable range to ignite second stage.
*
* @return True if acceptable, false if not.
*/
bool can_fire_igniter(AngularKalmanData angular_kalman_data) {
// With new GNC orientation code we can add a simple check.
return angular_kalman_data.sflp_tilt < MAXIMUM_TILT_ANGLE;
}
/**
* @brief Initializes the pyro thread. The main initialization will be done by the GPIO expander, so the pyro thread doesn't
* have to do anything special and will always return NoError.
*/
ErrorCode Pyro::init() {
bool has_failed_gpio_init = false;
// global arm
has_failed_gpio_init |= error_is_failure(gpioPinMode(PYRO_GLOBAL_ARM_PIN, OUTPUT));
// fire pins
has_failed_gpio_init |= error_is_failure(gpioPinMode(PYROA_FIRE_PIN, OUTPUT));
has_failed_gpio_init |= error_is_failure(gpioPinMode(PYROB_FIRE_PIN, OUTPUT));
has_failed_gpio_init |= error_is_failure(gpioPinMode(PYROC_FIRE_PIN, OUTPUT));
has_failed_gpio_init |= error_is_failure(gpioPinMode(PYROD_FIRE_PIN, OUTPUT));
// if (has_failed_gpio_init) {
// return ErrorCode::PyroGPIOCouldNotBeInitialized;
// } else {
return ErrorCode::NoError; // GPIO Driver always claimes it errored even when it doesn't.
// }
}
void Pyro::disarm_all_channels(PyroState& prev_state) {
gpioDigitalWrite(PYRO_GLOBAL_ARM_PIN, LOW);
gpioDigitalWrite(PYROA_FIRE_PIN, LOW);
gpioDigitalWrite(PYROB_FIRE_PIN, LOW);
gpioDigitalWrite(PYROC_FIRE_PIN, LOW);
gpioDigitalWrite(PYROD_FIRE_PIN, LOW);
prev_state.is_global_armed = false;
for(size_t i = 0; i < 4; ++i) {
// Update each channel's state sequentially
prev_state.channel_firing[i] = false;
}
}
void Pyro::set_pyro_safety() {
safety_pyro_start_firing_time = pdTICKS_TO_MS(xTaskGetTickCount());
safety_has_fired_pyros_this_cycle = true;
}
void Pyro::reset_pyro_safety() {
safety_has_fired_pyros_this_cycle = false;
}
#ifdef IS_SUSTAINER
/**
* @brief Upper stage only! Fires channels by setting their pin on the GPIO.
*
* @return A pyro struct indicating which pyro channels are armed and/or firing.
*/
PyroState Pyro::tick(FSMState fsm_state, AngularKalmanData angular_kalman_data, CommandFlags& telem_commands) {
PyroState new_pyro_state = PyroState();
double current_time = pdTICKS_TO_MS(xTaskGetTickCount());
if (fsm_state == FSMState::STATE_SAFE) {
disarm_all_channels(new_pyro_state);
return new_pyro_state;
}
// If the state is not SAFE, we arm the global arm pin
new_pyro_state.is_global_armed = true;
gpioDigitalWrite(PYRO_GLOBAL_ARM_PIN, HIGH);
switch (fsm_state) {
case FSMState::STATE_IDLE:
reset_pyro_safety(); // Ensure that pyros can be fired when we transition away from this state
break;
case FSMState::STATE_PYRO_TEST:
if(safety_has_fired_pyros_this_cycle) {
// If a fire pyro command has already be acknowledged, do not acknowledge more commands, just fire pyro for the defined time
// then, transition to SAFE.
if((current_time - safety_pyro_start_firing_time) >= PYRO_TEST_FIRE_TIME) {
telem_commands.should_transition_safe = true;
disarm_all_channels(new_pyro_state);
telem_commands.should_fire_pyro_a = false;
telem_commands.should_fire_pyro_b = false;
telem_commands.should_fire_pyro_c = false;
telem_commands.should_fire_pyro_d = false;
reset_pyro_safety();
}
break;
}
// Respond to telem commands to fire igniters
if(telem_commands.should_fire_pyro_a) {
// Fire pyro channel "A"
new_pyro_state.channel_firing[0] = true;
gpioDigitalWrite(PYROA_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_b) {
// Fire pyro channel "B"
new_pyro_state.channel_firing[1] = true;
gpioDigitalWrite(PYROB_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_c) {
// Fire pyro channel "C"
new_pyro_state.channel_firing[2] = true;
gpioDigitalWrite(PYROC_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_d) {
// Fire pyro channel "D"
new_pyro_state.channel_firing[0] = true;
gpioDigitalWrite(PYROD_FIRE_PIN, HIGH);
set_pyro_safety();
}
break;
case FSMState::STATE_SUSTAINER_IGNITION:
// Fire "Pyro C" to ignite sustainer (Pyro C is motor channel)
// Additionally, check if orientation allows for firing
if (can_fire_igniter(angular_kalman_data)) {
new_pyro_state.channel_firing[2] = true;
gpioDigitalWrite(PYROC_FIRE_PIN, HIGH);
}
break;
case FSMState::STATE_DROGUE_DEPLOY:
// Fire "Pyro A" to deploy upper stage drogue
new_pyro_state.channel_firing[0] = true;
gpioDigitalWrite(PYROA_FIRE_PIN, HIGH);
break;
case FSMState::STATE_MAIN_DEPLOY:
// Fire "Pyro B" to deploy main.
new_pyro_state.channel_firing[1] = true;
gpioDigitalWrite(PYROB_FIRE_PIN, HIGH);
break;
default:
break;
}
return new_pyro_state;
}
#endif
#ifdef IS_BOOSTER
/**
* Lower stage only!
*
* @brief Fires channels by setting their pin on the GPIO.
*
* @return A new pyro struct, with data depending on whether or not each pyro channel should be firing.
*/
PyroState Pyro::tick(FSMState fsm_state, AngularKalmanData angular_kalman_data, CommandFlags& telem_commands) {
PyroState new_pyro_state = PyroState();
double current_time = pdTICKS_TO_MS(xTaskGetTickCount());
if (fsm_state == FSMState::STATE_SAFE) {
disarm_all_channels(new_pyro_state);
return new_pyro_state;
}
// If the state is not SAFE, we arm the global arm pin
new_pyro_state.is_global_armed = true;
gpioDigitalWrite(PYRO_GLOBAL_ARM_PIN, HIGH);
// If the state is IDLE or any state after that, we arm the global arm pin
switch (fsm_state) {
case FSMState::STATE_IDLE:
reset_pyro_safety(); // Ensure that pyros can be fired when we transition away from this state
break;
case FSMState::STATE_PYRO_TEST:
if(safety_has_fired_pyros_this_cycle) {
// If a fire pyro command has already be acknowledged, do not acknowledge more commands, just fire pyro for the defined time
// then, transition to SAFE.
if((current_time - safety_pyro_start_firing_time) >= PYRO_TEST_FIRE_TIME) {
telem_commands.should_transition_safe = true;
disarm_all_channels(new_pyro_state);
telem_commands.should_fire_pyro_a = false;
telem_commands.should_fire_pyro_b = false;
telem_commands.should_fire_pyro_c = false;
telem_commands.should_fire_pyro_d = false;
reset_pyro_safety();
}
break;
}
// Respond to telem commands to fire igniters
if(telem_commands.should_fire_pyro_a) {
// Fire pyro channel "A"
new_pyro_state.channel_firing[0] = true;
gpioDigitalWrite(PYROA_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_b) {
// Fire pyro channel "B"
new_pyro_state.channel_firing[1] = true;
gpioDigitalWrite(PYROB_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_c) {
// Fire pyro channel "C"
new_pyro_state.channel_firing[2] = true;
gpioDigitalWrite(PYROC_FIRE_PIN, HIGH);
set_pyro_safety();
}
if(telem_commands.should_fire_pyro_d) {
// Fire pyro channel "D"
new_pyro_state.channel_firing[3] = true;
gpioDigitalWrite(PYROD_FIRE_PIN, HIGH);
set_pyro_safety();
}
break;
case FSMState::STATE_FIRST_SEPARATION:
// Fire "Pyro D" when separating stage 1
new_pyro_state.channel_firing[3] = true;
gpioDigitalWrite(PYROD_FIRE_PIN, HIGH);
break;
case FSMState::STATE_DROGUE_DEPLOY:
// Fire "Pyro A" to deploy drogue
new_pyro_state.channel_firing[0] = true;
gpioDigitalWrite(PYROA_FIRE_PIN, HIGH);
break;
case FSMState::STATE_MAIN_DEPLOY:
// Fire "Pyro B" to deploy Main
new_pyro_state.channel_firing[1] = true;
gpioDigitalWrite(PYROB_FIRE_PIN, HIGH);
break;
default:
break;
}
return new_pyro_state;
}
#endif