2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 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
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "SPI.h"
|
|
|
|
|
#include "SensorBase.h"
|
|
|
|
|
#include "interfaces/Accelerometer.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
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
class ADXL345_SPI : public SensorBase, public Accelerometer {
|
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
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
explicit ADXL345_SPI(SPI::Port port, Range range = kRange_2G);
|
2017-12-04 23:28:33 -08:00
|
|
|
~ADXL345_SPI() override = default;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-07-21 01:23:34 -07:00
|
|
|
ADXL345_SPI(const ADXL345_SPI&) = delete;
|
|
|
|
|
ADXL345_SPI& operator=(const ADXL345_SPI&) = delete;
|
|
|
|
|
|
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
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual double GetAcceleration(Axes axis);
|
|
|
|
|
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;
|
|
|
|
|
|
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
|