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