blob: c89150648fefac720139c4942ea1f6e0ed6b7826 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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 "frc/AnalogGyro.h"
9
10#include <climits>
11#include <utility>
12
13#include <hal/AnalogGyro.h>
14#include <hal/Errors.h>
James Kuszmaul4b81d302019-12-14 20:53:14 -080015#include <hal/FRCUsageReporting.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080016
17#include "frc/AnalogInput.h"
18#include "frc/Timer.h"
19#include "frc/WPIErrors.h"
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080020#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080021
22using namespace frc;
23
24AnalogGyro::AnalogGyro(int channel)
25 : AnalogGyro(std::make_shared<AnalogInput>(channel)) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080026 SendableRegistry::GetInstance().AddChild(this, m_analog.get());
Brian Silverman41cdd3e2019-01-19 19:48:58 -080027}
28
29AnalogGyro::AnalogGyro(AnalogInput* channel)
30 : AnalogGyro(
31 std::shared_ptr<AnalogInput>(channel, NullDeleter<AnalogInput>())) {}
32
33AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel)
34 : m_analog(channel) {
35 if (channel == nullptr) {
36 wpi_setWPIError(NullParameter);
37 } else {
38 InitGyro();
39 Calibrate();
40 }
41}
42
43AnalogGyro::AnalogGyro(int channel, int center, double offset)
44 : AnalogGyro(std::make_shared<AnalogInput>(channel), center, offset) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080045 SendableRegistry::GetInstance().AddChild(this, m_analog.get());
Brian Silverman41cdd3e2019-01-19 19:48:58 -080046}
47
48AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center,
49 double offset)
50 : m_analog(channel) {
51 if (channel == nullptr) {
52 wpi_setWPIError(NullParameter);
53 } else {
54 InitGyro();
55 int32_t status = 0;
56 HAL_SetAnalogGyroParameters(m_gyroHandle, kDefaultVoltsPerDegreePerSecond,
57 offset, center, &status);
58 if (status != 0) {
James Kuszmaul4b81d302019-12-14 20:53:14 -080059 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080060 m_gyroHandle = HAL_kInvalidHandle;
61 return;
62 }
63 Reset();
64 }
65}
66
67AnalogGyro::~AnalogGyro() { HAL_FreeAnalogGyro(m_gyroHandle); }
68
Brian Silverman41cdd3e2019-01-19 19:48:58 -080069double AnalogGyro::GetAngle() const {
70 if (StatusIsFatal()) return 0.0;
71 int32_t status = 0;
72 double value = HAL_GetAnalogGyroAngle(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080073 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080074 return value;
75}
76
77double AnalogGyro::GetRate() const {
78 if (StatusIsFatal()) return 0.0;
79 int32_t status = 0;
80 double value = HAL_GetAnalogGyroRate(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080081 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080082 return value;
83}
84
85int AnalogGyro::GetCenter() const {
86 if (StatusIsFatal()) return 0;
87 int32_t status = 0;
88 int value = HAL_GetAnalogGyroCenter(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080089 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080090 return value;
91}
92
93double AnalogGyro::GetOffset() const {
94 if (StatusIsFatal()) return 0.0;
95 int32_t status = 0;
96 double value = HAL_GetAnalogGyroOffset(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080097 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080098 return value;
99}
100
101void AnalogGyro::SetSensitivity(double voltsPerDegreePerSecond) {
102 int32_t status = 0;
103 HAL_SetAnalogGyroVoltsPerDegreePerSecond(m_gyroHandle,
104 voltsPerDegreePerSecond, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -0800105 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800106}
107
108void AnalogGyro::SetDeadband(double volts) {
109 if (StatusIsFatal()) return;
110 int32_t status = 0;
111 HAL_SetAnalogGyroDeadband(m_gyroHandle, volts, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -0800112 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800113}
114
115void AnalogGyro::Reset() {
116 if (StatusIsFatal()) return;
117 int32_t status = 0;
118 HAL_ResetAnalogGyro(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -0800119 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800120}
121
122void AnalogGyro::InitGyro() {
123 if (StatusIsFatal()) return;
124 if (m_gyroHandle == HAL_kInvalidHandle) {
125 int32_t status = 0;
126 m_gyroHandle = HAL_InitializeAnalogGyro(m_analog->m_port, &status);
127 if (status == PARAMETER_OUT_OF_RANGE) {
128 wpi_setWPIErrorWithContext(ParameterOutOfRange,
129 " channel (must be accumulator channel)");
130 m_analog = nullptr;
131 m_gyroHandle = HAL_kInvalidHandle;
132 return;
133 }
134 if (status != 0) {
James Kuszmaul4b81d302019-12-14 20:53:14 -0800135 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800136 m_analog = nullptr;
137 m_gyroHandle = HAL_kInvalidHandle;
138 return;
139 }
140 }
141
142 int32_t status = 0;
143 HAL_SetupAnalogGyro(m_gyroHandle, &status);
144 if (status != 0) {
James Kuszmaul4b81d302019-12-14 20:53:14 -0800145 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800146 m_analog = nullptr;
147 m_gyroHandle = HAL_kInvalidHandle;
148 return;
149 }
150
James Kuszmaul4b81d302019-12-14 20:53:14 -0800151 HAL_Report(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel() + 1);
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800152
153 SendableRegistry::GetInstance().AddLW(this, "AnalogGyro",
154 m_analog->GetChannel());
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800155}
156
157void AnalogGyro::Calibrate() {
158 if (StatusIsFatal()) return;
159 int32_t status = 0;
160 HAL_CalibrateAnalogGyro(m_gyroHandle, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -0800161 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800162}