blob: a1d6dcdfdd8e0613a4782784a2b2dec6a7bcff92 [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
Austin Schuhdf6cbb12019-02-02 13:46:52 -080014#include "aos/events/event-loop.h"
John Park33858a32018-09-28 23:05:48 -070015#include "aos/logging/logging.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080016#include "aos/robot_state/robot_state.q.h"
Brian Silverman003a4732018-03-11 14:02:15 -070017#include "frc971/wpilib/spi_rx_clearer.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050018
19namespace frc971 {
20namespace wpilib {
21
22// Handles interfacing with an Analog Devices ADIS16448 Inertial Sensor over
23// SPI and sending values out on a queue.
24//
25// The sensor is configured to generate samples at 204.8 Hz, and the values are
26// sent out as each sample is received.
27//
28// This is designed to be passed into ::std::thread's constructor so it will run
29// as a separate thread.
30class ADIS16448 {
31 public:
32 // port is where to find the sensor over SPI.
33 // dio1 must be connected to DIO1 on the sensor.
Austin Schuhdf6cbb12019-02-02 13:46:52 -080034 ADIS16448(::aos::EventLoop *event_loop, frc::SPI::Port port,
35 frc::DigitalInput *dio1);
Brian Silverman5f17a972016-02-28 01:49:32 -050036
Brian Silvermana70994f2017-03-16 22:32:55 -070037 // Sets the dummy SPI port to send values on to make the roboRIO deassert the
38 // chip select line. This is mainly useful when there are no other devices
39 // sharing the bus.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080040 void SetDummySPI(frc::SPI::Port port);
Brian Silvermana70994f2017-03-16 22:32:55 -070041
42 // Sets the reset line for the IMU to use for error recovery.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080043 void set_reset(frc::DigitalOutput *output) { reset_ = output; }
Brian Silvermana70994f2017-03-16 22:32:55 -070044
Brian Silverman5f17a972016-02-28 01:49:32 -050045 // For ::std::thread to call.
46 //
47 // Initializes the sensor and then loops until Quit() is called taking
48 // readings.
49 void operator()();
50
Brian Silvermancfc8fa42019-03-30 21:07:39 -060051 // Sets a function to be called immediately after each time this class uses
52 // the SPI bus. This is a good place to do other things on the bus.
Brian Silverman56c2bcb2019-02-24 15:10:18 -080053 void set_spi_idle_callback(std::function<void()> spi_idle_callback) {
54 spi_idle_callback_ = std::move(spi_idle_callback);
55 }
56
Brian Silverman5f17a972016-02-28 01:49:32 -050057 void Quit() { run_ = false; }
58
Philipp Schrader29d54f22016-04-02 22:14:48 +000059 double gyro_x_zeroed_offset() const { return gyro_x_zeroed_offset_; }
60 double gyro_y_zeroed_offset() const { return gyro_y_zeroed_offset_; }
61 double gyro_z_zeroed_offset() const { return gyro_z_zeroed_offset_; }
62
Brian Silverman5f17a972016-02-28 01:49:32 -050063 private:
Brian Silvermana70994f2017-03-16 22:32:55 -070064 // Try to initialize repeatedly as long as we're supposed to be running.
65 void InitializeUntilSuccessful();
66
Brian Silverman5f17a972016-02-28 01:49:32 -050067 // Converts a 16-bit value at data to a scaled output value where a value of 1
68 // corresponds to lsb_per_output.
69 float ConvertValue(uint8_t *data, double lsb_per_output, bool sign = true);
70
71 // Performs an SPI transaction.
72 // Returns true if it succeeds.
73 template <uint8_t size>
74 bool DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]);
75
76 // Reads one of the gyro's registers and returns the value in value.
77 // next_address is the address of the *next* register to read.
78 // Not sure what gets stored in value for the first read, but it should be
79 // ignored. Passing nullptr for value is allowed to completely ignore it.
80 // Returns true if it succeeds.
81 bool ReadRegister(uint8_t next_address, uint16_t *value);
82
83 // Writes a value to one of the registers.
84 // Returns true if it succeeds.
85 bool WriteRegister(uint8_t address, uint16_t value);
86
87 // Checks the given value of the DIAG_STAT register and logs any errors.
88 // Returns true if there are no errors we care about.
89 bool CheckDiagStatValue(uint16_t value) const;
90
91 // Starts everything up and runs a self test.
92 // Returns true if it succeeds.
93 bool Initialize();
94
Austin Schuhdf6cbb12019-02-02 13:46:52 -080095 ::aos::EventLoop *event_loop_;
96 ::aos::Fetcher<::aos::JoystickState> joystick_state_fetcher_;
97
Brian Silvermana70994f2017-03-16 22:32:55 -070098 // TODO(Brian): This object has no business owning these ones.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080099 const ::std::unique_ptr<frc::SPI> spi_;
100 ::std::unique_ptr<frc::SPI> dummy_spi_;
101 frc::DigitalInput *const dio1_;
102 frc::DigitalOutput *reset_ = nullptr;
Brian Silverman5f17a972016-02-28 01:49:32 -0500103
Brian Silverman56c2bcb2019-02-24 15:10:18 -0800104 std::function<void()> spi_idle_callback_ = []() {};
Brian Silverman5f17a972016-02-28 01:49:32 -0500105 ::std::atomic<bool> run_{true};
Philipp Schrader29d54f22016-04-02 22:14:48 +0000106
107 // The averaged values of the gyro over 6 seconds after power up.
Austin Schuh943fcbd2016-04-03 21:35:41 -0700108 bool gyros_are_zeroed_ = false;
Philipp Schrader29d54f22016-04-02 22:14:48 +0000109 double gyro_x_zeroed_offset_ = 0.0;
110 double gyro_y_zeroed_offset_ = 0.0;
111 double gyro_z_zeroed_offset_ = 0.0;
Brian Silverman003a4732018-03-11 14:02:15 -0700112
113 SpiRxClearer rx_clearer_;
Brian Silverman5f17a972016-02-28 01:49:32 -0500114};
115
116} // namespace wpilib
117} // namespace frc971
118
119#endif // FRC971_WPILIB_ADIS16448_H_