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