2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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"
|
2014-01-06 09:27:51 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "I2C.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.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param range The range (+ or -) that the accelerometer will measure.
|
|
|
|
|
*/
|
2014-07-22 18:04:00 -04:00
|
|
|
ADXL345_I2C::ADXL345_I2C(Port port, Range range):
|
2014-06-18 15:38:02 -04:00
|
|
|
I2C(port, kAddress)
|
2013-12-15 18:30:16 -05:00
|
|
|
{
|
2014-06-18 15:38:02 -04:00
|
|
|
//m_i2c = new I2C((I2C::Port)port, kAddress);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
// Turn on the measurements
|
2014-06-18 15:38:02 -04:00
|
|
|
Write(kPowerCtlRegister, kPowerCtl_Measure);
|
2013-12-15 18:30:16 -05:00
|
|
|
// Specify the data format to read
|
2014-07-22 18:04:00 -04:00
|
|
|
SetRange(range);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-06-13 17:45:10 -04:00
|
|
|
HALReport(HALUsageReporting::kResourceType_ADXL345, HALUsageReporting::kADXL345_I2C, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
|
|
|
|
ADXL345_I2C::~ADXL345_I2C()
|
|
|
|
|
{
|
2014-06-18 15:38:02 -04:00
|
|
|
//delete m_i2c;
|
|
|
|
|
//m_i2c = NULL;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-07-22 18:04:00 -04:00
|
|
|
/** {@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);
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis)
|
|
|
|
|
{
|
|
|
|
|
int16_t rawAccel = 0;
|
2014-06-18 15:38:02 -04:00
|
|
|
//if(m_i2c)
|
|
|
|
|
//{
|
|
|
|
|
Read(kDataRegister + (uint8_t)axis, sizeof(rawAccel), (uint8_t *)&rawAccel);
|
|
|
|
|
//}
|
2013-12-15 18:30:16 -05:00
|
|
|
return rawAccel * kGsPerLSB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the acceleration of all axes in Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Acceleration measured on all axes of the ADXL345 in Gs.
|
|
|
|
|
*/
|
|
|
|
|
ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations()
|
|
|
|
|
{
|
2014-05-02 17:52:49 -04:00
|
|
|
AllAxes data = AllAxes();
|
2013-12-15 18:30:16 -05:00
|
|
|
int16_t rawData[3];
|
2014-06-18 15:38:02 -04:00
|
|
|
//if (m_i2c)
|
|
|
|
|
//{
|
|
|
|
|
Read(kDataRegister, sizeof(rawData), (uint8_t*)rawData);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
data.XAxis = rawData[0] * kGsPerLSB;
|
|
|
|
|
data.YAxis = rawData[1] * kGsPerLSB;
|
|
|
|
|
data.ZAxis = rawData[2] * kGsPerLSB;
|
2014-06-18 15:38:02 -04:00
|
|
|
//}
|
2013-12-15 18:30:16 -05:00
|
|
|
return data;
|
|
|
|
|
}
|