WPILibC++  2020.2.2-33-g65eab93
ADXL345_I2C.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 FIRST. 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 <hal/SimDevice.h>
11 
12 #include "frc/ErrorBase.h"
13 #include "frc/I2C.h"
14 #include "frc/interfaces/Accelerometer.h"
15 #include "frc/smartdashboard/Sendable.h"
16 #include "frc/smartdashboard/SendableHelper.h"
17 
18 namespace frc {
19 
20 class SendableBuilder;
21 
29 class ADXL345_I2C : public ErrorBase,
30  public Accelerometer,
31  public Sendable,
32  public SendableHelper<ADXL345_I2C> {
33  public:
34  enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
35 
36  struct AllAxes {
37  double XAxis;
38  double YAxis;
39  double ZAxis;
40  };
41 
49  explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
50  int deviceAddress = kAddress);
51  ~ADXL345_I2C() override = default;
52 
53  ADXL345_I2C(ADXL345_I2C&&) = default;
54  ADXL345_I2C& operator=(ADXL345_I2C&&) = default;
55 
56  // Accelerometer interface
57  void SetRange(Range range) override;
58  double GetX() override;
59  double GetY() override;
60  double GetZ() override;
61 
68  virtual double GetAcceleration(Axes axis);
69 
76  virtual AllAxes GetAccelerations();
77 
78  void InitSendable(SendableBuilder& builder) override;
79 
80  protected:
81  I2C m_i2c;
82 
83  hal::SimDevice m_simDevice;
84  hal::SimEnum m_simRange;
85  hal::SimDouble m_simX;
86  hal::SimDouble m_simY;
87  hal::SimDouble m_simZ;
88 
89  static constexpr int kAddress = 0x1D;
90  static constexpr int kPowerCtlRegister = 0x2D;
91  static constexpr int kDataFormatRegister = 0x31;
92  static constexpr int kDataRegister = 0x32;
93  static constexpr double kGsPerLSB = 0.00390625;
94 
95  enum PowerCtlFields {
96  kPowerCtl_Link = 0x20,
97  kPowerCtl_AutoSleep = 0x10,
98  kPowerCtl_Measure = 0x08,
99  kPowerCtl_Sleep = 0x04
100  };
101 
102  enum DataFormatFields {
103  kDataFormat_SelfTest = 0x80,
104  kDataFormat_SPI = 0x40,
105  kDataFormat_IntInvert = 0x20,
106  kDataFormat_FullRes = 0x08,
107  kDataFormat_Justify = 0x04
108  };
109 };
110 
111 } // namespace frc
frc::ADXL345_I2C::AllAxes
Definition: ADXL345_I2C.h:36
frc::ADXL345_I2C::GetX
double GetX() override
Common interface for getting the x axis acceleration.
frc::ADXL345_I2C::ADXL345_I2C
ADXL345_I2C(I2C::Port port, Range range=kRange_2G, int deviceAddress=kAddress)
Constructs the ADXL345 Accelerometer over I2C.
frc::ADXL345_I2C::GetAcceleration
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
frc::ADXL345_I2C::GetY
double GetY() override
Common interface for getting the y axis acceleration.
frc::Accelerometer
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:15
frc::ADXL345_I2C::GetAccelerations
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::ADXL345_I2C::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::ADXL345_I2C
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:29
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc
WPILib FRC namespace.
Definition: AnalogOutSim.h:16
frc::ADXL345_I2C::GetZ
double GetZ() override
Common interface for getting the z axis acceleration.
frc::I2C
I2C bus interface class.
Definition: I2C.h:24
frc::ADXL345_I2C::SetRange
void SetRange(Range range) override
Common interface for setting the measuring range of an accelerometer.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23