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 "I2C.h" |
| 14 | #include "LiveWindow/LiveWindowSendable.h" |
| 15 | #include "interfaces/Accelerometer.h" |
| 16 | |
| 17 | namespace frc { |
| 18 | |
| 19 | /** |
| 20 | * ADXL345 Accelerometer on I2C. |
| 21 | * |
| 22 | * This class allows access to a Analog Devices ADXL345 3-axis accelerometer on |
| 23 | * an I2C bus. |
| 24 | * This class assumes the default (not alternate) sensor address of 0x1D (7-bit |
| 25 | * address). |
| 26 | */ |
| 27 | class ADXL345_I2C : public Accelerometer, public LiveWindowSendable { |
| 28 | protected: |
| 29 | static const int kAddress = 0x1D; |
| 30 | static const int kPowerCtlRegister = 0x2D; |
| 31 | static const int kDataFormatRegister = 0x31; |
| 32 | static const int kDataRegister = 0x32; |
| 33 | static constexpr double kGsPerLSB = 0.00390625; |
| 34 | enum PowerCtlFields { |
| 35 | kPowerCtl_Link = 0x20, |
| 36 | kPowerCtl_AutoSleep = 0x10, |
| 37 | kPowerCtl_Measure = 0x08, |
| 38 | kPowerCtl_Sleep = 0x04 |
| 39 | }; |
| 40 | enum DataFormatFields { |
| 41 | kDataFormat_SelfTest = 0x80, |
| 42 | kDataFormat_SPI = 0x40, |
| 43 | kDataFormat_IntInvert = 0x20, |
| 44 | kDataFormat_FullRes = 0x08, |
| 45 | kDataFormat_Justify = 0x04 |
| 46 | }; |
| 47 | |
| 48 | public: |
| 49 | enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 }; |
| 50 | struct AllAxes { |
| 51 | double XAxis; |
| 52 | double YAxis; |
| 53 | double ZAxis; |
| 54 | }; |
| 55 | |
| 56 | public: |
| 57 | explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G, |
| 58 | int deviceAddress = kAddress); |
| 59 | virtual ~ADXL345_I2C() = default; |
| 60 | |
| 61 | ADXL345_I2C(const ADXL345_I2C&) = delete; |
| 62 | ADXL345_I2C& operator=(const ADXL345_I2C&) = delete; |
| 63 | |
| 64 | // Accelerometer interface |
| 65 | void SetRange(Range range) override; |
| 66 | double GetX() override; |
| 67 | double GetY() override; |
| 68 | double GetZ() override; |
| 69 | |
| 70 | virtual double GetAcceleration(Axes axis); |
| 71 | virtual AllAxes GetAccelerations(); |
| 72 | |
| 73 | std::string GetSmartDashboardType() const override; |
| 74 | void InitTable(std::shared_ptr<ITable> subtable) override; |
| 75 | void UpdateTable() override; |
| 76 | std::shared_ptr<ITable> GetTable() const override; |
| 77 | void StartLiveWindowMode() override {} |
| 78 | void StopLiveWindowMode() override {} |
| 79 | |
| 80 | protected: |
| 81 | I2C m_i2c; |
| 82 | |
| 83 | private: |
| 84 | std::shared_ptr<ITable> m_table; |
| 85 | }; |
| 86 | |
| 87 | } // namespace frc |