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/PWM.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <hal/FRCUsageReporting.h> |
| 10 | #include <hal/HALBase.h> |
| 11 | #include <hal/PWM.h> |
| 12 | #include <hal/Ports.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include <wpi/StackTrace.h> |
| 14 | #include <wpi/sendable/SendableBuilder.h> |
| 15 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "frc/SensorUtil.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace frc; |
| 21 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 22 | PWM::PWM(int channel, bool registerSendable) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | if (!SensorUtil::CheckPWMChannel(channel)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 24 | throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | auto stack = wpi::GetStackTrace(1); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | m_handle = |
| 30 | HAL_InitializePWMPort(HAL_GetPort(channel), stack.c_str(), &status); |
| 31 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | |
| 33 | m_channel = channel; |
| 34 | |
| 35 | HAL_SetPWMDisabled(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 36 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | status = 0; |
| 38 | HAL_SetPWMEliminateDeadband(m_handle, false, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 39 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | |
| 41 | HAL_Report(HALUsageReporting::kResourceType_PWM, channel + 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | if (registerSendable) { |
| 43 | wpi::SendableRegistry::AddLW(this, "PWM", channel); |
| 44 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | PWM::~PWM() { |
| 48 | int32_t status = 0; |
| 49 | |
| 50 | HAL_SetPWMDisabled(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 51 | FRC_ReportError(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | |
| 53 | HAL_FreePWMPort(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 54 | FRC_ReportError(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | } |
| 56 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 57 | void PWM::SetPulseTime(units::microsecond_t time) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 59 | HAL_SetPWMPulseTimeMicroseconds(m_handle, time.value(), &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | } |
| 62 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 63 | units::microsecond_t PWM::GetPulseTime() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 65 | double value = HAL_GetPWMPulseTimeMicroseconds(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 68 | return units::microsecond_t{value}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void PWM::SetPosition(double pos) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | int32_t status = 0; |
| 73 | HAL_SetPWMPosition(m_handle, pos, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 74 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | double PWM::GetPosition() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | int32_t status = 0; |
| 79 | double position = HAL_GetPWMPosition(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 80 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 81 | return position; |
| 82 | } |
| 83 | |
| 84 | void PWM::SetSpeed(double speed) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 85 | int32_t status = 0; |
| 86 | HAL_SetPWMSpeed(m_handle, speed, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 87 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | double PWM::GetSpeed() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 91 | int32_t status = 0; |
| 92 | double speed = HAL_GetPWMSpeed(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 93 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 94 | return speed; |
| 95 | } |
| 96 | |
| 97 | void PWM::SetDisabled() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 99 | HAL_SetPWMDisabled(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 100 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void PWM::SetPeriodMultiplier(PeriodMultiplier mult) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 104 | int32_t status = 0; |
| 105 | |
| 106 | switch (mult) { |
| 107 | case kPeriodMultiplier_4X: |
| 108 | HAL_SetPWMPeriodScale(m_handle, 3, |
| 109 | &status); // Squelch 3 out of 4 outputs |
| 110 | break; |
| 111 | case kPeriodMultiplier_2X: |
| 112 | HAL_SetPWMPeriodScale(m_handle, 1, |
| 113 | &status); // Squelch 1 out of 2 outputs |
| 114 | break; |
| 115 | case kPeriodMultiplier_1X: |
| 116 | HAL_SetPWMPeriodScale(m_handle, 0, &status); // Don't squelch any outputs |
| 117 | break; |
| 118 | default: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 119 | throw FRC_MakeError(err::InvalidParameter, "PeriodMultiplier value {}", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 120 | static_cast<int>(mult)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void PWM::SetZeroLatch() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 128 | HAL_LatchPWMZero(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 129 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void PWM::EnableDeadbandElimination(bool eliminateDeadband) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 133 | int32_t status = 0; |
| 134 | HAL_SetPWMEliminateDeadband(m_handle, eliminateDeadband, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 135 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 136 | } |
| 137 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 138 | void PWM::SetBounds(units::microsecond_t max, units::microsecond_t deadbandMax, |
| 139 | units::microsecond_t center, |
| 140 | units::microsecond_t deadbandMin, |
| 141 | units::microsecond_t min) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 142 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 143 | HAL_SetPWMConfigMicroseconds(m_handle, max.value(), deadbandMax.value(), |
| 144 | center.value(), deadbandMin.value(), min.value(), |
| 145 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 146 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 147 | } |
| 148 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 149 | void PWM::GetBounds(units::microsecond_t* max, |
| 150 | units::microsecond_t* deadbandMax, |
| 151 | units::microsecond_t* center, |
| 152 | units::microsecond_t* deadbandMin, |
| 153 | units::microsecond_t* min) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 154 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 155 | int32_t rawMax, rawDeadbandMax, rawCenter, rawDeadbandMin, rawMin; |
| 156 | HAL_GetPWMConfigMicroseconds(m_handle, &rawMax, &rawDeadbandMax, &rawCenter, |
| 157 | &rawDeadbandMin, &rawMin, &status); |
| 158 | *max = units::microsecond_t{static_cast<double>(rawMax)}; |
| 159 | *deadbandMax = units::microsecond_t{static_cast<double>(rawDeadbandMax)}; |
| 160 | *center = units::microsecond_t{static_cast<double>(rawCenter)}; |
| 161 | *deadbandMin = units::microsecond_t{static_cast<double>(rawDeadbandMin)}; |
| 162 | *min = units::microsecond_t{static_cast<double>(rawMin)}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 163 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 164 | } |
| 165 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 166 | void PWM::SetAlwaysHighMode() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 167 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 168 | HAL_SetPWMAlwaysHighMode(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 169 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 172 | int PWM::GetChannel() const { |
| 173 | return m_channel; |
| 174 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 175 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 176 | void PWM::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 177 | builder.SetSmartDashboardType("PWM"); |
| 178 | builder.SetActuator(true); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 179 | builder.SetSafeState([=, this] { SetDisabled(); }); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 180 | builder.AddDoubleProperty( |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 181 | "Value", [=, this] { return GetPulseTime().value(); }, |
| 182 | [=, this](double value) { SetPulseTime(units::millisecond_t{value}); }); |
| 183 | builder.AddDoubleProperty( |
| 184 | "Speed", [=, this] { return GetSpeed(); }, |
| 185 | [=, this](double value) { SetSpeed(value); }); |
| 186 | builder.AddDoubleProperty( |
| 187 | "Position", [=, this] { return GetPosition(); }, |
| 188 | [=, this](double value) { SetPosition(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 189 | } |