2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* 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"
|
2014-08-08 17:05:49 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2014-12-30 18:20:35 -08:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
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.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param port The I2C port the accelerometer is attached to
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param range The range (+ or -) that the accelerometer will measure.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
ADXL345_I2C::ADXL345_I2C(Port port, Range range) : I2C(port, kAddress) {
|
|
|
|
|
// m_i2c = new I2C((I2C::Port)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);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-07-22 18:04:00 -04:00
|
|
|
/** {@inheritdoc} */
|
2015-06-25 15:07:55 -04:00
|
|
|
void ADXL345_I2C::SetRange(Range range) {
|
|
|
|
|
Write(kDataFormatRegister, kDataFormat_FullRes | (uint8_t)range);
|
2014-07-22 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetX() { return GetAcceleration(kAxis_X); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetY() { return GetAcceleration(kAxis_Y); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetZ() { return GetAcceleration(kAxis_Z); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Get the acceleration of one axis in Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param axis The axis to read from.
|
|
|
|
|
* @return Acceleration of the ADXL345 in Gs.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
|
|
|
|
|
int16_t rawAccel = 0;
|
|
|
|
|
// if(m_i2c)
|
|
|
|
|
//{
|
|
|
|
|
Read(kDataRegister + (uint8_t)axis, sizeof(rawAccel), (uint8_t *)&rawAccel);
|
|
|
|
|
//}
|
|
|
|
|
return rawAccel * kGsPerLSB;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the acceleration of all axes in Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return An object containing the acceleration measured on each axis of the
|
|
|
|
|
* ADXL345 in Gs.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
|
|
|
|
|
AllAxes data = AllAxes();
|
|
|
|
|
int16_t rawData[3];
|
|
|
|
|
// if (m_i2c)
|
|
|
|
|
//{
|
|
|
|
|
Read(kDataRegister, sizeof(rawData), (uint8_t *)rawData);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string ADXL345_I2C::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "3AxisAccelerometer";
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_I2C::InitTable(ITable *subtable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subtable;
|
|
|
|
|
UpdateTable();
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_I2C::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("X", GetX());
|
|
|
|
|
m_table->PutNumber("Y", GetY());
|
|
|
|
|
m_table->PutNumber("Z", GetZ());
|
|
|
|
|
}
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *ADXL345_I2C::GetTable() const { return m_table; }
|