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