WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ADXL345_I2C.hpp
Go to the documentation of this file.
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
11
12namespace wpi {
13
14/**
15 * ADXL345 Accelerometer on I2C.
16 *
17 * This class allows access to a Analog Devices ADXL345 3-axis accelerometer on
18 * an I2C bus. This class assumes the default (not alternate) sensor address of
19 * 0x1D (7-bit address).
20 */
22 public wpi::util::SendableHelper<ADXL345_I2C> {
23 public:
24 /**
25 * Accelerometer range.
26 */
27 enum Range {
28 /// 2 Gs max.
30 /// 4 Gs max.
32 /// 8 Gs max.
34 /// 16 Gs max.
36 };
37
38 /**
39 * Accelerometer axes.
40 */
41 enum Axes {
42 /// X axis.
43 kAxis_X = 0x00,
44 /// Y axis.
45 kAxis_Y = 0x02,
46 /// Z axis.
47 kAxis_Z = 0x04
48 };
49
50 /**
51 * Container type for accelerations from all axes.
52 */
53 struct AllAxes {
54 /// Acceleration along the X axis in g-forces.
55 double XAxis = 0.0;
56 /// Acceleration along the Y axis in g-forces.
57 double YAxis = 0.0;
58 /// Acceleration along the Z axis in g-forces.
59 double ZAxis = 0.0;
60 };
61
62 /// Default I2C device address.
63 static constexpr int kAddress = 0x1D;
64
65 /**
66 * Constructs the ADXL345 Accelerometer over I2C.
67 *
68 * @param port The I2C port the accelerometer is attached to
69 * @param range The range (+ or -) that the accelerometer will measure
70 * @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
71 */
72 explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
73 int deviceAddress = kAddress);
74 ~ADXL345_I2C() override = default;
75
78
81
82 /**
83 * Set the measuring range of the accelerometer.
84 *
85 * @param range The maximum acceleration, positive or negative, that the
86 * accelerometer will measure.
87 */
88 void SetRange(Range range);
89
90 /**
91 * Returns the acceleration along the X axis in g-forces.
92 *
93 * @return The acceleration along the X axis in g-forces.
94 */
95 double GetX();
96
97 /**
98 * Returns the acceleration along the Y axis in g-forces.
99 *
100 * @return The acceleration along the Y axis in g-forces.
101 */
102 double GetY();
103
104 /**
105 * Returns the acceleration along the Z axis in g-forces.
106 *
107 * @return The acceleration along the Z axis in g-forces.
108 */
109 double GetZ();
110
111 /**
112 * Get the acceleration of one axis in Gs.
113 *
114 * @param axis The axis to read from.
115 * @return Acceleration of the ADXL345 in Gs.
116 */
117 virtual double GetAcceleration(Axes axis);
118
119 /**
120 * Get the acceleration of all axes in Gs.
121 *
122 * @return An object containing the acceleration measured on each axis of the
123 * ADXL345 in Gs.
124 */
126
128
129 private:
130 I2C m_i2c;
131
132 wpi::hal::SimDevice m_simDevice;
133 wpi::hal::SimEnum m_simRange;
134 wpi::hal::SimDouble m_simX;
135 wpi::hal::SimDouble m_simY;
136 wpi::hal::SimDouble m_simZ;
137
138 static constexpr int kPowerCtlRegister = 0x2D;
139 static constexpr int kDataFormatRegister = 0x31;
140 static constexpr int kDataRegister = 0x32;
141 static constexpr double kGsPerLSB = 0.00390625;
142
143 enum PowerCtlFields {
144 kPowerCtl_Link = 0x20,
145 kPowerCtl_AutoSleep = 0x10,
146 kPowerCtl_Measure = 0x08,
147 kPowerCtl_Sleep = 0x04
148 };
149
150 enum DataFormatFields {
151 kDataFormat_SelfTest = 0x80,
152 kDataFormat_SPI = 0x40,
153 kDataFormat_IntInvert = 0x20,
154 kDataFormat_FullRes = 0x08,
155 kDataFormat_Justify = 0x04
156 };
157};
158
159} // namespace wpi
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
void SetRange(Range range)
Set the measuring range of the accelerometer.
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
int GetI2CDeviceAddress() const
double GetY()
Returns the acceleration along the Y axis in g-forces.
ADXL345_I2C & operator=(ADXL345_I2C &&)=default
Axes
Accelerometer axes.
Definition ADXL345_I2C.hpp:41
@ kAxis_X
X axis.
Definition ADXL345_I2C.hpp:43
@ kAxis_Z
Z axis.
Definition ADXL345_I2C.hpp:47
@ kAxis_Y
Y axis.
Definition ADXL345_I2C.hpp:45
I2C::Port GetI2CPort() const
ADXL345_I2C(I2C::Port port, Range range=kRange_2G, int deviceAddress=kAddress)
Constructs the ADXL345 Accelerometer over I2C.
Range
Accelerometer range.
Definition ADXL345_I2C.hpp:27
@ kRange_8G
8 Gs max.
Definition ADXL345_I2C.hpp:33
@ kRange_4G
4 Gs max.
Definition ADXL345_I2C.hpp:31
@ kRange_2G
2 Gs max.
Definition ADXL345_I2C.hpp:29
@ kRange_16G
16 Gs max.
Definition ADXL345_I2C.hpp:35
void InitSendable(wpi::nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
static constexpr int kAddress
Default I2C device address.
Definition ADXL345_I2C.hpp:63
~ADXL345_I2C() override=default
ADXL345_I2C(ADXL345_I2C &&)=default
double GetZ()
Returns the acceleration along the Z axis in g-forces.
double GetX()
Returns the acceleration along the X axis in g-forces.
I2C bus interface class.
Definition I2C.hpp:21
Port
I2C connection ports.
Definition I2C.hpp:26
A move-only C++ wrapper around a HAL simulator device handle.
Definition SimDevice.hpp:278
C++ wrapper around a HAL simulator double value handle.
Definition SimDevice.hpp:169
C++ wrapper around a HAL simulator enum value handle.
Definition SimDevice.hpp:210
Helper class for building Sendable dashboard representations for NetworkTables.
Definition NTSendableBuilder.hpp:21
Interface for NetworkTable Sendable objects.
Definition NTSendable.hpp:16
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Definition CvSource.hpp:15
Container type for accelerations from all axes.
Definition ADXL345_I2C.hpp:53
double ZAxis
Acceleration along the Z axis in g-forces.
Definition ADXL345_I2C.hpp:59
double YAxis
Acceleration along the Y axis in g-forces.
Definition ADXL345_I2C.hpp:57
double XAxis
Acceleration along the X axis in g-forces.
Definition ADXL345_I2C.hpp:55