WPILibC++ 2023.4.3-108-ge5452e3
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"
13
14namespace frc {
15
16/**
17 * ADXL345 Accelerometer on I2C.
18 *
19 * This class allows access to a Analog Devices ADXL345 3-axis accelerometer on
20 * an I2C bus. This class assumes the default (not alternate) sensor address of
21 * 0x1D (7-bit address).
22 *
23 * The Onboard I2C port is subject to system lockups. See <a
24 * href="https://docs.wpilib.org/en/stable/docs/yearly-overview/known-issues.html#onboard-i2c-causing-system-lockups">
25 * WPILib Known Issues</a> page for details.
26 */
28 public nt::NTSendable,
29 public wpi::SendableHelper<ADXL345_I2C> {
30 public:
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 // Accelerometer interface
57 void SetRange(Range range) final;
58 double GetX() override;
59 double GetY() override;
60 double GetZ() override;
61
62 /**
63 * Get the acceleration of one axis in Gs.
64 *
65 * @param axis The axis to read from.
66 * @return Acceleration of the ADXL345 in Gs.
67 */
68 virtual double GetAcceleration(Axes axis);
69
70 /**
71 * Get the acceleration of all axes in Gs.
72 *
73 * @return An object containing the acceleration measured on each axis of the
74 * ADXL345 in Gs.
75 */
77
78 void InitSendable(nt::NTSendableBuilder& builder) override;
79
80 protected:
82
88
89 static constexpr int kAddress = 0x1D;
90 static constexpr int kPowerCtlRegister = 0x2D;
91 static constexpr int kDataFormatRegister = 0x31;
92 static constexpr int kDataRegister = 0x32;
93 static constexpr double kGsPerLSB = 0.00390625;
94
99 kPowerCtl_Sleep = 0x04
100 };
101
108 };
109};
110
111} // namespace frc
ADXL345 Accelerometer on I2C.
Definition: ADXL345_I2C.h:29
I2C m_i2c
Definition: ADXL345_I2C.h:81
static constexpr int kDataFormatRegister
Definition: ADXL345_I2C.h:91
void SetRange(Range range) final
Common interface for setting the measuring range of an accelerometer.
static constexpr int kAddress
Definition: ADXL345_I2C.h:89
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
PowerCtlFields
Definition: ADXL345_I2C.h:95
@ kPowerCtl_Sleep
Definition: ADXL345_I2C.h:99
@ kPowerCtl_AutoSleep
Definition: ADXL345_I2C.h:97
@ kPowerCtl_Link
Definition: ADXL345_I2C.h:96
@ kPowerCtl_Measure
Definition: ADXL345_I2C.h:98
hal::SimDouble m_simX
Definition: ADXL345_I2C.h:85
hal::SimEnum m_simRange
Definition: ADXL345_I2C.h:84
~ADXL345_I2C() override=default
int GetI2CDeviceAddress() const
static constexpr int kDataRegister
Definition: ADXL345_I2C.h:92
ADXL345_I2C & operator=(ADXL345_I2C &&)=default
hal::SimDouble m_simZ
Definition: ADXL345_I2C.h:87
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
static constexpr double kGsPerLSB
Definition: ADXL345_I2C.h:93
static constexpr int kPowerCtlRegister
Definition: ADXL345_I2C.h:90
double GetX() override
Common interface for getting the x axis acceleration.
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
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:86
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
hal::SimDevice m_simDevice
Definition: ADXL345_I2C.h:83
double GetZ() override
Common interface for getting the z axis acceleration.
ADXL345_I2C(ADXL345_I2C &&)=default
DataFormatFields
Definition: ADXL345_I2C.h:102
@ kDataFormat_SelfTest
Definition: ADXL345_I2C.h:103
@ kDataFormat_IntInvert
Definition: ADXL345_I2C.h:105
@ kDataFormat_Justify
Definition: ADXL345_I2C.h:107
@ kDataFormat_FullRes
Definition: ADXL345_I2C.h:106
@ kDataFormat_SPI
Definition: ADXL345_I2C.h:104
I2C::Port GetI2CPort() const
double GetY() override
Common interface for getting the y axis acceleration.
Interface for 3-axis accelerometers.
Definition: Accelerometer.h:12
Range
Definition: Accelerometer.h:20
@ kRange_2G
Definition: Accelerometer.h:20
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