-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimu.cpp
More file actions
216 lines (192 loc) · 5.97 KB
/
Copy pathimu.cpp
File metadata and controls
216 lines (192 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef IMU_CPP
#define IMU_CPP
#include "imu.h"
#include "api/Common.h"
#include "logger.h"
#include "wiring_private.h"
using namespace IMUHelpers;
Uart SerialImu(&sercom1, 12, 11, SERCOM_RX_PAD_3, UART_TX_PAD_0);
void SERCOM1_Handler() { SerialImu.IrqHandler(); }
IMUSensor::IMUSensor(bool launch, bool test) {
// if (!launch) {
// inactive = true;
// return; // if (!launch) {
// inactive = true;
// return;
// }
Logger::info("IMU init\n\r");
SerialImu.begin(115200);
delay(15);
pinPeripheral(11, PIO_SERCOM);
pinPeripheral(12, PIO_SERCOM);
}
void IMUSensor::imuCrc16Update(uint16_t *currentCrc, const uint8_t *src,
uint32_t lengthInBytes) {
uint32_t crc = *currentCrc;
uint32_t j;
for (j = 0; j < lengthInBytes; ++j) {
uint32_t i;
uint32_t byte = src[j];
crc ^= byte << 8;
for (i = 0; i < 8; ++i) {
uint32_t temp = crc << 1;
if (crc & 0x8000) {
temp ^= 0x1021;
}
crc = temp;
}
}
*currentCrc = crc;
}
/**
* @brief fuck...
*
*
* @param c
*/
void IMUSensor::imuPacketDecode(uint8_t c) {
static uint16_t CRCReceived = 0; /* CRC value received from a frame */
static uint16_t CRCCalculated = 0; /* CRC value caluated from a frame */
static uint8_t status = kStatus_Idle; /* state machine */
static uint8_t crc_header[4] = {0x5A, 0xA5, 0x00, 0x00};
// SerialUSB.println(String(c, 2));
switch (status) {
case kStatus_Idle:
// SerialUSB.println("k_Idle");
if (c == 0x5A)
status = kStatus_Cmd;
break;
case kStatus_Cmd:
// SerialUSB.println("k_Cmd");
RxPkt.type = c;
if (c == 0xA5) {
status = kStatus_LenLow;
} else {
status = kStatus_Idle;
}
break;
case kStatus_LenLow:
// SerialUSB.println("k_LenLow");
RxPkt.payload_len = c;
crc_header[2] = c;
status = kStatus_LenHigh;
break;
case kStatus_LenHigh:
// SerialUSB.println("k_LenHigh");
RxPkt.payload_len |= (c << 8);
crc_header[3] = c;
status = kStatus_CRCLow;
break;
case kStatus_CRCLow:
// SerialUSB.println("k_crcLow");
CRCReceived = c;
status = kStatus_CRCHigh;
break;
case kStatus_CRCHigh:
// SerialUSB.println("k_crcHigh");
CRCReceived |= (c << 8);
RxPkt.ofs = 0;
CRCCalculated = 0;
status = kStatus_Data;
break;
case kStatus_Data:
// SerialUSB.println("k_Data");
if (RxPkt.ofs >= IMU_MAX_PACKET_LEN) {
status = kStatus_Idle;
RxPkt.ofs = 0;
CRCCalculated = 0;
break;
}
RxPkt.buf[RxPkt.ofs++] = c;
if (RxPkt.ofs >= RxPkt.payload_len && RxPkt.type == 0xA5) {
imuCrc16Update(&CRCCalculated, crc_header, 4);
imuCrc16Update(&CRCCalculated, RxPkt.buf, RxPkt.ofs);
/* CRC match */
if (CRCCalculated == CRCReceived) {
imuUpdateEuler(&RxPkt);
} else {
Logger::error(F("IMU CRC mismatch: "));
Logger::error(String(CRCReceived, HEX) + " vs " +
String(CRCCalculated, HEX) + "\n\r");
}
status = kStatus_Idle;
}
break;
default:
// SerialUSB.println("k_Default");
status = kStatus_Idle;
break;
}
// SerialUSB.println("end");
}
void IMUSensor::imuUpdateEuler(Packet_t *pkt) {
if (pkt->buf[0] == kItemID) /* user ID */
{
ID = pkt->buf[1];
}
if (pkt->buf[2] == kItemAccRaw) /* Acc raw value */
{
memcpy(AccelRaw, (uint8_t *)pkt->buf[3], 6);
Accel[0] = AccelRaw[0] * .001;
Accel[1] = AccelRaw[1] * .001;
Accel[2] = AccelRaw[2] * .001;
}
if (pkt->buf[9] == kItemGyoRaw) /* gyro raw value */
{
memcpy(GyroRaw, (uint8_t *)pkt->buf[10], 6);
}
if (pkt->buf[16] == kItemMagRaw) /* mag raw value */
{
memcpy(MagnetRaw, (uint8_t *)pkt->buf[17], 6);
}
if (pkt->buf[23] == kItemAtdE) /* atd E */
{
float val =
((float)(int16_t)(pkt->buf[24] + (pkt->buf[25] << 8))) / 100;
if (!isnan(val)) {
Euler[0] = val;
if (!calibrated) {
EulerCal[0] = -val;
Logger::debug("calibrating value 0 to " + String(-val));
}
}
val = ((float)(int16_t)(pkt->buf[26] + (pkt->buf[27] << 8))) / 100;
if (!isnan(val)) {
Euler[1] = val;
if (!calibrated) {
EulerCal[1] = -val;
Logger::debug("calibrating value 1 to " + String(-val));
}
}
val = ((float)(int16_t)(pkt->buf[28] + (pkt->buf[29] << 8))) / 10;
if (!isnan(val)) {
Euler[2] = val;
if (!calibrated) {
EulerCal[2] = -val;
Logger::debug("calibrating value 2 to " + String(-val));
}
}
if (!calibrated)
calibrated = true;
}
}
void IMUSensor::update() {
if (SerialImu.available()) {
while (SerialImu.available()) {
char ch = SerialImu.read();
this->imuPacketDecode(ch);
}
}
}
void IMUSensor::end() { SerialImu.end(); }
// axes swap: pitch is roll
float IMUSensor::getPitch() { return Euler[0] + EulerCal[0]; }
float IMUSensor::getRoll() { return Euler[1] + EulerCal[1]; }
float IMUSensor::getYaw() { return Euler[2] + EulerCal[2]; }
float IMUSensor::getPitchCal() { return EulerCal[0]; }
float IMUSensor::getRollCal() { return EulerCal[1]; }
float IMUSensor::getYawCal() { return EulerCal[2]; }
float IMUSensor::getAccel0() { return Accel[0]; }
float IMUSensor::getAccel1() { return Accel[1]; }
float IMUSensor::getAccel2() { return Accel[2]; }
#endif