Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2016. 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 the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Spark.h" |
| 9 | |
| 10 | #include "LiveWindow/LiveWindow.h" |
| 11 | |
| 12 | /** |
| 13 | * Note that the Spark uses the following bounds for PWM values. These values |
| 14 | * should work reasonably well for |
| 15 | * most controllers, but if users experience issues such as asymmetric behavior |
| 16 | * around |
| 17 | * the deadband or inability to saturate the controller in either direction, |
| 18 | * calibration is recommended. |
| 19 | * The calibration procedure can be found in the Spark User Manual available |
| 20 | * from REV Robotics. |
| 21 | * |
| 22 | * 2.003ms = full "forward" |
| 23 | * 1.55ms = the "high end" of the deadband range |
| 24 | * 1.50ms = center of the deadband range (off) |
| 25 | * 1.46ms = the "low end" of the deadband range |
| 26 | * 0.999ms = full "reverse" |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * Constructor for a Spark |
| 31 | * @param channel The PWM channel that the Spark is attached to. 0-9 are |
| 32 | * on-board, 10-19 are on the MXP port |
| 33 | */ |
| 34 | Spark::Spark(uint32_t channel) : SafePWM(channel) { |
| 35 | SetBounds(2.003, 1.55, 1.50, 1.46, .999); |
| 36 | SetPeriodMultiplier(kPeriodMultiplier_1X); |
| 37 | SetRaw(m_centerPwm); |
| 38 | SetZeroLatch(); |
| 39 | |
| 40 | HALReport(HALUsageReporting::kResourceType_RevSPARK, GetChannel()); |
| 41 | LiveWindow::GetInstance()->AddActuator("Spark", GetChannel(), this); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the PWM value. |
| 46 | * |
| 47 | * The PWM value is set using a range of -1.0 to 1.0, appropriately |
| 48 | * scaling the value for the FPGA. |
| 49 | * |
| 50 | * @param speed The speed value between -1.0 and 1.0 to set. |
| 51 | * @param syncGroup Unused interface. |
| 52 | */ |
| 53 | void Spark::Set(float speed, uint8_t syncGroup) { |
| 54 | SetSpeed(m_isInverted ? -speed : speed); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the recently set value of the PWM. |
| 59 | * |
| 60 | * @return The most recently set value for the PWM between -1.0 and 1.0. |
| 61 | */ |
| 62 | float Spark::Get() const { return GetSpeed(); } |
| 63 | |
| 64 | /** |
| 65 | * Common interface for inverting direction of a speed controller. |
| 66 | * @param isInverted The state of inversion, true is inverted. |
| 67 | */ |
| 68 | void Spark::SetInverted(bool isInverted) { m_isInverted = isInverted; } |
| 69 | |
| 70 | /** |
| 71 | * Common interface for the inverting direction of a speed controller. |
| 72 | * |
| 73 | * @return isInverted The state of inversion, true is inverted. |
| 74 | * |
| 75 | */ |
| 76 | bool Spark::GetInverted() const { return m_isInverted; } |
| 77 | |
| 78 | /** |
| 79 | * Common interface for disabling a motor. |
| 80 | */ |
| 81 | void Spark::Disable() { SetRaw(kPwmDisabled); } |
| 82 | |
| 83 | /** |
| 84 | * Write out the PID value as seen in the PIDOutput base object. |
| 85 | * |
| 86 | * @param output Write out the PWM value as was found in the PIDController |
| 87 | */ |
| 88 | void Spark::PIDWrite(float output) { Set(output); } |
| 89 | |
| 90 | /** |
| 91 | * Common interface to stop the motor until Set is called again. |
| 92 | */ |
| 93 | void Spark::StopMotor() { this->SafePWM::StopMotor(); } |