2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ADXL345_I2C.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <networktables/NTSendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-05-26 21:37:23 -07:00
|
|
|
ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
|
2019-09-29 00:57:16 -07:00
|
|
|
: m_i2c(port, deviceAddress),
|
2020-12-23 15:54:11 -08:00
|
|
|
m_simDevice("Accel:ADXL345_I2C", port, deviceAddress) {
|
2019-09-29 00:57:16 -07:00
|
|
|
if (m_simDevice) {
|
2020-12-23 15:54:11 -08:00
|
|
|
m_simRange = m_simDevice.CreateEnumDouble("range", hal::SimDevice::kOutput,
|
|
|
|
|
{"2G", "4G", "8G", "16G"},
|
|
|
|
|
{2.0, 4.0, 8.0, 16.0}, 0);
|
|
|
|
|
m_simX = m_simDevice.CreateDouble("x", hal::SimDevice::kInput, 0.0);
|
|
|
|
|
m_simY = m_simDevice.CreateDouble("y", hal::SimDevice::kInput, 0.0);
|
|
|
|
|
m_simZ = m_simDevice.CreateDouble("z", hal::SimDevice::kInput, 0.0);
|
2019-09-29 00:57:16 -07:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
// Turn on the measurements
|
2016-05-26 21:37:23 -07:00
|
|
|
m_i2c.Write(kPowerCtlRegister, kPowerCtl_Measure);
|
2015-06-25 15:07:55 -04:00
|
|
|
// Specify the data format to read
|
|
|
|
|
SetRange(range);
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_ADXL345,
|
|
|
|
|
HALUsageReporting::kADXL345_I2C, 0);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
wpi::SendableRegistry::GetInstance().AddLW(this, "ADXL345_I2C", port);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void ADXL345_I2C::SetRange(Range range) {
|
2016-08-11 23:38:45 -07:00
|
|
|
m_i2c.Write(kDataFormatRegister,
|
|
|
|
|
kDataFormat_FullRes | static_cast<uint8_t>(range));
|
2014-07-22 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double ADXL345_I2C::GetX() {
|
|
|
|
|
return GetAcceleration(kAxis_X);
|
|
|
|
|
}
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double ADXL345_I2C::GetY() {
|
|
|
|
|
return GetAcceleration(kAxis_Y);
|
|
|
|
|
}
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double ADXL345_I2C::GetZ() {
|
|
|
|
|
return GetAcceleration(kAxis_Z);
|
|
|
|
|
}
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (axis == kAxis_X && m_simX) {
|
|
|
|
|
return m_simX.Get();
|
|
|
|
|
}
|
|
|
|
|
if (axis == kAxis_Y && m_simY) {
|
|
|
|
|
return m_simY.Get();
|
|
|
|
|
}
|
|
|
|
|
if (axis == kAxis_Z && m_simZ) {
|
|
|
|
|
return m_simZ.Get();
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
int16_t rawAccel = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
m_i2c.Read(kDataRegister + static_cast<int>(axis), sizeof(rawAccel),
|
2016-07-10 17:47:44 -07:00
|
|
|
reinterpret_cast<uint8_t*>(&rawAccel));
|
2015-06-25 15:07:55 -04:00
|
|
|
return rawAccel * kGsPerLSB;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
|
2019-09-29 00:57:16 -07:00
|
|
|
AllAxes data;
|
|
|
|
|
if (m_simX && m_simY && m_simZ) {
|
|
|
|
|
data.XAxis = m_simX.Get();
|
|
|
|
|
data.YAxis = m_simY.Get();
|
|
|
|
|
data.ZAxis = m_simZ.Get();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
int16_t rawData[3];
|
2016-07-10 17:47:44 -07:00
|
|
|
m_i2c.Read(kDataRegister, sizeof(rawData),
|
|
|
|
|
reinterpret_cast<uint8_t*>(rawData));
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
data.XAxis = rawData[0] * kGsPerLSB;
|
|
|
|
|
data.YAxis = rawData[1] * kGsPerLSB;
|
|
|
|
|
data.ZAxis = rawData[2] * kGsPerLSB;
|
|
|
|
|
return data;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-12-30 18:20:35 -08:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void ADXL345_I2C::InitSendable(nt::NTSendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("3AxisAccelerometer");
|
|
|
|
|
auto x = builder.GetEntry("X").GetHandle();
|
|
|
|
|
auto y = builder.GetEntry("Y").GetHandle();
|
|
|
|
|
auto z = builder.GetEntry("Z").GetHandle();
|
2021-06-12 08:06:45 -07:00
|
|
|
builder.SetUpdateTable([=] {
|
2017-12-04 23:28:33 -08:00
|
|
|
auto data = GetAccelerations();
|
|
|
|
|
nt::NetworkTableEntry(x).SetDouble(data.XAxis);
|
|
|
|
|
nt::NetworkTableEntry(y).SetDouble(data.YAxis);
|
|
|
|
|
nt::NetworkTableEntry(z).SetDouble(data.ZAxis);
|
|
|
|
|
});
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|