WPILibC++  unspecified
ADXL345_SPI.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 <memory>
11 #include <string>
12 
13 #include "LiveWindow/LiveWindowSendable.h"
14 #include "SPI.h"
15 #include "SensorBase.h"
16 #include "interfaces/Accelerometer.h"
17 #include "networktables/NetworkTableEntry.h"
18 
19 namespace frc {
20 
21 class DigitalInput;
22 class DigitalOutput;
23 
32  protected:
33  static const int kPowerCtlRegister = 0x2D;
34  static const int kDataFormatRegister = 0x31;
35  static const int kDataRegister = 0x32;
36  static constexpr double kGsPerLSB = 0.00390625;
37  enum SPIAddressFields { kAddress_Read = 0x80, kAddress_MultiByte = 0x40 };
38  enum PowerCtlFields {
39  kPowerCtl_Link = 0x20,
40  kPowerCtl_AutoSleep = 0x10,
41  kPowerCtl_Measure = 0x08,
42  kPowerCtl_Sleep = 0x04
43  };
44  enum DataFormatFields {
45  kDataFormat_SelfTest = 0x80,
46  kDataFormat_SPI = 0x40,
47  kDataFormat_IntInvert = 0x20,
48  kDataFormat_FullRes = 0x08,
49  kDataFormat_Justify = 0x04
50  };
51 
52  public:
53  enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
54  struct AllAxes {
55  double XAxis;
56  double YAxis;
57  double ZAxis;
58  };
59 
60  public:
61  explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G);
62  virtual ~ADXL345_SPI() = default;
63 
64  ADXL345_SPI(const ADXL345_SPI&) = delete;
65  ADXL345_SPI& operator=(const ADXL345_SPI&) = delete;
66 
67  // Accelerometer interface
68  void SetRange(Range range) override;
69  double GetX() override;
70  double GetY() override;
71  double GetZ() override;
72 
73  virtual double GetAcceleration(Axes axis);
74  virtual AllAxes GetAccelerations();
75 
76  std::string GetSmartDashboardType() const override;
77  void InitTable(std::shared_ptr<nt::NetworkTable> subtable) override;
78  void UpdateTable() override;
79  void StartLiveWindowMode() override {}
80  void StopLiveWindowMode() override {}
81 
82  protected:
83  SPI m_spi;
84 
85  private:
86  nt::NetworkTableEntry m_xEntry;
87  nt::NetworkTableEntry m_yEntry;
88  nt::NetworkTableEntry m_zEntry;
89 };
90 
91 } // namespace frc
double GetX() override
Common interface for getting the x axis acceleration.
Definition: ADXL345_SPI.cpp:60
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: ADXL345_SPI.h:80
Definition: Timer.cpp:18
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
void UpdateTable() override
Update the table for this sendable object with the latest values.
Definition: ADXL345_SPI.cpp:128
double GetZ() override
Common interface for getting the z axis acceleration.
Definition: ADXL345_SPI.cpp:64
SPI bus interface class.
Definition: SPI.h:28
void InitTable(std::shared_ptr< nt::NetworkTable > subtable) override
Initializes a table for this sendable object.
Definition: ADXL345_SPI.cpp:115
ADXL345_SPI(SPI::Port port, Range range=kRange_2G)
Constructor.
Definition: ADXL345_SPI.cpp:29
Definition: ADXL345_SPI.h:54
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
Definition: ADXL345_SPI.cpp:90
std::string GetSmartDashboardType() const override
Definition: ADXL345_SPI.cpp:111
Class to write to digital outputs.
Definition: DigitalOutput.h:27
ADXL345 Accelerometer on SPI.
Definition: ADXL345_SPI.h:31
NetworkTables Entry.
Definition: NetworkTableEntry.h:30
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: ADXL345_SPI.h:79
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:15
Class to read a digital input.
Definition: DigitalInput.h:29
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
Definition: ADXL345_SPI.cpp:72
void SetRange(Range range) override
Common interface for setting the measuring range of an accelerometer.
Definition: ADXL345_SPI.cpp:51
double GetY() override
Common interface for getting the y axis acceleration.
Definition: ADXL345_SPI.cpp:62