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"
|
|
|
|
|
#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.
|
|
|
|
|
*/
|
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);
|
2014-12-30 18:20:35 -08:00
|
|
|
LiveWindow::GetInstance()->AddSensor("ADXL345_I2C", port, this);
|
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
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return An object containing the acceleration measured on each axis of the ADXL345 in Gs.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
2014-12-30 18:20:35 -08:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string ADXL345_I2C::GetSmartDashboardType() const {
|
2014-12-30 18:20:35 -08:00
|
|
|
return "3AxisAccelerometer";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_I2C::InitTable(ITable *subtable) {
|
|
|
|
|
m_table = subtable;
|
|
|
|
|
UpdateTable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_I2C::UpdateTable() {
|
|
|
|
|
if (m_table != NULL) {
|
|
|
|
|
m_table->PutNumber("X", GetX());
|
|
|
|
|
m_table->PutNumber("Y", GetY());
|
|
|
|
|
m_table->PutNumber("Z", GetZ());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
ITable* ADXL345_I2C::GetTable() const {
|
2014-12-30 18:20:35 -08:00
|
|
|
return m_table;
|
|
|
|
|
}
|