blob: 52090a145af03c475feafdd80f81de4290ba364b [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
6#include <memory>
7#include <atomic>
8
9#include "SPI.h"
10#include "DigitalInput.h"
Brian Silvermana70994f2017-03-16 22:32:55 -070011#include "DigitalOutput.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050012#undef ERROR
13
14#include "aos/common/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.
32 ADIS16448(SPI::Port port, DigitalInput *dio1);
33
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.
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 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
48 void Quit() { run_ = false; }
49
Philipp Schrader29d54f22016-04-02 22:14:48 +000050 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 Silverman5f17a972016-02-28 01:49:32 -050054 private:
Brian Silvermana70994f2017-03-16 22:32:55 -070055 // Try to initialize repeatedly as long as we're supposed to be running.
56 void InitializeUntilSuccessful();
57
Brian Silverman5f17a972016-02-28 01:49:32 -050058 // 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 Silvermana70994f2017-03-16 22:32:55 -070086 // TODO(Brian): This object has no business owning these ones.
Brian Silverman5f17a972016-02-28 01:49:32 -050087 const ::std::unique_ptr<SPI> spi_;
Brian Silvermana70994f2017-03-16 22:32:55 -070088 ::std::unique_ptr<SPI> dummy_spi_;
Brian Silverman5f17a972016-02-28 01:49:32 -050089 DigitalInput *const dio1_;
Brian Silvermana70994f2017-03-16 22:32:55 -070090 DigitalOutput *reset_ = nullptr;
Brian Silverman5f17a972016-02-28 01:49:32 -050091
92 ::std::atomic<bool> run_{true};
Philipp Schrader29d54f22016-04-02 22:14:48 +000093
94 // The averaged values of the gyro over 6 seconds after power up.
Austin Schuh943fcbd2016-04-03 21:35:41 -070095 bool gyros_are_zeroed_ = false;
Philipp Schrader29d54f22016-04-02 22:14:48 +000096 double gyro_x_zeroed_offset_ = 0.0;
97 double gyro_y_zeroed_offset_ = 0.0;
98 double gyro_z_zeroed_offset_ = 0.0;
Brian Silverman003a4732018-03-11 14:02:15 -070099
100 SpiRxClearer rx_clearer_;
Brian Silverman5f17a972016-02-28 01:49:32 -0500101};
102
103} // namespace wpilib
104} // namespace frc971
105
106#endif // FRC971_WPILIB_ADIS16448_H_