blob: 3568a47c981227a9518bb30e50f90bc65afe0cf5 [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
9namespace frc971 {
10namespace wpilib {
11
12class GyroInterface {
13 public:
14 GyroInterface();
15
16 // Runs the recommended gyro startup procedure including checking all of the
17 // self-test bits.
18 // Returns true if it succeeds.
19 bool InitializeGyro();
20
21 // Reads one of the gyro's "registers" and returns the value.
22 // Retries until it succeeds.
23 uint16_t DoRead(uint8_t address);
24
25 // Returns all of the non-data bits in the "header" except the parity from
26 // value.
27 uint8_t ExtractStatus(uint32_t value) { return (value >> 26) & ~4; }
28 // Returns all of the error bits in the "footer" from value.
29 uint8_t ExtractErrors(uint32_t value) { return (value >> 1) & 0x7F; }
30
Austin Schuhc5a23752015-10-28 19:45:24 -070031 // Returns the anglular rate contained in value in radians/sec.
Brian Silverman07ec88e2014-12-28 00:13:08 -080032 double ExtractAngle(uint32_t value);
33
34 // Performs a transaction with the gyro.
35 // to_write is the value to write. This function handles setting the checksum
36 // bit.
37 // result is where to stick the result. This function verifies the parity bit.
38 // Returns true for success.
39 bool DoTransaction(uint32_t to_write, uint32_t *result);
40
41 // Returns the part ID from the gyro.
42 // Retries until it succeeds.
43 uint32_t ReadPartID();
44
45 // Gets a reading from the gyro.
46 // Returns a value to be passed to the Extract* methods or 0 for error.
47 uint32_t GetReading();
48
49 private:
Parker Schuhd3b7a8872018-02-19 16:42:27 -080050 ::std::unique_ptr<frc::SPI> gyro_;
Brian Silverman07ec88e2014-12-28 00:13:08 -080051};
52
53} // namespace wpilib
54} // namespace frc971
55
56#endif // FRC971_WPILIB_GYRO_INTERFACE_H_