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