Da Vinci Firmware 1
Firmware for the DaVinci-M rocket avionics board.
Loading...
Searching...
No Matches
RTOS Tasks

Implementations of the main application threads (RTOS tasks). More...

Collaboration diagram for RTOS Tasks:

Functions

void StartSensor_Task (void *argument)
 Function implementing the Sensor_Task thread.
 
void StartFSM (void *argument)
 Function implementing the FSM thread.
 
void StartKalman_mkf (void *argument)
 Function implementing the Kalman_mkf thread.
 
void StartMPC_Task (void *argument)
 Function implementing the MPC_Task thread.
 

Detailed Description

Implementations of the main application threads (RTOS tasks).

Function Documentation

◆ StartFSM()

void StartFSM ( void *  argument)

Function implementing the FSM thread.

Parameters
argumentNot used
Return values
None

Definition at line 1721 of file main.c.

1722{
1723 /* USER CODE BEGIN StartFSM */
1724 osDelay(50);
1725 HAL_TIM_Base_Start_IT(&htim13);
1726 /* Infinite loop */
1727 for(;;)
1728 {
1729 osThreadFlagsWait(FSM_FLAG, osFlagsWaitAny, osWaitForever);
1730
1732 acc1.accX = acceleration_mg_1[0];
1733 acc1.accY = acceleration_mg_1[1];
1734 acc1.accZ = acceleration_mg_1[2];
1735
1736 estimation_output_t MotionData;
1737 MotionData.acceleration = sqrt(
1741 MotionData.velocity = velocity;
1742 MotionData.height = altitude;
1743
1744 /* Check Flight Phases */
1745 check_flight_phase(&flight_state,MotionData, acc1);
1746 }
1747 /* USER CODE END StartFSM */
1748}
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.
TIM_HandleTypeDef htim13
Definition main.c:68
static float_t acceleration_mg_1[3]
Definition main.c:132
flight_fsm_t flight_state
Definition main.c:117
float_t altitude
Definition main.c:224
float_t velocity
Definition main.c:225
#define FSM_FLAG
Definition main.c:287
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).
float_t acceleration
Vertical acceleration in meters per second squared (m/s^2).
Represents a 3-axis linear acceleration vector from an IMU.
float_t accZ
Acceleration along the Z-axis in m/s^2.
float_t accX
Acceleration along the X-axis in m/s^2.
float_t accY
Acceleration along the Y-axis in m/s^2.

References estimation_output_t::acceleration, acceleration_mg_1, linear_acceleration_t::accX, linear_acceleration_t::accY, linear_acceleration_t::accZ, altitude, check_flight_phase(), flight_state, FSM_FLAG, estimation_output_t::height, htim13, estimation_output_t::velocity, and velocity.

Referenced by main().

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

◆ StartKalman_mkf()

void StartKalman_mkf ( void *  argument)

Function implementing the Kalman_mkf thread.

Parameters
argumentNot used
Return values
None

gnss_flag 0: no fix dal GNSS, 1:fix dal GNSS

Definition at line 1758 of file main.c.

1759{
1760 /* USER CODE BEGIN StartKalman_mkf */
1761 static uint32_t prescaler = 0;
1762
1763 /* Infinite loop */
1764 for(;;) {
1765 osThreadFlagsWait(KALMAN_FLAG, osFlagsWaitAny, osWaitForever);
1766
1767 if(flag_attitude) {
1769 alpha = M_PI/2 - alpha;
1770 //mekf(q,beta,P,omega, b_B, 0.0, q_new, beta_new, P_new); //mekf tinkered because of the magnetometers question, Height_AGL was replaced with permanent 0
1771 memcpy(q,q_new,sizeof(q_new));
1772 }
1773
1774 //memcpy(beta,beta_new,sizeof(beta_new));
1775 //memcpy(P,P_new,sizeof(P_new));
1776//--------------------------------------------------------------------------------------------- TODO : to b e reintroduced with the actual mekf
1777 // z_component = 1 - 2*( q[0]*q[0] + q[1]*q[1] );
1778
1779 // if(z_component < - 1){
1780 // z_component = -1.0;
1781 // }
1782 // else if(z_component > 1){
1783 // z_component = 1.0;
1784 //}
1785
1786 //alpha = acos(z_component);
1787//----------------------------------------------------------------------------------------------------
1788 if(flag_kf) {
1790 gnss_flag = false;
1791 altitude = x_est[0];
1792 velocity = x_est[1];
1793 } else {
1794 velocity = gps_vel;
1795 }
1796
1798 ++prescaler;
1799
1800 if (prescaler == 25) { // 1 second
1801 prescaler = 0;
1802 //toggle_led();
1803 LED_TOGGLE(GPIO_LED1_GPIO_Port,GPIO_LED1_Pin);
1804 }
1805
1806
1808 memcpy(cpy_buffer,data_buffer,2112);
1809 } else if(num_of_saved_rest_packages_locked == 8) {
1810 memcpy(cpy_buffer,data_buffer+2112,2112);
1811 }
1812
1815
1816 if (flash_address >= (67108864 - 211200)) {
1818 flag_flash = false;
1819 } else {
1820 QFlash_Write(flash_address,(uint8_t*) cpy_buffer,sizeof(cpy_buffer));
1823 }
1824 }
1825 }
1826 /* USER CODE END StartKalman_mkf */
1827}
void attitude_estimation(const double quat[4], double ts, double omega_x, double omega_y, double omega_z, double *alpha, double quat_new[4])
Propagates the rocket's attitude quaternion and calculates the angle from vertical.
bool flag_kf
RTOS event flag to trigger the Kalman Filter task.
Definition main.c:301
bool flag_flash
RTOS event flag to trigger the data logging task.
Definition main.c:307
bool flag_attitude
RTOS event flag to trigger the attitude estimation task.
Definition main.c:302
HAL_StatusTypeDef QFlash_Write(uint32_t addr, uint8_t *data, uint32_t dataSize)
void kalmanFilter(double x_est[2], double P[4], const double z[2], double alpha, double a_m, double Ts, const double Q[4], const double R[4], bool gnss_signal)
Executes one prediction and correction step of the Kalman filter.
bool gnss_flag
Definition main.c:300
double alpha
Definition main.c:249
uint32_t flash_address
Definition main.c:308
uint32_t num_of_lin_acc_saved_flash
Definition main.c:314
uint32_t num_of_saved_rest_packages_flash
Definition main.c:315
double a_m
Definition main.c:250
double q_new[]
Definition main.c:243
uint8_t data_buffer[4224]
Definition main.c:309
double omega[]
Definition main.c:240
const double R[]
Definition main.c:253
uint8_t cpy_buffer[2112]
Definition main.c:310
#define KALMAN_FLAG
Definition main.c:286
double q[]
Definition main.c:237
float gps_vel
Definition main.c:293
double z[]
Definition main.c:248
const double Q[]
Definition main.c:252
uint32_t num_of_saved_rest_packages_locked
Definition main.c:313
double P_2[]
Definition main.c:247
double x_est[]
Definition main.c:254
double Ts
Definition main.c:251
A comprehensive sensor data packet, excluding linear acceleration.
Definition main.c:166
Represents a 3-axis linear acceleration data packet.
Definition main.c:149

References a_m, alpha, altitude, attitude_estimation(), cpy_buffer, data_buffer, flag_attitude, flag_flash, flag_kf, flash_address, gnss_flag, gps_vel, KALMAN_FLAG, kalmanFilter(), num_of_lin_acc_saved_flash, num_of_saved_rest_packages_flash, num_of_saved_rest_packages_locked, omega, P_2, q, Q, q_new, QFlash_Write(), R, Ts, velocity, x_est, and z.

Referenced by main().

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

◆ StartMPC_Task()

void StartMPC_Task ( void *  argument)

Function implementing the MPC_Task thread.

Parameters
argumentNot used
Return values
None

Definition at line 1837 of file main.c.

1838{
1839 /* USER CODE BEGIN StartMPC_Task */
1840 static uint32_t prescaler = 0;
1841 osDelay(50);
1842 HAL_TIM_Base_Start_IT(&htim7);
1843
1844 /* Infinite loop */
1845 for(;;)
1846 {
1847 osThreadFlagsWait(MPC_FLAG, osFlagsWaitAny, osWaitForever);
1848 HAL_ADC_Start(&hadc3);
1849 HAL_ADC_PollForConversion(&hadc3, HAL_MAX_DELAY);
1850 adcVal =(HAL_ADC_GetValue(&hadc3));
1851 HAL_ADC_Stop(&hadc3);
1852 Vin=((adcVal/3180.0f)*3.09f);
1853 Vr=(Vin*((2700.0f+4700.0f)/2700.0f));
1855
1862 switch (flight_state.flight_state){
1863 case INVALID:
1864 message.phase = 0;
1865 break;
1866 case CALIBRATING:
1867 message.phase = 1;
1868 break;
1869 case LIFTOFF:
1870 message.phase = 2;
1871 break;
1872 case BURNING:
1873 message.phase = 3;
1874 break;
1875 case ABCSDEPLOYED:
1876 message.phase = 4;
1877 break;
1878 case DROGUE:
1879 message.phase = 5;
1880 break;
1881 case MAIN:
1882 message.phase = 6;
1883 break;
1884 case TOUCHDOWN:
1885 message.phase = 7;
1886 break;
1887 }
1888 //------------------------------------------------------------------------------------------------- TODO given that magnetometers will not fly we will replace their data with other stuff
1889 message.mag_x = (float_t)alpha;
1890
1891 if(altitude> max_alt){
1894 }
1895 if(velocity> max_vel){
1898
1899 }
1907
1908 double deg = optimalAngle( sim_time_step,
1913 cd_table);
1914 //------------------------------------------------------------------------------------------------- TODO given that magnetometers will not fly we will replace their data with other stuff
1915
1916 message.deg = (float_t)deg;
1917
1918 }else if(flight_state.flight_state>=DROGUE){
1919 message.deg = 60.0;
1920 }
1921 ++prescaler;
1922 if (prescaler == 2) // 1 second
1923 {
1924 prescaler = 0;
1925 // //toggle_led();
1926 LED_TOGGLE(GPIO_LED3_GPIO_Port,GPIO_LED3_Pin);
1927 }
1928 memcpy(array,(uint8_t*)&message,sizeof(message));
1929 HAL_UART_Transmit_DMA(&huart1, array, sizeof(array));
1930
1931
1932 }
1933 /* USER CODE END StartMPC_Task */
1934}
@ 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_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
uint8_t mainp
Status flag for the main parachute pyro channel.
Definition main.c:230
const double mach_states[]
Definition main.c:261
UART_HandleTypeDef huart1
Definition main.c:72
#define MPC_FLAG
Definition main.c:284
float Vr
Definition main.c:322
Data_Package_For_Marconi message
Definition main.c:228
double sim_time_step
Definition main.c:256
float Vin
Definition main.c:321
double tolerance
Definition main.c:259
ADC_HandleTypeDef hadc3
Definition main.c:54
uint32_t adcVal
Definition main.c:323
TIM_HandleTypeDef htim7
Definition main.c:67
double target_apogee
Definition main.c:258
struct bmp3_data barometer_data_1
Definition main.c:126
static float_t angular_rate_mdps_1[3]
Definition main.c:133
const double deployment_states[]
Definition main.c:262
uint8_t array[63]
Definition main.c:296
float max_vel
Definition main.c:232
static double cd_table[]
Definition main.c:263
float max_alt
Definition main.c:231
double optimalAngle(double sim_time_step, double altitude, double temperature, double velocity, double pressure, double current_delta, double tilt_angle, double target_apogee, double tolerance, const double mach_states[5], const double deployment_states[6], const double cd_table[30])
Calculates the optimal constant airbrake angle to reach a target apogee.
float_t acc_x
Acceleration along the X-axis in g's.
Definition main.c:195
float_t acc_y
Acceleration along the Y-axis in g's.
Definition main.c:196
float_t Battery_Voltage
Current battery voltage in Volts.
Definition main.c:214
float_t mag_z
Magnetic field strength on the Z-axis in mG.
Definition main.c:205
uint8_t parachute_2
Status of the main parachute (e.g., 0=Armed, 1=Deployed).
Definition main.c:218
float_t mag_y
Magnetic field strength on the Y-axis in mG.
Definition main.c:204
float_t altitude
Calculated altitude above sea level in meters (m).
Definition main.c:209
float_t temperature
Ambient temperature in degrees Celsius (°C).
Definition main.c:207
float_t acc_z
Acceleration along the Z-axis in g's.
Definition main.c:197
float_t dps_y
Angular rate around the Y-axis in dps.
Definition main.c:200
float_t pressure
Atmospheric pressure in Pascals (Pa).
Definition main.c:208
uint8_t parachute_1
Status of the drogue parachute (e.g., 0=Armed, 1=Deployed).
Definition main.c:217
uint8_t phase
Current flight phase identifier (e.g., 0=Calibration, 1=Liftoff).
Definition main.c:212
float_t mag_x
Magnetic field strength on the X-axis in mG (milligauss).
Definition main.c:203
float_t deg
Current airbrakes opening degrees.
Definition main.c:215
float_t velocity
Calculated module of velocity in meters per second (m/s).
Definition main.c:211
float_t dps_z
Angular rate around the Z-axis in dps.
Definition main.c:201
float_t dps_x
Angular rate around the X-axis in degrees per second (dps).
Definition main.c:199
double pressure
Definition bmp3_defs.h:847
double temperature
Definition bmp3_defs.h:844
flight_phase_t flight_state
The current state of the FSM.

References ABCSDEPLOYED, Data_Package_For_Marconi::acc_x, Data_Package_For_Marconi::acc_y, Data_Package_For_Marconi::acc_z, acceleration_mg_1, adcVal, alpha, Data_Package_For_Marconi::altitude, altitude, angular_rate_mdps_1, array, barometer_data_1, Data_Package_For_Marconi::Battery_Voltage, BURNING, CALIBRATING, cd_table, Data_Package_For_Marconi::deg, deployment_states, Data_Package_For_Marconi::dps_x, Data_Package_For_Marconi::dps_y, Data_Package_For_Marconi::dps_z, DROGUE, drogue, flag_MPC, flight_fsm_t::flight_state, flight_state, hadc3, htim7, huart1, INVALID, LIFTOFF, mach_states, Data_Package_For_Marconi::mag_x, Data_Package_For_Marconi::mag_y, Data_Package_For_Marconi::mag_z, MAIN, mainp, max_alt, max_vel, message, MPC_FLAG, optimalAngle(), Data_Package_For_Marconi::parachute_1, Data_Package_For_Marconi::parachute_2, Data_Package_For_Marconi::phase, bmp3_data::pressure, Data_Package_For_Marconi::pressure, sim_time_step, target_apogee, bmp3_data::temperature, Data_Package_For_Marconi::temperature, tolerance, TOUCHDOWN, Data_Package_For_Marconi::velocity, velocity, Vin, Vr, and x_est.

Referenced by main().

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

◆ StartSensor_Task()

void StartSensor_Task ( void *  argument)

Function implementing the Sensor_Task thread.

Parameters
argumentNot used
Return values
None

Definition at line 1588 of file main.c.

1589{
1590 /* USER CODE BEGIN 5 */
1591 //xSensorsTaskHandle = xTaskGetCurrentTaskHandle();
1592 //static uint32_t prescaler = 0;
1593 uint32_t num_esecuzioni = 0;
1594 Data_Package_no_Linear_IMU the_rest = {0};
1595 osDelay(50);
1596 HAL_TIM_Base_Start_IT(&htim6);
1597
1598 /* Infinite loop */
1599 for(;;)
1600 {
1601 //-----------------------------------------------------------------------------------every 0.25 ms
1602 osThreadFlagsWait(SENSOR_FLAG, osFlagsWaitAny, osWaitForever);
1603
1604
1606
1610
1611
1612 //Lettura IMU
1613 num_esecuzioni +=1;
1614 num_esecuzioni %= (uint32_t)40;
1615 if(flag_flash){
1618 }
1619
1620 if (num_esecuzioni == 0){
1621
1623
1625
1629
1630 the_rest.dps_x = angular_rate_mdps_1[0];
1631 the_rest.dps_y = angular_rate_mdps_1[1];
1632 the_rest.dps_z = angular_rate_mdps_1[2];
1635
1636
1637 if(first_measure ){
1638 Pressure_1 = the_rest.pressure;
1639 Temperature_1 = the_rest.temperature;
1640 first_measure = false;
1641 }
1643 z[0] = (double)altitude;
1644 the_rest.altitude = altitude;
1645 the_rest.velocity=velocity;
1646 z[1] = (double)gps_vel;
1647
1648 switch (flight_state.flight_state){
1649 case INVALID:
1650 the_rest.phase = 0;
1651 break;
1652 case CALIBRATING:
1653 the_rest.phase = 1;
1654 break;
1655 case LIFTOFF:
1656 the_rest.phase = 2;
1657 break;
1658 case BURNING:
1659 the_rest.phase = 3;
1660 break;
1661 case ABCSDEPLOYED:
1662 the_rest.phase = 4;
1663 break;
1664 case DROGUE:
1665 the_rest.phase = 5;
1666 break;
1667 case MAIN:
1668 the_rest.phase = 6;
1669 break;
1670 case TOUCHDOWN:
1671 the_rest.phase = 7;
1672 break;
1673 }
1674 if(flag_flash){
1677 //-------------------------------------------------------- We enter this portion of code every 10 ms
1678 if (num_of_saved_rest_packages == 8) {
1682 } else if (num_of_saved_rest_packages == 4) {
1684 }
1685 }
1687 omega[0] = -angular_rate_mdps_1[2]*M_PI/180000;
1688 omega[1] = angular_rate_mdps_1[1]*M_PI/180000;
1689 omega[2] = angular_rate_mdps_1[0]*M_PI/180000;
1690
1691 //++prescaler;
1692 //if (prescaler == 100) // 1 second
1693 // {
1694 // prescaler = 0;
1695 // //toggle_led();
1696 // LED_TOGGLE(GPIO_LED1_GPIO_Port,GPIO_LED1_Pin);
1697 //}
1698
1699 osThreadFlagsSet(Kalman_mkfHandle, KALMAN_FLAG);
1700 //-----------------------------------------------------------------------------------------------------------------------
1701 //lis2mdl_magnetic_raw_get(&lis2mdl_dev_ctx, raw_mag_data);
1702
1703 //the_rest.mag_x = lis2mdl_from_lsb_to_nanotesla(raw_mag_data[0]);
1704 //the_rest.mag_y = lis2mdl_from_lsb_to_nanotesla(raw_mag_data[1]);
1705 //the_rest.mag_z = lis2mdl_from_lsb_to_nanotesla(raw_mag_data[1]);
1706 //-----------------------------------------------------------------------------------------------------------------------
1707 }
1708
1709 }
1710 /* USER CODE END 5 */
1711}
int8_t bmp3_get_sensor_data(uint8_t sensor_comp, struct bmp3_data *comp_data, struct bmp3_dev *dev)
This API reads the pressure, temperature or both data from the sensor, compensates the data and store...
Definition bmp3.c:1428
#define BMP3_PRESS_TEMP
Definition bmp3_defs.h:313
int32_t lsm6dso32_angular_rate_raw_get(stmdev_ctx_t *ctx, int16_t *val)
Angular rate sensor. The value is expressed as a 16-bit word in two’s complement.[get].
int32_t lsm6dso32_acceleration_raw_get(stmdev_ctx_t *ctx, int16_t *val)
Linear acceleration output register. The value is expressed as a 16-bit word in two’s complement....
float_t lsm6dso32_from_fs16_to_mg(int16_t lsb)
float_t lsm6dso32_from_fs2000_to_mdps(int16_t lsb)
float_t Temperature_1
Definition main.c:227
struct bmp3_dev bmp390_1
Definition main.c:122
TIM_HandleTypeDef htim6
Definition main.c:66
bool first_measure
Definition main.c:233
#define SENSOR_FLAG
Definition main.c:283
uint32_t num_of_lin_acc_saved
Definition main.c:311
static int16_t data_raw_acceleration_1[3]
Definition main.c:130
float_t Pressure_1
Definition main.c:226
stmdev_ctx_t imu_1
Definition main.c:124
osThreadId_t Kalman_mkfHandle
Definition main.c:93
uint32_t num_of_saved_rest_packages
Definition main.c:312
static int16_t data_raw_angular_rate_1[3]
Definition main.c:129
float_t altitude
Calculated altitude above sea level in meters (m).
Definition main.c:178
float_t dps_x
Angular rate around the X-axis in degrees per second (dps).
Definition main.c:167
float_t pressure
Atmospheric pressure in Pascals (Pa).
Definition main.c:176
float_t dps_y
Angular rate around the Y-axis in dps.
Definition main.c:168
float_t dps_z
Angular rate around the Z-axis in dps.
Definition main.c:169
float_t temperature
Ambient temperature in degrees Celsius (°C).
Definition main.c:175
float_t phase
Current flight phase (as a floating point representation for memory alignment).
Definition main.c:180
float_t velocity
Calculated vertical velocity in meters per second (m/s).
Definition main.c:179
float_t readAltitude(float_t seaLevelPa, float_t seaLevelT, float_t currentPa)
Calculates altitude based on the barometric formula.
Definition utilities.c:780

References a_m, ABCSDEPLOYED, acceleration_mg_1, Data_Package_no_Linear_IMU::altitude, altitude, angular_rate_mdps_1, barometer_data_1, bmp390_1, bmp3_get_sensor_data(), BMP3_PRESS_TEMP, BURNING, CALIBRATING, data_buffer, data_raw_acceleration_1, data_raw_angular_rate_1, Data_Package_no_Linear_IMU::dps_x, Data_Package_no_Linear_IMU::dps_y, Data_Package_no_Linear_IMU::dps_z, DROGUE, first_measure, flag_flash, flight_fsm_t::flight_state, flight_state, gps_vel, htim6, imu_1, INVALID, KALMAN_FLAG, Kalman_mkfHandle, LIFTOFF, lsm6dso32_acceleration_raw_get(), lsm6dso32_angular_rate_raw_get(), lsm6dso32_from_fs16_to_mg(), lsm6dso32_from_fs2000_to_mdps(), MAIN, num_of_lin_acc_saved, num_of_saved_rest_packages, num_of_saved_rest_packages_locked, omega, Data_Package_no_Linear_IMU::phase, bmp3_data::pressure, Data_Package_no_Linear_IMU::pressure, Pressure_1, readAltitude(), SENSOR_FLAG, bmp3_data::temperature, Data_Package_no_Linear_IMU::temperature, Temperature_1, TOUCHDOWN, Data_Package_no_Linear_IMU::velocity, velocity, and z.

Referenced by main().

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