jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/
|
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */
|
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */
|
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
| 5 | /*----------------------------------------------------------------------------*/
|
| 6 |
|
| 7 | #include "PWM.h"
|
| 8 |
|
| 9 | #include "DigitalModule.h"
|
| 10 | #include "NetworkCommunication/UsageReporting.h"
|
| 11 | #include "Resource.h"
|
| 12 | #include "Utility.h"
|
| 13 | #include "WPIErrors.h"
|
| 14 |
|
| 15 | const UINT32 PWM::kDefaultPwmPeriod;
|
| 16 | const UINT32 PWM::kDefaultMinPwmHigh;
|
| 17 | const INT32 PWM::kPwmDisabled;
|
| 18 | static Resource *allocated = NULL;
|
| 19 |
|
| 20 | /**
|
| 21 | * Initialize PWMs given an module and channel.
|
| 22 | *
|
| 23 | * This method is private and is the common path for all the constructors for creating PWM
|
| 24 | * instances. Checks module and channel value ranges and allocates the appropriate channel.
|
| 25 | * The allocation is only done to help users ensure that they don't double assign channels.
|
| 26 | */
|
| 27 | void PWM::InitPWM(UINT8 moduleNumber, UINT32 channel)
|
| 28 | {
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame^] | 29 | m_table = NULL;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 30 | char buf[64];
|
| 31 | Resource::CreateResourceObject(&allocated, tDIO::kNumSystems * kPwmChannels);
|
| 32 | if (!CheckPWMModule(moduleNumber))
|
| 33 | {
|
| 34 | snprintf(buf, 64, "Digital Module %d", moduleNumber);
|
| 35 | wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
|
| 36 | return;
|
| 37 | }
|
| 38 | if (!CheckPWMChannel(channel))
|
| 39 | {
|
| 40 | snprintf(buf, 64, "PWM Channel %d", channel);
|
| 41 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
| 42 | return;
|
| 43 | }
|
| 44 |
|
| 45 | snprintf(buf, 64, "PWM %d (Module: %d)", channel, moduleNumber);
|
| 46 | if (allocated->Allocate((moduleNumber - 1) * kPwmChannels + channel - 1, buf) == ~0ul)
|
| 47 | {
|
| 48 | CloneError(allocated);
|
| 49 | return;
|
| 50 | }
|
| 51 | m_channel = channel;
|
| 52 | m_module = DigitalModule::GetInstance(moduleNumber);
|
| 53 | m_module->SetPWM(m_channel, kPwmDisabled);
|
| 54 | m_eliminateDeadband = false;
|
| 55 |
|
| 56 | nUsageReporting::report(nUsageReporting::kResourceType_PWM, channel, moduleNumber - 1);
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * Allocate a PWM given a module and channel.
|
| 61 | * Allocate a PWM using a module and channel number.
|
| 62 | *
|
| 63 | * @param moduleNumber The digital module (1 or 2).
|
| 64 | * @param channel The PWM channel on the digital module (1..10).
|
| 65 | */
|
| 66 | PWM::PWM(UINT8 moduleNumber, UINT32 channel)
|
| 67 | : m_module(NULL)
|
| 68 | {
|
| 69 | InitPWM(moduleNumber, channel);
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Allocate a PWM in the default module given a channel.
|
| 74 | *
|
| 75 | * Using a default module allocate a PWM given the channel number. The default module is the first
|
| 76 | * slot numerically in the cRIO chassis.
|
| 77 | *
|
| 78 | * @param channel The PWM channel on the digital module.
|
| 79 | */
|
| 80 | PWM::PWM(UINT32 channel)
|
| 81 | : m_module(NULL)
|
| 82 | {
|
| 83 | InitPWM(GetDefaultDigitalModule(), channel);
|
| 84 | }
|
| 85 |
|
| 86 | /**
|
| 87 | * Free the PWM channel.
|
| 88 | *
|
| 89 | * Free the resource associated with the PWM channel and set the value to 0.
|
| 90 | */
|
| 91 | PWM::~PWM()
|
| 92 | {
|
| 93 | if (m_module)
|
| 94 | {
|
| 95 | m_module->SetPWM(m_channel, kPwmDisabled);
|
| 96 | allocated->Free((m_module->GetNumber() - 1) * kPwmChannels + m_channel - 1);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | /**
|
| 101 | * Optionally eliminate the deadband from a speed controller.
|
| 102 | * @param eliminateDeadband If true, set the motor curve on the Jaguar to eliminate
|
| 103 | * the deadband in the middle of the range. Otherwise, keep the full range without
|
| 104 | * modifying any values.
|
| 105 | */
|
| 106 | void PWM::EnableDeadbandElimination(bool eliminateDeadband)
|
| 107 | {
|
| 108 | if (StatusIsFatal()) return;
|
| 109 | m_eliminateDeadband = eliminateDeadband;
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * Set the bounds on the PWM values.
|
| 114 | * This sets the bounds on the PWM values for a particular each type of controller. The values
|
| 115 | * determine the upper and lower speeds as well as the deadband bracket.
|
| 116 | * @param max The Minimum pwm value
|
| 117 | * @param deadbandMax The high end of the deadband range
|
| 118 | * @param center The center speed (off)
|
| 119 | * @param deadbandMin The low end of the deadband range
|
| 120 | * @param min The minimum pwm value
|
| 121 | */
|
| 122 | void PWM::SetBounds(INT32 max, INT32 deadbandMax, INT32 center, INT32 deadbandMin, INT32 min)
|
| 123 | {
|
| 124 | if (StatusIsFatal()) return;
|
| 125 | m_maxPwm = max;
|
| 126 | m_deadbandMaxPwm = deadbandMax;
|
| 127 | m_centerPwm = center;
|
| 128 | m_deadbandMinPwm = deadbandMin;
|
| 129 | m_minPwm = min;
|
| 130 | }
|
| 131 |
|
| 132 | UINT32 PWM::GetModuleNumber()
|
| 133 | {
|
| 134 | return m_module->GetNumber();
|
| 135 | }
|
| 136 |
|
| 137 | /**
|
| 138 | * Set the PWM value based on a position.
|
| 139 | *
|
| 140 | * This is intended to be used by servos.
|
| 141 | *
|
| 142 | * @pre SetMaxPositivePwm() called.
|
| 143 | * @pre SetMinNegativePwm() called.
|
| 144 | *
|
| 145 | * @param pos The position to set the servo between 0.0 and 1.0.
|
| 146 | */
|
| 147 | void PWM::SetPosition(float pos)
|
| 148 | {
|
| 149 | if (StatusIsFatal()) return;
|
| 150 | if (pos < 0.0)
|
| 151 | {
|
| 152 | pos = 0.0;
|
| 153 | }
|
| 154 | else if (pos > 1.0)
|
| 155 | {
|
| 156 | pos = 1.0;
|
| 157 | }
|
| 158 |
|
| 159 | INT32 rawValue;
|
| 160 | // note, need to perform the multiplication below as floating point before converting to int
|
| 161 | rawValue = (INT32)( (pos * (float) GetFullRangeScaleFactor()) + GetMinNegativePwm());
|
| 162 |
|
| 163 | wpi_assert((rawValue >= GetMinNegativePwm()) && (rawValue <= GetMaxPositivePwm()));
|
| 164 | wpi_assert(rawValue != kPwmDisabled);
|
| 165 |
|
| 166 | // send the computed pwm value to the FPGA
|
| 167 | SetRaw((UINT8)rawValue);
|
| 168 | }
|
| 169 |
|
| 170 | /**
|
| 171 | * Get the PWM value in terms of a position.
|
| 172 | *
|
| 173 | * This is intended to be used by servos.
|
| 174 | *
|
| 175 | * @pre SetMaxPositivePwm() called.
|
| 176 | * @pre SetMinNegativePwm() called.
|
| 177 | *
|
| 178 | * @return The position the servo is set to between 0.0 and 1.0.
|
| 179 | */
|
| 180 | float PWM::GetPosition()
|
| 181 | {
|
| 182 | if (StatusIsFatal()) return 0.0;
|
| 183 | INT32 value = GetRaw();
|
| 184 | if (value < GetMinNegativePwm())
|
| 185 | {
|
| 186 | return 0.0;
|
| 187 | }
|
| 188 | else if (value > GetMaxPositivePwm())
|
| 189 | {
|
| 190 | return 1.0;
|
| 191 | }
|
| 192 | else
|
| 193 | {
|
| 194 | return (float)(value - GetMinNegativePwm()) / (float)GetFullRangeScaleFactor();
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | /**
|
| 199 | * Set the PWM value based on a speed.
|
| 200 | *
|
| 201 | * This is intended to be used by speed controllers.
|
| 202 | *
|
| 203 | * @pre SetMaxPositivePwm() called.
|
| 204 | * @pre SetMinPositivePwm() called.
|
| 205 | * @pre SetCenterPwm() called.
|
| 206 | * @pre SetMaxNegativePwm() called.
|
| 207 | * @pre SetMinNegativePwm() called.
|
| 208 | *
|
| 209 | * @param speed The speed to set the speed controller between -1.0 and 1.0.
|
| 210 | */
|
| 211 | void PWM::SetSpeed(float speed)
|
| 212 | {
|
| 213 | if (StatusIsFatal()) return;
|
| 214 | // clamp speed to be in the range 1.0 >= speed >= -1.0
|
| 215 | if (speed < -1.0)
|
| 216 | {
|
| 217 | speed = -1.0;
|
| 218 | }
|
| 219 | else if (speed > 1.0)
|
| 220 | {
|
| 221 | speed = 1.0;
|
| 222 | }
|
| 223 |
|
| 224 | // calculate the desired output pwm value by scaling the speed appropriately
|
| 225 | INT32 rawValue;
|
| 226 | if (speed == 0.0)
|
| 227 | {
|
| 228 | rawValue = GetCenterPwm();
|
| 229 | }
|
| 230 | else if (speed > 0.0)
|
| 231 | {
|
| 232 | rawValue = (INT32)(speed * ((float)GetPositiveScaleFactor()) +
|
| 233 | ((float) GetMinPositivePwm()) + 0.5);
|
| 234 | }
|
| 235 | else
|
| 236 | {
|
| 237 | rawValue = (INT32)(speed * ((float)GetNegativeScaleFactor()) +
|
| 238 | ((float) GetMaxNegativePwm()) + 0.5);
|
| 239 | }
|
| 240 |
|
| 241 | // the above should result in a pwm_value in the valid range
|
| 242 | wpi_assert((rawValue >= GetMinNegativePwm()) && (rawValue <= GetMaxPositivePwm()));
|
| 243 | wpi_assert(rawValue != kPwmDisabled);
|
| 244 |
|
| 245 | // send the computed pwm value to the FPGA
|
| 246 | SetRaw((UINT8)rawValue);
|
| 247 | }
|
| 248 |
|
| 249 | /**
|
| 250 | * Get the PWM value in terms of speed.
|
| 251 | *
|
| 252 | * This is intended to be used by speed controllers.
|
| 253 | *
|
| 254 | * @pre SetMaxPositivePwm() called.
|
| 255 | * @pre SetMinPositivePwm() called.
|
| 256 | * @pre SetMaxNegativePwm() called.
|
| 257 | * @pre SetMinNegativePwm() called.
|
| 258 | *
|
| 259 | * @return The most recently set speed between -1.0 and 1.0.
|
| 260 | */
|
| 261 | float PWM::GetSpeed()
|
| 262 | {
|
| 263 | if (StatusIsFatal()) return 0.0;
|
| 264 | INT32 value = GetRaw();
|
| 265 | if (value > GetMaxPositivePwm())
|
| 266 | {
|
| 267 | return 1.0;
|
| 268 | }
|
| 269 | else if (value < GetMinNegativePwm())
|
| 270 | {
|
| 271 | return -1.0;
|
| 272 | }
|
| 273 | else if (value > GetMinPositivePwm())
|
| 274 | {
|
| 275 | return (float)(value - GetMinPositivePwm()) / (float)GetPositiveScaleFactor();
|
| 276 | }
|
| 277 | else if (value < GetMaxNegativePwm())
|
| 278 | {
|
| 279 | return (float)(value - GetMaxNegativePwm()) / (float)GetNegativeScaleFactor();
|
| 280 | }
|
| 281 | else
|
| 282 | {
|
| 283 | return 0.0;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | /**
|
| 288 | * Set the PWM value directly to the hardware.
|
| 289 | *
|
| 290 | * Write a raw value to a PWM channel.
|
| 291 | *
|
| 292 | * @param value Raw PWM value. Range 0 - 255.
|
| 293 | */
|
| 294 | void PWM::SetRaw(UINT8 value)
|
| 295 | {
|
| 296 | if (StatusIsFatal()) return;
|
| 297 | m_module->SetPWM(m_channel, value);
|
| 298 | }
|
| 299 |
|
| 300 | /**
|
| 301 | * Get the PWM value directly from the hardware.
|
| 302 | *
|
| 303 | * Read a raw value from a PWM channel.
|
| 304 | *
|
| 305 | * @return Raw PWM control value. Range: 0 - 255.
|
| 306 | */
|
| 307 | UINT8 PWM::GetRaw()
|
| 308 | {
|
| 309 | if (StatusIsFatal()) return 0;
|
| 310 | return m_module->GetPWM(m_channel);
|
| 311 | }
|
| 312 |
|
| 313 | /**
|
| 314 | * Slow down the PWM signal for old devices.
|
| 315 | *
|
| 316 | * @param mult The period multiplier to apply to this channel
|
| 317 | */
|
| 318 | void PWM::SetPeriodMultiplier(PeriodMultiplier mult)
|
| 319 | {
|
| 320 | if (StatusIsFatal()) return;
|
| 321 | switch(mult)
|
| 322 | {
|
| 323 | case kPeriodMultiplier_4X:
|
| 324 | m_module->SetPWMPeriodScale(m_channel, 3); // Squelch 3 out of 4 outputs
|
| 325 | break;
|
| 326 | case kPeriodMultiplier_2X:
|
| 327 | m_module->SetPWMPeriodScale(m_channel, 1); // Squelch 1 out of 2 outputs
|
| 328 | break;
|
| 329 | case kPeriodMultiplier_1X:
|
| 330 | m_module->SetPWMPeriodScale(m_channel, 0); // Don't squelch any outputs
|
| 331 | break;
|
| 332 | default:
|
| 333 | wpi_assert(false);
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 |
|
| 338 | void PWM::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
|
| 339 | SetSpeed(value.f);
|
| 340 | }
|
| 341 |
|
| 342 | void PWM::UpdateTable() {
|
| 343 | if (m_table != NULL) {
|
| 344 | m_table->PutNumber("Value", GetSpeed());
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | void PWM::StartLiveWindowMode() {
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame^] | 349 | if (m_table != NULL) {
|
| 350 | m_table->AddTableListener("Value", this, true);
|
| 351 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 352 | }
|
| 353 |
|
| 354 | void PWM::StopLiveWindowMode() {
|
| 355 | SetSpeed(0);
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame^] | 356 | if (m_table != NULL) {
|
| 357 | m_table->RemoveTableListener(this);
|
| 358 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 359 | }
|
| 360 |
|
| 361 | std::string PWM::GetSmartDashboardType() {
|
| 362 | return "Speed Controller";
|
| 363 | }
|
| 364 |
|
| 365 | void PWM::InitTable(ITable *subTable) {
|
| 366 | m_table = subTable;
|
| 367 | UpdateTable();
|
| 368 | }
|
| 369 |
|
| 370 | ITable * PWM::GetTable() {
|
| 371 | return m_table;
|
| 372 | }
|
| 373 |
|