Loading...
Searching...
No Matches
stateupdate.c
1/***********************************************************************************
2 * @file stateUpdate.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "membuff.h"
10#include "stateupdate.h"
11
12extern EventGroupHandle_t xTaskEnableGroup;
13extern MessageBufferHandle_t xUsbTxBuff;
14extern SemaphoreHandle_t xUsbMutex;
15
16/* =============================================================================== */
28void vStateUpdate(void *argument) {
29 const TickType_t xFrequency = pdMS_TO_TICKS(20); // 50Hz
30
31 unsigned int CANHigh = 0;
32 unsigned int CANLow = 0;
33 unsigned int id = 0;
34
35 float avgPressCurrent = 0;
36 float avgPressPrevious = 0;
37 float avgVelCurrent = 0;
38 float avgVelPrevious = 0;
39
40 DeviceHandle_t accelHandle = DeviceList_getDeviceHandle(DEVICE_ACCEL);
41 KX134_1211_t *accel = accelHandle.device;
42
43 MemBuff *mem = StateHandle_getHandle("Memory").state;
44 enum State *flightState = StateHandle_getHandle("FlightState").state;
45 float *tilt = StateHandle_getHandle("Tilt").state;
46 float *altitude = StateHandle_getHandle("Altitude").state;
47 float *velocity = StateHandle_getHandle("Velocity").state;
48 SlidingWindow *avgVel = StateHandle_getHandle("AvgVelBuffer").state;
49 SlidingWindow *avgPress = StateHandle_getHandle("AvgPressBuffer").state;
50
51 for (;;) {
52 // Block until 20ms interval
53 TickType_t xLastWakeTime = xTaskGetTickCount();
54 vTaskDelayUntil(&xLastWakeTime, xFrequency);
55
56 switch (*flightState) {
57 case PRELAUNCH:
58 if (accel->accelData[ZINDEX] >= ACCEL_LAUNCH) {
59 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_FLASH); // Enable flash
60 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_HIGHRES); // Enable high resolution data acquisition
61 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES); // Enable low resolution data acquisition
62 *flightState = LAUNCH;
63 }
64 break;
65
66 case LAUNCH:
67 avgVel->calculateMovingAverage(avgVel, &avgVelCurrent);
68 // Transition to motor burnout state on velocity decrease
69 if ((avgVelCurrent - avgVelPrevious) < 0) {
70 *flightState = COAST;
71 }
72 avgVelPrevious = avgVelCurrent;
73 break;
74
75 case COAST:
76 avgPress->calculateMovingAverage(avgPress, &avgPressCurrent);
77 // Transition to apogee state on three way vote of altitude, velocity, and tilt
78 // apogee is determined as two of three conditions evaluating true
79 if ((((avgPressCurrent - avgPressPrevious) > 0) + (*tilt >= 90) + (*velocity < 0.0f)) >= 2) {
80 *flightState = APOGEE;
81
82 union U {
83 TickType_t ticks;
84 uint8_t *bytes;
85 };
86 union U u;
87 u.ticks = xTaskGetTickCount();
88
89 // Log apogee event to flash
90 mem->append(mem, HEADER_EVENT_APOGEE);
91 mem->appendBytes(mem, u.bytes, sizeof(TickType_t));
92 // Send transmission to trigger apogee E-matches
93 }
94 avgPressPrevious = avgPressCurrent;
95 break;
96
97 case APOGEE:
98 // Deploy drogue chute
99 GPIOD->ODR |= 0x8000;
100 // Transition to descent state when below main deployment altitude
101 if (*altitude <= MAIN_ALTITUDE_METERS) {
102 *flightState = DESCENT;
103 // Add descent event dataframe to buffer
104 }
105 break;
106
107 case DESCENT:
108 // Handle descent state actions
109 break;
110 }
111 }
112}
113
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
float accelData[KX134_1211_DATA_COUNT]
Processed accelerations array.
Definition kx134_1211.h:61