Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
flight_control.c
Go to the documentation of this file.
1/*
2 * FlightControl.c
3 *
4 * Created on: Apr 2, 2024
5 * Author: Tommaso & Francesco Abate
6 */
7#ifndef FLIGHTCONTROL_H
8#include "flight_control.h"
9#endif
10
11#include "z_qflash_W25QXXX.h"
12
14
15static void clear_fsm_memory(flight_fsm_t *fsm_state);
16
18
19 const flight_fsm_t old_phase = *phase;
20
21 switch (phase->flight_state) {
22 case CALIBRATING:
23 check_Calibrating_phase(phase,MotionData,acc_data);
24 break;
25
26 case LIFTOFF:
27 check_Liftoff_phase(phase, MotionData);
28 break;
29
30 case BURNING:
31 check_Burning_phase(phase, MotionData);
32 break;
33
34 case ABCSDEPLOYED:
35 check_AbcsDeployed_phase(phase,MotionData);
36 break;
37
38 case DROGUE:
39 check_Drogue_phase(phase,MotionData);
40 break;
41
42 case MAIN:
43 check_Main_phase(phase, MotionData);
44 break;
45
46 case TOUCHDOWN:
47 break;
48 case INVALID:
49 break;
50
51 }
52
53 phase->state_changed = old_phase.flight_state != phase->flight_state;
54}
55
57 if(fsm_state->flight_state > CALIBRATING) return;
58
59 if (acc_data.accX>LIFTOFF_TRESHOLD_ACC) {
60
61 fsm_state->memory[0]++;
62 } else {
63
64 fsm_state->memory[0] = 0;
65 }
66
67 if(fsm_state->memory[0] > AIRBRAKES_SAFETY_COUNTER) {
68
69 float_t flag = 1.0;
70 QFlash_Write(0, (uint8_t*)&flag, 4);
71 flag_flash = true;
72 flag_attitude = true;
73 change_state_to(fsm_state, LIFTOFF);
74 }
75//----------------------------------------------------------------------------------------- JUST IN CASE WE HAVE AN UMEXPECTED APOGEE
76 if (MotionData.height > 20) {
77 fsm_state->memory_bis[0]++;
78 } else {
79 fsm_state->memory_bis[0] = 0;
80 }
81
82 if(fsm_state->memory_bis[0] > LIFTOFF_SAFETY_COUNTER){
83 float_t flag = 1.0;
84 QFlash_Write(0, (uint8_t*)&flag, 4);
85 flag_flash = true;
86 flag_attitude = true;
87 change_state_to(fsm_state,LIFTOFF);
88 }
89//--------------------------------------------------------------------------------------------
90
91}
92
94 // if I'm in a higher state I cannot come back
95 if(fsm_state->flight_state > LIFTOFF) return;
96
97 if (MotionData.height > 500) {
98 fsm_state->memory[0]++;
99 } else {
100 fsm_state->memory[0] = 0;
101 }
102
103 if (fsm_state->memory[0] > COASTING_SAFETY_COUNTER) {
104 flag_kf = true;
105 change_state_to(fsm_state, BURNING);//ABCSDEPLOYED
106 }
107
108
109//----------------------------------------------------------------------------------------- JUST IN CASE WE HAVE AN UMEXPECTED APOGEE
110 if (MotionData.height < previous_altitude) {
111 fsm_state->memory_bis[0]++;
112 } else {
113 fsm_state->memory_bis[0] = 0;
114 previous_altitude = MotionData.height;
115 }
116
117 if(fsm_state->memory_bis[0] > APOGEE_SAFETY_COUNTER) {
118 drogue = 1;
119 flag_MPC = false;
120 flag_attitude = false;
121 change_state_to(fsm_state,DROGUE);
122 }
123//--------------------------------------------------------------------------------------------
124}
125
127 // if I'm in a higher state I cannot come back
128
129 if(phase->flight_state > BURNING) return;
130
131 if (MotionData.height > 1000) {
132 phase->memory[0]++;
133 } else {
134 phase->memory[0] = 0;
135 }
136
137 if (phase->memory[0] > COASTING_SAFETY_COUNTER) {
138 flag_MPC = true;
139 flag_attitude = false;
140 change_state_to(phase, ABCSDEPLOYED);//ABCSDEPLOYED
141 }
142
143//----------------------------------------------------------------------------------------- JUST IN CASE WE HAVE AN UMEXPECTED APOGEE
144 if (MotionData.height < previous_altitude) {
145 phase->memory_bis[0]++;
146 } else{
147 phase->memory_bis[0] = 0;
148 previous_altitude = MotionData.height;
149 }
150
151 if(phase->memory_bis[0] > APOGEE_SAFETY_COUNTER){
152 drogue = 1;
153 flag_MPC = false;
154 flag_attitude = false;
155 change_state_to(phase,DROGUE);
156 }
157//--------------------------------------------------------------------------------------------
158
159
160}
161
163 if(phase->flight_state > ABCSDEPLOYED) return;
164
165 if (MotionData.height < previous_altitude) {
166 phase->memory[0]++;
167 } else {
168 phase->memory[0] = 0;
169 previous_altitude = MotionData.height;
170 }
171
172 if(phase->memory[0] > APOGEE_SAFETY_COUNTER){
173 drogue = 1;
174 flag_MPC = false;
175 flag_attitude = false;
176 change_state_to(phase,DROGUE);
177 }
178}
179
180
182 // if I'm in a higher state I cannot come back
183 if(phase->flight_state > DROGUE) return;
184
185 if(MotionData.height < MAIN_DEPLOY_HEIGHT) {
186 phase->memory[0]++;
187 } else {
188 phase->memory[0] = 0;
189 }
190
191 if(phase->memory[0] > MAIN_SAFETY_COUNTER) {
192 mainp = 1;
193 change_state_to(phase, MAIN);
194 }
195}
196
198 // if I'm in a higher state I cannot come back
199 if(phase->flight_state > MAIN) return;
200
201 //HAL_GPIO_WritePin(GPIOC, E_Match_Parachute_2_Pin, GPIO_PIN_SET);
202
203 /* If the velocity is very small we have touchdown */
204 // check the altitude for a specific amount of time
205 if (fabsf(MotionData.velocity) < VELOCITY_BOUND_TOUCHDOWN) {
206// /* Touchdown achieved */
207 phase->memory[0]++;
208 } else {
209 /* Touchdown not achieved */
210 phase->memory[0] = 0;
211 }
212
213 if (phase->memory[0] > TOUCHDOWN_SAFETY_COUNTER) {
214 flag_flash = false;
216 }
217
218}
219
220
221/* Function that needs to be called every time that a state transition is done */
222static void clear_fsm_memory(flight_fsm_t *phase) {
223 phase->clock_memory = 0;
224 phase->memory[0] = 0;
225 phase->memory[1] = 0;
226 phase->memory[2] = 0;
227}
228
230
231 phase->flight_state=new_phase;
232 clear_fsm_memory(phase);
233}
void check_Burning_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the BURNING state (powered ascent).
static void clear_fsm_memory(flight_fsm_t *fsm_state)
void check_Calibrating_phase(flight_fsm_t *fsm_state, estimation_output_t MotionData, linear_acceleration_t acc_data)
Handler for the CALIBRATING state.
void check_AbcsDeployed_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the ABCSDEPLOYED state (apogee control).
void check_flight_phase(flight_fsm_t *phase, estimation_output_t MotionData, linear_acceleration_t acc_data)
Main FSM router function that delegates to the current state's handler.
void check_Main_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the MAIN state (descent under main parachute).
float_t previous_altitude
void check_Liftoff_phase(flight_fsm_t *fsm_state, estimation_output_t MotionData)
Handler for the LIFTOFF state.
void change_state_to(flight_fsm_t *phase, flight_phase_t new_phase)
Handles the mechanics of a state transition.
void check_Drogue_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the DROGUE state (descent under drogue parachute).
Defines the states, events, and data structures for the rocket's main Flight State Machine (FSM).
#define LIFTOFF_TRESHOLD_ACC
Raw accelerometer reading threshold for liftoff detection.
#define MAIN_SAFETY_COUNTER
Cycles for main parachute deployment altitude check.
#define AIRBRAKES_SAFETY_COUNTER
Safety counter related to airbrake deployment logic.
#define TOUCHDOWN_SAFETY_COUNTER
Cycles for touchdown logic.
#define COASTING_SAFETY_COUNTER
Cycles for burnout detection.
#define LIFTOFF_SAFETY_COUNTER
Cycles for liftoff logic.
#define APOGEE_SAFETY_COUNTER
Cycles for apogee detection (velocity < 0).
#define VELOCITY_BOUND_TOUCHDOWN
Maximum vertical velocity (m/s) below which touchdown is considered possible.
#define MAIN_DEPLOY_HEIGHT
Altitude in meters (m) for main parachute deployment.
flight_phase_t
Enumeration of all possible flight phases (states) in the FSM.
@ CALIBRATING
On the launchpad, calibrating sensors.
@ MAIN
Main parachute has been deployed.
@ LIFTOFF
Rocket has cleared the launch rail.
@ ABCSDEPLOYED
Airbrakes are deployed for apogee control.
@ DROGUE
Drogue parachute has been deployed.
@ BURNING
Engine is burning; under powered ascent.
@ INVALID
An undefined or error state.
@ TOUCHDOWN
Rocket has landed.
bool flag_kf
RTOS event flag to trigger the Kalman Filter task.
Definition main.c:301
bool flag_MPC
RTOS event flag to trigger the MPC task.
Definition main.c:303
bool flag_flash
RTOS event flag to trigger the data logging task.
Definition main.c:307
uint8_t drogue
Status flag for the drogue parachute pyro channel.
Definition main.c:229
bool flag_attitude
RTOS event flag to trigger the attitude estimation task.
Definition main.c:302
uint8_t mainp
Status flag for the main parachute pyro channel.
Definition main.c:230
HAL_StatusTypeDef QFlash_Write(uint32_t addr, uint8_t *data, uint32_t dataSize)
Holds the primary outputs of the state estimation filter (e.g., Kalman filter).
float_t velocity
Vertical velocity in meters per second (m/s).
float_t height
Vertical altitude above ground level in meters (m).
Main data structure for the Flight State Machine.
uint32_t memory_bis[3]
Additional general-purpose memory.
uint32_t memory[3]
General-purpose memory for state-specific logic.
uint32_t clock_memory
Timestamp of the last state transition.
flight_phase_t flight_state
The current state of the FSM.
bool state_changed
Flag set to true for one cycle after a state change.
Represents a 3-axis linear acceleration vector from an IMU.
float_t accX
Acceleration along the X-axis in m/s^2.
Driver for W25Qxxx series Quad-SPI NOR Flash memory.