2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this 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>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/Sendable.h>
|
|
|
|
|
#include <wpi/sendable/SendableHelper.h>
|
2019-09-29 00:57:16 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SPI.h"
|
2021-03-12 15:52:02 -08:00
|
|
|
#include "frc/interfaces/Gyro.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
|
|
|
*
|
2021-09-12 15:18:45 -07:00
|
|
|
* The %Gyro class tracks the robots heading based on the starting position. As
|
2015-11-06 14:49:28 -08:00
|
|
|
* 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.
|
2021-09-12 15:18:45 -07:00
|
|
|
* Only one instance of an ADXRS %Gyro is supported.
|
2015-11-06 14:49:28 -08:00
|
|
|
*/
|
2021-03-12 15:52:02 -08:00
|
|
|
class ADXRS450_Gyro : public Gyro,
|
2021-06-13 16:38:05 -07:00
|
|
|
public wpi::Sendable,
|
|
|
|
|
public wpi::SendableHelper<ADXRS450_Gyro> {
|
2015-11-06 14:49:28 -08:00
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
2021-09-12 15:18:45 -07:00
|
|
|
* %Gyro constructor on onboard CS0.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
2015-11-06 14:49:28 -08:00
|
|
|
ADXRS450_Gyro();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
2021-09-12 15:18:45 -07:00
|
|
|
* %Gyro constructor on the specified SPI port.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* @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;
|
|
|
|
|
|
2022-10-24 20:04:16 -07:00
|
|
|
bool IsConnected() const;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Return the actual angle in degrees that the robot is currently facing.
|
|
|
|
|
*
|
2020-07-12 15:34:28 -07:00
|
|
|
* The angle is based on integration of the returned rate from the gyro.
|
2018-05-31 20:47:15 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2020-07-12 15:34:28 -07:00
|
|
|
* @return the current heading of the robot in degrees.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
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
|
|
|
|
|
*
|
2020-07-12 15:34:28 -07:00
|
|
|
* The rate is based on the most recent reading of the gyro.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
2020-12-26 22:06:28 -08:00
|
|
|
void Calibrate() final;
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2020-10-22 20:40:27 -07:00
|
|
|
/**
|
|
|
|
|
* Get the SPI port number.
|
|
|
|
|
*
|
|
|
|
|
* @return The SPI port number.
|
|
|
|
|
*/
|
|
|
|
|
int GetPort() const;
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void InitSendable(wpi::SendableBuilder& builder) override;
|
2021-03-12 15:52:02 -08:00
|
|
|
|
2015-11-06 14:49:28 -08:00
|
|
|
private:
|
|
|
|
|
SPI m_spi;
|
2020-10-22 20:40:27 -07:00
|
|
|
SPI::Port m_port;
|
2022-10-24 20:04:16 -07:00
|
|
|
bool m_connected{false};
|
2015-11-06 14:49:28 -08:00
|
|
|
|
2019-09-29 00:57:16 -07:00
|
|
|
hal::SimDevice m_simDevice;
|
2022-10-24 20:04:16 -07:00
|
|
|
hal::SimBoolean m_simConnected;
|
2019-09-29 00:57:16 -07:00
|
|
|
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
|