Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/gyro_interface.h" |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cinttypes> |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
| 7 | #include "aos/time/time.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 8 | |
| 9 | #ifndef M_PI |
| 10 | #define M_PI 3.14159265358979323846 |
| 11 | #endif |
| 12 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 13 | namespace frc971::wpilib { |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 14 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 15 | GyroInterface::GyroInterface() : gyro_(new frc::SPI(frc::SPI::kOnboardCS0)) { |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 16 | // The gyro goes up to 8.08MHz. |
| 17 | // The myRIO goes up to 4MHz, so the roboRIO probably does too. |
| 18 | gyro_->SetClockRate(4e6); |
| 19 | gyro_->SetChipSelectActiveLow(); |
James Kuszmaul | 9776b39 | 2023-01-14 14:08:08 -0800 | [diff] [blame] | 20 | gyro_->SetMode(frc::SPI::Mode::kMode3); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | bool GyroInterface::InitializeGyro() { |
| 24 | uint32_t result; |
| 25 | if (!DoTransaction(0x20000003, &result)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 26 | AOS_LOG(WARNING, "failed to start a self-check\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | if (result != 1) { |
| 30 | // We might have hit a parity error or something and are now retrying, so |
| 31 | // this isn't a very big deal. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 32 | AOS_LOG(INFO, "gyro unexpected initial response 0x%" PRIx32 "\n", result); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Wait for it to assert the fault conditions before reading them. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 36 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(50)); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 37 | |
| 38 | if (!DoTransaction(0x20000000, &result)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 39 | AOS_LOG(WARNING, "failed to clear latched non-fault data\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 40 | return false; |
| 41 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 42 | AOS_LOG(DEBUG, "gyro dummy response is 0x%" PRIx32 "\n", result); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 43 | |
| 44 | if (!DoTransaction(0x20000000, &result)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 45 | AOS_LOG(WARNING, "failed to read self-test data\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | if (ExtractStatus(result) != 2) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 49 | AOS_LOG(WARNING, "gyro first value 0x%" PRIx32 " not self-test data\n", |
| 50 | result); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | if (ExtractErrors(result) != 0x7F) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 54 | AOS_LOG(WARNING, |
| 55 | "gyro first value 0x%" PRIx32 " does not have all errors\n", |
| 56 | result); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (!DoTransaction(0x20000000, &result)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 61 | AOS_LOG(WARNING, "failed to clear latched self-test data\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | if (ExtractStatus(result) != 2) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 65 | AOS_LOG(WARNING, "gyro second value 0x%" PRIx32 " not self-test data\n", |
| 66 | result); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool GyroInterface::DoTransaction(uint32_t to_write, uint32_t *result) { |
| 74 | static const uint8_t kBytes = 4; |
| 75 | static_assert(kBytes == sizeof(to_write), |
| 76 | "need the same number of bytes as sizeof(the data)"); |
| 77 | |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 78 | if (__builtin_parity(to_write & ~1) == 0) { |
| 79 | to_write |= 1; |
| 80 | } else { |
| 81 | to_write &= ~1; |
| 82 | } |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 83 | |
| 84 | uint8_t to_send[kBytes], to_receive[kBytes]; |
| 85 | const uint32_t to_write_flipped = __builtin_bswap32(to_write); |
| 86 | memcpy(to_send, &to_write_flipped, kBytes); |
| 87 | |
| 88 | switch (gyro_->Transaction(to_send, to_receive, kBytes)) { |
| 89 | case -1: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 90 | AOS_LOG(INFO, "SPI::Transaction failed\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 91 | return false; |
| 92 | case kBytes: |
| 93 | break; |
| 94 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 95 | AOS_LOG(FATAL, "SPI::Transaction returned something weird\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | memcpy(result, to_receive, kBytes); |
| 99 | if (__builtin_parity(*result & 0xFFFF) != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 100 | AOS_LOG(INFO, "high byte parity failure\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | if (__builtin_parity(*result) != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 104 | AOS_LOG(INFO, "whole value parity failure\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | *result = __builtin_bswap32(*result); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | uint16_t GyroInterface::DoRead(uint8_t address) { |
| 113 | const uint32_t command = (0x8 << 28) | (address << 17); |
| 114 | uint32_t response; |
| 115 | while (true) { |
| 116 | if (!DoTransaction(command, &response)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 117 | AOS_LOG(WARNING, "reading 0x%" PRIx8 " failed\n", address); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 118 | continue; |
| 119 | } |
| 120 | if ((response & 0xEFE00000) != 0x4E000000) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 121 | AOS_LOG(WARNING, |
| 122 | "gyro read from 0x%" PRIx8 " gave unexpected response 0x%" PRIx32 |
| 123 | "\n", |
| 124 | address, response); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | return (response >> 5) & 0xFFFF; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | double GyroInterface::ExtractAngle(uint32_t value) { |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 132 | const int16_t reading = -static_cast<int16_t>(value >> 10 & 0xFFFF); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 133 | return static_cast<double>(reading) * 2.0 * M_PI / 360.0 / 80.0; |
| 134 | } |
| 135 | |
| 136 | uint32_t GyroInterface::ReadPartID() { |
| 137 | return (DoRead(0x0E) << 16) | DoRead(0x10); |
| 138 | } |
| 139 | |
| 140 | uint32_t GyroInterface::GetReading() { |
| 141 | uint32_t result; |
| 142 | if (!DoTransaction(0x20000000, &result)) { |
| 143 | return 0; |
| 144 | } |
| 145 | return result; |
| 146 | } |
| 147 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 148 | } // namespace frc971::wpilib |