[wpilib] Rename ADXL345_I2C constants to all caps

This commit is contained in:
Peter Johnson
2026-03-20 20:35:13 -07:00
parent ea32c247db
commit f2929af00f
7 changed files with 145 additions and 156 deletions

View File

@@ -11,7 +11,23 @@
using namespace wpi;
ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
static constexpr int POWER_CTL_REGISTER = 0x2D;
static constexpr int DATA_FORMAT_REGISTER = 0x31;
static constexpr int DATA_REGISTER = 0x32;
static constexpr double GS_PER_LSB = 0.00390625;
// static constexpr uint8_t POWER_CTL_LINK = 0x20;
// static constexpr uint8_t POWER_CTL_AUTO_SLEEP = 0x10;
static constexpr uint8_t POWER_CTL_MEASURE = 0x08;
// static constexpr uint8_t POWER_CTL_SLEEP = 0x04;
// static constexpr uint8_t DATA_FORMAT_SELF_TEST = 0x80;
// static constexpr uint8_t DATA_FORMAT_SPI = 0x40;
// static constexpr uint8_t DATA_FORMAT_INT_INVERT = 0x20;
static constexpr uint8_t DATA_FORMAT_FULL_RES = 0x08;
// static constexpr uint8_t DATA_FORMAT_JUSTIFY = 0x04;
ADXL345_I2C::ADXL345_I2C(I2C::Port port, int range, int deviceAddress)
: m_i2c(port, deviceAddress),
m_simDevice("Accel:ADXL345_I2C", static_cast<int>(port), deviceAddress) {
if (m_simDevice) {
@@ -26,7 +42,7 @@ ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
"z", wpi::hal::SimDevice::Direction::INPUT, 0.0);
}
// Turn on the measurements
m_i2c.Write(kPowerCtlRegister, kPowerCtl_Measure);
m_i2c.Write(POWER_CTL_REGISTER, POWER_CTL_MEASURE);
// Specify the data format to read
SetRange(range);
@@ -45,54 +61,73 @@ int ADXL345_I2C::GetI2CDeviceAddress() const {
return m_i2c.GetDeviceAddress();
}
void ADXL345_I2C::SetRange(Range range) {
m_i2c.Write(kDataFormatRegister,
kDataFormat_FullRes | static_cast<uint8_t>(range));
void ADXL345_I2C::SetRange(int range) {
uint8_t value;
switch (range) {
case 2:
value = 0;
break;
case 4:
value = 1;
break;
case 8:
value = 2;
break;
case 16:
value = 3;
break;
default:
value = 0; // default to 2G if invalid value is passed in
}
m_i2c.Write(DATA_FORMAT_REGISTER, DATA_FORMAT_FULL_RES | value);
if (m_simRange) {
m_simRange.Set(range);
}
}
double ADXL345_I2C::GetX() {
return GetAcceleration(kAxis_X);
return GetAcceleration(Axis::X);
}
double ADXL345_I2C::GetY() {
return GetAcceleration(kAxis_Y);
return GetAcceleration(Axis::Y);
}
double ADXL345_I2C::GetZ() {
return GetAcceleration(kAxis_Z);
return GetAcceleration(Axis::Z);
}
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
if (axis == kAxis_X && m_simX) {
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axis axis) {
if (axis == Axis::X && m_simX) {
return m_simX.Get();
}
if (axis == kAxis_Y && m_simY) {
if (axis == Axis::Y && m_simY) {
return m_simY.Get();
}
if (axis == kAxis_Z && m_simZ) {
if (axis == Axis::Z && m_simZ) {
return m_simZ.Get();
}
int16_t rawAccel = 0;
m_i2c.Read(kDataRegister + static_cast<int>(axis), sizeof(rawAccel),
m_i2c.Read(DATA_REGISTER + static_cast<int>(axis), sizeof(rawAccel),
reinterpret_cast<uint8_t*>(&rawAccel));
return rawAccel * kGsPerLSB;
return rawAccel * GS_PER_LSB;
}
ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
AllAxes data;
if (m_simX && m_simY && m_simZ) {
data.XAxis = m_simX.Get();
data.YAxis = m_simY.Get();
data.ZAxis = m_simZ.Get();
data.x = m_simX.Get();
data.y = m_simY.Get();
data.z = m_simZ.Get();
return data;
}
int16_t rawData[3];
m_i2c.Read(kDataRegister, sizeof(rawData),
m_i2c.Read(DATA_REGISTER, sizeof(rawData),
reinterpret_cast<uint8_t*>(rawData));
data.XAxis = rawData[0] * kGsPerLSB;
data.YAxis = rawData[1] * kGsPerLSB;
data.ZAxis = rawData[2] * kGsPerLSB;
data.x = rawData[0] * GS_PER_LSB;
data.y = rawData[1] * GS_PER_LSB;
data.z = rawData[2] * GS_PER_LSB;
return data;
}
@@ -103,8 +138,8 @@ void ADXL345_I2C::InitSendable(wpi::nt::NTSendableBuilder& builder) {
y = wpi::nt::DoubleTopic{builder.GetTopic("Y")}.Publish(),
z = wpi::nt::DoubleTopic{builder.GetTopic("Z")}.Publish()]() mutable {
auto data = GetAccelerations();
x.Set(data.XAxis);
y.Set(data.YAxis);
z.Set(data.ZAxis);
x.Set(data.x);
y.Set(data.y);
z.Set(data.z);
});
}

View File

@@ -21,30 +21,16 @@ namespace wpi {
class ADXL345_I2C : public wpi::nt::NTSendable,
public wpi::util::SendableHelper<ADXL345_I2C> {
public:
/**
* Accelerometer range.
*/
enum Range {
/// 2 Gs max.
kRange_2G = 0,
/// 4 Gs max.
kRange_4G = 1,
/// 8 Gs max.
kRange_8G = 2,
/// 16 Gs max.
kRange_16G = 3
};
/**
* Accelerometer axes.
*/
enum Axes {
enum class Axis {
/// X axis.
kAxis_X = 0x00,
X = 0x00,
/// Y axis.
kAxis_Y = 0x02,
Y = 0x02,
/// Z axis.
kAxis_Z = 0x04
Z = 0x04
};
/**
@@ -52,25 +38,27 @@ class ADXL345_I2C : public wpi::nt::NTSendable,
*/
struct AllAxes {
/// Acceleration along the X axis in g-forces.
double XAxis = 0.0;
double x = 0.0;
/// Acceleration along the Y axis in g-forces.
double YAxis = 0.0;
double y = 0.0;
/// Acceleration along the Z axis in g-forces.
double ZAxis = 0.0;
double z = 0.0;
};
/// Default I2C device address.
static constexpr int kAddress = 0x1D;
static constexpr int DEFAULT_ADDRESS = 0x1D;
/**
* Constructs the ADXL345 Accelerometer over I2C.
*
* @param port The I2C port the accelerometer is attached to
* @param range The range (+ or -) that the accelerometer will measure
* @param range The range (+ or -) that the accelerometer will
* measure; valid values are 2, 4, 8, or 16 Gs. The
* default is 2 Gs.
* @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
*/
explicit ADXL345_I2C(I2C::Port port, Range range = kRange_2G,
int deviceAddress = kAddress);
explicit ADXL345_I2C(I2C::Port port, int range = 2,
int deviceAddress = DEFAULT_ADDRESS);
~ADXL345_I2C() override = default;
ADXL345_I2C(ADXL345_I2C&&) = default;
@@ -83,9 +71,9 @@ class ADXL345_I2C : public wpi::nt::NTSendable,
* Set the measuring range of the accelerometer.
*
* @param range The maximum acceleration, positive or negative, that the
* accelerometer will measure.
* accelerometer will measure. Valid values are 2, 4, 8, or 16 Gs.
*/
void SetRange(Range range);
void SetRange(int range);
/**
* Returns the acceleration along the X axis in g-forces.
@@ -114,7 +102,7 @@ class ADXL345_I2C : public wpi::nt::NTSendable,
* @param axis The axis to read from.
* @return Acceleration of the ADXL345 in Gs.
*/
virtual double GetAcceleration(Axes axis);
virtual double GetAcceleration(Axis axis);
/**
* Get the acceleration of all axes in Gs.
@@ -134,26 +122,6 @@ class ADXL345_I2C : public wpi::nt::NTSendable,
wpi::hal::SimDouble m_simX;
wpi::hal::SimDouble m_simY;
wpi::hal::SimDouble m_simZ;
static constexpr int kPowerCtlRegister = 0x2D;
static constexpr int kDataFormatRegister = 0x31;
static constexpr int kDataRegister = 0x32;
static constexpr double kGsPerLSB = 0.00390625;
enum PowerCtlFields {
kPowerCtl_Link = 0x20,
kPowerCtl_AutoSleep = 0x10,
kPowerCtl_Measure = 0x08,
kPowerCtl_Sleep = 0x04
};
enum DataFormatFields {
kDataFormat_SelfTest = 0x80,
kDataFormat_SPI = 0x40,
kDataFormat_IntInvert = 0x20,
kDataFormat_FullRes = 0x08,
kDataFormat_Justify = 0x04
};
};
} // namespace wpi

View File

@@ -3,15 +3,12 @@ extra_includes:
classes:
wpi::ADXL345_I2C:
constants:
- wpi::ADXL345_I2C::Range::kRange_2G
ignored_bases:
- wpi::util::SendableHelper<ADXL345_I2C>
enums:
Axes:
Range:
Axis:
attributes:
kAddress:
DEFAULT_ADDRESS:
methods:
ADXL345_I2C:
GetI2CPort:
@@ -25,6 +22,6 @@ classes:
InitSendable:
wpi::ADXL345_I2C::AllAxes:
attributes:
XAxis:
YAxis:
ZAxis:
x:
y:
z:

View File

@@ -5,7 +5,6 @@
#include "wpi/framework/TimedRobot.hpp"
#include "wpi/hardware/accelerometer/ADXL345_I2C.hpp"
#include "wpi/hardware/bus/I2C.hpp"
#include "wpi/hardware/discrete/AnalogInput.hpp"
/**
* ADXL346, 362 Accelerometer snippets for frc-docs.
@@ -27,8 +26,7 @@ class Robot : public wpi::TimedRobot {
private:
// Creates an ADXL345 accelerometer object with a measurement range from -8 to
// 8 G's
wpi::ADXL345_I2C m_accelerometer{wpi::I2C::Port::PORT_0,
wpi::ADXL345_I2C::Range::kRange_8G};
wpi::ADXL345_I2C m_accelerometer{wpi::I2C::Port::PORT_0, 8};
};
#ifndef RUNNING_WPILIB_TESTS

View File

@@ -21,49 +21,37 @@ import org.wpilib.util.sendable.SendableRegistry;
@SuppressWarnings("TypeName")
public class ADXL345_I2C implements NTSendable, AutoCloseable {
/** Default I2C device address. */
public static final byte kAddress = 0x1D;
public static final byte DEFAULT_ADDRESS = 0x1D;
private static final byte kPowerCtlRegister = 0x2D;
private static final byte kDataFormatRegister = 0x31;
private static final byte kDataRegister = 0x32;
private static final double kGsPerLSB = 0.00390625;
// private static final byte kPowerCtl_Link = 0x20;
// private static final byte kPowerCtl_AutoSleep = 0x10;
private static final byte kPowerCtl_Measure = 0x08;
// private static final byte kPowerCtl_Sleep = 0x04;
private static final byte POWER_CTL_REGISTER = 0x2D;
private static final byte DATA_FORMAT_REGISTER = 0x31;
private static final byte DATA_REGISTER = 0x32;
private static final double GS_PER_LSB = 0.00390625;
// private static final byte POWER_CTL_LINK = 0x20;
// private static final byte POWER_CTL_AUTO_SLEEP = 0x10;
private static final byte POWER_CTL_MEASURE = 0x08;
// private static final byte POWER_CTL_SLEEP = 0x04;
// private static final byte kDataFormat_SelfTest = (byte) 0x80;
// private static final byte kDataFormat_SPI = 0x40;
// private static final byte kDataFormat_IntInvert = 0x20;
private static final byte kDataFormat_FullRes = 0x08;
// private static final byte DATA_FORMAT_SELF_TEST = (byte) 0x80;
// private static final byte DATA_FORMAT_SPI = 0x40;
// private static final byte DATA_FORMAT_INT_INVERT = 0x20;
private static final byte DATA_FORMAT_FULL_RES = 0x08;
// private static final byte kDataFormat_Justify = 0x04;
/** Accelerometer range. */
public enum Range {
/** 2 Gs max. */
k2G,
/** 4 Gs max. */
k4G,
/** 8 Gs max. */
k8G,
/** 16 Gs max. */
k16G
}
// private static final byte DATA_FORMAT_JUSTIFY = 0x04;
/** Accelerometer axes. */
public enum Axes {
public enum Axis {
/** X axis. */
kX((byte) 0x00),
X((byte) 0x00),
/** Y axis. */
kY((byte) 0x02),
Y((byte) 0x02),
/** Z axis. */
kZ((byte) 0x04);
Z((byte) 0x04);
/** The integer value representing this enumeration. */
public final byte value;
Axes(byte value) {
Axis(byte value) {
this.value = value;
}
}
@@ -72,13 +60,13 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
@SuppressWarnings("MemberName")
public static class AllAxes {
/** Acceleration along the X axis in g-forces. */
public double XAxis;
public double x;
/** Acceleration along the Y axis in g-forces. */
public double YAxis;
public double y;
/** Acceleration along the Z axis in g-forces. */
public double ZAxis;
public double z;
/** Default constructor. */
public AllAxes() {}
@@ -96,21 +84,23 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* Constructs the ADXL345 Accelerometer with I2C address 0x1D.
*
* @param port The I2C port the accelerometer is attached to
* @param range The range (+ or -) that the accelerometer will measure.
* @param range The range (+ or -) that the accelerometer will measure. Valid values are 2, 4, 8,
* or 16 Gs.
*/
public ADXL345_I2C(I2C.Port port, Range range) {
this(port, range, kAddress);
public ADXL345_I2C(I2C.Port port, int range) {
this(port, range, DEFAULT_ADDRESS);
}
/**
* Constructs the ADXL345 Accelerometer over I2C.
*
* @param port The I2C port the accelerometer is attached to
* @param range The range (+ or -) that the accelerometer will measure.
* @param range The range (+ or -) that the accelerometer will measure. Valid values are 2, 4, 8,
* or 16 Gs.
* @param deviceAddress I2C address of the accelerometer (0x1D or 0x53)
*/
@SuppressWarnings("this-escape")
public ADXL345_I2C(I2C.Port port, Range range, int deviceAddress) {
public ADXL345_I2C(I2C.Port port, int range, int deviceAddress) {
m_i2c = new I2C(port, deviceAddress);
// simulation
@@ -129,7 +119,7 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
}
// Turn on the measurements
m_i2c.write(kPowerCtlRegister, kPowerCtl_Measure);
m_i2c.write(POWER_CTL_REGISTER, POWER_CTL_MEASURE);
setRange(range);
@@ -172,19 +162,20 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* Set the measuring range of the accelerometer.
*
* @param range The maximum acceleration, positive or negative, that the accelerometer will
* measure.
* measure. Valid values are 2, 4, 8, or 16 Gs.
*/
public final void setRange(Range range) {
public final void setRange(int range) {
final byte value =
switch (range) {
case k2G -> 0;
case k4G -> 1;
case k8G -> 2;
case k16G -> 3;
case 2 -> 0;
case 4 -> 1;
case 8 -> 2;
case 16 -> 3;
default -> 0; // default to 2G if invalid value is passed in
};
// Specify the data format to read
m_i2c.write(kDataFormatRegister, kDataFormat_FullRes | value);
m_i2c.write(DATA_FORMAT_REGISTER, DATA_FORMAT_FULL_RES | value);
if (m_simRange != null) {
m_simRange.set(value);
@@ -197,7 +188,7 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* @return The acceleration along the X axis in g-forces.
*/
public double getX() {
return getAcceleration(Axes.kX);
return getAcceleration(Axis.X);
}
/**
@@ -206,7 +197,7 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* @return The acceleration along the Y axis in g-forces.
*/
public double getY() {
return getAcceleration(Axes.kY);
return getAcceleration(Axis.Y);
}
/**
@@ -215,7 +206,7 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* @return The acceleration along the Z axis in g-forces.
*/
public double getZ() {
return getAcceleration(Axes.kZ);
return getAcceleration(Axis.Z);
}
/**
@@ -224,22 +215,22 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
* @param axis The axis to read from.
* @return Acceleration of the ADXL345 in Gs.
*/
public double getAcceleration(Axes axis) {
if (axis == Axes.kX && m_simX != null) {
public double getAcceleration(Axis axis) {
if (axis == Axis.X && m_simX != null) {
return m_simX.get();
}
if (axis == Axes.kY && m_simY != null) {
if (axis == Axis.Y && m_simY != null) {
return m_simY.get();
}
if (axis == Axes.kZ && m_simZ != null) {
if (axis == Axis.Z && m_simZ != null) {
return m_simZ.get();
}
ByteBuffer rawAccel = ByteBuffer.allocate(2);
m_i2c.read(kDataRegister + axis.value, 2, rawAccel);
m_i2c.read(DATA_REGISTER + axis.value, 2, rawAccel);
// Sensor is little endian... swap bytes
rawAccel.order(ByteOrder.LITTLE_ENDIAN);
return rawAccel.getShort(0) * kGsPerLSB;
return rawAccel.getShort(0) * GS_PER_LSB;
}
/**
@@ -250,19 +241,19 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
public AllAxes getAccelerations() {
AllAxes data = new AllAxes();
if (m_simX != null && m_simY != null && m_simZ != null) {
data.XAxis = m_simX.get();
data.YAxis = m_simY.get();
data.ZAxis = m_simZ.get();
data.x = m_simX.get();
data.y = m_simY.get();
data.z = m_simZ.get();
return data;
}
ByteBuffer rawData = ByteBuffer.allocate(6);
m_i2c.read(kDataRegister, 6, rawData);
m_i2c.read(DATA_REGISTER, 6, rawData);
// Sensor is little endian... swap bytes
rawData.order(ByteOrder.LITTLE_ENDIAN);
data.XAxis = rawData.getShort(0) * kGsPerLSB;
data.YAxis = rawData.getShort(2) * kGsPerLSB;
data.ZAxis = rawData.getShort(4) * kGsPerLSB;
data.x = rawData.getShort(0) * GS_PER_LSB;
data.y = rawData.getShort(2) * GS_PER_LSB;
data.z = rawData.getShort(4) * GS_PER_LSB;
return data;
}
@@ -278,9 +269,9 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
builder.setUpdateTable(
() -> {
AllAxes data = getAccelerations();
pubX.set(data.XAxis);
pubY.set(data.YAxis);
pubZ.set(data.ZAxis);
pubX.set(data.x);
pubY.set(data.y);
pubZ.set(data.z);
});
}
}

View File

@@ -7,15 +7,15 @@ package org.wpilib.simulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.wpilib.hardware.accelerometer.ADXL345_I2C;
import org.wpilib.hardware.bus.I2C;
import org.wpilib.hardware.hal.HAL;
class ADXL345SimTest {
@ParameterizedTest
@EnumSource(ADXL345_I2C.Range.class)
void testInitI2C(ADXL345_I2C.Range range) {
@ValueSource(ints = {2, 4, 8, 16})
void testInitI2C(int range) {
HAL.initialize(500, 0);
try (ADXL345_I2C accel = new ADXL345_I2C(I2C.Port.PORT_0, range)) {
@@ -30,9 +30,9 @@ class ADXL345SimTest {
assertEquals(2.29, accel.getZ());
ADXL345_I2C.AllAxes allAccel = accel.getAccelerations();
assertEquals(1.91, allAccel.XAxis);
assertEquals(-3.405, allAccel.YAxis);
assertEquals(2.29, allAccel.ZAxis);
assertEquals(1.91, allAccel.x);
assertEquals(-3.405, allAccel.y);
assertEquals(2.29, allAccel.z);
}
}
}

View File

@@ -14,7 +14,7 @@ import org.wpilib.hardware.bus.I2C;
*/
public class Robot extends TimedRobot {
// Creates an ADXL345 accelerometer object with a measurement range from -8 to 8 G's
ADXL345_I2C m_accelerometer345I2C = new ADXL345_I2C(I2C.Port.PORT_0, ADXL345_I2C.Range.k8G);
ADXL345_I2C m_accelerometer345I2C = new ADXL345_I2C(I2C.Port.PORT_0, 8);
/** Called once at the beginning of the robot program. */
public Robot() {}