WPILibC++ 2024.1.1-beta-4
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 enum Range { kRange_2G = 0, kRange_4G = 1, kRange_8G = 2, kRange_16G = 3 };
30
31 enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
32
33 struct AllAxes {
34 double XAxis;
35 double YAxis;
36 double ZAxis;
37 };
38
39 /**
40 * Constructs the ADXL345 Accelerometer over I2C.
41 *
42 * @param port The I2C port the accelerometer is attached to
43 * @param range The range (+ or -) that the accelerometer will measure
44 * @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
45 */
46 explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
47 int deviceAddress = kAddress);
48 ~ADXL345_I2C() override = default;
49
52
55
56 /**
57 * Set the measuring range of the accelerometer.
58 *
59 * @param range The maximum acceleration, positive or negative, that the
60 * accelerometer will measure.
61 */
62 void SetRange(Range range);
63
64 /**
65 * Returns the acceleration along the X axis in g-forces.
66 *
67 * @return The acceleration along the X axis in g-forces.
68 */
69 double GetX();
70
71 /**
72 * Returns the acceleration along the Y axis in g-forces.
73 *
74 * @return The acceleration along the Y axis in g-forces.
75 */
76 double GetY();
77
78 /**
79 * Returns the acceleration along the Z axis in g-forces.
80 *
81 * @return The acceleration along the Z axis in g-forces.
82 */
83 double GetZ();
84
85 /**
86 * Get the acceleration of one axis in Gs.
87 *
88 * @param axis The axis to read from.
89 * @return Acceleration of the ADXL345 in Gs.
90 */
91 virtual double GetAcceleration(Axes axis);
92
93 /**
94 * Get the acceleration of all axes in Gs.
95 *
96 * @return An object containing the acceleration measured on each axis of the
97 * ADXL345 in Gs.
98 */
100
101 void InitSendable(nt::NTSendableBuilder& builder) override;
102
103 protected:
105
111
112 static constexpr int kAddress = 0x1D;
113 static constexpr int kPowerCtlRegister = 0x2D;
114 static constexpr int kDataFormatRegister = 0x31;
115 static constexpr int kDataRegister = 0x32;
116 static constexpr double kGsPerLSB = 0.00390625;
117
122 kPowerCtl_Sleep = 0x04
123 };
124
131 };
132};
133
134} // namespace frc
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:27
I2C m_i2c
Definition: ADXL345_I2C.h:104
static constexpr int kDataFormatRegister
Definition: ADXL345_I2C.h:114
static constexpr int kAddress
Definition: ADXL345_I2C.h:112
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
PowerCtlFields
Definition: ADXL345_I2C.h:118
@ kPowerCtl_Sleep
Definition: ADXL345_I2C.h:122
@ kPowerCtl_AutoSleep
Definition: ADXL345_I2C.h:120
@ kPowerCtl_Link
Definition: ADXL345_I2C.h:119
@ kPowerCtl_Measure
Definition: ADXL345_I2C.h:121
hal::SimDouble m_simX
Definition: ADXL345_I2C.h:108
double GetZ()
Returns the acceleration along the Z axis in g-forces.
double GetY()
Returns the acceleration along the Y axis in g-forces.
hal::SimEnum m_simRange
Definition: ADXL345_I2C.h:107
~ADXL345_I2C() override=default
int GetI2CDeviceAddress() const
static constexpr int kDataRegister
Definition: ADXL345_I2C.h:115
Range
Definition: ADXL345_I2C.h:29
@ kRange_16G
Definition: ADXL345_I2C.h:29
@ kRange_2G
Definition: ADXL345_I2C.h:29
@ kRange_4G
Definition: ADXL345_I2C.h:29
@ kRange_8G
Definition: ADXL345_I2C.h:29
ADXL345_I2C & operator=(ADXL345_I2C &&)=default
hal::SimDouble m_simZ
Definition: ADXL345_I2C.h:110
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
static constexpr double kGsPerLSB
Definition: ADXL345_I2C.h:116
static constexpr int kPowerCtlRegister
Definition: ADXL345_I2C.h:113
Axes
Definition: ADXL345_I2C.h:31
@ kAxis_X
Definition: ADXL345_I2C.h:31
@ kAxis_Y
Definition: ADXL345_I2C.h:31
@ kAxis_Z
Definition: ADXL345_I2C.h:31
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.
hal::SimDouble m_simY
Definition: ADXL345_I2C.h:109
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
hal::SimDevice m_simDevice
Definition: ADXL345_I2C.h:106
void SetRange(Range range)
Set the measuring range of the accelerometer.
ADXL345_I2C(ADXL345_I2C &&)=default
DataFormatFields
Definition: ADXL345_I2C.h:125
@ kDataFormat_SelfTest
Definition: ADXL345_I2C.h:126
@ kDataFormat_IntInvert
Definition: ADXL345_I2C.h:128
@ kDataFormat_Justify
Definition: ADXL345_I2C.h:130
@ kDataFormat_FullRes
Definition: ADXL345_I2C.h:129
@ kDataFormat_SPI
Definition: ADXL345_I2C.h:127
I2C::Port GetI2CPort() const
I2C bus interface class.
Definition: I2C.h:23
Port
Definition: I2C.h:25
A move-only C++ wrapper around a HAL simulator device handle.
Definition: SimDevice.h:644
C++ wrapper around a HAL simulator double value handle.
Definition: SimDevice.h:535
C++ wrapper around a HAL simulator enum value handle.
Definition: SimDevice.h:576
Definition: NTSendableBuilder.h:18
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
Definition: ADXL345_I2C.h:33
double XAxis
Definition: ADXL345_I2C.h:34
double YAxis
Definition: ADXL345_I2C.h:35
double ZAxis
Definition: ADXL345_I2C.h:36