Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "PWM.h" |
| 9 | |
| 10 | //#include "NetworkCommunication/UsageReporting.h" |
| 11 | #include "Utility.h" |
| 12 | #include "WPIErrors.h" |
| 13 | |
| 14 | const float PWM::kDefaultPwmPeriod = 5.05; |
| 15 | const float PWM::kDefaultPwmCenter = 1.5; |
| 16 | const int32_t PWM::kDefaultPwmStepsDown = 1000; |
| 17 | const int32_t PWM::kPwmDisabled = 0; |
| 18 | |
| 19 | /** |
| 20 | * Allocate a PWM given a channel number. |
| 21 | * |
| 22 | * Checks channel value range and allocates the appropriate channel. |
| 23 | * The allocation is only done to help users ensure that they don't double |
| 24 | * assign channels. |
| 25 | * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP |
| 26 | * port |
| 27 | */ |
| 28 | PWM::PWM(uint32_t channel) |
| 29 | { |
| 30 | char buf[64]; |
| 31 | |
| 32 | if (!CheckPWMChannel(channel)) |
| 33 | { |
| 34 | snprintf(buf, 64, "PWM Channel %d", channel); |
| 35 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | sprintf(buf, "pwm/%d", channel); |
| 40 | impl = new SimContinuousOutput(buf); |
| 41 | m_channel = channel; |
| 42 | m_eliminateDeadband = false; |
| 43 | |
| 44 | m_centerPwm = kPwmDisabled; // In simulation, the same thing. |
| 45 | } |
| 46 | |
| 47 | PWM::~PWM() { |
| 48 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Optionally eliminate the deadband from a speed controller. |
| 53 | * @param eliminateDeadband If true, set the motor curve on the Jaguar to eliminate |
| 54 | * the deadband in the middle of the range. Otherwise, keep the full range without |
| 55 | * modifying any values. |
| 56 | */ |
| 57 | void PWM::EnableDeadbandElimination(bool eliminateDeadband) |
| 58 | { |
| 59 | m_eliminateDeadband = eliminateDeadband; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the bounds on the PWM values. |
| 64 | * This sets the bounds on the PWM values for a particular each type of controller. The values |
| 65 | * determine the upper and lower speeds as well as the deadband bracket. |
| 66 | * @param max The Minimum pwm value |
| 67 | * @param deadbandMax The high end of the deadband range |
| 68 | * @param center The center speed (off) |
| 69 | * @param deadbandMin The low end of the deadband range |
| 70 | * @param min The minimum pwm value |
| 71 | */ |
| 72 | void PWM::SetBounds(int32_t max, int32_t deadbandMax, int32_t center, int32_t deadbandMin, int32_t min) |
| 73 | { |
| 74 | // Nothing to do in simulation. |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Set the bounds on the PWM pulse widths. |
| 80 | * This sets the bounds on the PWM values for a particular type of controller. The values |
| 81 | * determine the upper and lower speeds as well as the deadband bracket. |
| 82 | * @param max The max PWM pulse width in ms |
| 83 | * @param deadbandMax The high end of the deadband range pulse width in ms |
| 84 | * @param center The center (off) pulse width in ms |
| 85 | * @param deadbandMin The low end of the deadband pulse width in ms |
| 86 | * @param min The minimum pulse width in ms |
| 87 | */ |
| 88 | void PWM::SetBounds(double max, double deadbandMax, double center, double deadbandMin, double min) |
| 89 | { |
| 90 | // Nothing to do in simulation. |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set the PWM value based on a position. |
| 95 | * |
| 96 | * This is intended to be used by servos. |
| 97 | * |
| 98 | * @pre SetMaxPositivePwm() called. |
| 99 | * @pre SetMinNegativePwm() called. |
| 100 | * |
| 101 | * @param pos The position to set the servo between 0.0 and 1.0. |
| 102 | */ |
| 103 | void PWM::SetPosition(float pos) |
| 104 | { |
| 105 | if (pos < 0.0) |
| 106 | { |
| 107 | pos = 0.0; |
| 108 | } |
| 109 | else if (pos > 1.0) |
| 110 | { |
| 111 | pos = 1.0; |
| 112 | } |
| 113 | |
| 114 | impl->Set(pos); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the PWM value in terms of a position. |
| 119 | * |
| 120 | * This is intended to be used by servos. |
| 121 | * |
| 122 | * @pre SetMaxPositivePwm() called. |
| 123 | * @pre SetMinNegativePwm() called. |
| 124 | * |
| 125 | * @return The position the servo is set to between 0.0 and 1.0. |
| 126 | */ |
| 127 | float PWM::GetPosition() const |
| 128 | { |
| 129 | float value = impl->Get(); |
| 130 | if (value < 0.0) |
| 131 | { |
| 132 | return 0.0; |
| 133 | } |
| 134 | else if (value > 1.0) |
| 135 | { |
| 136 | return 1.0; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return value; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set the PWM value based on a speed. |
| 146 | * |
| 147 | * This is intended to be used by speed controllers. |
| 148 | * |
| 149 | * @pre SetMaxPositivePwm() called. |
| 150 | * @pre SetMinPositivePwm() called. |
| 151 | * @pre SetCenterPwm() called. |
| 152 | * @pre SetMaxNegativePwm() called. |
| 153 | * @pre SetMinNegativePwm() called. |
| 154 | * |
| 155 | * @param speed The speed to set the speed controller between -1.0 and 1.0. |
| 156 | */ |
| 157 | void PWM::SetSpeed(float speed) |
| 158 | { |
| 159 | // clamp speed to be in the range 1.0 >= speed >= -1.0 |
| 160 | if (speed < -1.0) |
| 161 | { |
| 162 | speed = -1.0; |
| 163 | } |
| 164 | else if (speed > 1.0) |
| 165 | { |
| 166 | speed = 1.0; |
| 167 | } |
| 168 | |
| 169 | impl->Set(speed); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Get the PWM value in terms of speed. |
| 174 | * |
| 175 | * This is intended to be used by speed controllers. |
| 176 | * |
| 177 | * @pre SetMaxPositivePwm() called. |
| 178 | * @pre SetMinPositivePwm() called. |
| 179 | * @pre SetMaxNegativePwm() called. |
| 180 | * @pre SetMinNegativePwm() called. |
| 181 | * |
| 182 | * @return The most recently set speed between -1.0 and 1.0. |
| 183 | */ |
| 184 | float PWM::GetSpeed() const |
| 185 | { |
| 186 | float value = impl->Get(); |
| 187 | if (value > 1.0) |
| 188 | { |
| 189 | return 1.0; |
| 190 | } |
| 191 | else if (value < -1.0) |
| 192 | { |
| 193 | return -1.0; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | return value; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Set the PWM value directly to the hardware. |
| 203 | * |
| 204 | * Write a raw value to a PWM channel. |
| 205 | * |
| 206 | * @param value Raw PWM value. |
| 207 | */ |
| 208 | void PWM::SetRaw(unsigned short value) |
| 209 | { |
| 210 | wpi_assert(value == kPwmDisabled); |
| 211 | impl->Set(0); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Slow down the PWM signal for old devices. |
| 216 | * |
| 217 | * @param mult The period multiplier to apply to this channel |
| 218 | */ |
| 219 | void PWM::SetPeriodMultiplier(PeriodMultiplier mult) |
| 220 | { |
| 221 | // Do nothing in simulation. |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void PWM::ValueChanged(ITable* source, llvm::StringRef key, |
| 226 | std::shared_ptr<nt::Value> value, bool isNew) { |
| 227 | if (!value->IsDouble()) return; |
| 228 | SetSpeed(value->GetDouble()); |
| 229 | } |
| 230 | |
| 231 | void PWM::UpdateTable() { |
| 232 | if (m_table != nullptr) { |
| 233 | m_table->PutNumber("Value", GetSpeed()); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void PWM::StartLiveWindowMode() { |
| 238 | SetSpeed(0); |
| 239 | if (m_table != nullptr) { |
| 240 | m_table->AddTableListener("Value", this, true); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void PWM::StopLiveWindowMode() { |
| 245 | SetSpeed(0); |
| 246 | if (m_table != nullptr) { |
| 247 | m_table->RemoveTableListener(this); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | std::string PWM::GetSmartDashboardType() const { |
| 252 | return "Speed Controller"; |
| 253 | } |
| 254 | |
| 255 | void PWM::InitTable(std::shared_ptr<ITable> subTable) { |
| 256 | m_table = subTable; |
| 257 | UpdateTable(); |
| 258 | } |
| 259 | |
| 260 | std::shared_ptr<ITable> PWM::GetTable() const { |
| 261 | return m_table; |
| 262 | } |