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 | |
| 57 | void PWM::SetRaw(uint16_t value) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | int32_t status = 0; |
| 59 | HAL_SetPWMRaw(m_handle, 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 | |
| 63 | uint16_t PWM::GetRaw() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | int32_t status = 0; |
| 65 | uint16_t value = HAL_GetPWMRaw(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 | |
| 68 | return value; |
| 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 {}", |
| 120 | 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 | |
| 138 | void PWM::SetBounds(double max, double deadbandMax, double center, |
| 139 | double deadbandMin, double min) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 140 | int32_t status = 0; |
| 141 | HAL_SetPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min, |
| 142 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 143 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void PWM::SetRawBounds(int max, int deadbandMax, int center, int deadbandMin, |
| 147 | int min) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 148 | int32_t status = 0; |
| 149 | HAL_SetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min, |
| 150 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 151 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void PWM::GetRawBounds(int* max, int* deadbandMax, int* center, |
| 155 | int* deadbandMin, int* min) { |
| 156 | int32_t status = 0; |
| 157 | HAL_GetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min, |
| 158 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 159 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 162 | int PWM::GetChannel() const { |
| 163 | return m_channel; |
| 164 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 166 | void PWM::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 167 | builder.SetSmartDashboardType("PWM"); |
| 168 | builder.SetActuator(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 169 | builder.SetSafeState([=] { SetDisabled(); }); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 170 | builder.AddDoubleProperty( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 171 | "Value", [=] { return GetRaw(); }, [=](double value) { SetRaw(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 172 | } |