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