blob: 43fc7cf65628779140ae3cdbec38ac30a9b74ba1 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include <memory>
11
12#include "GyroBase.h"
13#include "HAL/Types.h"
14
15namespace frc {
16
17class AnalogInput;
18
19/**
20 * Use a rate gyro to return the robots heading relative to a starting position.
21 * The Gyro class tracks the robots heading based on the starting position. As
22 * the robot rotates the new heading is computed by integrating the rate of
23 * rotation returned by the sensor. When the class is instantiated, it does a
24 * short calibration routine where it samples the gyro while at rest to
25 * determine the default offset. This is subtracted from each sample to
26 * determine the heading. This gyro class must be used with a channel that is
27 * assigned one of the Analog accumulators from the FPGA. See AnalogInput for
28 * the current accumulator assignments.
29 *
30 * This class is for gyro sensors that connect to an analog input.
31 */
32class AnalogGyro : public GyroBase {
33 public:
34 static const int kOversampleBits = 10;
35 static const int kAverageBits = 0;
36 static constexpr double kSamplesPerSecond = 50.0;
37 static constexpr double kCalibrationSampleTime = 5.0;
38 static constexpr double kDefaultVoltsPerDegreePerSecond = 0.007;
39
40 explicit AnalogGyro(int channel);
41 explicit AnalogGyro(AnalogInput* channel);
42 explicit AnalogGyro(std::shared_ptr<AnalogInput> channel);
43 AnalogGyro(int channel, int center, double offset);
44 AnalogGyro(std::shared_ptr<AnalogInput> channel, int center, double offset);
45 virtual ~AnalogGyro();
46
47 double GetAngle() const override;
48 double GetRate() const override;
49 virtual int GetCenter() const;
50 virtual double GetOffset() const;
51 void SetSensitivity(double voltsPerDegreePerSecond);
52 void SetDeadband(double volts);
53 void Reset() override;
54 virtual void InitGyro();
55 void Calibrate() override;
56
57 protected:
58 std::shared_ptr<AnalogInput> m_analog;
59
60 private:
61 HAL_GyroHandle m_gyroHandle = HAL_kInvalidHandle;
62};
63
64} // namespace frc