mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
This is a major restructuring of the WPILib repository to simply build procedures and remove the remnants of Maven from everything except the eclipse plugins. Gradle files have been largely simplified or rewritten, taking advantage of splitting up parts of the build into separate build files for ease of reading. The eclipse plugins are now in a separate project, as is ntcore. All dependencies are resolved via Maven dependencies, with the Jenkins-maintained WPILib repo. Project structures have also been simplified: we no longer have separate subprojects inside wpilibc and wpilibj. Where possible, these changes hav been done with git renames, to make sure we still have full history for all repositories. Other unrelated subprojects have also been broken out: OutlineViewer is now a separate project. Change-Id: Ib4e2a6e1a2f66427a14f16612b0e0d69ed661878
95 lines
2.8 KiB
C++
95 lines
2.8 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
*/
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#include "ADXL345_I2C.h"
|
|
#include "I2C.h"
|
|
#include "HAL/HAL.hpp"
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
const uint8_t ADXL345_I2C::kAddress;
|
|
const uint8_t ADXL345_I2C::kPowerCtlRegister;
|
|
const uint8_t ADXL345_I2C::kDataFormatRegister;
|
|
const uint8_t ADXL345_I2C::kDataRegister;
|
|
constexpr double ADXL345_I2C::kGsPerLSB;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param port The I2C port the accelerometer is attached to
|
|
* @param range The range (+ or -) that the accelerometer will measure.
|
|
*/
|
|
ADXL345_I2C::ADXL345_I2C(Port port, Range range) : I2C(port, kAddress) {
|
|
// Turn on the measurements
|
|
Write(kPowerCtlRegister, kPowerCtl_Measure);
|
|
// Specify the data format to read
|
|
SetRange(range);
|
|
|
|
HALReport(HALUsageReporting::kResourceType_ADXL345,
|
|
HALUsageReporting::kADXL345_I2C, 0);
|
|
LiveWindow::GetInstance()->AddSensor("ADXL345_I2C", port, this);
|
|
}
|
|
|
|
/** {@inheritdoc} */
|
|
void ADXL345_I2C::SetRange(Range range) {
|
|
Write(kDataFormatRegister, kDataFormat_FullRes | (uint8_t)range);
|
|
}
|
|
|
|
/** {@inheritdoc} */
|
|
double ADXL345_I2C::GetX() { return GetAcceleration(kAxis_X); }
|
|
|
|
/** {@inheritdoc} */
|
|
double ADXL345_I2C::GetY() { return GetAcceleration(kAxis_Y); }
|
|
|
|
/** {@inheritdoc} */
|
|
double ADXL345_I2C::GetZ() { return GetAcceleration(kAxis_Z); }
|
|
|
|
/**
|
|
* Get the acceleration of one axis in Gs.
|
|
*
|
|
* @param axis The axis to read from.
|
|
* @return Acceleration of the ADXL345 in Gs.
|
|
*/
|
|
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
|
|
int16_t rawAccel = 0;
|
|
Read(kDataRegister + (uint8_t)axis, sizeof(rawAccel), (uint8_t *)&rawAccel);
|
|
return rawAccel * kGsPerLSB;
|
|
}
|
|
|
|
/**
|
|
* Get the acceleration of all axes in Gs.
|
|
*
|
|
* @return An object containing the acceleration measured on each axis of the
|
|
* ADXL345 in Gs.
|
|
*/
|
|
ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
|
|
AllAxes data = AllAxes();
|
|
int16_t rawData[3];
|
|
Read(kDataRegister, sizeof(rawData), (uint8_t *)rawData);
|
|
|
|
data.XAxis = rawData[0] * kGsPerLSB;
|
|
data.YAxis = rawData[1] * kGsPerLSB;
|
|
data.ZAxis = rawData[2] * kGsPerLSB;
|
|
return data;
|
|
}
|
|
|
|
std::string ADXL345_I2C::GetSmartDashboardType() const {
|
|
return "3AxisAccelerometer";
|
|
}
|
|
|
|
void ADXL345_I2C::InitTable(std::shared_ptr<ITable> subtable) {
|
|
m_table = subtable;
|
|
UpdateTable();
|
|
}
|
|
|
|
void ADXL345_I2C::UpdateTable() {
|
|
m_table->PutNumber("X", GetX());
|
|
m_table->PutNumber("Y", GetY());
|
|
m_table->PutNumber("Z", GetZ());
|
|
}
|
|
|
|
std::shared_ptr<ITable> ADXL345_I2C::GetTable() const { return m_table; }
|