Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
flight_control.c File Reference
#include "flight_control.h"
#include "z_qflash_W25QXXX.h"
Include dependency graph for flight_control.c:

Go to the source code of this file.

Functions

static void clear_fsm_memory (flight_fsm_t *fsm_state)
 
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_Calibrating_phase (flight_fsm_t *fsm_state, estimation_output_t MotionData, linear_acceleration_t acc_data)
 Handler for the CALIBRATING state.
 
void check_Liftoff_phase (flight_fsm_t *fsm_state, estimation_output_t MotionData)
 Handler for the LIFTOFF state.
 
void check_Burning_phase (flight_fsm_t *phase, estimation_output_t MotionData)
 Handler for the BURNING state (powered ascent).
 
void check_AbcsDeployed_phase (flight_fsm_t *phase, estimation_output_t MotionData)
 Handler for the ABCSDEPLOYED state (apogee control).
 
void check_Drogue_phase (flight_fsm_t *phase, estimation_output_t MotionData)
 Handler for the DROGUE state (descent under drogue parachute).
 
void check_Main_phase (flight_fsm_t *phase, estimation_output_t MotionData)
 Handler for the MAIN state (descent under main parachute).
 
void change_state_to (flight_fsm_t *phase, flight_phase_t new_phase)
 Handles the mechanics of a state transition.
 

Variables

float_t previous_altitude = 0
 

Function Documentation

◆ change_state_to()

void change_state_to ( flight_fsm_t fsm_state,
flight_phase_t  new_state 
)

Handles the mechanics of a state transition.

This utility function updates the FSM's state to the new phase, sets the state_changed flag, and records the transition timestamp. It ensures state changes are handled consistently.

Parameters
[in,out]fsm_statePointer to the FSM state object to be updated.
[in]new_stateThe new flight_phase_t to transition into.
Returns
None

Definition at line 229 of file flight_control.c.

229 {
230
231 phase->flight_state=new_phase;
232 clear_fsm_memory(phase);
233}
static void clear_fsm_memory(flight_fsm_t *fsm_state)
flight_phase_t flight_state
The current state of the FSM.

References clear_fsm_memory(), and flight_fsm_t::flight_state.

Referenced by check_AbcsDeployed_phase(), check_Burning_phase(), check_Calibrating_phase(), check_Drogue_phase(), check_Liftoff_phase(), and check_Main_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_AbcsDeployed_phase()

void check_AbcsDeployed_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData 
)

Handler for the ABCSDEPLOYED state (apogee control).

Monitors for apogee detection (velocity changing from positive to negative) to trigger drogue parachute deployment.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).

Definition at line 162 of file flight_control.c.

162 {
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}
float_t previous_altitude
void change_state_to(flight_fsm_t *phase, flight_phase_t new_phase)
Handles the mechanics of a state transition.
#define APOGEE_SAFETY_COUNTER
Cycles for apogee detection (velocity < 0).
@ ABCSDEPLOYED
Airbrakes are deployed for apogee control.
@ DROGUE
Drogue parachute has been deployed.
bool flag_MPC
RTOS event flag to trigger the MPC task.
Definition main.c:303
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
float_t height
Vertical altitude above ground level in meters (m).
uint32_t memory[3]
General-purpose memory for state-specific logic.

References ABCSDEPLOYED, APOGEE_SAFETY_COUNTER, change_state_to(), DROGUE, drogue, flag_attitude, flag_MPC, flight_fsm_t::flight_state, estimation_output_t::height, flight_fsm_t::memory, and previous_altitude.

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_Burning_phase()

void check_Burning_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData 
)

Handler for the BURNING state (powered ascent).

Monitors for motor burnout and transitions to the coasting/apogee control phase.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).

Definition at line 126 of file flight_control.c.

126 {
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}
#define COASTING_SAFETY_COUNTER
Cycles for burnout detection.
@ BURNING
Engine is burning; under powered ascent.
uint32_t memory_bis[3]
Additional general-purpose memory.

References ABCSDEPLOYED, APOGEE_SAFETY_COUNTER, BURNING, change_state_to(), COASTING_SAFETY_COUNTER, DROGUE, drogue, flag_attitude, flag_MPC, flight_fsm_t::flight_state, estimation_output_t::height, flight_fsm_t::memory, flight_fsm_t::memory_bis, and previous_altitude.

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_Calibrating_phase()

void check_Calibrating_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData,
linear_acceleration_t  acc_data 
)

Handler for the CALIBRATING state.

Monitors for a sustained high vertical acceleration that indicates liftoff.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).
[in]acc_dataRaw linear acceleration data from the IMU.

Definition at line 56 of file flight_control.c.

56 {
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}
#define LIFTOFF_TRESHOLD_ACC
Raw accelerometer reading threshold for liftoff detection.
#define AIRBRAKES_SAFETY_COUNTER
Safety counter related to airbrake deployment logic.
#define LIFTOFF_SAFETY_COUNTER
Cycles for liftoff logic.
@ CALIBRATING
On the launchpad, calibrating sensors.
@ LIFTOFF
Rocket has cleared the launch rail.
bool flag_flash
RTOS event flag to trigger the data logging task.
Definition main.c:307
HAL_StatusTypeDef QFlash_Write(uint32_t addr, uint8_t *data, uint32_t dataSize)
float_t accX
Acceleration along the X-axis in m/s^2.

References linear_acceleration_t::accX, AIRBRAKES_SAFETY_COUNTER, CALIBRATING, change_state_to(), flag_attitude, flag_flash, flight_fsm_t::flight_state, estimation_output_t::height, LIFTOFF, LIFTOFF_SAFETY_COUNTER, LIFTOFF_TRESHOLD_ACC, flight_fsm_t::memory, flight_fsm_t::memory_bis, and QFlash_Write().

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_Drogue_phase()

void check_Drogue_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData 
)

Handler for the DROGUE state (descent under drogue parachute).

Monitors altitude to trigger main parachute deployment at a predefined height.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).

Definition at line 181 of file flight_control.c.

181 {
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}
#define MAIN_SAFETY_COUNTER
Cycles for main parachute deployment altitude check.
#define MAIN_DEPLOY_HEIGHT
Altitude in meters (m) for main parachute deployment.
@ MAIN
Main parachute has been deployed.
uint8_t mainp
Status flag for the main parachute pyro channel.
Definition main.c:230

References change_state_to(), DROGUE, flight_fsm_t::flight_state, estimation_output_t::height, MAIN, MAIN_DEPLOY_HEIGHT, MAIN_SAFETY_COUNTER, mainp, and flight_fsm_t::memory.

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_flight_phase()

void check_flight_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData,
linear_acceleration_t  acc_data 
)

Main FSM router function that delegates to the current state's handler.

This function acts as a switchboard. Based on the flight_state member of the FSM object, it calls the appropriate handler function for the current phase (e.g., check_Calibrating_phase, check_Liftoff_phase, etc.).

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataThe latest output from the state estimation filter.
[in]acc_dataThe latest raw linear acceleration data.
Returns
None

Definition at line 17 of file flight_control.c.

17 {
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}
void check_Burning_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the BURNING state (powered ascent).
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_Main_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the MAIN state (descent under main parachute).
void check_Liftoff_phase(flight_fsm_t *fsm_state, estimation_output_t MotionData)
Handler for the LIFTOFF state.
void check_Drogue_phase(flight_fsm_t *phase, estimation_output_t MotionData)
Handler for the DROGUE state (descent under drogue parachute).
@ INVALID
An undefined or error state.
@ TOUCHDOWN
Rocket has landed.
Main data structure for the Flight State Machine.
bool state_changed
Flag set to true for one cycle after a state change.

References ABCSDEPLOYED, BURNING, CALIBRATING, check_AbcsDeployed_phase(), check_Burning_phase(), check_Calibrating_phase(), check_Drogue_phase(), check_Liftoff_phase(), check_Main_phase(), DROGUE, flight_fsm_t::flight_state, INVALID, LIFTOFF, MAIN, flight_fsm_t::state_changed, and TOUCHDOWN.

Referenced by StartFSM().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_Liftoff_phase()

void check_Liftoff_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData 
)

Handler for the LIFTOFF state.

Monitors for the end of the motor burn phase based on acceleration drop.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).

Definition at line 93 of file flight_control.c.

93 {
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}
bool flag_kf
RTOS event flag to trigger the Kalman Filter task.
Definition main.c:301

References APOGEE_SAFETY_COUNTER, BURNING, change_state_to(), COASTING_SAFETY_COUNTER, DROGUE, drogue, flag_attitude, flag_kf, flag_MPC, flight_fsm_t::flight_state, estimation_output_t::height, LIFTOFF, flight_fsm_t::memory, flight_fsm_t::memory_bis, and previous_altitude.

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_Main_phase()

void check_Main_phase ( flight_fsm_t fsm_state,
estimation_output_t  MotionData 
)

Handler for the MAIN state (descent under main parachute).

Monitors for a stable, low altitude and a spike in acceleration to detect landing.

Parameters
[in,out]fsm_statePointer to the FSM state object.
[in]MotionDataFiltered estimation data (altitude, velocity).

Definition at line 197 of file flight_control.c.

197 {
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}
#define TOUCHDOWN_SAFETY_COUNTER
Cycles for touchdown logic.
#define VELOCITY_BOUND_TOUCHDOWN
Maximum vertical velocity (m/s) below which touchdown is considered possible.
float_t velocity
Vertical velocity in meters per second (m/s).

References change_state_to(), flag_flash, flight_fsm_t::flight_state, MAIN, flight_fsm_t::memory, TOUCHDOWN, TOUCHDOWN_SAFETY_COUNTER, estimation_output_t::velocity, and VELOCITY_BOUND_TOUCHDOWN.

Referenced by check_flight_phase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear_fsm_memory()

static void clear_fsm_memory ( flight_fsm_t fsm_state)
static

Definition at line 222 of file flight_control.c.

222 {
223 phase->clock_memory = 0;
224 phase->memory[0] = 0;
225 phase->memory[1] = 0;
226 phase->memory[2] = 0;
227}

References flight_fsm_t::clock_memory, and flight_fsm_t::memory.

Referenced by change_state_to().

Here is the caller graph for this function:

Variable Documentation

◆ previous_altitude

float_t previous_altitude = 0