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