WPILibC++  unspecified
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 "I2C.h"
11 #include "SensorBase.h"
12 #include "interfaces/Accelerometer.h"
13 
14 namespace frc {
15 
23 class ADXL345_I2C : public SensorBase, public Accelerometer {
24  public:
25  enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
26 
27  struct AllAxes {
28  double XAxis;
29  double YAxis;
30  double ZAxis;
31  };
32 
33  explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
34  int deviceAddress = kAddress);
35  ~ADXL345_I2C() override = default;
36 
37  ADXL345_I2C(const ADXL345_I2C&) = delete;
38  ADXL345_I2C& operator=(const ADXL345_I2C&) = delete;
39 
40  // Accelerometer interface
41  void SetRange(Range range) override;
42  double GetX() override;
43  double GetY() override;
44  double GetZ() override;
45 
46  virtual double GetAcceleration(Axes axis);
47  virtual AllAxes GetAccelerations();
48 
49  void InitSendable(SendableBuilder& builder) override;
50 
51  protected:
52  I2C m_i2c;
53 
54  static constexpr int kAddress = 0x1D;
55  static constexpr int kPowerCtlRegister = 0x2D;
56  static constexpr int kDataFormatRegister = 0x31;
57  static constexpr int kDataRegister = 0x32;
58  static constexpr double kGsPerLSB = 0.00390625;
59 
60  enum PowerCtlFields {
61  kPowerCtl_Link = 0x20,
62  kPowerCtl_AutoSleep = 0x10,
63  kPowerCtl_Measure = 0x08,
64  kPowerCtl_Sleep = 0x04
65  };
66 
67  enum DataFormatFields {
68  kDataFormat_SelfTest = 0x80,
69  kDataFormat_SPI = 0x40,
70  kDataFormat_IntInvert = 0x20,
71  kDataFormat_FullRes = 0x08,
72  kDataFormat_Justify = 0x04
73  };
74 };
75 
76 } // namespace frc
Definition: RobotController.cpp:14
ADXL345_I2C(I2C::Port port, Range range=kRange_2G, int deviceAddress=kAddress)
Constructs the ADXL345 Accelerometer over I2C.
Definition: ADXL345_I2C.cpp:23
Base class for all sensors.
Definition: SensorBase.h:25
Definition: ADXL345_I2C.h:27
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: ADXL345_I2C.cpp:77
void SetRange(Range range) override
Common interface for setting the measuring range of an accelerometer.
Definition: ADXL345_I2C.cpp:35
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:23
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
Definition: ADXL345_I2C.cpp:52
I2C bus interface class.
Definition: I2C.h:24
double GetZ() override
Common interface for getting the z axis acceleration.
Definition: ADXL345_I2C.cpp:44
double GetX() override
Common interface for getting the x axis acceleration.
Definition: ADXL345_I2C.cpp:40
Definition: SendableBuilder.h:23
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:15
double GetY() override
Common interface for getting the y axis acceleration.
Definition: ADXL345_I2C.cpp:42
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
Definition: ADXL345_I2C.cpp:65