Loading...
Searching...
No Matches
ldataacquisition.c
1/************************************************************************
2 * @file lDataAcquisition.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "math.h"
10
11#include "FreeRTOS.h"
12#include "event_groups.h"
13#include "message_buffer.h"
14#include "semphr.h"
15
16#include "dataframe.h"
17#include "groups.h"
18
19#include "devicelist.h"
20#include "kalmanfilter.h"
21#include "membuff.h"
22#include "sensors.h"
23#include "barometer.h"
24#include "accelerometer.h"
25#include "state.h"
26#include "uart.h"
27
28#include "ldataacquisition.h"
29
30extern EventGroupHandle_t xTaskEnableGroup;
31extern SemaphoreHandle_t xUsbMutex;
32extern MessageBufferHandle_t xUsbTxBuff;
33/* =============================================================================== */
49void vLDataAcquisition(void *argument) {
50 float dt = 0.020;
51 KalmanFilter kf;
52 KalmanFilter_init(&kf);
53
55 // Initialise filter parameters
56 float A[9] = {
57 1.0, dt, 0.5 * (dt * dt),
58 0.0, 1.0, dt,
59 0.0, 0.0, 1.0
60 };
61 kf.A.pData = A;
62 float Q[9] = {
63 99.52, 0.0, 0.0,
64 0.0, 1.42, 0.0,
65 0.0, 0.0, 6.27
66 };
67 kf.Q.pData = Q;
68 float R[4] = {
69 97.92, 0.0,
70 0.0, 0.61
71 };
72 kf.R.pData = R;
73 float P[9] = {
74 1, 0.0, 0.0,
75 0.0, 0.1, 0.0,
76 0.0, 0.0, 100.0
77 };
78 kf.P.pData = P;
79
80 // Initialise measurement matrix
81 arm_matrix_instance_f32 z;
82 float zData[2] = {0.0, 0.0};
83 arm_mat_init_f32(&z, 2, 1, zData);
84
85 TickType_t xLastWakeTime;
86 const TickType_t xFrequency = pdMS_TO_TICKS(20); // 50Hz
87 const TickType_t blockTime = pdMS_TO_TICKS(0);
88
89 UART_t *usb = DeviceList_getDeviceHandle(DEVICE_UART_USB).device;
90 Baro_t *baro = DeviceList_getDeviceHandle(DEVICE_BARO).device;
91 Accel_t *accel = DeviceList_getDeviceHandle(DEVICE_ACCEL).device;
92
93 State *state = State_getState();
94
95 for (;;) {
96 // Block until 20ms interval
97 TickType_t xLastWakeTime = xTaskGetTickCount();
98 vTaskDelayUntil(&xLastWakeTime, xFrequency);
99
100 taskENTER_CRITICAL();
101 baro->update(baro);
102 taskEXIT_CRITICAL();
103
104 // Calculate altitude
105 state->altitude = 44330 * (1.0 - pow(baro->press / baro->groundPress, 0.1903));
106
107 // Add sensor data and barometer data to dataframe
108 state->mem.append(&state->mem, HEADER_LOWRES);
109 state->mem.appendBytes(&state->mem, baro->rawTemp, baro->tempDataSize);
110 state->mem.appendBytes(&state->mem, baro->rawPress, baro->pressDataSize);
111
112 // Only run calculations when enabled
113 EventBits_t uxBits = xEventGroupWaitBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES, pdFALSE, pdFALSE, blockTime);
114 if (uxBits & GROUP_TASK_ENABLE_LOWRES) {
115 // Calculate state
116 z.pData[0] = state->altitude;
117 z.pData[1] = (state->cosine * 9.81 * accel->accelData[ZINDEX] - 9.81); // Acceleration measured in m/s^2
118 kf.update(&kf, &z);
119
120 state->velocity = kf.x.pData[1];
121 state->avgPress.append(&state->avgPress, baro->press);
122 state->avgVel.append(&state->avgVel, state->velocity);
123 }
124 }
125}
126
Defines the API for the Accelerometer sensor.
Defines the API for the Barometer sensor.
float * accelData
Pointer to driver defined data array.
uint8_t * rawTemp
Pointer to driver defined raw temperature data array.
Definition barometer.h:112
uint8_t pressDataSize
Size of raw pressure data in bytes.
Definition barometer.h:109
float press
Last read processed pressure value.
Definition barometer.h:114
float groundPress
Stored ground pressure reading.
Definition barometer.h:113
uint8_t tempDataSize
Size of raw temperature data in bytes.
Definition barometer.h:110
uint8_t * rawPress
Pointer to driver defined raw pressure data array.
Definition barometer.h:111
void(* update)(struct Baro *baro)
Pointer to update method.
Definition barometer.h:25
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
State * State_getState()
Definition state.c:69
State variable struct.
Definition state.h:36
Struct definition for UART interface.
Definition uart.h:132