blob: 106293be2eeb06c30b9a420fa225fef188f7bee1 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/AnalogGyro.h"
6
7#include <climits>
8#include <utility>
9
10#include <hal/AnalogGyro.h>
11#include <hal/Errors.h>
12#include <hal/FRCUsageReporting.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070013#include <wpi/NullDeleter.h>
14#include <wpi/StackTrace.h>
15#include <wpi/sendable/SendableBuilder.h>
16#include <wpi/sendable/SendableRegistry.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080017
18#include "frc/AnalogInput.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070019#include "frc/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080020#include "frc/Timer.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080021
22using namespace frc;
23
24AnalogGyro::AnalogGyro(int channel)
25 : AnalogGyro(std::make_shared<AnalogInput>(channel)) {
Austin Schuh812d0d12021-11-04 20:16:48 -070026 wpi::SendableRegistry::AddChild(this, m_analog.get());
Brian Silverman8fce7482020-01-05 13:18:21 -080027}
28
29AnalogGyro::AnalogGyro(AnalogInput* channel)
Austin Schuh812d0d12021-11-04 20:16:48 -070030 : AnalogGyro(std::shared_ptr<AnalogInput>(
31 channel, wpi::NullDeleter<AnalogInput>())) {}
Brian Silverman8fce7482020-01-05 13:18:21 -080032
33AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel)
34 : m_analog(channel) {
Austin Schuh812d0d12021-11-04 20:16:48 -070035 if (!channel) {
36 throw FRC_MakeError(err::NullParameter, "{}", "channel");
Brian Silverman8fce7482020-01-05 13:18:21 -080037 }
Austin Schuh812d0d12021-11-04 20:16:48 -070038 InitGyro();
39 Calibrate();
Brian Silverman8fce7482020-01-05 13:18:21 -080040}
41
42AnalogGyro::AnalogGyro(int channel, int center, double offset)
43 : AnalogGyro(std::make_shared<AnalogInput>(channel), center, offset) {
Austin Schuh812d0d12021-11-04 20:16:48 -070044 wpi::SendableRegistry::AddChild(this, m_analog.get());
Brian Silverman8fce7482020-01-05 13:18:21 -080045}
46
47AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center,
48 double offset)
49 : m_analog(channel) {
Austin Schuh812d0d12021-11-04 20:16:48 -070050 if (!channel) {
51 throw FRC_MakeError(err::NullParameter, "{}", "channel");
Brian Silverman8fce7482020-01-05 13:18:21 -080052 }
Austin Schuh812d0d12021-11-04 20:16:48 -070053 InitGyro();
54 int32_t status = 0;
55 HAL_SetAnalogGyroParameters(m_gyroHandle, kDefaultVoltsPerDegreePerSecond,
56 offset, center, &status);
57 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
58 Reset();
Brian Silverman8fce7482020-01-05 13:18:21 -080059}
60
Austin Schuh812d0d12021-11-04 20:16:48 -070061AnalogGyro::~AnalogGyro() {
62 HAL_FreeAnalogGyro(m_gyroHandle);
63}
Brian Silverman8fce7482020-01-05 13:18:21 -080064
65double AnalogGyro::GetAngle() const {
Brian Silverman8fce7482020-01-05 13:18:21 -080066 int32_t status = 0;
67 double value = HAL_GetAnalogGyroAngle(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070068 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080069 return value;
70}
71
72double AnalogGyro::GetRate() const {
Brian Silverman8fce7482020-01-05 13:18:21 -080073 int32_t status = 0;
74 double value = HAL_GetAnalogGyroRate(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070075 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080076 return value;
77}
78
79int AnalogGyro::GetCenter() const {
Brian Silverman8fce7482020-01-05 13:18:21 -080080 int32_t status = 0;
81 int value = HAL_GetAnalogGyroCenter(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070082 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080083 return value;
84}
85
86double AnalogGyro::GetOffset() const {
Brian Silverman8fce7482020-01-05 13:18:21 -080087 int32_t status = 0;
88 double value = HAL_GetAnalogGyroOffset(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070089 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080090 return value;
91}
92
93void AnalogGyro::SetSensitivity(double voltsPerDegreePerSecond) {
94 int32_t status = 0;
95 HAL_SetAnalogGyroVoltsPerDegreePerSecond(m_gyroHandle,
96 voltsPerDegreePerSecond, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070097 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080098}
99
100void AnalogGyro::SetDeadband(double volts) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800101 int32_t status = 0;
102 HAL_SetAnalogGyroDeadband(m_gyroHandle, volts, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -0700103 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800104}
105
106void AnalogGyro::Reset() {
Brian Silverman8fce7482020-01-05 13:18:21 -0800107 int32_t status = 0;
108 HAL_ResetAnalogGyro(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -0700109 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800110}
111
112void AnalogGyro::InitGyro() {
Brian Silverman8fce7482020-01-05 13:18:21 -0800113 if (m_gyroHandle == HAL_kInvalidHandle) {
114 int32_t status = 0;
Austin Schuh812d0d12021-11-04 20:16:48 -0700115 std::string stackTrace = wpi::GetStackTrace(1);
116 m_gyroHandle =
117 HAL_InitializeAnalogGyro(m_analog->m_port, stackTrace.c_str(), &status);
118 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800119 }
120
121 int32_t status = 0;
122 HAL_SetupAnalogGyro(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -0700123 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800124
125 HAL_Report(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel() + 1);
126
Austin Schuh812d0d12021-11-04 20:16:48 -0700127 wpi::SendableRegistry::AddLW(this, "AnalogGyro", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800128}
129
130void AnalogGyro::Calibrate() {
Brian Silverman8fce7482020-01-05 13:18:21 -0800131 int32_t status = 0;
132 HAL_CalibrateAnalogGyro(m_gyroHandle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -0700133 FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800134}
Austin Schuh1e69f942020-11-14 15:06:14 -0800135
136std::shared_ptr<AnalogInput> AnalogGyro::GetAnalogInput() const {
137 return m_analog;
138}
Austin Schuh812d0d12021-11-04 20:16:48 -0700139
140void AnalogGyro::InitSendable(wpi::SendableBuilder& builder) {
141 builder.SetSmartDashboardType("Gyro");
142 builder.AddDoubleProperty(
143 "Value", [=] { return GetAngle(); }, nullptr);
144}