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 BMP581 *baro = DeviceHandle_getHandle("Baro").device;
77
78 UART *usb = DeviceHandle_getHandle("USB").device;
79 DeviceHandle_t accelHandle = DeviceHandle_getHandle("Accel");
80 KX134_1211 *accel = accelHandle.device;
81
82 float *altitude = StateHandle_getHandle("Altitude").state;
83 float *cosine = StateHandle_getHandle("Cosine").state;
84 float *velocity = StateHandle_getHandle("Velocity").state;
85 SlidingWindow *avgVel = StateHandle_getHandle("AvgVelBuffer").state;
86 SlidingWindow *avgPress = StateHandle_getHandle("AvgPressBuffer").state;
87
88 for (;;) {
89 // Block until 20ms interval
90 TickType_t xLastWakeTime = xTaskGetTickCount();
91 vTaskDelayUntil(&xLastWakeTime, xFrequency);
92
93 // Update baro data
94#ifdef DUMMY
95 const unsigned long press_length = 0x00003A5C;
96 if (lDummyIdx < PRESS_LENGTH - 1) {
97 uint32_t tempPress = (uint32_t)press[lDummyIdx + 1] << 16 | press[lDummyIdx];
98 memcpy(&baro->press, &tempPress, sizeof(float));
99 lDummyIdx += 2;
100 }
101#else
102 taskENTER_CRITICAL();
103 baro->update(baro);
104 taskEXIT_CRITICAL();
105#endif
106
107 // Calculate altitude
108 *altitude = 44330 * (1.0 - pow(baro->press / baro->groundPress, 0.1903));
109
110 // Add sensor data and barometer data to dataframe
111 mem->append(mem, HEADER_LOWRES);
112 mem->appendBytes(mem, baro->rawTemp, BMP581_DATA_SIZE);
113 mem->appendBytes(mem, baro->rawPress, BMP581_DATA_SIZE);
114
115 // Only run calculations when enabled
116 EventBits_t uxBits = xEventGroupWaitBits(xTaskEnableGroup, GROUP_TASK_ENABLE_LOWRES, pdFALSE, pdFALSE, blockTime);
117 if (uxBits & GROUP_TASK_ENABLE_LOWRES) {
118 // Calculate state
119 z.pData[0] = *altitude;
120 z.pData[1] = (*cosine * 9.81 * accel->accelData[ZINDEX] - 9.81); // Acceleration measured in m/s^2
121 kf.update(&kf, &z);
122
123 *velocity = kf.x.pData[1];
124 avgPress->append(avgPress, baro->press);
125 avgVel->append(avgVel, *velocity);
126 }
127
128#ifdef DEBUG
131 if ((xSemaphoreTake(xUsbMutex, pdMS_TO_TICKS(0))) == pdTRUE) {
132 char debugStr[100];
133 snprintf(debugStr, 100, "[LDataAcq] %d\tBaro\tPressure: %.0f\tTemperature: %.1f\n\r", lDummyIdx / 2, baro->press, baro->temp);
134 xMessageBufferSend(xUsbTxBuff, (void *)debugStr, 100, pdMS_TO_TICKS(10));
135 xSemaphoreGive(xUsbMutex);
136 }
137#endif
138 }
139}
140
Struct definition for UART interface.
Definition uart.h:53
float accelData[KX134_1211_DATA_COUNT]
Processed accelerations array.
Definition kx134_1211.h:63
DeviceType device
Enum specifier for device type.
Definition spi.h:50