WPILibC++  2021.3.1
ADXL345_SPI.h
1 // Copyright (c) FIRST and other WPILib contributors.
2 // Open Source Software; you can modify and/or share it under the terms of
3 // the WPILib BSD license file in the root directory of this project.
4 
5 #pragma once
6 
7 #include <hal/SimDevice.h>
8 
9 #include "frc/ErrorBase.h"
10 #include "frc/SPI.h"
11 #include "frc/interfaces/Accelerometer.h"
12 #include "frc/smartdashboard/Sendable.h"
13 #include "frc/smartdashboard/SendableHelper.h"
14 
15 namespace frc {
16 
23 class ADXL345_SPI : public ErrorBase,
24  public Accelerometer,
25  public Sendable,
26  public SendableHelper<ADXL345_SPI> {
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 
42  explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G);
43 
44  ~ADXL345_SPI() override = default;
45 
46  ADXL345_SPI(ADXL345_SPI&&) = default;
47  ADXL345_SPI& operator=(ADXL345_SPI&&) = default;
48 
49  // Accelerometer interface
50  void SetRange(Range range) final;
51  double GetX() override;
52  double GetY() override;
53  double GetZ() override;
54 
61  virtual double GetAcceleration(Axes axis);
62 
70 
71  void InitSendable(SendableBuilder& builder) override;
72 
73  protected:
74  SPI m_spi;
75 
76  hal::SimDevice m_simDevice;
77  hal::SimEnum m_simRange;
78  hal::SimDouble m_simX;
79  hal::SimDouble m_simY;
80  hal::SimDouble m_simZ;
81 
82  static constexpr int kPowerCtlRegister = 0x2D;
83  static constexpr int kDataFormatRegister = 0x31;
84  static constexpr int kDataRegister = 0x32;
85  static constexpr double kGsPerLSB = 0.00390625;
86 
87  enum SPIAddressFields { kAddress_Read = 0x80, kAddress_MultiByte = 0x40 };
88 
89  enum PowerCtlFields {
90  kPowerCtl_Link = 0x20,
91  kPowerCtl_AutoSleep = 0x10,
92  kPowerCtl_Measure = 0x08,
93  kPowerCtl_Sleep = 0x04
94  };
95 
96  enum DataFormatFields {
97  kDataFormat_SelfTest = 0x80,
98  kDataFormat_SPI = 0x40,
99  kDataFormat_IntInvert = 0x20,
100  kDataFormat_FullRes = 0x08,
101  kDataFormat_Justify = 0x04
102  };
103 };
104 
105 } // 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::SetRange
void SetRange(Range range) final
Common interface for setting the measuring range of an accelerometer.
frc::ADXL345_SPI::GetAccelerations
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
frc::ADXL345_SPI::AllAxes
Definition: ADXL345_SPI.h:30
frc::Accelerometer
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:12
frc::ADXL345_SPI::GetY
double GetY() override
Common interface for getting the y axis acceleration.
frc::SPI
SPI bus interface class.
Definition: SPI.h:29
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:99
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:14
frc
WPILib FRC namespace.
Definition: VisionRunner.inc:9
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:25
frc::SendableBuilder
Definition: SendableBuilder.h:21