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