2015-11-06 14:49:28 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-02-08 10:24:18 -08:00
|
|
|
/* Copyright (c) 2015-2020 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2015-11-06 14:49:28 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
2017-06-25 09:05:49 -07:00
|
|
|
#include <stdint.h>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
#include <hal/SimDevice.h>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/GyroBase.h"
|
|
|
|
|
#include "frc/SPI.h"
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2015-11-06 14:49:28 -08:00
|
|
|
/**
|
|
|
|
|
* Use a rate gyro to return the robots heading relative to a starting position.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
2015-11-06 14:49:28 -08:00
|
|
|
* The Gyro class tracks the robots heading based on the starting position. As
|
|
|
|
|
* the robot rotates the new heading is computed by integrating the rate of
|
|
|
|
|
* rotation returned by the sensor. When the class is instantiated, it does a
|
|
|
|
|
* short calibration routine where it samples the gyro while at rest to
|
|
|
|
|
* determine the default offset. This is subtracted from each sample to
|
|
|
|
|
* determine the heading.
|
|
|
|
|
*
|
|
|
|
|
* This class is for the digital ADXRS450 gyro sensor that connects via SPI.
|
2020-02-08 10:24:18 -08:00
|
|
|
* Only one instance of an ADXRS Gyro is supported.
|
2015-11-06 14:49:28 -08:00
|
|
|
*/
|
|
|
|
|
class ADXRS450_Gyro : public GyroBase {
|
|
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Gyro constructor on onboard CS0.
|
|
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
ADXRS450_Gyro();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gyro constructor on the specified SPI port.
|
|
|
|
|
*
|
|
|
|
|
* @param port The SPI port the gyro is attached to.
|
|
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
explicit ADXRS450_Gyro(SPI::Port port);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
~ADXRS450_Gyro() override = default;
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
ADXRS450_Gyro(ADXRS450_Gyro&&) = default;
|
|
|
|
|
ADXRS450_Gyro& operator=(ADXRS450_Gyro&&) = default;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Return the actual angle in degrees that the robot is currently facing.
|
|
|
|
|
*
|
|
|
|
|
* The angle is based on the current accumulator value corrected by the
|
|
|
|
|
* oversampling rate, the gyro type and the A/D calibration values.
|
|
|
|
|
* The angle is continuous, that is it will continue from 360->361 degrees.
|
|
|
|
|
* This allows algorithms that wouldn't want to see a discontinuity in the
|
|
|
|
|
* gyro output as it sweeps from 360 to 0 on the second time around.
|
|
|
|
|
*
|
|
|
|
|
* @return the current heading of the robot in degrees. This heading is based
|
|
|
|
|
* on integration of the returned rate from the gyro.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetAngle() const override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the rate of rotation of the gyro
|
|
|
|
|
*
|
|
|
|
|
* The rate is based on the most recent reading of the gyro analog value
|
|
|
|
|
*
|
|
|
|
|
* @return the current rate in degrees per second
|
|
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
double GetRate() const override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the gyro.
|
|
|
|
|
*
|
|
|
|
|
* Resets the gyro to a heading of zero. This can be used if there is
|
|
|
|
|
* significant drift in the gyro and it needs to be recalibrated after it has
|
|
|
|
|
* been running.
|
|
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
void Reset() override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the gyro.
|
|
|
|
|
*
|
|
|
|
|
* Calibrate the gyro by running for a number of samples and computing the
|
|
|
|
|
* center value. Then use the center value as the Accumulator center value for
|
|
|
|
|
* subsequent measurements.
|
|
|
|
|
*
|
|
|
|
|
* It's important to make sure that the robot is not moving while the
|
|
|
|
|
* centering calculations are in progress, this is typically done when the
|
|
|
|
|
* robot is first turned on while it's sitting at rest before the competition
|
|
|
|
|
* starts.
|
|
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
void Calibrate() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SPI m_spi;
|
|
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
hal::SimDevice m_simDevice;
|
|
|
|
|
hal::SimDouble m_simAngle;
|
|
|
|
|
hal::SimDouble m_simRate;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
uint16_t ReadRegister(int reg);
|
2015-11-06 14:49:28 -08:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|