WPILibC++  2018.4.1-20180926031727-1206-g8b1274d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
ADXL345_I2C.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 "frc/ErrorBase.h"
11 #include "frc/I2C.h"
12 #include "frc/interfaces/Accelerometer.h"
13 #include "frc/smartdashboard/SendableBase.h"
14 
15 namespace frc {
16 
24 class ADXL345_I2C : public ErrorBase,
25  public SendableBase,
26  public Accelerometer {
27  public:
28  enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
29 
30  struct AllAxes {
31  double XAxis;
32  double YAxis;
33  double ZAxis;
34  };
35 
43  explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
44  int deviceAddress = kAddress);
45  ~ADXL345_I2C() override = default;
46 
47  ADXL345_I2C(ADXL345_I2C&&) = default;
48  ADXL345_I2C& operator=(ADXL345_I2C&&) = default;
49 
50  // Accelerometer interface
51  void SetRange(Range range) override;
52  double GetX() override;
53  double GetY() override;
54  double GetZ() override;
55 
62  virtual double GetAcceleration(Axes axis);
63 
70  virtual AllAxes GetAccelerations();
71 
72  void InitSendable(SendableBuilder& builder) override;
73 
74  protected:
75  I2C m_i2c;
76 
77  static constexpr int kAddress = 0x1D;
78  static constexpr int kPowerCtlRegister = 0x2D;
79  static constexpr int kDataFormatRegister = 0x31;
80  static constexpr int kDataRegister = 0x32;
81  static constexpr double kGsPerLSB = 0.00390625;
82 
83  enum PowerCtlFields {
84  kPowerCtl_Link = 0x20,
85  kPowerCtl_AutoSleep = 0x10,
86  kPowerCtl_Measure = 0x08,
87  kPowerCtl_Sleep = 0x04
88  };
89 
90  enum DataFormatFields {
91  kDataFormat_SelfTest = 0x80,
92  kDataFormat_SPI = 0x40,
93  kDataFormat_IntInvert = 0x20,
94  kDataFormat_FullRes = 0x08,
95  kDataFormat_Justify = 0x04
96  };
97 };
98 
99 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
Definition: ADXL345_I2C.h:30
void SetRange(Range range) override
Common interface for setting the measuring range of an accelerometer.
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:24
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
double GetX() override
Common interface for getting the x axis acceleration.
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
I2C bus interface class.
Definition: I2C.h:24
ADXL345_I2C(I2C::Port port, Range range=kRange_2G, int deviceAddress=kAddress)
Constructs the ADXL345 Accelerometer over I2C.
Base class for most objects.
Definition: ErrorBase.h:74
double GetZ() override
Common interface for getting the z axis acceleration.
Definition: SendableBase.h:19
Definition: SendableBuilder.h:23
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:15
double GetY() override
Common interface for getting the y axis acceleration.
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.