Loading...
Searching...
No Matches
hdataacquisition.c
1/***********************************************************************************
2 * @file hDataAcquisition.c *
3 * @author Matt Ricci *
4 * @addtogroup RTOS *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "hdataacquisition.h"
10
11extern long hDummyIdx;
12char HdebugStr[100] = {};
13
14extern EventGroupHandle_t xTaskEnableGroup;
15extern MessageBufferHandle_t xUsbTxBuff;
16extern SemaphoreHandle_t xUsbMutex;
17
18/* =============================================================================== */
33void vHDataAcquisition(void *argument) {
34 float dt = 0.002;
35
36 const TickType_t xFrequency = pdMS_TO_TICKS(2); // 500Hz
37 const TickType_t blockTime = pdMS_TO_TICKS(0);
38
39 // Devices
40 MemBuff *mem = (MemBuff *)argument;
41 KX134_1211 *hAccel = DeviceHandle_getHandle("HAccel").device;
42 KX134_1211 *lAccel = DeviceHandle_getHandle("LAccel").device;
43 A3G4250D *gyro = DeviceHandle_getHandle("Gyro").device;
44
45 // Selected accelerometer (high/low)
46 DeviceHandle_t accelHandle = DeviceHandle_getHandle("Accel");
47 KX134_1211 *accel = accelHandle.device;
48
49 // State variables
50 float *tilt = StateHandle_getHandle("Tilt").state;
51 float *cosine = StateHandle_getHandle("Cosine").state;
52 float *vLaunch = StateHandle_getHandle("LaunchVector").state;
53 float *vAttitude = StateHandle_getHandle("AttitudeVector").state;
54 Quaternion *qRot = StateHandle_getHandle("RotationQuaternion").state;
55
56 for (;;) {
57 // Block until 2ms interval
58 TickType_t xLastWakeTime = xTaskGetTickCount();
59 vTaskDelayUntil(&xLastWakeTime, xFrequency);
60
61 // Select which accelerometer to use
62 accelHandle.ref->device = (accel->accelData[ZINDEX] < 15) ? lAccel : hAccel;
63
64#ifdef DUMMY
65 // Load bearing definition???
66 const unsigned long accelX_length = 0x00007568;
67 /*
68 * Update sensor data with dummy values
69 * These arrays are defined in the files under /Data and are generated from
70 * past flight data binaries with srec_cat.
71 */
72 if (hDummyIdx < ACCELX_LENGTH - 1) {
73 // Shift in floating point values and add to processed accelerometer array
74 uint32_t tempX = (uint32_t)accelX[hDummyIdx + 1] << 16 | accelX[hDummyIdx];
75 uint32_t tempY = (uint32_t)accelY[hDummyIdx + 1] << 16 | accelY[hDummyIdx];
76 uint32_t tempZ = (uint32_t)accelZ[hDummyIdx + 1] << 16 | accelZ[hDummyIdx];
77 memcpy(&accel->accelData[0], &tempX, sizeof(float));
78 memcpy(&accel->accelData[1], &tempY, sizeof(float));
79 memcpy(&accel->accelData[2], &tempZ, sizeof(float));
80
81 // Back convert to raw data
82 uint16_t xRaw = (short)(accel->accelData[0] / accel->sensitivity);
83 uint16_t yRaw = (short)(accel->accelData[1] / accel->sensitivity);
84 uint16_t zRaw = (short)(accel->accelData[2] / accel->sensitivity);
85 accel->rawAccelData[0] = xRaw >> 8;
86 accel->rawAccelData[1] = xRaw;
87 accel->rawAccelData[2] = yRaw >> 8;
88 accel->rawAccelData[3] = yRaw;
89 accel->rawAccelData[4] = zRaw >> 8;
90 accel->rawAccelData[5] = zRaw;
91
92 // Shift in floating point values and add to processed gyroscope array
93 tempX = (uint32_t)gyroX[hDummyIdx + 1] << 16 | gyroX[hDummyIdx];
94 tempY = (uint32_t)gyroY[hDummyIdx + 1] << 16 | gyroY[hDummyIdx];
95 tempZ = (uint32_t)gyroZ[hDummyIdx + 1] << 16 | gyroZ[hDummyIdx];
96 memcpy(&gyro->gyroData[0], &tempX, sizeof(float));
97 memcpy(&gyro->gyroData[1], &tempY, sizeof(float));
98 memcpy(&gyro->gyroData[2], &tempZ, sizeof(float));
99
100 // Back convert to raw data
101 xRaw = (short)(gyro->gyroData[0] / gyro->sensitivity);
102 yRaw = (short)(gyro->gyroData[1] / gyro->sensitivity);
103 zRaw = (short)(gyro->gyroData[2] / gyro->sensitivity);
104 gyro->rawGyroData[0] = xRaw >> 8;
105 gyro->rawGyroData[1] = xRaw;
106 gyro->rawGyroData[2] = yRaw >> 8;
107 gyro->rawGyroData[3] = yRaw;
108 gyro->rawGyroData[4] = zRaw >> 8;
109 gyro->rawGyroData[5] = zRaw;
110
111 hDummyIdx += 2;
112 }
113#else
114 taskENTER_CRITICAL();
115 lAccel->update(lAccel);
116 hAccel->update(hAccel);
117 gyro->update(gyro);
118 taskEXIT_CRITICAL();
119#endif
120
121 // Add sensor data to dataframe
122 mem->append(mem, HEADER_HIGHRES);
123 mem->appendBytes(mem, accel->rawAccelData, KX134_1211_DATA_TOTAL);
124 mem->appendBytes(mem, gyro->rawGyroData, A3G4250D_DATA_TOTAL);
125
126 // Only run calculations when enabled
127 EventBits_t uxBits = xEventGroupWaitBits(xTaskEnableGroup, GROUP_TASK_ENABLE_HIGHRES, pdFALSE, pdFALSE, blockTime);
128 if (uxBits & GROUP_TASK_ENABLE_HIGHRES) {
129 // Integrate attitude quaternion from rotations
130 Quaternion qDot;
131 Quaternion_init(&qDot);
132 qDot.fromEuler(
133 &qDot,
134 (float)(dt * gyro->gyroData[ROLL_INDEX]),
135 (float)(dt * gyro->gyroData[PITCH_INDEX]),
136 (float)(dt * gyro->gyroData[YAW_INDEX])
137 );
138 *qRot = Quaternion_mul(qRot, &qDot);
139 qRot->normalise(qRot); // New attitude quaternion
140
141 // Apply rotation to z-axis unit vector
142 qRot->fRotateVector3D(qRot, vLaunch, vAttitude);
143
144 // Calculate tilt angle
145 // tilt = cos^-1(attitude ยท initial)
146 *cosine = vLaunch[0] * vAttitude[0] + vLaunch[1] * vAttitude[1] + vLaunch[2] * vAttitude[2];
147 *tilt = acos(*cosine) * 180 / M_PI;
148 }
149
150#ifdef DEBUG
153 if ((xSemaphoreTake(xUsbMutex, pdMS_TO_TICKS(0))) == pdTRUE) {
154 memset(HdebugStr, 100, sizeof(char));
155
156 snprintf(HdebugStr, 100, "[HDataAcq] %d\tAccel\tX: %.3f\tY: %.3f\tZ: %.3f\n\r", hDummyIdx / 2, accel->accelData[0], accel->accelData[1], accel->accelData[2]);
157 xMessageBufferSend(xUsbTxBuff, (void *)HdebugStr, 100, pdMS_TO_TICKS(0));
158
159 snprintf(HdebugStr, 100, "[HDataAcq] %d\tGyro\tX: %.3f\tY: %.3f\tZ: %.3f\n\r", hDummyIdx / 2, gyro->gyroData[0], gyro->gyroData[1], gyro->gyroData[2]);
160
161 xMessageBufferSend(xUsbTxBuff, (void *)HdebugStr, 100, pdMS_TO_TICKS(0));
162 xSemaphoreGive(xUsbMutex);
163 }
164#endif
165 }
166}
167
uint8_t rawGyroData[A3G4250D_DATA_TOTAL]
Raw gyro rates array.
Definition a3g4250d.h:49
float sensitivity
Gyroscope sensitivity.
Definition a3g4250d.h:42
void(* update)(struct A3G4250D *)
Gyro update method.
Definition a3g4250d.h:43
float gyroData[A3G4250D_DATA_COUNT]
Processed gyro rates array.
Definition a3g4250d.h:50
void(* update)(struct KX134_1211 *)
Accel update method.
Definition kx134_1211.h:56
float accelData[KX134_1211_DATA_COUNT]
Processed accelerations array.
Definition kx134_1211.h:63
uint8_t rawAccelData[KX134_1211_DATA_TOTAL]
Raw accelerations array.
Definition kx134_1211.h:62
float sensitivity
Accelerometer sensitivity.
Definition kx134_1211.h:55
DeviceType device
Enum specifier for device type.
Definition spi.h:50