WPILibC++ 2027.0.0-alpha-5
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 axes.
26 */
27 enum class Axis {
28 /// X axis.
29 X = 0x00,
30 /// Y axis.
31 Y = 0x02,
32 /// Z axis.
33 Z = 0x04
34 };
35
36 /**
37 * Container type for accelerations from all axes.
38 */
39 struct AllAxes {
40 /// Acceleration along the X axis in g-forces.
41 double x = 0.0;
42 /// Acceleration along the Y axis in g-forces.
43 double y = 0.0;
44 /// Acceleration along the Z axis in g-forces.
45 double z = 0.0;
46 };
47
48 /// Default I2C device address.
49 static constexpr int DEFAULT_ADDRESS = 0x1D;
50
51 /**
52 * Constructs the ADXL345 Accelerometer over I2C.
53 *
54 * @param port The I2C port the accelerometer is attached to
55 * @param range The range (+ or -) that the accelerometer will
56 * measure; valid values are 2, 4, 8, or 16 Gs. The
57 * default is 2 Gs.
58 * @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
59 */
60 explicit ADXL345_I2C(I2C::Port port, int range = 2,
61 int deviceAddress = DEFAULT_ADDRESS);
62 ~ADXL345_I2C() override = default;
63
66
69
70 /**
71 * Set the measuring range of the accelerometer.
72 *
73 * @param range The maximum acceleration, positive or negative, that the
74 * accelerometer will measure. Valid values are 2, 4, 8, or 16 Gs.
75 */
76 void SetRange(int range);
77
78 /**
79 * Returns the acceleration along the X axis in g-forces.
80 *
81 * @return The acceleration along the X axis in g-forces.
82 */
83 double GetX();
84
85 /**
86 * Returns the acceleration along the Y axis in g-forces.
87 *
88 * @return The acceleration along the Y axis in g-forces.
89 */
90 double GetY();
91
92 /**
93 * Returns the acceleration along the Z axis in g-forces.
94 *
95 * @return The acceleration along the Z axis in g-forces.
96 */
97 double GetZ();
98
99 /**
100 * Get the acceleration of one axis in Gs.
101 *
102 * @param axis The axis to read from.
103 * @return Acceleration of the ADXL345 in Gs.
104 */
105 virtual double GetAcceleration(Axis axis);
106
107 /**
108 * Get the acceleration of all axes in Gs.
109 *
110 * @return An object containing the acceleration measured on each axis of the
111 * ADXL345 in Gs.
112 */
114
116
117 private:
118 I2C m_i2c;
119
120 wpi::hal::SimDevice m_simDevice;
121 wpi::hal::SimEnum m_simRange;
122 wpi::hal::SimDouble m_simX;
123 wpi::hal::SimDouble m_simY;
124 wpi::hal::SimDouble m_simZ;
125};
126
127} // namespace wpi
ADXL345_I2C(I2C::Port port, int range=2, int deviceAddress=DEFAULT_ADDRESS)
Constructs the ADXL345 Accelerometer over I2C.
void SetRange(int range)
Set the measuring range of the accelerometer.
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
int GetI2CDeviceAddress() const
static constexpr int DEFAULT_ADDRESS
Default I2C device address.
Definition ADXL345_I2C.hpp:49
double GetY()
Returns the acceleration along the Y axis in g-forces.
ADXL345_I2C & operator=(ADXL345_I2C &&)=default
I2C::Port GetI2CPort() const
Axis
Accelerometer axes.
Definition ADXL345_I2C.hpp:27
@ X
X axis.
Definition ADXL345_I2C.hpp:29
@ Z
Z axis.
Definition ADXL345_I2C.hpp:33
@ Y
Y axis.
Definition ADXL345_I2C.hpp:31
virtual double GetAcceleration(Axis axis)
Get the acceleration of one axis in Gs.
void InitSendable(wpi::nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
~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:39
double x
Acceleration along the X axis in g-forces.
Definition ADXL345_I2C.hpp:41
double z
Acceleration along the Z axis in g-forces.
Definition ADXL345_I2C.hpp:45
double y
Acceleration along the Y axis in g-forces.
Definition ADXL345_I2C.hpp:43