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