2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-09-14 15:22:54 -05:00
|
|
|
/* Copyright (c) 2008-2019 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
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
#include <hal/SimDevice.h>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ErrorBase.h"
|
|
|
|
|
#include "frc/SPI.h"
|
|
|
|
|
#include "frc/interfaces/Accelerometer.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/Sendable.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableHelper.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* ADXL345 Accelerometer on SPI.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This class allows access to an Analog Devices ADXL345 3-axis accelerometer
|
2017-11-16 00:33:51 -08:00
|
|
|
* via SPI. This class assumes the sensor is wired in 4-wire SPI mode.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2018-05-23 20:22:30 -07:00
|
|
|
class ADXL345_SPI : public ErrorBase,
|
2019-09-14 15:22:54 -05:00
|
|
|
public Accelerometer,
|
|
|
|
|
public Sendable,
|
|
|
|
|
public SendableHelper<ADXL345_SPI> {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
|
|
|
|
enum Axes { kAxis_X = 0x00, kAxis_Y = 0x02, kAxis_Z = 0x04 };
|
2017-11-16 00:33:51 -08:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
struct AllAxes {
|
|
|
|
|
double XAxis;
|
|
|
|
|
double YAxis;
|
|
|
|
|
double ZAxis;
|
|
|
|
|
};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param port The SPI port the accelerometer is attached to
|
|
|
|
|
* @param range The range (+ or -) that the accelerometer will measure
|
|
|
|
|
*/
|
2016-07-10 17:47:44 -07:00
|
|
|
explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
~ADXL345_SPI() override = default;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
ADXL345_SPI(ADXL345_SPI&&) = default;
|
|
|
|
|
ADXL345_SPI& operator=(ADXL345_SPI&&) = default;
|
2015-07-21 01:23:34 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Accelerometer interface
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetRange(Range range) override;
|
|
|
|
|
double GetX() override;
|
|
|
|
|
double GetY() override;
|
|
|
|
|
double GetZ() override;
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get the acceleration of one axis in Gs.
|
|
|
|
|
*
|
|
|
|
|
* @param axis The axis to read from.
|
|
|
|
|
* @return Acceleration of the ADXL345 in Gs.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual double GetAcceleration(Axes axis);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the acceleration of all axes in Gs.
|
|
|
|
|
*
|
|
|
|
|
* @return An object containing the acceleration measured on each axis of the
|
|
|
|
|
* ADXL345 in Gs.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual AllAxes GetAccelerations();
|
2014-07-22 18:04:00 -04:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void InitSendable(SendableBuilder& builder) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-26 21:37:23 -07:00
|
|
|
protected:
|
|
|
|
|
SPI m_spi;
|
|
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
hal::SimDevice m_simDevice;
|
|
|
|
|
hal::SimEnum m_simRange;
|
|
|
|
|
hal::SimDouble m_simX;
|
|
|
|
|
hal::SimDouble m_simY;
|
|
|
|
|
hal::SimDouble m_simZ;
|
|
|
|
|
|
2017-11-19 19:04:28 -08:00
|
|
|
static constexpr int kPowerCtlRegister = 0x2D;
|
|
|
|
|
static constexpr int kDataFormatRegister = 0x31;
|
|
|
|
|
static constexpr int kDataRegister = 0x32;
|
2017-11-16 00:33:51 -08:00
|
|
|
static constexpr double kGsPerLSB = 0.00390625;
|
|
|
|
|
|
|
|
|
|
enum SPIAddressFields { kAddress_Read = 0x80, kAddress_MultiByte = 0x40 };
|
|
|
|
|
|
|
|
|
|
enum PowerCtlFields {
|
|
|
|
|
kPowerCtl_Link = 0x20,
|
|
|
|
|
kPowerCtl_AutoSleep = 0x10,
|
|
|
|
|
kPowerCtl_Measure = 0x08,
|
|
|
|
|
kPowerCtl_Sleep = 0x04
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum DataFormatFields {
|
|
|
|
|
kDataFormat_SelfTest = 0x80,
|
|
|
|
|
kDataFormat_SPI = 0x40,
|
|
|
|
|
kDataFormat_IntInvert = 0x20,
|
|
|
|
|
kDataFormat_FullRes = 0x08,
|
|
|
|
|
kDataFormat_Justify = 0x04
|
|
|
|
|
};
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|