2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "ADXL345_I2C.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "I2C.h"
|
2014-12-30 18:20:35 -08:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-12-31 14:56:11 -08:00
|
|
|
* Constructs the ADXL345 Accelerometer over I2C.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param port The I2C port the accelerometer is attached to
|
|
|
|
|
* @param range The range (+ or -) that the accelerometer will measure
|
|
|
|
|
* @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-05-26 21:37:23 -07:00
|
|
|
ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
|
|
|
|
|
: m_i2c(port, deviceAddress) {
|
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);
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
LiveWindow::GetInstance()->AddSensor("ADXL345_I2C", port, this);
|
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
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetX() { return GetAcceleration(kAxis_X); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_I2C::GetY() { return GetAcceleration(kAxis_Y); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2016-05-20 17:30:37 -07:00
|
|
|
* 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];
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void ADXL345_I2C::InitTable(std::shared_ptr<nt::NetworkTable> subtable) {
|
2017-09-02 00:35:30 -07:00
|
|
|
if (subtable) {
|
|
|
|
|
m_xEntry = subtable->GetEntry("X");
|
|
|
|
|
m_yEntry = subtable->GetEntry("Y");
|
|
|
|
|
m_zEntry = subtable->GetEntry("Z");
|
2017-09-02 00:17:43 -07:00
|
|
|
UpdateTable();
|
|
|
|
|
} else {
|
|
|
|
|
m_xEntry = nt::NetworkTableEntry();
|
|
|
|
|
m_yEntry = nt::NetworkTableEntry();
|
|
|
|
|
m_zEntry = nt::NetworkTableEntry();
|
|
|
|
|
}
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_I2C::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_xEntry) m_xEntry.SetDouble(GetX());
|
|
|
|
|
if (m_yEntry) m_yEntry.SetDouble(GetY());
|
|
|
|
|
if (m_zEntry) m_zEntry.SetDouble(GetZ());
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|