Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_ADIS16448_H_ |
| 2 | #define FRC971_WPILIB_ADIS16448_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <atomic> |
| 8 | |
| 9 | #include "SPI.h" |
| 10 | #include "DigitalInput.h" |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 11 | #include "DigitalOutput.h" |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 12 | #undef ERROR |
| 13 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 14 | #include "aos/logging/logging.h" |
Brian Silverman | 003a473 | 2018-03-11 14:02:15 -0700 | [diff] [blame] | 15 | #include "frc971/wpilib/spi_rx_clearer.h" |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 16 | |
| 17 | namespace frc971 { |
| 18 | namespace wpilib { |
| 19 | |
| 20 | // Handles interfacing with an Analog Devices ADIS16448 Inertial Sensor over |
| 21 | // SPI and sending values out on a queue. |
| 22 | // |
| 23 | // The sensor is configured to generate samples at 204.8 Hz, and the values are |
| 24 | // sent out as each sample is received. |
| 25 | // |
| 26 | // This is designed to be passed into ::std::thread's constructor so it will run |
| 27 | // as a separate thread. |
| 28 | class ADIS16448 { |
| 29 | public: |
| 30 | // port is where to find the sensor over SPI. |
| 31 | // dio1 must be connected to DIO1 on the sensor. |
| 32 | ADIS16448(SPI::Port port, DigitalInput *dio1); |
| 33 | |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 34 | // Sets the dummy SPI port to send values on to make the roboRIO deassert the |
| 35 | // chip select line. This is mainly useful when there are no other devices |
| 36 | // sharing the bus. |
| 37 | void SetDummySPI(SPI::Port port); |
| 38 | |
| 39 | // Sets the reset line for the IMU to use for error recovery. |
| 40 | void set_reset(DigitalOutput *output) { reset_ = output; } |
| 41 | |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 42 | // For ::std::thread to call. |
| 43 | // |
| 44 | // Initializes the sensor and then loops until Quit() is called taking |
| 45 | // readings. |
| 46 | void operator()(); |
| 47 | |
| 48 | void Quit() { run_ = false; } |
| 49 | |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 50 | double gyro_x_zeroed_offset() const { return gyro_x_zeroed_offset_; } |
| 51 | double gyro_y_zeroed_offset() const { return gyro_y_zeroed_offset_; } |
| 52 | double gyro_z_zeroed_offset() const { return gyro_z_zeroed_offset_; } |
| 53 | |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 54 | private: |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 55 | // Try to initialize repeatedly as long as we're supposed to be running. |
| 56 | void InitializeUntilSuccessful(); |
| 57 | |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 58 | // Converts a 16-bit value at data to a scaled output value where a value of 1 |
| 59 | // corresponds to lsb_per_output. |
| 60 | float ConvertValue(uint8_t *data, double lsb_per_output, bool sign = true); |
| 61 | |
| 62 | // Performs an SPI transaction. |
| 63 | // Returns true if it succeeds. |
| 64 | template <uint8_t size> |
| 65 | bool DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]); |
| 66 | |
| 67 | // Reads one of the gyro's registers and returns the value in value. |
| 68 | // next_address is the address of the *next* register to read. |
| 69 | // Not sure what gets stored in value for the first read, but it should be |
| 70 | // ignored. Passing nullptr for value is allowed to completely ignore it. |
| 71 | // Returns true if it succeeds. |
| 72 | bool ReadRegister(uint8_t next_address, uint16_t *value); |
| 73 | |
| 74 | // Writes a value to one of the registers. |
| 75 | // Returns true if it succeeds. |
| 76 | bool WriteRegister(uint8_t address, uint16_t value); |
| 77 | |
| 78 | // Checks the given value of the DIAG_STAT register and logs any errors. |
| 79 | // Returns true if there are no errors we care about. |
| 80 | bool CheckDiagStatValue(uint16_t value) const; |
| 81 | |
| 82 | // Starts everything up and runs a self test. |
| 83 | // Returns true if it succeeds. |
| 84 | bool Initialize(); |
| 85 | |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 86 | // TODO(Brian): This object has no business owning these ones. |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 87 | const ::std::unique_ptr<SPI> spi_; |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 88 | ::std::unique_ptr<SPI> dummy_spi_; |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 89 | DigitalInput *const dio1_; |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 90 | DigitalOutput *reset_ = nullptr; |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 91 | |
| 92 | ::std::atomic<bool> run_{true}; |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 93 | |
| 94 | // The averaged values of the gyro over 6 seconds after power up. |
Austin Schuh | 943fcbd | 2016-04-03 21:35:41 -0700 | [diff] [blame] | 95 | bool gyros_are_zeroed_ = false; |
Philipp Schrader | 29d54f2 | 2016-04-02 22:14:48 +0000 | [diff] [blame] | 96 | double gyro_x_zeroed_offset_ = 0.0; |
| 97 | double gyro_y_zeroed_offset_ = 0.0; |
| 98 | double gyro_z_zeroed_offset_ = 0.0; |
Brian Silverman | 003a473 | 2018-03-11 14:02:15 -0700 | [diff] [blame] | 99 | |
| 100 | SpiRxClearer rx_clearer_; |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace wpilib |
| 104 | } // namespace frc971 |
| 105 | |
| 106 | #endif // FRC971_WPILIB_ADIS16448_H_ |