blob: 49c4fb241f3de957f4ebb614061feef2650c54ef [file] [log] [blame]
Brian Silverman5f17a972016-02-28 01:49:32 -05001#ifndef FRC971_WPILIB_ADIS16448_H_
2#define FRC971_WPILIB_ADIS16448_H_
3
4#include <stdint.h>
5
Brian Silverman5f17a972016-02-28 01:49:32 -05006#include <atomic>
Parker Schuhd3b7a8872018-02-19 16:42:27 -08007#include <memory>
Brian Silverman5f17a972016-02-28 01:49:32 -05008
Parker Schuhd3b7a8872018-02-19 16:42:27 -08009#include "frc971/wpilib/ahal/DigitalInput.h"
10#include "frc971/wpilib/ahal/DigitalOutput.h"
11#include "frc971/wpilib/ahal/SPI.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050012#undef ERROR
13
John Park33858a32018-09-28 23:05:48 -070014#include "aos/logging/logging.h"
Brian Silverman003a4732018-03-11 14:02:15 -070015#include "frc971/wpilib/spi_rx_clearer.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050016
17namespace frc971 {
18namespace 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.
28class ADIS16448 {
29 public:
30 // port is where to find the sensor over SPI.
31 // dio1 must be connected to DIO1 on the sensor.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080032 ADIS16448(frc::SPI::Port port, frc::DigitalInput *dio1);
Brian Silverman5f17a972016-02-28 01:49:32 -050033
Brian Silvermana70994f2017-03-16 22:32:55 -070034 // 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.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080037 void SetDummySPI(frc::SPI::Port port);
Brian Silvermana70994f2017-03-16 22:32:55 -070038
39 // Sets the reset line for the IMU to use for error recovery.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080040 void set_reset(frc::DigitalOutput *output) { reset_ = output; }
Brian Silvermana70994f2017-03-16 22:32:55 -070041
Brian Silverman5f17a972016-02-28 01:49:32 -050042 // For ::std::thread to call.
43 //
44 // Initializes the sensor and then loops until Quit() is called taking
45 // readings.
46 void operator()();
47
Brian Silvermancfc8fa42019-03-30 21:07:39 -060048 // Sets a function to be called immediately after each time this class uses
49 // the SPI bus. This is a good place to do other things on the bus.
Brian Silverman56c2bcb2019-02-24 15:10:18 -080050 void set_spi_idle_callback(std::function<void()> spi_idle_callback) {
51 spi_idle_callback_ = std::move(spi_idle_callback);
52 }
53
Brian Silverman5f17a972016-02-28 01:49:32 -050054 void Quit() { run_ = false; }
55
Philipp Schrader29d54f22016-04-02 22:14:48 +000056 double gyro_x_zeroed_offset() const { return gyro_x_zeroed_offset_; }
57 double gyro_y_zeroed_offset() const { return gyro_y_zeroed_offset_; }
58 double gyro_z_zeroed_offset() const { return gyro_z_zeroed_offset_; }
59
Brian Silverman5f17a972016-02-28 01:49:32 -050060 private:
Brian Silvermana70994f2017-03-16 22:32:55 -070061 // Try to initialize repeatedly as long as we're supposed to be running.
62 void InitializeUntilSuccessful();
63
Brian Silverman5f17a972016-02-28 01:49:32 -050064 // Converts a 16-bit value at data to a scaled output value where a value of 1
65 // corresponds to lsb_per_output.
66 float ConvertValue(uint8_t *data, double lsb_per_output, bool sign = true);
67
68 // Performs an SPI transaction.
69 // Returns true if it succeeds.
70 template <uint8_t size>
71 bool DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]);
72
73 // Reads one of the gyro's registers and returns the value in value.
74 // next_address is the address of the *next* register to read.
75 // Not sure what gets stored in value for the first read, but it should be
76 // ignored. Passing nullptr for value is allowed to completely ignore it.
77 // Returns true if it succeeds.
78 bool ReadRegister(uint8_t next_address, uint16_t *value);
79
80 // Writes a value to one of the registers.
81 // Returns true if it succeeds.
82 bool WriteRegister(uint8_t address, uint16_t value);
83
84 // Checks the given value of the DIAG_STAT register and logs any errors.
85 // Returns true if there are no errors we care about.
86 bool CheckDiagStatValue(uint16_t value) const;
87
88 // Starts everything up and runs a self test.
89 // Returns true if it succeeds.
90 bool Initialize();
91
Brian Silvermana70994f2017-03-16 22:32:55 -070092 // TODO(Brian): This object has no business owning these ones.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080093 const ::std::unique_ptr<frc::SPI> spi_;
94 ::std::unique_ptr<frc::SPI> dummy_spi_;
95 frc::DigitalInput *const dio1_;
96 frc::DigitalOutput *reset_ = nullptr;
Brian Silverman5f17a972016-02-28 01:49:32 -050097
Brian Silverman56c2bcb2019-02-24 15:10:18 -080098 std::function<void()> spi_idle_callback_ = []() {};
Brian Silverman5f17a972016-02-28 01:49:32 -050099 ::std::atomic<bool> run_{true};
Philipp Schrader29d54f22016-04-02 22:14:48 +0000100
101 // The averaged values of the gyro over 6 seconds after power up.
Austin Schuh943fcbd2016-04-03 21:35:41 -0700102 bool gyros_are_zeroed_ = false;
Philipp Schrader29d54f22016-04-02 22:14:48 +0000103 double gyro_x_zeroed_offset_ = 0.0;
104 double gyro_y_zeroed_offset_ = 0.0;
105 double gyro_z_zeroed_offset_ = 0.0;
Brian Silverman003a4732018-03-11 14:02:15 -0700106
107 SpiRxClearer rx_clearer_;
Brian Silverman5f17a972016-02-28 01:49:32 -0500108};
109
110} // namespace wpilib
111} // namespace frc971
112
113#endif // FRC971_WPILIB_ADIS16448_H_