blob: 4fc2c7b3bc27a8b90038ac34a77d6b478bfbd79c [file] [log] [blame]
Brian Silverman5f17a972016-02-28 01:49:32 -05001#include "frc971/wpilib/ADIS16448.h"
2
Parker Schuhd3b7a8872018-02-19 16:42:27 -08003#include "frc971/wpilib/ahal/InterruptableSensorBase.h"
Brian Silverman5f17a972016-02-28 01:49:32 -05004#undef ERROR
5
6#include <inttypes.h>
7#include <math.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08008#include <chrono>
Brian Silverman5f17a972016-02-28 01:49:32 -05009
James Kuszmaul651fc3f2019-05-15 21:14:25 -070010#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/queue_logging.h"
12#include "aos/robot_state/robot_state.q.h"
13#include "aos/time/time.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050014#include "frc971/wpilib/imu.q.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000015#include "frc971/zeroing/averager.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050016
17namespace frc971 {
18namespace wpilib {
19
Austin Schuhf2a50ba2016-12-24 16:16:26 -080020using ::aos::monotonic_clock;
21namespace chrono = ::std::chrono;
22
Brian Silverman5f17a972016-02-28 01:49:32 -050023template <uint8_t size>
24bool ADIS16448::DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]) {
Brian Silverman003a4732018-03-11 14:02:15 -070025 rx_clearer_.ClearRxFifo();
Brian Silverman5f17a972016-02-28 01:49:32 -050026 switch (spi_->Transaction(to_send, to_receive, size)) {
27 case -1:
Austin Schuhf257f3c2019-10-27 21:00:43 -070028 AOS_LOG(INFO, "SPI::Transaction of %zd bytes failed\n", size);
Brian Silverman5f17a972016-02-28 01:49:32 -050029 return false;
30 case size:
Brian Silvermana70994f2017-03-16 22:32:55 -070031 if (dummy_spi_) {
32 uint8_t dummy_send, dummy_receive;
33 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
34 }
Brian Silverman5f17a972016-02-28 01:49:32 -050035 return true;
36 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -070037 AOS_LOG(FATAL, "SPI::Transaction returned something weird\n");
Brian Silverman5f17a972016-02-28 01:49:32 -050038 }
39}
40
41namespace {
42
43// Addresses pulled out of the documentation.
44constexpr uint8_t kMscCtrlAddress = 0x34;
45constexpr uint8_t kSmplPrdAddress = 0x36;
46constexpr uint8_t kDiagStatAddress = 0x3C;
47constexpr uint8_t kGlobalReadAddress = 0x3E;
48constexpr uint8_t kLotId1Address = 0x52;
49constexpr uint8_t kLotId2Address = 0x54;
50constexpr uint8_t kProdIdAddress = 0x56;
51constexpr uint8_t kSerialNumberAddress = 0x58;
52
Austin Schuh871a1362016-04-02 12:25:00 -070053// degree/second/LSB for the gyros.
54constexpr double kGyroLsbDegreeSecond = 1.0 / 25.0;
55// G/LSB for the accelerometers.
56constexpr double kAccelerometerLsbG = 1.0 / 1200.0;
57// gauss/LSB for the magnetometers.
58constexpr double kMagnetometerLsbGauss =
59 1.0 / (7.0 / 1000.0) /* mgauss to gauss */;
60// bar/LSB for the barometer.
61constexpr double kBarometerLsbPascal = 0.02 * 100;
62// degree/LSB C for the temperature sensor.
63constexpr double kTemperatureLsbDegree = 0.07386;
Brian Silverman5f17a972016-02-28 01:49:32 -050064// Degrees C corresponding to 0 for the temperature sensor.
65constexpr double kTemperatureZero = 31;
66
67// From somebody online who says this works with the sensor. I don't feel like
68// re-deriving this, and I can't find what all the CRC parameters are supposed
69// to be.
70const uint16_t kCrcTable[256] = {
71 0x0000, 0x17CE, 0x0FDF, 0x1811, 0x1FBE, 0x0870, 0x1061, 0x07AF, 0x1F3F,
72 0x08F1, 0x10E0, 0x072E, 0x0081, 0x174F, 0x0F5E, 0x1890, 0x1E3D, 0x09F3,
73 0x11E2, 0x062C, 0x0183, 0x164D, 0x0E5C, 0x1992, 0x0102, 0x16CC, 0x0EDD,
74 0x1913, 0x1EBC, 0x0972, 0x1163, 0x06AD, 0x1C39, 0x0BF7, 0x13E6, 0x0428,
75 0x0387, 0x1449, 0x0C58, 0x1B96, 0x0306, 0x14C8, 0x0CD9, 0x1B17, 0x1CB8,
76 0x0B76, 0x1367, 0x04A9, 0x0204, 0x15CA, 0x0DDB, 0x1A15, 0x1DBA, 0x0A74,
77 0x1265, 0x05AB, 0x1D3B, 0x0AF5, 0x12E4, 0x052A, 0x0285, 0x154B, 0x0D5A,
78 0x1A94, 0x1831, 0x0FFF, 0x17EE, 0x0020, 0x078F, 0x1041, 0x0850, 0x1F9E,
79 0x070E, 0x10C0, 0x08D1, 0x1F1F, 0x18B0, 0x0F7E, 0x176F, 0x00A1, 0x060C,
80 0x11C2, 0x09D3, 0x1E1D, 0x19B2, 0x0E7C, 0x166D, 0x01A3, 0x1933, 0x0EFD,
81 0x16EC, 0x0122, 0x068D, 0x1143, 0x0952, 0x1E9C, 0x0408, 0x13C6, 0x0BD7,
82 0x1C19, 0x1BB6, 0x0C78, 0x1469, 0x03A7, 0x1B37, 0x0CF9, 0x14E8, 0x0326,
83 0x0489, 0x1347, 0x0B56, 0x1C98, 0x1A35, 0x0DFB, 0x15EA, 0x0224, 0x058B,
84 0x1245, 0x0A54, 0x1D9A, 0x050A, 0x12C4, 0x0AD5, 0x1D1B, 0x1AB4, 0x0D7A,
85 0x156B, 0x02A5, 0x1021, 0x07EF, 0x1FFE, 0x0830, 0x0F9F, 0x1851, 0x0040,
86 0x178E, 0x0F1E, 0x18D0, 0x00C1, 0x170F, 0x10A0, 0x076E, 0x1F7F, 0x08B1,
87 0x0E1C, 0x19D2, 0x01C3, 0x160D, 0x11A2, 0x066C, 0x1E7D, 0x09B3, 0x1123,
88 0x06ED, 0x1EFC, 0x0932, 0x0E9D, 0x1953, 0x0142, 0x168C, 0x0C18, 0x1BD6,
89 0x03C7, 0x1409, 0x13A6, 0x0468, 0x1C79, 0x0BB7, 0x1327, 0x04E9, 0x1CF8,
90 0x0B36, 0x0C99, 0x1B57, 0x0346, 0x1488, 0x1225, 0x05EB, 0x1DFA, 0x0A34,
91 0x0D9B, 0x1A55, 0x0244, 0x158A, 0x0D1A, 0x1AD4, 0x02C5, 0x150B, 0x12A4,
92 0x056A, 0x1D7B, 0x0AB5, 0x0810, 0x1FDE, 0x07CF, 0x1001, 0x17AE, 0x0060,
93 0x1871, 0x0FBF, 0x172F, 0x00E1, 0x18F0, 0x0F3E, 0x0891, 0x1F5F, 0x074E,
94 0x1080, 0x162D, 0x01E3, 0x19F2, 0x0E3C, 0x0993, 0x1E5D, 0x064C, 0x1182,
95 0x0912, 0x1EDC, 0x06CD, 0x1103, 0x16AC, 0x0162, 0x1973, 0x0EBD, 0x1429,
96 0x03E7, 0x1BF6, 0x0C38, 0x0B97, 0x1C59, 0x0448, 0x1386, 0x0B16, 0x1CD8,
97 0x04C9, 0x1307, 0x14A8, 0x0366, 0x1B77, 0x0CB9, 0x0A14, 0x1DDA, 0x05CB,
98 0x1205, 0x15AA, 0x0264, 0x1A75, 0x0DBB, 0x152B, 0x02E5, 0x1AF4, 0x0D3A,
99 0x0A95, 0x1D5B, 0x054A, 0x1284};
100
101uint16_t CalculateCrc(const uint8_t *data, size_t data_length) {
102 uint16_t crc = 0xFFFF;
103 uint16_t byte;
104
105 while (data_length--) {
106 // Compute lower byte CRC first.
107 byte = data[1];
108 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
109 // Compute upper byte of CRC.
110 byte = data[0];
111 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
112 data += 2;
113 }
114 crc = ~crc; // Compute complement of CRC
115 return static_cast<uint16_t>(
116 (crc << 8) | (crc >> 8)); // Perform byte swap prior to returning CRC;
117}
118
119} // namespace
120
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800121ADIS16448::ADIS16448(::aos::EventLoop *event_loop, frc::SPI::Port port,
122 frc::DigitalInput *dio1)
123 : event_loop_(event_loop),
124 joystick_state_fetcher_(event_loop_->MakeFetcher<::aos::JoystickState>(
125 ".aos.joystick_state")),
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700126 imu_values_sender_(
127 event_loop_->MakeSender<::frc971::IMUValues>(".frc971.imu_values")),
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800128 spi_(new frc::SPI(port)),
129 dio1_(dio1) {
Brian Silvermana70994f2017-03-16 22:32:55 -0700130 // 1MHz is the maximum supported for burst reads, but we
131 // want to go slower to hopefully make it more reliable.
Brian Silverman56c2bcb2019-02-24 15:10:18 -0800132 // Note that the roboRIO's minimum supported clock rate appears to be
133 // 0.781MHz, so that's what this actually does.
Brian Silvermana70994f2017-03-16 22:32:55 -0700134 spi_->SetClockRate(1e5);
Brian Silverman5f17a972016-02-28 01:49:32 -0500135 spi_->SetChipSelectActiveLow();
136 spi_->SetClockActiveLow();
137 spi_->SetSampleDataOnFalling();
138 spi_->SetMSBFirst();
139
140 dio1_->RequestInterrupts();
141 dio1_->SetUpSourceEdge(true, false);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700142
143 // NI's SPI driver defaults to SCHED_OTHER. Find it's PID with ps, and change
144 // it to a RT priority of 33.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700145 AOS_PCHECK(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700146 system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
147 "33") == 0);
148
149 event_loop_->set_name("IMU");
150 event_loop_->SetRuntimeRealtimePriority(33);
151
152 event_loop_->OnRun([this]() { DoRun(); });
Brian Silverman5f17a972016-02-28 01:49:32 -0500153}
154
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800155void ADIS16448::SetDummySPI(frc::SPI::Port port) {
156 dummy_spi_.reset(new frc::SPI(port));
Brian Silvermana70994f2017-03-16 22:32:55 -0700157 // Pick the same settings here in case the roboRIO decides to try something
158 // stupid when switching.
159 if (dummy_spi_) {
160 dummy_spi_->SetClockRate(1e5);
161 dummy_spi_->SetChipSelectActiveLow();
162 dummy_spi_->SetClockActiveLow();
163 dummy_spi_->SetSampleDataOnFalling();
164 dummy_spi_->SetMSBFirst();
165 }
166}
167
168void ADIS16448::InitializeUntilSuccessful() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700169 while (event_loop_->is_running() && !Initialize()) {
Brian Silvermana70994f2017-03-16 22:32:55 -0700170 if (reset_) {
171 reset_->Set(false);
172 // Datasheet says this needs to be at least 10 us long, so 10 ms is
173 // plenty.
174 ::std::this_thread::sleep_for(::std::chrono::milliseconds(10));
175 reset_->Set(true);
176 // Datasheet says this takes 90 ms typically, and we want to give it
177 // plenty of margin.
178 ::std::this_thread::sleep_for(::std::chrono::milliseconds(150));
179 } else {
180 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
181 }
182 }
Austin Schuhf257f3c2019-10-27 21:00:43 -0700183 AOS_LOG(INFO, "IMU initialized successfully\n");
Brian Silvermana70994f2017-03-16 22:32:55 -0700184}
185
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700186void ADIS16448::DoRun() {
Brian Silvermana70994f2017-03-16 22:32:55 -0700187 InitializeUntilSuccessful();
Austin Schuhf266db52017-03-05 22:27:50 -0800188
Philipp Schrader29d54f22016-04-02 22:14:48 +0000189 // Rounded to approximate the 204.8 Hz.
190 constexpr size_t kImuSendRate = 205;
191
192 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_x;
193 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_y;
194 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_z;
195
Brian Silverman5f17a972016-02-28 01:49:32 -0500196 bool got_an_interrupt = false;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700197 while (event_loop_->is_running()) {
Brian Silverman5f17a972016-02-28 01:49:32 -0500198 {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700199 // Wait for an interrupt. (This prevents us from going to sleep in the
200 // event loop like we normally would)
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800201 const frc::InterruptableSensorBase::WaitResult result =
Brian Silverman5f17a972016-02-28 01:49:32 -0500202 dio1_->WaitForInterrupt(0.1, !got_an_interrupt);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800203 if (result == frc::InterruptableSensorBase::kTimeout) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700204 AOS_LOG(WARNING, "IMU read timed out\n");
Brian Silvermana70994f2017-03-16 22:32:55 -0700205 InitializeUntilSuccessful();
Brian Silverman5f17a972016-02-28 01:49:32 -0500206 continue;
207 }
208 }
209 got_an_interrupt = true;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800210 const monotonic_clock::time_point read_time = monotonic_clock::now();
Brian Silverman5f17a972016-02-28 01:49:32 -0500211
Austin Schuh871a1362016-04-02 12:25:00 -0700212 uint8_t to_send[2 * 14], to_receive[2 * 14];
213 memset(&to_send[0], 0, sizeof(to_send));
Brian Silverman5f17a972016-02-28 01:49:32 -0500214 to_send[0] = kGlobalReadAddress;
Austin Schuh871a1362016-04-02 12:25:00 -0700215 if (!DoTransaction<2 * 14>(to_send, to_receive)) continue;
Brian Silverman5f17a972016-02-28 01:49:32 -0500216
217 // If it's false now or another edge happened, then we're in trouble. This
218 // won't catch all instances of being a little bit slow (because of the
219 // interrupt delay among other things), but it will catch the code
220 // constantly falling behind, which seems like the most likely failure
221 // scenario.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700222 if (!dio1_->Get() || dio1_->WaitForInterrupt(0, false) !=
223 frc::InterruptableSensorBase::kTimeout) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700224 AOS_LOG(ERROR, "IMU read took too long\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500225 continue;
226 }
227
228 {
229 const uint16_t calculated_crc = CalculateCrc(&to_receive[4], 11);
Austin Schuh871a1362016-04-02 12:25:00 -0700230 uint16_t received_crc =
231 to_receive[13 * 2 + 1] | (to_receive[13 * 2] << 8);
Brian Silverman5f17a972016-02-28 01:49:32 -0500232 if (received_crc != calculated_crc) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700233 AOS_LOG(WARNING,
234 "received CRC %" PRIx16 " but calculated %" PRIx16 "\n",
235 received_crc, calculated_crc);
Brian Silvermana70994f2017-03-16 22:32:55 -0700236 InitializeUntilSuccessful();
Brian Silverman5f17a972016-02-28 01:49:32 -0500237 continue;
238 }
239 }
240
241 {
242 uint16_t diag_stat;
Austin Schuh871a1362016-04-02 12:25:00 -0700243 memcpy(&diag_stat, &to_receive[2], 2);
Brian Silvermana70994f2017-03-16 22:32:55 -0700244 if (!CheckDiagStatValue(diag_stat)) {
245 InitializeUntilSuccessful();
246 continue;
247 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500248 }
249
Austin Schuh73b6e3b2019-05-27 16:37:15 -0700250 auto message = imu_values_sender_.MakeMessage();
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700251 message->fpga_timestamp = ::aos::time::DurationInSeconds(
252 dio1_->ReadRisingTimestamp().time_since_epoch());
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800253 message->monotonic_timestamp_ns =
254 chrono::duration_cast<chrono::nanoseconds>(read_time.time_since_epoch())
255 .count();
Brian Silverman5f17a972016-02-28 01:49:32 -0500256
257 message->gyro_x =
Austin Schuh871a1362016-04-02 12:25:00 -0700258 ConvertValue(&to_receive[4], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500259 message->gyro_y =
Austin Schuh871a1362016-04-02 12:25:00 -0700260 ConvertValue(&to_receive[6], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500261 message->gyro_z =
Austin Schuh871a1362016-04-02 12:25:00 -0700262 ConvertValue(&to_receive[8], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500263
Philipp Schrader29d54f22016-04-02 22:14:48 +0000264 // The first few seconds of samples are averaged and subtracted from
265 // subsequent samples for zeroing purposes.
Austin Schuh943fcbd2016-04-03 21:35:41 -0700266 if (!gyros_are_zeroed_) {
Philipp Schrader29d54f22016-04-02 22:14:48 +0000267 average_gyro_x.AddData(message->gyro_x);
268 average_gyro_y.AddData(message->gyro_y);
269 average_gyro_z.AddData(message->gyro_z);
270
271 if (average_gyro_x.full() && average_gyro_y.full() &&
272 average_gyro_z.full()) {
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800273 joystick_state_fetcher_.Fetch();
274 if (joystick_state_fetcher_.get() && joystick_state_fetcher_->enabled) {
Austin Schuh943fcbd2016-04-03 21:35:41 -0700275 gyro_x_zeroed_offset_ = -average_gyro_x.GetAverage();
276 gyro_y_zeroed_offset_ = -average_gyro_y.GetAverage();
277 gyro_z_zeroed_offset_ = -average_gyro_z.GetAverage();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700278 AOS_LOG(INFO, "total gyro zero offset X:%f, Y:%f, Z:%f\n",
279 gyro_x_zeroed_offset_, gyro_y_zeroed_offset_,
280 gyro_z_zeroed_offset_);
Austin Schuh943fcbd2016-04-03 21:35:41 -0700281 gyros_are_zeroed_ = true;
282 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000283 }
284 }
285
Austin Schuh943fcbd2016-04-03 21:35:41 -0700286 message->gyro_x += gyro_x_zeroed_offset_;
287 message->gyro_y += gyro_y_zeroed_offset_;
288 message->gyro_z += gyro_z_zeroed_offset_;
289
Austin Schuh871a1362016-04-02 12:25:00 -0700290 message->accelerometer_x =
291 ConvertValue(&to_receive[10], kAccelerometerLsbG);
292 message->accelerometer_y =
293 ConvertValue(&to_receive[12], kAccelerometerLsbG);
294 message->accelerometer_z =
295 ConvertValue(&to_receive[14], kAccelerometerLsbG);
Brian Silverman5f17a972016-02-28 01:49:32 -0500296
297 message->magnetometer_x =
Austin Schuh871a1362016-04-02 12:25:00 -0700298 ConvertValue(&to_receive[16], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500299 message->magnetometer_y =
Austin Schuh871a1362016-04-02 12:25:00 -0700300 ConvertValue(&to_receive[18], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500301 message->magnetometer_z =
Austin Schuh871a1362016-04-02 12:25:00 -0700302 ConvertValue(&to_receive[20], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500303
304 message->barometer =
Austin Schuh871a1362016-04-02 12:25:00 -0700305 ConvertValue(&to_receive[22], kBarometerLsbPascal, false);
Brian Silverman5f17a972016-02-28 01:49:32 -0500306
307 message->temperature =
Austin Schuh871a1362016-04-02 12:25:00 -0700308 ConvertValue(&to_receive[24], kTemperatureLsbDegree) + kTemperatureZero;
Brian Silverman5f17a972016-02-28 01:49:32 -0500309
Austin Schuhf257f3c2019-10-27 21:00:43 -0700310 AOS_LOG_STRUCT(DEBUG, "sending", *message);
Brian Silverman5f17a972016-02-28 01:49:32 -0500311 if (!message.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700312 AOS_LOG(WARNING, "sending queue message failed\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500313 }
Brian Silverman56c2bcb2019-02-24 15:10:18 -0800314
315 spi_idle_callback_();
Brian Silverman5f17a972016-02-28 01:49:32 -0500316 }
317}
318
319float ADIS16448::ConvertValue(uint8_t *data, double lsb_per_output, bool sign) {
320 double value;
321 if (sign) {
Austin Schuh871a1362016-04-02 12:25:00 -0700322 int16_t raw_value = static_cast<int16_t>(
323 (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]));
Brian Silverman5f17a972016-02-28 01:49:32 -0500324 value = raw_value;
325 } else {
Austin Schuh871a1362016-04-02 12:25:00 -0700326 uint16_t raw_value =
327 (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]);
Brian Silverman5f17a972016-02-28 01:49:32 -0500328 value = raw_value;
329 }
330 return value * lsb_per_output;
331}
332
333bool ADIS16448::ReadRegister(uint8_t next_address, uint16_t *value) {
334 uint8_t to_send[2], to_receive[2];
335 to_send[0] = next_address;
336 to_send[1] = 0;
337
338 if (!DoTransaction<2>(to_send, to_receive)) return false;
339
Austin Schuh871a1362016-04-02 12:25:00 -0700340 if (value) {
341 memcpy(value, to_receive, 2);
342 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500343 return true;
344}
345
346bool ADIS16448::WriteRegister(uint8_t address, uint16_t value) {
347 uint8_t to_send[4], to_receive[4];
348 to_send[0] = address | 0x80;
349 to_send[1] = value & 0xFF;
350 to_send[2] = address | 0x81;
351 to_send[3] = value >> 8;
352 if (!DoTransaction<4>(to_send, to_receive)) return false;
353 return true;
354}
355
356bool ADIS16448::CheckDiagStatValue(uint16_t value) const {
357 bool r = true;
Brian Silverman5f17a972016-02-28 01:49:32 -0500358 if (value & (1 << 2)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700359 AOS_LOG(WARNING, "IMU gave flash update failure\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500360 }
361 if (value & (1 << 3)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700362 AOS_LOG(WARNING, "IMU gave SPI communication failure\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500363 }
364 if (value & (1 << 4)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700365 AOS_LOG(WARNING, "IMU gave sensor overrange\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500366 }
367 if (value & (1 << 5)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700368 AOS_LOG(WARNING, "IMU gave self-test failure\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500369 r = false;
Austin Schuh871a1362016-04-02 12:25:00 -0700370 if (value & (1 << 10)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700371 AOS_LOG(WARNING, "IMU gave X-axis gyro self-test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700372 }
373 if (value & (1 << 11)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700374 AOS_LOG(WARNING, "IMU gave Y-axis gyro self-test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700375 }
376 if (value & (1 << 12)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700377 AOS_LOG(WARNING, "IMU gave Z-axis gyro self-test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700378 }
379 if (value & (1 << 13)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700380 AOS_LOG(WARNING, "IMU gave X-axis accelerometer self-test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700381 }
382 if (value & (1 << 14)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700383 AOS_LOG(WARNING, "IMU gave Y-axis accelerometer self-test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700384 }
385 if (value & (1 << 15)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700386 AOS_LOG(WARNING, "IMU gave Z-axis accelerometer self-test failure, %x\n",
387 value);
Austin Schuh871a1362016-04-02 12:25:00 -0700388 }
389 if (value & (1 << 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700390 AOS_LOG(WARNING, "IMU gave magnetometer functional test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700391 }
392 if (value & (1 << 1)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700393 AOS_LOG(WARNING, "IMU gave barometer functional test failure\n");
Austin Schuh871a1362016-04-02 12:25:00 -0700394 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500395 }
396 if (value & (1 << 6)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700397 AOS_LOG(WARNING, "IMU gave flash test checksum failure\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500398 }
399 if (value & (1 << 8)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700400 AOS_LOG(WARNING, "IMU says alarm 1 is active\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500401 }
402 if (value & (1 << 9)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700403 AOS_LOG(WARNING, "IMU says alarm 2 is active\n");
Brian Silverman5f17a972016-02-28 01:49:32 -0500404 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500405 return r;
406}
407
408bool ADIS16448::Initialize() {
409 if (!ReadRegister(kProdIdAddress, nullptr)) return false;
410 uint16_t product_id;
411 if (!ReadRegister(kLotId1Address, &product_id)) return false;
412 if (product_id != 0x4040) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700413 AOS_LOG(ERROR, "product ID is %" PRIx16 " instead of 0x4040\n", product_id);
Brian Silverman5f17a972016-02-28 01:49:32 -0500414 return false;
415 }
416
417 uint16_t lot_id1, lot_id2, serial_number;
418 if (!ReadRegister(kLotId2Address, &lot_id1)) return false;
419 if (!ReadRegister(kSerialNumberAddress, &lot_id2)) return false;
420 if (!ReadRegister(0, &serial_number)) return false;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700421 AOS_LOG(INFO, "have IMU %" PRIx16 "%" PRIx16 ": %" PRIx16 "\n", lot_id1,
422 lot_id2, serial_number);
Brian Silverman5f17a972016-02-28 01:49:32 -0500423
424 // Divide the sampling by 2^2 = 4 to get 819.2 / 4 = 204.8 Hz.
Austin Schuh871a1362016-04-02 12:25:00 -0700425 if (!WriteRegister(kSmplPrdAddress, (2 << 8) | 1)) return false;
Brian Silverman5f17a972016-02-28 01:49:32 -0500426
427 // Start a self test.
428 if (!WriteRegister(kMscCtrlAddress, 1 << 10)) return false;
429 // Wait for the self test to finish.
430 {
431 uint16_t value;
432 do {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800433 ::std::this_thread::sleep_for(::std::chrono::milliseconds(10));
Brian Silverman5f17a972016-02-28 01:49:32 -0500434 if (!ReadRegister(kMscCtrlAddress, &value)) return false;
435 } while ((value & (1 << 10)) != 0);
436 }
437
438 if (!ReadRegister(kDiagStatAddress, nullptr)) return false;
439 uint16_t diag_stat;
440 if (!ReadRegister(0, &diag_stat)) return false;
441 if (!CheckDiagStatValue(diag_stat)) return false;
442
443 if (!WriteRegister(kMscCtrlAddress,
444 ((0 << 0) | // DIO1
445 (1 << 1) | // DIO goes high when data is valid
446 (1 << 2) | // enable DIO changing when data is vald
Austin Schuh871a1362016-04-02 12:25:00 -0700447 (1 << 4) | // enable CRC16 for burst mode
448 (1 << 6)))) {
Brian Silverman5f17a972016-02-28 01:49:32 -0500449 return false;
450 }
451 return true;
452}
453
454} // namespace wpilib
455} // namespace frc971