Loading...
Searching...
No Matches
stateupdate.c
1/***********************************************************************************
2 * @file stateUpdate.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "FreeRTOS.h"
10#include "semphr.h"
11#include "message_buffer.h"
12#include "event_groups.h"
13
14#include "devicelist.h"
15
16#include "dataframe.h"
17#include "sensors.h"
18#include "groups.h"
19
20#include "accelerometer.h"
21#include "state.h"
22#include "membuff.h"
23#include "stateupdate.h"
24
25#include "AustralisConfig.h"
26
27extern EventGroupHandle_t xTaskEnableGroup;
28EventGroupHandle_t xFlightStateGroup;
29
30/* =============================================================================== */
42void vStateUpdate(void *argument) {
43 const TickType_t xFrequency = pdMS_TO_TICKS(20); // 50Hz
44
45 xFlightStateGroup = xEventGroupCreate();
46 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_PRELAUNCH);
47
48 float avgPressCurrent = 0;
49 float avgPressPrevious = 0;
50 float avgVelCurrent = 0;
51 float avgVelPrevious = 0;
52
53 State *state = State_getState();
54
55 DeviceHandle_t accelHandle = DeviceList_getDeviceHandle(DEVICE_ACCEL);
56 Accel_t *accel = accelHandle.device;
57
58 for (;;) {
59 // Block until 20ms interval
60 TickType_t xLastWakeTime = xTaskGetTickCount();
61 vTaskDelayUntil(&xLastWakeTime, xFrequency);
62
63 switch (state->flightState) {
64 case PRELAUNCH:
65 if (accel->accelData[ZINDEX] >= ACCEL_LAUNCH) {
66 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_FLASH); // Enable flash
67 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_HIGHRES); // Enable high resolution data acquisition
68 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES); // Enable low resolution data acquisition
69 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_LAUNCH);
70 state->flightState = LAUNCH;
71 }
72 break;
73
74 case LAUNCH:
75 state->avgVel.calculateMovingAverage(&state->avgVel, &avgVelCurrent);
76 // Transition to motor burnout state on velocity decrease
77 if ((avgVelCurrent - avgVelPrevious) < 0) {
78 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_COAST);
79 state->flightState = COAST;
80 }
81 avgVelPrevious = avgVelCurrent;
82 break;
83
84 case COAST:
85 state->avgPress.calculateMovingAverage(&state->avgPress, &avgPressCurrent);
86 // Transition to apogee state on three way vote of altitude, velocity, and tilt
87 // apogee is determined as two of three conditions evaluating true
88 if ((((avgPressCurrent - avgPressPrevious) > 0) + (state->tilt >= 90) + (state->velocity < 0.0f)) >= 2) {
89 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_APOGEE);
90 state->flightState = APOGEE;
91
92 // Log apogee event to flash
93 state->mem.appendBytes(&state->mem, (uint8_t[]){HEADER_EVENT_APOGEE, 0xB0, 0x0B}, 3);
94 state->mem.appendBytes(
95 &state->mem,
96 (uint8_t *)&state->flightTimeMs,
97 sizeof(state->flightTimeMs)
98 );
99
100 // Send transmission to trigger apogee E-matches
101 }
102 avgPressPrevious = avgPressCurrent;
103 break;
104
105 case APOGEE:
106 // Deploy drogue chute
107 //
108 // Transition to descent state when below main deployment altitude
109 if (state->altitude <= MAIN_ALTITUDE_METERS) {
110 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_DESCENT);
111 state->flightState = DESCENT;
112 // Add descent event dataframe to buffer
113 }
114 break;
115
116 case DESCENT:
117 // Handle descent state actions
118 break;
119 }
120 }
121}
122
Defines the API for the Accelerometer sensor.
float * accelData
Pointer to driver defined data array.
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
State * State_getState()
Definition state.c:69
@ APOGEE
At least 2: velocity negative, pressure increasing, tilt > 90 degrees.
Definition state.h:28
@ PRELAUNCH
Initial boot condition.
Definition state.h:25
@ LAUNCH
Body reference Z-axis acceleration above threshold.
Definition state.h:26
@ COAST
Global reference Z-axis velocity decreasing.
Definition state.h:27
@ DESCENT
Altitude below main threshold.
Definition state.h:29
State variable struct.
Definition state.h:36