blob: 32196a86a8e9e5fdf60db30b1dbcb3fe4fa19c23 [file] [log] [blame]
Brian Silverman1a675112016-02-20 20:42:49 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2016. 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#include "AnalogGyro.h"
9#include "Timer.h"
10#include "WPIErrors.h"
11#include "LiveWindow/LiveWindow.h"
12
13const uint32_t AnalogGyro::kOversampleBits = 10;
14const uint32_t AnalogGyro::kAverageBits = 0;
15const float AnalogGyro::kSamplesPerSecond = 50.0;
16const float AnalogGyro::kCalibrationSampleTime = 5.0;
17const float AnalogGyro::kDefaultVoltsPerDegreePerSecond = 0.007;
18
19/**
20 * Initialize the gyro.
21 * Calibrate the gyro by running for a number of samples and computing the center value for this
22 * part. Then use the center value as the Accumulator center value for subsequent measurements.
23 * It's important to make sure that the robot is not moving while the centering calculations are
24 * in progress, this is typically done when the robot is first turned on while it's sitting at
25 * rest before the competition starts.
26 */
27void AnalogGyro::InitAnalogGyro(int channel)
28{
29 SetPIDSourceType(PIDSourceType::kDisplacement);
30
31 char buffer[50];
32 int n = sprintf(buffer, "analog/%d", channel);
33 impl = new SimGyro(buffer);
34
35 LiveWindow::GetInstance()->AddSensor("AnalogGyro", channel, this);
36}
37
38/**
39 * AnalogGyro constructor with only a channel..
40 *
41 * @param channel The analog channel the gyro is connected to.
42 */
43AnalogGyro::AnalogGyro(uint32_t channel)
44{
45 InitAnalogGyro(channel);
46}
47
48/**
49 * Reset the gyro.
50 * Resets the gyro to a heading of zero. This can be used if there is significant
51 * drift in the gyro and it needs to be recalibrated after it has been running.
52 */
53void AnalogGyro::Reset()
54{
55 impl->Reset();
56}
57
58void AnalogGyro::Calibrate(){
59 Reset();
60}
61
62/**
63 * Return the actual angle in degrees that the robot is currently facing.
64 *
65 * The angle is based on the current accumulator value corrected by the oversampling rate, the
66 * gyro type and the A/D calibration values.
67 * The angle is continuous, that is can go beyond 360 degrees. This make algorithms that wouldn't
68 * want to see a discontinuity in the gyro output as it sweeps past 0 on the second time around.
69 *
70 * @return the current heading of the robot in degrees. This heading is based on integration
71 * of the returned rate from the gyro.
72 */
73float AnalogGyro::GetAngle() const
74{
75 return impl->GetAngle();
76}
77
78
79/**
80 * Return the rate of rotation of the gyro
81 *
82 * The rate is based on the most recent reading of the gyro analog value
83 *
84 * @return the current rate in degrees per second
85 */
86double AnalogGyro::GetRate() const
87{
88 return impl->GetVelocity();
89}