WPILibC++ 2024.3.2
ADXL345_SPI.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/SPI.h"
12
13namespace frc {
14
15/**
16 * ADXL345 Accelerometer on SPI.
17 *
18 * This class allows access to an Analog Devices ADXL345 3-axis accelerometer
19 * via SPI. This class assumes the sensor is wired in 4-wire SPI mode.
20 */
22 public wpi::SendableHelper<ADXL345_SPI> {
23 public:
24 /**
25 * Accelerometer range.
26 */
27 enum Range {
28 /// 2 Gs max.
30 /// 4 Gs max.
32 /// 8 Gs max.
34 /// 16 Gs max.
35 kRange_16G = 3
36 };
37
38 /**
39 * Accelerometer axes.
40 */
41 enum Axes {
42 /// X axis.
43 kAxis_X = 0x00,
44 /// Y axis.
45 kAxis_Y = 0x02,
46 /// Z axis.
47 kAxis_Z = 0x04
48 };
49
50 /**
51 * Container type for accelerations from all axes.
52 */
53 struct AllAxes {
54 /// Acceleration along the X axis in g-forces.
55 double XAxis = 0.0;
56 /// Acceleration along the Y axis in g-forces.
57 double YAxis = 0.0;
58 /// Acceleration along the Z axis in g-forces.
59 double ZAxis = 0.0;
60 };
61
62 /**
63 * Constructor.
64 *
65 * @param port The SPI port the accelerometer is attached to
66 * @param range The range (+ or -) that the accelerometer will measure
67 */
68 explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G);
69
70 ~ADXL345_SPI() override = default;
71
74
76
77 /**
78 * Set the measuring range of the accelerometer.
79 *
80 * @param range The maximum acceleration, positive or negative, that the
81 * accelerometer will measure.
82 */
83 void SetRange(Range range);
84
85 /**
86 * Returns the acceleration along the X axis in g-forces.
87 *
88 * @return The acceleration along the X axis in g-forces.
89 */
90 double GetX();
91
92 /**
93 * Returns the acceleration along the Y axis in g-forces.
94 *
95 * @return The acceleration along the Y axis in g-forces.
96 */
97 double GetY();
98
99 /**
100 * Returns the acceleration along the Z axis in g-forces.
101 *
102 * @return The acceleration along the Z axis in g-forces.
103 */
104 double GetZ();
105
106 /**
107 * Get the acceleration of one axis in Gs.
108 *
109 * @param axis The axis to read from.
110 * @return Acceleration of the ADXL345 in Gs.
111 */
112 virtual double GetAcceleration(Axes axis);
113
114 /**
115 * Get the acceleration of all axes in Gs.
116 *
117 * @return An object containing the acceleration measured on each axis of the
118 * ADXL345 in Gs.
119 */
121
122 void InitSendable(nt::NTSendableBuilder& builder) override;
123
124 private:
125 SPI m_spi;
126
127 hal::SimDevice m_simDevice;
128 hal::SimEnum m_simRange;
129 hal::SimDouble m_simX;
130 hal::SimDouble m_simY;
131 hal::SimDouble m_simZ;
132
133 static constexpr int kPowerCtlRegister = 0x2D;
134 static constexpr int kDataFormatRegister = 0x31;
135 static constexpr int kDataRegister = 0x32;
136 static constexpr double kGsPerLSB = 0.00390625;
137
138 enum SPIAddressFields { kAddress_Read = 0x80, kAddress_MultiByte = 0x40 };
139
140 enum PowerCtlFields {
141 kPowerCtl_Link = 0x20,
142 kPowerCtl_AutoSleep = 0x10,
143 kPowerCtl_Measure = 0x08,
144 kPowerCtl_Sleep = 0x04
145 };
146
147 enum DataFormatFields {
148 kDataFormat_SelfTest = 0x80,
149 kDataFormat_SPI = 0x40,
150 kDataFormat_IntInvert = 0x20,
151 kDataFormat_FullRes = 0x08,
152 kDataFormat_Justify = 0x04
153 };
154};
155
156} // namespace frc
ADXL345 Accelerometer on SPI.
Definition: ADXL345_SPI.h:22
double GetX()
Returns the acceleration along the X axis in g-forces.
double GetZ()
Returns the acceleration along the Z axis in g-forces.
~ADXL345_SPI() override=default
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
SPI::Port GetSpiPort() const
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
double GetY()
Returns the acceleration along the Y axis in g-forces.
void SetRange(Range range)
Set the measuring range of the accelerometer.
Axes
Accelerometer axes.
Definition: ADXL345_SPI.h:41
@ kAxis_Y
Y axis.
Definition: ADXL345_SPI.h:45
@ kAxis_Z
Z axis.
Definition: ADXL345_SPI.h:47
@ kAxis_X
X axis.
Definition: ADXL345_SPI.h:43
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
ADXL345_SPI & operator=(ADXL345_SPI &&)=default
ADXL345_SPI(ADXL345_SPI &&)=default
ADXL345_SPI(SPI::Port port, Range range=kRange_2G)
Constructor.
Range
Accelerometer range.
Definition: ADXL345_SPI.h:27
@ kRange_16G
16 Gs max.
Definition: ADXL345_SPI.h:35
@ kRange_4G
4 Gs max.
Definition: ADXL345_SPI.h:31
@ kRange_2G
2 Gs max.
Definition: ADXL345_SPI.h:29
@ kRange_8G
8 Gs max.
Definition: ADXL345_SPI.h:33
SPI bus interface class.
Definition: SPI.h:26
Port
SPI port.
Definition: SPI.h:31
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_SPI.h:53
double YAxis
Acceleration along the Y axis in g-forces.
Definition: ADXL345_SPI.h:57
double ZAxis
Acceleration along the Z axis in g-forces.
Definition: ADXL345_SPI.h:59
double XAxis
Acceleration along the X axis in g-forces.
Definition: ADXL345_SPI.h:55