blob: 0c6b519d7c50ec5b53c7b4627c6f60437006ac27 [file] [log] [blame]
Brian Silverman07ec88e2014-12-28 00:13:08 -08001#ifndef FRC971_WPILIB_GYRO_INTERFACE_H_
2#define FRC971_WPILIB_GYRO_INTERFACE_H_
3
4#include <memory>
5
Parker Schuhd3b7a8872018-02-19 16:42:27 -08006#include "frc971/wpilib/ahal/SPI.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -07007#undef ERROR
Brian Silverman07ec88e2014-12-28 00:13:08 -08008
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08009namespace frc971::wpilib {
Brian Silverman07ec88e2014-12-28 00:13:08 -080010
11class 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 Schuhc5a23752015-10-28 19:45:24 -070030 // Returns the anglular rate contained in value in radians/sec.
Brian Silverman07ec88e2014-12-28 00:13:08 -080031 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 Schuhd3b7a8872018-02-19 16:42:27 -080049 ::std::unique_ptr<frc::SPI> gyro_;
Brian Silverman07ec88e2014-12-28 00:13:08 -080050};
51
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080052} // namespace frc971::wpilib
Brian Silverman07ec88e2014-12-28 00:13:08 -080053
54#endif // FRC971_WPILIB_GYRO_INTERFACE_H_