Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "LiveWindow/LiveWindowSendable.h" |
| 14 | #include "SPI.h" |
| 15 | #include "SensorBase.h" |
| 16 | #include "interfaces/Accelerometer.h" |
| 17 | |
| 18 | namespace frc { |
| 19 | |
| 20 | class DigitalInput; |
| 21 | class DigitalOutput; |
| 22 | |
| 23 | /** |
| 24 | * ADXL345 Accelerometer on SPI. |
| 25 | * |
| 26 | * This class allows access to an Analog Devices ADXL345 3-axis accelerometer |
| 27 | * via SPI. |
| 28 | * This class assumes the sensor is wired in 4-wire SPI mode. |
| 29 | */ |
| 30 | class ADXL345_SPI : public Accelerometer, public LiveWindowSendable { |
| 31 | protected: |
| 32 | static const int kPowerCtlRegister = 0x2D; |
| 33 | static const int kDataFormatRegister = 0x31; |
| 34 | static const int kDataRegister = 0x32; |
| 35 | static constexpr double kGsPerLSB = 0.00390625; |
| 36 | enum SPIAddressFields { kAddress_Read = 0x80, kAddress_MultiByte = 0x40 }; |
| 37 | enum PowerCtlFields { |
| 38 | kPowerCtl_Link = 0x20, |
| 39 | kPowerCtl_AutoSleep = 0x10, |
| 40 | kPowerCtl_Measure = 0x08, |
| 41 | kPowerCtl_Sleep = 0x04 |
| 42 | }; |
| 43 | enum DataFormatFields { |
| 44 | kDataFormat_SelfTest = 0x80, |
| 45 | kDataFormat_SPI = 0x40, |
| 46 | kDataFormat_IntInvert = 0x20, |
| 47 | kDataFormat_FullRes = 0x08, |
| 48 | kDataFormat_Justify = 0x04 |
| 49 | }; |
| 50 | |
| 51 | public: |
| 52 | enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 }; |
| 53 | struct AllAxes { |
| 54 | double XAxis; |
| 55 | double YAxis; |
| 56 | double ZAxis; |
| 57 | }; |
| 58 | |
| 59 | public: |
| 60 | explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G); |
| 61 | virtual ~ADXL345_SPI() = default; |
| 62 | |
| 63 | ADXL345_SPI(const ADXL345_SPI&) = delete; |
| 64 | ADXL345_SPI& operator=(const ADXL345_SPI&) = delete; |
| 65 | |
| 66 | // Accelerometer interface |
| 67 | void SetRange(Range range) override; |
| 68 | double GetX() override; |
| 69 | double GetY() override; |
| 70 | double GetZ() override; |
| 71 | |
| 72 | virtual double GetAcceleration(Axes axis); |
| 73 | virtual AllAxes GetAccelerations(); |
| 74 | |
| 75 | std::string GetSmartDashboardType() const override; |
| 76 | void InitTable(std::shared_ptr<ITable> subtable) override; |
| 77 | void UpdateTable() override; |
| 78 | std::shared_ptr<ITable> GetTable() const override; |
| 79 | void StartLiveWindowMode() override {} |
| 80 | void StopLiveWindowMode() override {} |
| 81 | |
| 82 | protected: |
| 83 | SPI m_spi; |
| 84 | |
| 85 | private: |
| 86 | std::shared_ptr<ITable> m_table; |
| 87 | }; |
| 88 | |
| 89 | } // namespace frc |