WPILibC++ 2024.3.2
ADXL362.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 * ADXL362 SPI Accelerometer.
17 *
18 * This class allows access to an Analog Devices ADXL362 3-axis accelerometer.
19 */
20class ADXL362 : public nt::NTSendable, public wpi::SendableHelper<ADXL362> {
21 public:
22 /**
23 * Accelerometer range.
24 */
25 enum Range {
26 /// 2 Gs max.
28 /// 4 Gs max.
30 /// 8 Gs max.
31 kRange_8G = 2
32 };
33
34 /**
35 * Accelerometer axes.
36 */
37 enum Axes {
38 /// X axis.
39 kAxis_X = 0x00,
40 /// Y axis.
41 kAxis_Y = 0x02,
42 /// Z axis.
43 kAxis_Z = 0x04
44 };
45
46 /**
47 * Container type for accelerations from all axes.
48 */
49 struct AllAxes {
50 /// Acceleration along the X axis in g-forces.
51 double XAxis = 0.0;
52 /// Acceleration along the Y axis in g-forces.
53 double YAxis = 0.0;
54 /// Acceleration along the Z axis in g-forces.
55 double ZAxis = 0.0;
56 };
57
58 public:
59 /**
60 * Constructor. Uses the onboard CS1.
61 *
62 * @param range The range (+ or -) that the accelerometer will measure.
63 */
64 explicit ADXL362(Range range = kRange_2G);
65
66 /**
67 * Constructor.
68 *
69 * @param port The SPI port the accelerometer is attached to
70 * @param range The range (+ or -) that the accelerometer will measure.
71 */
72 explicit ADXL362(SPI::Port port, Range range = kRange_2G);
73
74 ~ADXL362() override = default;
75
76 ADXL362(ADXL362&&) = default;
77 ADXL362& operator=(ADXL362&&) = default;
78
80
81 /**
82 * Set the measuring range of the accelerometer.
83 *
84 * @param range The maximum acceleration, positive or negative, that the
85 * accelerometer will measure.
86 */
87 void SetRange(Range range);
88
89 /**
90 * Returns the acceleration along the X axis in g-forces.
91 *
92 * @return The acceleration along the X axis in g-forces.
93 */
94 double GetX();
95
96 /**
97 * Returns the acceleration along the Y axis in g-forces.
98 *
99 * @return The acceleration along the Y axis in g-forces.
100 */
101 double GetY();
102
103 /**
104 * Returns the acceleration along the Z axis in g-forces.
105 *
106 * @return The acceleration along the Z axis in g-forces.
107 */
108 double GetZ();
109
110 /**
111 * Get the acceleration of one axis in Gs.
112 *
113 * @param axis The axis to read from.
114 * @return Acceleration of the ADXL362 in Gs.
115 */
116 virtual double GetAcceleration(Axes axis);
117
118 /**
119 * Get the acceleration of all axes in Gs.
120 *
121 * @return An object containing the acceleration measured on each axis of the
122 * ADXL362 in Gs.
123 */
125
126 void InitSendable(nt::NTSendableBuilder& builder) override;
127
128 private:
129 SPI m_spi;
130 hal::SimDevice m_simDevice;
131 hal::SimEnum m_simRange;
132 hal::SimDouble m_simX;
133 hal::SimDouble m_simY;
134 hal::SimDouble m_simZ;
135 double m_gsPerLSB = 0.001;
136};
137
138} // namespace frc
ADXL362 SPI Accelerometer.
Definition: ADXL362.h:20
ADXL362(SPI::Port port, Range range=kRange_2G)
Constructor.
~ADXL362() override=default
void SetRange(Range range)
Set the measuring range of the accelerometer.
Range
Accelerometer range.
Definition: ADXL362.h:25
@ kRange_8G
8 Gs max.
Definition: ADXL362.h:31
@ kRange_2G
2 Gs max.
Definition: ADXL362.h:27
@ kRange_4G
4 Gs max.
Definition: ADXL362.h:29
virtual AllAxes GetAccelerations()
Get the acceleration of all axes in Gs.
double GetX()
Returns the acceleration along the X axis in g-forces.
virtual double GetAcceleration(Axes axis)
Get the acceleration of one axis in Gs.
ADXL362 & operator=(ADXL362 &&)=default
Axes
Accelerometer axes.
Definition: ADXL362.h:37
@ kAxis_X
X axis.
Definition: ADXL362.h:39
@ kAxis_Y
Y axis.
Definition: ADXL362.h:41
@ kAxis_Z
Z axis.
Definition: ADXL362.h:43
double GetY()
Returns the acceleration along the Y axis in g-forces.
ADXL362(ADXL362 &&)=default
SPI::Port GetSpiPort() const
double GetZ()
Returns the acceleration along the Z axis in g-forces.
void InitSendable(nt::NTSendableBuilder &builder) override
Initializes this Sendable object.
ADXL362(Range range=kRange_2G)
Constructor.
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: ADXL362.h:49
double YAxis
Acceleration along the Y axis in g-forces.
Definition: ADXL362.h:53
double XAxis
Acceleration along the X axis in g-forces.
Definition: ADXL362.h:51
double ZAxis
Acceleration along the Z axis in g-forces.
Definition: ADXL362.h:55