2015-11-06 14:49:28 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-24 21:28:20 -07:00
|
|
|
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
|
2015-11-06 14:49:28 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ADXRS450_Gyro.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
|
|
|
|
#include "frc/Timer.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2019-08-24 21:28:20 -07:00
|
|
|
static constexpr auto kSamplePeriod = 0.0005_s;
|
2015-11-06 14:49:28 -08:00
|
|
|
static constexpr double kCalibrationSampleTime = 5.0;
|
|
|
|
|
static constexpr double kDegreePerSecondPerLSB = 0.0125;
|
|
|
|
|
|
2019-10-19 11:36:44 -07:00
|
|
|
// static constexpr int kRateRegister = 0x00;
|
|
|
|
|
// static constexpr int kTemRegister = 0x02;
|
|
|
|
|
// static constexpr int kLoCSTRegister = 0x04;
|
|
|
|
|
// static constexpr int kHiCSTRegister = 0x06;
|
|
|
|
|
// static constexpr int kQuadRegister = 0x08;
|
|
|
|
|
// static constexpr int kFaultRegister = 0x0A;
|
2016-09-06 00:01:45 -07:00
|
|
|
static constexpr int kPIDRegister = 0x0C;
|
2019-10-19 11:36:44 -07:00
|
|
|
// static constexpr int kSNHighRegister = 0x0E;
|
|
|
|
|
// static constexpr int kSNLowRegister = 0x10;
|
2015-11-06 14:49:28 -08:00
|
|
|
|
|
|
|
|
ADXRS450_Gyro::ADXRS450_Gyro() : ADXRS450_Gyro(SPI::kOnboardCS0) {}
|
|
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
ADXRS450_Gyro::ADXRS450_Gyro(SPI::Port port)
|
|
|
|
|
: m_spi(port), m_simDevice("ADXRS450_Gyro", port) {
|
|
|
|
|
if (m_simDevice) {
|
|
|
|
|
m_simAngle = m_simDevice.CreateDouble("Angle", false, 0.0);
|
|
|
|
|
m_simRate = m_simDevice.CreateDouble("Rate", false, 0.0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 14:49:28 -08:00
|
|
|
m_spi.SetClockRate(3000000);
|
|
|
|
|
m_spi.SetMSBFirst();
|
2018-05-14 18:16:36 -07:00
|
|
|
m_spi.SetSampleDataOnLeadingEdge();
|
2015-11-06 14:49:28 -08:00
|
|
|
m_spi.SetClockActiveHigh();
|
|
|
|
|
m_spi.SetChipSelectActiveLow();
|
|
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
if (!m_simDevice) {
|
|
|
|
|
// Validate the part ID
|
|
|
|
|
if ((ReadRegister(kPIDRegister) & 0xff00) != 0x5200) {
|
|
|
|
|
DriverStation::ReportError("could not find ADXRS450 gyro");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
m_spi.InitAccumulator(kSamplePeriod, 0x20000000u, 4, 0x0c00000eu,
|
|
|
|
|
0x04000000u, 10u, 16u, true, true);
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
Calibrate();
|
|
|
|
|
}
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_ADXRS450, port + 1);
|
2019-09-14 15:22:54 -05:00
|
|
|
|
|
|
|
|
SendableRegistry::GetInstance().AddLW(this, "ADXRS450_Gyro", port);
|
2015-11-06 14:49:28 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
static bool CalcParity(int v) {
|
2015-11-06 14:49:28 -08:00
|
|
|
bool parity = false;
|
|
|
|
|
while (v != 0) {
|
|
|
|
|
parity = !parity;
|
|
|
|
|
v = v & (v - 1);
|
|
|
|
|
}
|
|
|
|
|
return parity;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
static inline int BytesToIntBE(uint8_t* buf) {
|
|
|
|
|
int result = static_cast<int>(buf[0]) << 24;
|
|
|
|
|
result |= static_cast<int>(buf[1]) << 16;
|
|
|
|
|
result |= static_cast<int>(buf[2]) << 8;
|
|
|
|
|
result |= static_cast<int>(buf[3]);
|
2015-11-06 14:49:28 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
uint16_t ADXRS450_Gyro::ReadRegister(int reg) {
|
|
|
|
|
int cmd = 0x80000000 | static_cast<int>(reg) << 17;
|
2015-11-06 14:49:28 -08:00
|
|
|
if (!CalcParity(cmd)) cmd |= 1u;
|
|
|
|
|
|
|
|
|
|
// big endian
|
2016-08-11 23:38:45 -07:00
|
|
|
uint8_t buf[4] = {static_cast<uint8_t>((cmd >> 24) & 0xff),
|
|
|
|
|
static_cast<uint8_t>((cmd >> 16) & 0xff),
|
|
|
|
|
static_cast<uint8_t>((cmd >> 8) & 0xff),
|
|
|
|
|
static_cast<uint8_t>(cmd & 0xff)};
|
2015-11-06 14:49:28 -08:00
|
|
|
|
|
|
|
|
m_spi.Write(buf, 4);
|
|
|
|
|
m_spi.Read(false, buf, 4);
|
|
|
|
|
if ((buf[0] & 0xe0) == 0) return 0; // error, return 0
|
2016-08-11 23:38:45 -07:00
|
|
|
return static_cast<uint16_t>((BytesToIntBE(buf) >> 5) & 0xffff);
|
2015-11-06 14:49:28 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double ADXRS450_Gyro::GetAngle() const {
|
2019-09-29 00:57:16 -07:00
|
|
|
if (m_simAngle) return m_simAngle.Get();
|
2018-12-06 22:29:20 -08:00
|
|
|
return m_spi.GetAccumulatorIntegratedValue() * kDegreePerSecondPerLSB;
|
2015-11-06 14:49:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double ADXRS450_Gyro::GetRate() const {
|
2019-09-29 00:57:16 -07:00
|
|
|
if (m_simRate) return m_simRate.Get();
|
2016-07-10 17:47:44 -07:00
|
|
|
return static_cast<double>(m_spi.GetAccumulatorLastValue()) *
|
|
|
|
|
kDegreePerSecondPerLSB;
|
2015-11-06 14:49:28 -08:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
void ADXRS450_Gyro::Reset() {
|
|
|
|
|
if (m_simAngle) m_simAngle.Set(0.0);
|
|
|
|
|
if (m_simRate) m_simRate.Set(0.0);
|
|
|
|
|
m_spi.ResetAccumulator();
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
void ADXRS450_Gyro::Calibrate() {
|
|
|
|
|
Wait(0.1);
|
|
|
|
|
|
2018-12-06 22:29:20 -08:00
|
|
|
m_spi.SetAccumulatorIntegratedCenter(0);
|
2018-05-31 20:47:15 -07:00
|
|
|
m_spi.ResetAccumulator();
|
|
|
|
|
|
|
|
|
|
Wait(kCalibrationSampleTime);
|
|
|
|
|
|
2018-12-06 22:29:20 -08:00
|
|
|
m_spi.SetAccumulatorIntegratedCenter(m_spi.GetAccumulatorIntegratedAverage());
|
2018-05-31 20:47:15 -07:00
|
|
|
m_spi.ResetAccumulator();
|
|
|
|
|
}
|