blob: a28bb9b5231e66c40df1a6c8b60bb50c66b3a2b6 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "Gyro.h"
8#include "Timer.h"
9#include "WPIErrors.h"
10#include "LiveWindow/LiveWindow.h"
11
12const uint32_t Gyro::kOversampleBits = 10;
13const uint32_t Gyro::kAverageBits = 0;
14const float Gyro::kSamplesPerSecond = 50.0;
15const float Gyro::kCalibrationSampleTime = 5.0;
16const float Gyro::kDefaultVoltsPerDegreePerSecond = 0.007;
17
18/**
19 * Initialize the gyro.
20 * Calibrate the gyro by running for a number of samples and computing the center value for this
21 * part. Then use the center value as the Accumulator center value for subsequent measurements.
22 * It's important to make sure that the robot is not moving while the centering calculations are
23 * in progress, this is typically done when the robot is first turned on while it's sitting at
24 * rest before the competition starts.
25 */
26void Gyro::InitGyro(int channel)
27{
28 SetPIDSourceType(PIDSourceType::kDisplacement);
29
30 char buffer[50];
31 int n = sprintf(buffer, "analog/%d", channel);
32 impl = new SimGyro(buffer);
33
34 LiveWindow::GetInstance()->AddSensor("Gyro", channel, this);
35}
36
37/**
38 * Gyro constructor with only a channel..
39 *
40 * @param channel The analog channel the gyro is connected to.
41 */
42Gyro::Gyro(uint32_t channel)
43{
44 InitGyro(channel);
45}
46
47/**
48 * Reset the gyro.
49 * Resets the gyro to a heading of zero. This can be used if there is significant
50 * drift in the gyro and it needs to be recalibrated after it has been running.
51 */
52void Gyro::Reset()
53{
54 impl->Reset();
55}
56
57/**
58 * Return the actual angle in degrees that the robot is currently facing.
59 *
60 * The angle is based on the current accumulator value corrected by the oversampling rate, the
61 * gyro type and the A/D calibration values.
62 * The angle is continuous, that is can go beyond 360 degrees. This make algorithms that wouldn't
63 * want to see a discontinuity in the gyro output as it sweeps past 0 on the second time around.
64 *
65 * @return the current heading of the robot in degrees. This heading is based on integration
66 * of the returned rate from the gyro.
67 */
68float Gyro::GetAngle() const
69{
70 return impl->GetAngle();
71}
72
73
74/**
75 * Return the rate of rotation of the gyro
76 *
77 * The rate is based on the most recent reading of the gyro analog value
78 *
79 * @return the current rate in degrees per second
80 */
81double Gyro::GetRate() const
82{
83 return impl->GetVelocity();
84}
85
86void Gyro::SetPIDSourceType(PIDSourceType pidSource)
87{
88 m_pidSource = pidSource;
89}
90
91/**
92 * Get the angle in degrees for the PIDSource base object.
93 *
94 * @return The angle in degrees.
95 */
96double Gyro::PIDGet()
97{
98 switch(GetPIDSourceType()){
99 case PIDSourceType::kRate:
100 return GetRate();
101 case PIDSourceType::kDisplacement:
102 return GetAngle();
103 default:
104 return 0;
105 }
106}
107
108void Gyro::UpdateTable() {
109 if (m_table != nullptr) {
110 m_table->PutNumber("Value", GetAngle());
111 }
112}
113
114void Gyro::StartLiveWindowMode() {
115
116}
117
118void Gyro::StopLiveWindowMode() {
119
120}
121
122std::string Gyro::GetSmartDashboardType() const {
123 return "Gyro";
124}
125
126void Gyro::InitTable(std::shared_ptr<ITable> subTable) {
127 m_table = subTable;
128 UpdateTable();
129}
130
131std::shared_ptr<ITable> Gyro::GetTable() const {
132 return m_table;
133}