Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include <wpi/NullDeleter.h> |
| 14 | #include <wpi/StackTrace.h> |
| 15 | #include <wpi/sendable/SendableBuilder.h> |
| 16 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | #include "frc/AnalogInput.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 19 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 20 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | |
| 22 | using namespace frc; |
| 23 | |
| 24 | AnalogGyro::AnalogGyro(int channel) |
| 25 | : AnalogGyro(std::make_shared<AnalogInput>(channel)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 26 | wpi::SendableRegistry::AddChild(this, m_analog.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | AnalogGyro::AnalogGyro(AnalogInput* channel) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | : AnalogGyro(std::shared_ptr<AnalogInput>( |
| 31 | channel, wpi::NullDeleter<AnalogInput>())) {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | |
| 33 | AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel) |
| 34 | : m_analog(channel) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 35 | if (!channel) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 36 | throw FRC_MakeError(err::NullParameter, "channel"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | InitGyro(); |
| 39 | Calibrate(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | AnalogGyro::AnalogGyro(int channel, int center, double offset) |
| 43 | : AnalogGyro(std::make_shared<AnalogInput>(channel), center, offset) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 44 | wpi::SendableRegistry::AddChild(this, m_analog.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center, |
| 48 | double offset) |
| 49 | : m_analog(channel) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 50 | if (!channel) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 51 | throw FRC_MakeError(err::NullParameter, "channel"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 61 | AnalogGyro::~AnalogGyro() { |
| 62 | HAL_FreeAnalogGyro(m_gyroHandle); |
| 63 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | |
| 65 | double AnalogGyro::GetAngle() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | int32_t status = 0; |
| 67 | double value = HAL_GetAnalogGyroAngle(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 68 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | return value; |
| 70 | } |
| 71 | |
| 72 | double AnalogGyro::GetRate() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | int32_t status = 0; |
| 74 | double value = HAL_GetAnalogGyroRate(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 75 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | return value; |
| 77 | } |
| 78 | |
| 79 | int AnalogGyro::GetCenter() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | int32_t status = 0; |
| 81 | int value = HAL_GetAnalogGyroCenter(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 82 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | return value; |
| 84 | } |
| 85 | |
| 86 | double AnalogGyro::GetOffset() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | int32_t status = 0; |
| 88 | double value = HAL_GetAnalogGyroOffset(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 89 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 90 | return value; |
| 91 | } |
| 92 | |
| 93 | void AnalogGyro::SetSensitivity(double voltsPerDegreePerSecond) { |
| 94 | int32_t status = 0; |
| 95 | HAL_SetAnalogGyroVoltsPerDegreePerSecond(m_gyroHandle, |
| 96 | voltsPerDegreePerSecond, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 97 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void AnalogGyro::SetDeadband(double volts) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | int32_t status = 0; |
| 102 | HAL_SetAnalogGyroDeadband(m_gyroHandle, volts, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 103 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void AnalogGyro::Reset() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | int32_t status = 0; |
| 108 | HAL_ResetAnalogGyro(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 109 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void AnalogGyro::InitGyro() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 113 | if (m_gyroHandle == HAL_kInvalidHandle) { |
| 114 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 115 | 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int32_t status = 0; |
| 122 | HAL_SetupAnalogGyro(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | |
| 125 | HAL_Report(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel() + 1); |
| 126 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 127 | wpi::SendableRegistry::AddLW(this, "AnalogGyro", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void AnalogGyro::Calibrate() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | int32_t status = 0; |
| 132 | HAL_CalibrateAnalogGyro(m_gyroHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 133 | FRC_CheckErrorStatus(status, "Channel {}", m_analog->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 134 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 135 | |
| 136 | std::shared_ptr<AnalogInput> AnalogGyro::GetAnalogInput() const { |
| 137 | return m_analog; |
| 138 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | |
| 140 | void AnalogGyro::InitSendable(wpi::SendableBuilder& builder) { |
| 141 | builder.SetSmartDashboardType("Gyro"); |
| 142 | builder.AddDoubleProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 143 | "Value", [=, this] { return GetAngle(); }, nullptr); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 144 | } |