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