Loading...
Searching...
No Matches
ldataacquisition.c
1/************************************************************************
2 * @file lDataAcquisition.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "ldataacquisition.h"
10#include "math.h"
11#include "uart.h"
12
13extern EventGroupHandle_t xTaskEnableGroup;
14extern SemaphoreHandle_t xUsbMutex;
15extern MessageBufferHandle_t xUsbTxBuff;
16extern long lDummyIdx;
17char LdebugStr[100] = {};
18
19/* =============================================================================== */
35void vLDataAcquisition(void *argument) {
36 float dt = 0.020;
37 KalmanFilter kf;
38 KalmanFilter_init(&kf);
39
41 // Initialise filter parameters
42 float A[9] = {
43 1.0, dt, 0.5 * (dt * dt),
44 0.0, 1.0, dt,
45 0.0, 0.0, 1.0
46 };
47 kf.A.pData = A;
48 float Q[9] = {
49 99.52, 0.0, 0.0,
50 0.0, 1.42, 0.0,
51 0.0, 0.0, 6.27
52 };
53 kf.Q.pData = Q;
54 float R[4] = {
55 97.92, 0.0,
56 0.0, 0.61
57 };
58 kf.R.pData = R;
59 float P[9] = {
60 1, 0.0, 0.0,
61 0.0, 0.1, 0.0,
62 0.0, 0.0, 100.0
63 };
64 kf.P.pData = P;
65
66 // Initialise measurement matrix
67 arm_matrix_instance_f32 z;
68 float zData[2] = {0.0, 0.0};
69 arm_mat_init_f32(&z, 2, 1, zData);
70
71 TickType_t xLastWakeTime;
72 const TickType_t xFrequency = pdMS_TO_TICKS(20); // 50Hz
73 const TickType_t blockTime = pdMS_TO_TICKS(0);
74
75 MemBuff *mem = (MemBuff *)argument;
76 UART_t *usb = DeviceList_getDeviceHandle(DEVICE_UART_USB).device;
77 BMP581_t *baro = DeviceList_getDeviceHandle(DEVICE_BARO).device;
78 DeviceHandle_t accelHandle = DeviceList_getDeviceHandle(DEVICE_ACCEL);
79 KX134_1211_t *accel = accelHandle.device;
80
81 float *altitude = StateHandle_getHandle("Altitude").state;
82 float *cosine = StateHandle_getHandle("Cosine").state;
83 float *velocity = StateHandle_getHandle("Velocity").state;
84 SlidingWindow *avgVel = StateHandle_getHandle("AvgVelBuffer").state;
85 SlidingWindow *avgPress = StateHandle_getHandle("AvgPressBuffer").state;
86
87 for (;;) {
88 // Block until 20ms interval
89 TickType_t xLastWakeTime = xTaskGetTickCount();
90 vTaskDelayUntil(&xLastWakeTime, xFrequency);
91
92 // Update baro data
93 #ifdef DUMMY
94 const unsigned long press_length = 0x00003A5C;
95 if (lDummyIdx < PRESS_LENGTH - 1) {
96 uint32_t tempPress = (uint32_t)press[lDummyIdx + 1] << 16 | press[lDummyIdx];
97 memcpy(&baro->press, &tempPress, sizeof(float));
98 lDummyIdx += 2;
99 }
100 #else
101 taskENTER_CRITICAL();
102 baro->update(baro);
103 taskEXIT_CRITICAL();
104 #endif
105
106 // Calculate altitude
107 *altitude = 44330 * (1.0 - pow(baro->press / baro->groundPress, 0.1903));
108
109 // Add sensor data and barometer data to dataframe
110 mem->append(mem, HEADER_LOWRES);
111 mem->appendBytes(mem, baro->rawTemp, BMP581_DATA_SIZE);
112 mem->appendBytes(mem, baro->rawPress, BMP581_DATA_SIZE);
113
114 // Only run calculations when enabled
115 EventBits_t uxBits = xEventGroupWaitBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES, pdFALSE, pdFALSE, blockTime);
116 if (uxBits & GROUP_TASK_ENABLE_LOWRES) {
117 // Calculate state
118 z.pData[0] = *altitude;
119 z.pData[1] = (*cosine * 9.81 * accel->accelData[ZINDEX] - 9.81); // Acceleration measured in m/s^2
120 kf.update(&kf, &z);
121
122 *velocity = kf.x.pData[1];
123 avgPress->append(avgPress, baro->press);
124 avgVel->append(avgVel, *velocity);
125 }
126
127 #ifdef DEBUG
130 if ((xSemaphoreTake(xUsbMutex, pdMS_TO_TICKS(0))) == pdTRUE) {
131 char debugStr[100];
132 snprintf(debugStr, 100, "[LDataAcq] %d\tBaro\tPressure: %.0f\tTemperature: %.1f\n\r", lDummyIdx / 2, baro->press, baro->temp);
133 xMessageBufferSend(xUsbTxBuff, (void *)debugStr, 100, pdMS_TO_TICKS(10));
134 xSemaphoreGive(xUsbMutex);
135 }
136 #endif
137 }
138}
139
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
Struct definition for UART interface.
Definition uart.h:52
float accelData[KX134_1211_DATA_COUNT]
Processed accelerations array.
Definition kx134_1211.h:61