blob: 778ab49625b725e2d2cc2f757a4b388f8c37b487 [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"
11#undef ERROR
12
13#include "aos/common/logging/logging.h"
14
15namespace frc971 {
16namespace wpilib {
17
18// Handles interfacing with an Analog Devices ADIS16448 Inertial Sensor over
19// SPI and sending values out on a queue.
20//
21// The sensor is configured to generate samples at 204.8 Hz, and the values are
22// sent out as each sample is received.
23//
24// This is designed to be passed into ::std::thread's constructor so it will run
25// as a separate thread.
26class ADIS16448 {
27 public:
28 // port is where to find the sensor over SPI.
29 // dio1 must be connected to DIO1 on the sensor.
30 ADIS16448(SPI::Port port, DigitalInput *dio1);
31
32 // For ::std::thread to call.
33 //
34 // Initializes the sensor and then loops until Quit() is called taking
35 // readings.
36 void operator()();
37
38 void Quit() { run_ = false; }
39
40 private:
41 // Converts a 16-bit value at data to a scaled output value where a value of 1
42 // corresponds to lsb_per_output.
43 float ConvertValue(uint8_t *data, double lsb_per_output, bool sign = true);
44
45 // Performs an SPI transaction.
46 // Returns true if it succeeds.
47 template <uint8_t size>
48 bool DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]);
49
50 // Reads one of the gyro's registers and returns the value in value.
51 // next_address is the address of the *next* register to read.
52 // Not sure what gets stored in value for the first read, but it should be
53 // ignored. Passing nullptr for value is allowed to completely ignore it.
54 // Returns true if it succeeds.
55 bool ReadRegister(uint8_t next_address, uint16_t *value);
56
57 // Writes a value to one of the registers.
58 // Returns true if it succeeds.
59 bool WriteRegister(uint8_t address, uint16_t value);
60
61 // Checks the given value of the DIAG_STAT register and logs any errors.
62 // Returns true if there are no errors we care about.
63 bool CheckDiagStatValue(uint16_t value) const;
64
65 // Starts everything up and runs a self test.
66 // Returns true if it succeeds.
67 bool Initialize();
68
69 const ::std::unique_ptr<SPI> spi_;
70 DigitalInput *const dio1_;
71
72 ::std::atomic<bool> run_{true};
73};
74
75} // namespace wpilib
76} // namespace frc971
77
78#endif // FRC971_WPILIB_ADIS16448_H_