Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_GYRO_INTERFACE_H_ |
| 2 | #define FRC971_WPILIB_GYRO_INTERFACE_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 6 | #include "frc971/wpilib/ahal/SPI.h" |
Austin Schuh | 6d5d9ae | 2015-10-31 19:39:57 -0700 | [diff] [blame] | 7 | #undef ERROR |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 8 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 9 | namespace frc971::wpilib { |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 10 | |
| 11 | class GyroInterface { |
| 12 | public: |
| 13 | GyroInterface(); |
| 14 | |
| 15 | // Runs the recommended gyro startup procedure including checking all of the |
| 16 | // self-test bits. |
| 17 | // Returns true if it succeeds. |
| 18 | bool InitializeGyro(); |
| 19 | |
| 20 | // Reads one of the gyro's "registers" and returns the value. |
| 21 | // Retries until it succeeds. |
| 22 | uint16_t DoRead(uint8_t address); |
| 23 | |
| 24 | // Returns all of the non-data bits in the "header" except the parity from |
| 25 | // value. |
| 26 | uint8_t ExtractStatus(uint32_t value) { return (value >> 26) & ~4; } |
| 27 | // Returns all of the error bits in the "footer" from value. |
| 28 | uint8_t ExtractErrors(uint32_t value) { return (value >> 1) & 0x7F; } |
| 29 | |
Austin Schuh | c5a2375 | 2015-10-28 19:45:24 -0700 | [diff] [blame] | 30 | // Returns the anglular rate contained in value in radians/sec. |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 31 | double ExtractAngle(uint32_t value); |
| 32 | |
| 33 | // Performs a transaction with the gyro. |
| 34 | // to_write is the value to write. This function handles setting the checksum |
| 35 | // bit. |
| 36 | // result is where to stick the result. This function verifies the parity bit. |
| 37 | // Returns true for success. |
| 38 | bool DoTransaction(uint32_t to_write, uint32_t *result); |
| 39 | |
| 40 | // Returns the part ID from the gyro. |
| 41 | // Retries until it succeeds. |
| 42 | uint32_t ReadPartID(); |
| 43 | |
| 44 | // Gets a reading from the gyro. |
| 45 | // Returns a value to be passed to the Extract* methods or 0 for error. |
| 46 | uint32_t GetReading(); |
| 47 | |
| 48 | private: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 49 | ::std::unique_ptr<frc::SPI> gyro_; |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 52 | } // namespace frc971::wpilib |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 53 | |
| 54 | #endif // FRC971_WPILIB_GYRO_INTERFACE_H_ |