001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2017. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010/**
011 * Common base class for all PWM Speed Controllers.
012 */
013public abstract class PWMSpeedController extends SafePWM implements SpeedController {
014  private boolean m_isInverted = false;
015
016  /**
017   * Constructor.
018   *
019   * @param channel The PWM channel that the controller is attached to. 0-9 are on-board, 10-19 are
020   *                on the MXP port
021   */
022  protected PWMSpeedController(int channel) {
023    super(channel);
024  }
025
026  /**
027   * Set the PWM value.
028   *
029   * <p>The PWM value is set using a range of -1.0 to 1.0, appropriately scaling the value for the
030   * FPGA.
031   *
032   * @param speed The speed value between -1.0 and 1.0 to set.
033   */
034  @Override
035  public void set(double speed) {
036    setSpeed(m_isInverted ? -speed : speed);
037    Feed();
038  }
039
040  /**
041   * Common interface for inverting direction of a speed controller.
042   *
043   * @param isInverted The state of inversion true is inverted
044   */
045  @Override
046  public void setInverted(boolean isInverted) {
047    m_isInverted = isInverted;
048  }
049
050  /**
051   * Common interface for the inverting direction of a speed controller.
052   *
053   * @return isInverted The state of inversion, true is inverted.
054   */
055  @Override
056  public boolean getInverted() {
057    return m_isInverted;
058  }
059
060  /**
061   * Get the recently set value of the PWM.
062   *
063   * @return The most recently set value for the PWM between -1.0 and 1.0.
064   */
065  @Override
066  public double get() {
067    return getSpeed();
068  }
069
070  /**
071   * Write out the PID value as seen in the PIDOutput base object.
072   *
073   * @param output Write out the PWM value as was found in the PIDController
074   */
075  @Override
076  public void pidWrite(double output) {
077    set(output);
078  }
079}