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