2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. 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_SPI.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "DigitalInput.h"
|
|
|
|
|
#include "DigitalOutput.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "HAL/HAL.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;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
const int ADXL345_SPI::kPowerCtlRegister;
|
|
|
|
|
const int ADXL345_SPI::kDataFormatRegister;
|
|
|
|
|
const int ADXL345_SPI::kDataRegister;
|
2013-12-15 18:30:16 -05:00
|
|
|
constexpr double ADXL345_SPI::kGsPerLSB;
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param port The SPI port the accelerometer is attached to
|
|
|
|
|
* @param range The range (+ or -) that the accelerometer will measure
|
2014-12-29 14:09:37 -05:00
|
|
|
*/
|
2016-05-26 21:37:23 -07:00
|
|
|
ADXL345_SPI::ADXL345_SPI(SPI::Port port, ADXL345_SPI::Range range)
|
|
|
|
|
: m_spi(port) {
|
|
|
|
|
m_spi.SetClockRate(500000);
|
|
|
|
|
m_spi.SetMSBFirst();
|
|
|
|
|
m_spi.SetSampleDataOnFalling();
|
|
|
|
|
m_spi.SetClockActiveLow();
|
|
|
|
|
m_spi.SetChipSelectActiveHigh();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
uint8_t commands[2];
|
|
|
|
|
// Turn on the measurements
|
|
|
|
|
commands[0] = kPowerCtlRegister;
|
|
|
|
|
commands[1] = kPowerCtl_Measure;
|
2016-05-26 21:37:23 -07:00
|
|
|
m_spi.Transaction(commands, commands, 2);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
SetRange(range);
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_ADXL345,
|
|
|
|
|
HALUsageReporting::kADXL345_SPI);
|
2015-07-21 01:38:17 -07:00
|
|
|
|
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_SPI", port, this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void ADXL345_SPI::SetRange(Range range) {
|
|
|
|
|
uint8_t commands[2];
|
|
|
|
|
|
|
|
|
|
// Specify the data format to read
|
|
|
|
|
commands[0] = kDataFormatRegister;
|
2016-08-11 23:38:45 -07:00
|
|
|
commands[1] = kDataFormat_FullRes | static_cast<uint8_t>(range & 0x03);
|
2016-05-26 21:37:23 -07:00
|
|
|
m_spi.Transaction(commands, commands, 2);
|
2014-07-22 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_SPI::GetX() { return GetAcceleration(kAxis_X); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_SPI::GetY() { return GetAcceleration(kAxis_Y); }
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double ADXL345_SPI::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_SPI::GetAcceleration(ADXL345_SPI::Axes axis) {
|
2015-07-21 01:38:17 -07:00
|
|
|
uint8_t buffer[3];
|
|
|
|
|
uint8_t command[3] = {0, 0, 0};
|
2016-08-11 23:38:45 -07:00
|
|
|
command[0] = (kAddress_Read | kAddress_MultiByte | kDataRegister) +
|
|
|
|
|
static_cast<uint8_t>(axis);
|
2016-05-26 21:37:23 -07:00
|
|
|
m_spi.Transaction(command, buffer, 3);
|
2015-07-21 01:38:17 -07:00
|
|
|
|
|
|
|
|
// Sensor is little endian... swap bytes
|
|
|
|
|
int16_t rawAccel = buffer[2] << 8 | buffer[1];
|
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_SPI::AllAxes ADXL345_SPI::GetAccelerations() {
|
|
|
|
|
AllAxes data = AllAxes();
|
|
|
|
|
uint8_t dataBuffer[7] = {0, 0, 0, 0, 0, 0, 0};
|
|
|
|
|
int16_t rawData[3];
|
2015-07-21 01:38:17 -07:00
|
|
|
|
|
|
|
|
// Select the data address.
|
|
|
|
|
dataBuffer[0] = (kAddress_Read | kAddress_MultiByte | kDataRegister);
|
2016-05-26 21:37:23 -07:00
|
|
|
m_spi.Transaction(dataBuffer, dataBuffer, 7);
|
2015-07-21 01:38:17 -07:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-07-21 01:38:17 -07:00
|
|
|
// Sensor is little endian... swap bytes
|
|
|
|
|
rawData[i] = dataBuffer[i * 2 + 2] << 8 | dataBuffer[i * 2 + 1];
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2015-07-21 01:38:17 -07:00
|
|
|
|
|
|
|
|
data.XAxis = rawData[0] * kGsPerLSB;
|
|
|
|
|
data.YAxis = rawData[1] * kGsPerLSB;
|
|
|
|
|
data.ZAxis = rawData[2] * kGsPerLSB;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
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_SPI::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "3AxisAccelerometer";
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void ADXL345_SPI::InitTable(std::shared_ptr<ITable> subtable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subtable;
|
|
|
|
|
UpdateTable();
|
2014-12-30 18:20:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ADXL345_SPI::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-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> ADXL345_SPI::GetTable() const { return m_table; }
|