blob: d834f3329b44cfd753f69d6d323c9e6a555e8c76 [file] [log] [blame]
Brian Silverman5f17a972016-02-28 01:49:32 -05001#include "frc971/wpilib/ADIS16448.h"
2
3#include "InterruptableSensorBase.h"
4#undef ERROR
5
6#include <inttypes.h>
7#include <math.h>
8
9#include "aos/common/logging/queue_logging.h"
10#include "aos/common/time.h"
11#include "aos/linux_code/init.h"
12
13#include "frc971/wpilib/imu.q.h"
14
15namespace frc971 {
16namespace wpilib {
17
18template <uint8_t size>
19bool ADIS16448::DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]) {
20 switch (spi_->Transaction(to_send, to_receive, size)) {
21 case -1:
22 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", size);
23 return false;
24 case size:
25 return true;
26 default:
27 LOG(FATAL, "SPI::Transaction returned something weird\n");
28 }
29}
30
31namespace {
32
33// Addresses pulled out of the documentation.
34constexpr uint8_t kMscCtrlAddress = 0x34;
35constexpr uint8_t kSmplPrdAddress = 0x36;
36constexpr uint8_t kDiagStatAddress = 0x3C;
37constexpr uint8_t kGlobalReadAddress = 0x3E;
38constexpr uint8_t kLotId1Address = 0x52;
39constexpr uint8_t kLotId2Address = 0x54;
40constexpr uint8_t kProdIdAddress = 0x56;
41constexpr uint8_t kSerialNumberAddress = 0x58;
42
43// LSB/degree/second for the gyros.
44constexpr double kGyroLsbDegreeSecond = 25;
45// LSB/G for the accelerometers.
46constexpr double kAccelerometerLsbG = 1200;
47// LSB/gauss for the magnetometers.
48constexpr double kMagnetometerLsbGauss = 7.0 / 1000.0 /* mgauss to gauss */;
49// LSB/bar for the barometer.
50constexpr double kBarometerLsbPascal = 1.0 / (20.0 / 10 /* ubar to pascals */);
51// LSB/degree C for the temperature sensor.
52constexpr double kTemperatureLsbDegree = 1.0 / 0.07386;
53// Degrees C corresponding to 0 for the temperature sensor.
54constexpr double kTemperatureZero = 31;
55
56// From somebody online who says this works with the sensor. I don't feel like
57// re-deriving this, and I can't find what all the CRC parameters are supposed
58// to be.
59const uint16_t kCrcTable[256] = {
60 0x0000, 0x17CE, 0x0FDF, 0x1811, 0x1FBE, 0x0870, 0x1061, 0x07AF, 0x1F3F,
61 0x08F1, 0x10E0, 0x072E, 0x0081, 0x174F, 0x0F5E, 0x1890, 0x1E3D, 0x09F3,
62 0x11E2, 0x062C, 0x0183, 0x164D, 0x0E5C, 0x1992, 0x0102, 0x16CC, 0x0EDD,
63 0x1913, 0x1EBC, 0x0972, 0x1163, 0x06AD, 0x1C39, 0x0BF7, 0x13E6, 0x0428,
64 0x0387, 0x1449, 0x0C58, 0x1B96, 0x0306, 0x14C8, 0x0CD9, 0x1B17, 0x1CB8,
65 0x0B76, 0x1367, 0x04A9, 0x0204, 0x15CA, 0x0DDB, 0x1A15, 0x1DBA, 0x0A74,
66 0x1265, 0x05AB, 0x1D3B, 0x0AF5, 0x12E4, 0x052A, 0x0285, 0x154B, 0x0D5A,
67 0x1A94, 0x1831, 0x0FFF, 0x17EE, 0x0020, 0x078F, 0x1041, 0x0850, 0x1F9E,
68 0x070E, 0x10C0, 0x08D1, 0x1F1F, 0x18B0, 0x0F7E, 0x176F, 0x00A1, 0x060C,
69 0x11C2, 0x09D3, 0x1E1D, 0x19B2, 0x0E7C, 0x166D, 0x01A3, 0x1933, 0x0EFD,
70 0x16EC, 0x0122, 0x068D, 0x1143, 0x0952, 0x1E9C, 0x0408, 0x13C6, 0x0BD7,
71 0x1C19, 0x1BB6, 0x0C78, 0x1469, 0x03A7, 0x1B37, 0x0CF9, 0x14E8, 0x0326,
72 0x0489, 0x1347, 0x0B56, 0x1C98, 0x1A35, 0x0DFB, 0x15EA, 0x0224, 0x058B,
73 0x1245, 0x0A54, 0x1D9A, 0x050A, 0x12C4, 0x0AD5, 0x1D1B, 0x1AB4, 0x0D7A,
74 0x156B, 0x02A5, 0x1021, 0x07EF, 0x1FFE, 0x0830, 0x0F9F, 0x1851, 0x0040,
75 0x178E, 0x0F1E, 0x18D0, 0x00C1, 0x170F, 0x10A0, 0x076E, 0x1F7F, 0x08B1,
76 0x0E1C, 0x19D2, 0x01C3, 0x160D, 0x11A2, 0x066C, 0x1E7D, 0x09B3, 0x1123,
77 0x06ED, 0x1EFC, 0x0932, 0x0E9D, 0x1953, 0x0142, 0x168C, 0x0C18, 0x1BD6,
78 0x03C7, 0x1409, 0x13A6, 0x0468, 0x1C79, 0x0BB7, 0x1327, 0x04E9, 0x1CF8,
79 0x0B36, 0x0C99, 0x1B57, 0x0346, 0x1488, 0x1225, 0x05EB, 0x1DFA, 0x0A34,
80 0x0D9B, 0x1A55, 0x0244, 0x158A, 0x0D1A, 0x1AD4, 0x02C5, 0x150B, 0x12A4,
81 0x056A, 0x1D7B, 0x0AB5, 0x0810, 0x1FDE, 0x07CF, 0x1001, 0x17AE, 0x0060,
82 0x1871, 0x0FBF, 0x172F, 0x00E1, 0x18F0, 0x0F3E, 0x0891, 0x1F5F, 0x074E,
83 0x1080, 0x162D, 0x01E3, 0x19F2, 0x0E3C, 0x0993, 0x1E5D, 0x064C, 0x1182,
84 0x0912, 0x1EDC, 0x06CD, 0x1103, 0x16AC, 0x0162, 0x1973, 0x0EBD, 0x1429,
85 0x03E7, 0x1BF6, 0x0C38, 0x0B97, 0x1C59, 0x0448, 0x1386, 0x0B16, 0x1CD8,
86 0x04C9, 0x1307, 0x14A8, 0x0366, 0x1B77, 0x0CB9, 0x0A14, 0x1DDA, 0x05CB,
87 0x1205, 0x15AA, 0x0264, 0x1A75, 0x0DBB, 0x152B, 0x02E5, 0x1AF4, 0x0D3A,
88 0x0A95, 0x1D5B, 0x054A, 0x1284};
89
90uint16_t CalculateCrc(const uint8_t *data, size_t data_length) {
91 uint16_t crc = 0xFFFF;
92 uint16_t byte;
93
94 while (data_length--) {
95 // Compute lower byte CRC first.
96 byte = data[1];
97 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
98 // Compute upper byte of CRC.
99 byte = data[0];
100 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
101 data += 2;
102 }
103 crc = ~crc; // Compute complement of CRC
104 return static_cast<uint16_t>(
105 (crc << 8) | (crc >> 8)); // Perform byte swap prior to returning CRC;
106}
107
108} // namespace
109
110ADIS16448::ADIS16448(SPI::Port port, DigitalInput *dio1)
111 : spi_(new SPI(port)), dio1_(dio1) {
112 // 1MHz is the maximum supported for burst reads.
113 spi_->SetClockRate(1e6);
114 spi_->SetChipSelectActiveLow();
115 spi_->SetClockActiveLow();
116 spi_->SetSampleDataOnFalling();
117 spi_->SetMSBFirst();
118
119 dio1_->RequestInterrupts();
120 dio1_->SetUpSourceEdge(true, false);
121}
122
123void ADIS16448::operator()() {
124 ::aos::SetCurrentThreadName("IMU");
125
126 // Try to initialize repeatedly as long as we're supposed to be running.
127 while (run_ && !Initialize()) {
128 ::aos::time::SleepFor(::aos::time::Time::InMS(50));
129 }
130 LOG(INFO, "IMU initialized successfully\n");
131
132 bool got_an_interrupt = false;
133 while (run_) {
134 {
135 const InterruptableSensorBase::WaitResult result =
136 dio1_->WaitForInterrupt(0.1, !got_an_interrupt);
137 if (result == InterruptableSensorBase::kTimeout) {
138 LOG(WARNING, "IMU read timed out\n");
139 continue;
140 }
141 }
142 got_an_interrupt = true;
143 const ::aos::time::Time read_time = ::aos::time::Time::Now();
144
145 uint8_t to_send[8 * 2 * 14], to_receive[8 * 2 * 14];
146 to_send[0] = kGlobalReadAddress;
147 memset(&to_send[1], 0, sizeof(to_send) - 1);
148 if (!DoTransaction<8 * 2 * 14>(to_send, to_receive)) continue;
149
150 // If it's false now or another edge happened, then we're in trouble. This
151 // won't catch all instances of being a little bit slow (because of the
152 // interrupt delay among other things), but it will catch the code
153 // constantly falling behind, which seems like the most likely failure
154 // scenario.
155 if (!dio1_->Get() ||
156 dio1_->WaitForInterrupt(0, false) !=
157 InterruptableSensorBase::kTimeout) {
158 LOG(ERROR, "IMU read took too long\n");
159 continue;
160 }
161
162 {
163 const uint16_t calculated_crc = CalculateCrc(&to_receive[4], 11);
164 uint16_t received_crc;
165 memcpy(&received_crc, &to_receive[13], 2);
166 if (received_crc != calculated_crc) {
167 LOG(WARNING, "received CRC %" PRIx16 " but calculated %" PRIx16 "\n",
168 received_crc, calculated_crc);
169 continue;
170 }
171 }
172
173 {
174 uint16_t diag_stat;
175 memcpy(&diag_stat, &to_receive[1], 2);
176 if (!CheckDiagStatValue(diag_stat)) continue;
177 }
178
179 auto message = imu_values.MakeMessage();
180 message->fpga_timestamp = dio1_->ReadRisingTimestamp();
181 message->monotonic_timestamp_ns = read_time.ToNSec();
182
183 message->gyro_x =
184 ConvertValue(&to_receive[2], kGyroLsbDegreeSecond) * (M_PI / 2.0);
185 message->gyro_y =
186 ConvertValue(&to_receive[3], kGyroLsbDegreeSecond) * (M_PI / 2.0);
187 message->gyro_z =
188 ConvertValue(&to_receive[4], kGyroLsbDegreeSecond) * (M_PI / 2.0);
189
190 message->accelerometer_x = ConvertValue(&to_receive[5], kAccelerometerLsbG);
191 message->accelerometer_y = ConvertValue(&to_receive[6], kAccelerometerLsbG);
192 message->accelerometer_z = ConvertValue(&to_receive[7], kAccelerometerLsbG);
193
194 message->magnetometer_x =
195 ConvertValue(&to_receive[8], kMagnetometerLsbGauss);
196 message->magnetometer_y =
197 ConvertValue(&to_receive[9], kMagnetometerLsbGauss);
198 message->magnetometer_z =
199 ConvertValue(&to_receive[10], kMagnetometerLsbGauss);
200
201 message->barometer =
202 ConvertValue(&to_receive[11], kBarometerLsbPascal, false);
203
204 message->temperature =
205 ConvertValue(&to_receive[12], kTemperatureLsbDegree) + kTemperatureZero;
206
207 LOG_STRUCT(DEBUG, "sending", *message);
208 if (!message.Send()) {
209 LOG(WARNING, "sending queue message failed\n");
210 }
211 }
212}
213
214float ADIS16448::ConvertValue(uint8_t *data, double lsb_per_output, bool sign) {
215 double value;
216 if (sign) {
217 int16_t raw_value;
218 memcpy(&raw_value, data, 2);
219 value = raw_value;
220 } else {
221 uint16_t raw_value;
222 memcpy(&raw_value, data, 2);
223 value = raw_value;
224 }
225 return value * lsb_per_output;
226}
227
228bool ADIS16448::ReadRegister(uint8_t next_address, uint16_t *value) {
229 uint8_t to_send[2], to_receive[2];
230 to_send[0] = next_address;
231 to_send[1] = 0;
232
233 if (!DoTransaction<2>(to_send, to_receive)) return false;
234
235 if (value) memcpy(value, to_receive, 2);
236 return true;
237}
238
239bool ADIS16448::WriteRegister(uint8_t address, uint16_t value) {
240 uint8_t to_send[4], to_receive[4];
241 to_send[0] = address | 0x80;
242 to_send[1] = value & 0xFF;
243 to_send[2] = address | 0x81;
244 to_send[3] = value >> 8;
245 if (!DoTransaction<4>(to_send, to_receive)) return false;
246 return true;
247}
248
249bool ADIS16448::CheckDiagStatValue(uint16_t value) const {
250 bool r = true;
251 if (value & (1 << 0)) {
252 LOG(WARNING, "IMU gave magnetometer functional test failure\n");
253 }
254 if (value & (1 << 1)) {
255 LOG(WARNING, "IMU gave barometer functional test failure\n");
256 }
257 if (value & (1 << 2)) {
258 LOG(WARNING, "IMU gave flash update failure\n");
259 }
260 if (value & (1 << 3)) {
261 LOG(WARNING, "IMU gave SPI communication failure\n");
262 }
263 if (value & (1 << 4)) {
264 LOG(WARNING, "IMU gave sensor overrange\n");
265 }
266 if (value & (1 << 5)) {
267 LOG(WARNING, "IMU gave self-test failure\n");
268 r = false;
269 }
270 if (value & (1 << 6)) {
271 LOG(WARNING, "IMU gave flash test checksum failure\n");
272 }
273 if (value & (1 << 8)) {
274 LOG(WARNING, "IMU says alarm 1 is active\n");
275 }
276 if (value & (1 << 9)) {
277 LOG(WARNING, "IMU says alarm 2 is active\n");
278 }
279 if (value & (1 << 10)) {
280 LOG(WARNING, "IMU gave X-axis gyro self-test failure\n");
281 r = false;
282 }
283 if (value & (1 << 11)) {
284 LOG(WARNING, "IMU gave Y-axis gyro self-test failure\n");
285 r = false;
286 }
287 if (value & (1 << 12)) {
288 LOG(WARNING, "IMU gave Z-axis gyro self-test failure\n");
289 r = false;
290 }
291 if (value & (1 << 13)) {
292 LOG(WARNING, "IMU gave X-axis accelerometer self-test failure\n");
293 r = false;
294 }
295 if (value & (1 << 14)) {
296 LOG(WARNING, "IMU gave Y-axis accelerometer self-test failure\n");
297 r = false;
298 }
299 if (value & (1 << 15)) {
300 LOG(WARNING, "IMU gave Z-axis accelerometer self-test failure\n");
301 r = false;
302 }
303 return r;
304}
305
306bool ADIS16448::Initialize() {
307 if (!ReadRegister(kProdIdAddress, nullptr)) return false;
308 uint16_t product_id;
309 if (!ReadRegister(kLotId1Address, &product_id)) return false;
310 if (product_id != 0x4040) {
311 LOG(ERROR, "product ID is %" PRIx16 " instead of 0x4040\n", product_id);
312 return false;
313 }
314
315 uint16_t lot_id1, lot_id2, serial_number;
316 if (!ReadRegister(kLotId2Address, &lot_id1)) return false;
317 if (!ReadRegister(kSerialNumberAddress, &lot_id2)) return false;
318 if (!ReadRegister(0, &serial_number)) return false;
319 LOG(INFO, "have IMU %" PRIx16 "%" PRIx16 ": %" PRIx16 "\n", lot_id1, lot_id2,
320 serial_number);
321
322 // Divide the sampling by 2^2 = 4 to get 819.2 / 4 = 204.8 Hz.
323 if (!WriteRegister(kSmplPrdAddress, 2 << 8)) return false;
324
325 // Start a self test.
326 if (!WriteRegister(kMscCtrlAddress, 1 << 10)) return false;
327 // Wait for the self test to finish.
328 {
329 uint16_t value;
330 do {
331 ::aos::time::SleepFor(::aos::time::Time::InMS(10));
332 if (!ReadRegister(kMscCtrlAddress, &value)) return false;
333 } while ((value & (1 << 10)) != 0);
334 }
335
336 if (!ReadRegister(kDiagStatAddress, nullptr)) return false;
337 uint16_t diag_stat;
338 if (!ReadRegister(0, &diag_stat)) return false;
339 if (!CheckDiagStatValue(diag_stat)) return false;
340
341 if (!WriteRegister(kMscCtrlAddress,
342 ((0 << 0) | // DIO1
343 (1 << 1) | // DIO goes high when data is valid
344 (1 << 2) | // enable DIO changing when data is vald
345 (1 << 4)) // enable CRC16 for burst mode
346 )) {
347 return false;
348 }
349 return true;
350}
351
352} // namespace wpilib
353} // namespace frc971