11#include "message_buffer.h"
12#include "event_groups.h"
14#include "devicelist.h"
23#include "stateupdate.h"
25#include "AustralisConfig.h"
27extern EventGroupHandle_t xTaskEnableGroup;
28EventGroupHandle_t xFlightStateGroup;
42void vStateUpdate(
void *argument) {
43 const TickType_t xFrequency = pdMS_TO_TICKS(20);
45 xFlightStateGroup = xEventGroupCreate();
46 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_PRELAUNCH);
48 float avgPressCurrent = 0;
49 float avgPressPrevious = 0;
50 float avgVelCurrent = 0;
51 float avgVelPrevious = 0;
56 Accel_t *accel = accelHandle.device;
60 TickType_t xLastWakeTime = xTaskGetTickCount();
61 vTaskDelayUntil(&xLastWakeTime, xFrequency);
63 switch (state->flightState) {
65 if (accel->
accelData[ZINDEX] >= ACCEL_LAUNCH) {
66 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_FLASH);
67 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_HIGHRES);
68 xEventGroupSetBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES);
69 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_LAUNCH);
70 state->flightState =
LAUNCH;
75 state->avgVel.calculateMovingAverage(&state->avgVel, &avgVelCurrent);
77 if ((avgVelCurrent - avgVelPrevious) < 0) {
78 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_COAST);
79 state->flightState =
COAST;
81 avgVelPrevious = avgVelCurrent;
85 state->avgPress.calculateMovingAverage(&state->avgPress, &avgPressCurrent);
88 if ((((avgPressCurrent - avgPressPrevious) > 0) + (state->tilt >= 90) + (state->velocity < 0.0f)) >= 2) {
89 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_APOGEE);
90 state->flightState =
APOGEE;
93 state->mem.appendBytes(&state->mem, (uint8_t[]){HEADER_EVENT_APOGEE, 0xB0, 0x0B}, 3);
94 state->mem.appendBytes(
96 (uint8_t *)&state->flightTimeMs,
97 sizeof(state->flightTimeMs)
102 avgPressPrevious = avgPressCurrent;
109 if (state->altitude <= MAIN_ALTITUDE_METERS) {
110 xEventGroupSetBits(xFlightStateGroup, FLIGHT_STATE_BIT_DESCENT);
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.
@ APOGEE
At least 2: velocity negative, pressure increasing, tilt > 90 degrees.
@ PRELAUNCH
Initial boot condition.
@ LAUNCH
Body reference Z-axis acceleration above threshold.
@ COAST
Global reference Z-axis velocity decreasing.
@ DESCENT
Altitude below main threshold.