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