WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
ADXL345_I2C.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. 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 
17 namespace frc {
18 
28  protected:
29  static const int kAddress = 0x1D;
30  static const int kPowerCtlRegister = 0x2D;
31  static const int kDataFormatRegister = 0x31;
32  static const int kDataRegister = 0x32;
33  static constexpr double kGsPerLSB = 0.00390625;
34  enum PowerCtlFields {
35  kPowerCtl_Link = 0x20,
36  kPowerCtl_AutoSleep = 0x10,
37  kPowerCtl_Measure = 0x08,
38  kPowerCtl_Sleep = 0x04
39  };
40  enum DataFormatFields {
41  kDataFormat_SelfTest = 0x80,
42  kDataFormat_SPI = 0x40,
43  kDataFormat_IntInvert = 0x20,
44  kDataFormat_FullRes = 0x08,
45  kDataFormat_Justify = 0x04
46  };
47 
48  public:
49  enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
50  struct AllAxes {
51  double XAxis;
52  double YAxis;
53  double ZAxis;
54  };
55 
56  public:
57  explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
58  int deviceAddress = kAddress);
59  virtual ~ADXL345_I2C() = default;
60 
61  ADXL345_I2C(const ADXL345_I2C&) = delete;
62  ADXL345_I2C& operator=(const ADXL345_I2C&) = delete;
63 
64  // Accelerometer interface
65  void SetRange(Range range) override;
66  double GetX() override;
67  double GetY() override;
68  double GetZ() override;
69 
70  virtual double GetAcceleration(Axes axis);
71  virtual AllAxes GetAccelerations();
72 
73  std::string GetSmartDashboardType() const override;
74  void InitTable(std::shared_ptr<ITable> subtable) override;
75  void UpdateTable() override;
76  std::shared_ptr<ITable> GetTable() const override;
77  void StartLiveWindowMode() override {}
78  void StopLiveWindowMode() override {}
79 
80  protected:
81  I2C m_i2c;
82 
83  private:
84  std::shared_ptr<ITable> m_table;
85 };
86 
87 } // namespace frc
void InitTable(std::shared_ptr< ITable > subtable) override
Initializes a table for this sendable object.
Definition: ADXL345_I2C.cpp:87
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:29
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
Definition: ADXL345_I2C.h:50
void SetRange(Range range) override
Common interface for setting the measuring range of an accelerometer.
Definition: ADXL345_I2C.cpp:41
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:27
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:83
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
Definition: ADXL345_I2C.cpp:58
I2C bus interface class.
Definition: I2C.h:23
double GetZ() override
Common interface for getting the z axis acceleration.
Definition: ADXL345_I2C.cpp:50
double GetX() override
Common interface for getting the x axis acceleration.
Definition: ADXL345_I2C.cpp:46
std::shared_ptr< ITable > GetTable() const override
Definition: ADXL345_I2C.cpp:98
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:92
double GetY() override
Common interface for getting the y axis acceleration.
Definition: ADXL345_I2C.cpp:48
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
Definition: ADXL345_I2C.cpp:71