001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2016-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
010import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
011import edu.wpi.first.wpilibj.tables.ITable;
012import edu.wpi.first.wpilibj.tables.ITableListener;
013
014public interface CANSpeedController extends SpeedController, PIDInterface, LiveWindowSendable {
015  /**
016   * Mode determines how the motor is controlled, used internally. This is meant to be subclassed by
017   * enums
018   *
019   * <p>Note that the Jaguar does not support follower mode.
020   */
021  interface ControlMode {
022    /**
023     * Gets the name of this control mode. Since this interface should only be subclassed by
024     * enumerations, this will be overridden by the builtin name() method.
025     */
026    String name();
027
028    /**
029     * Checks if this control mode is PID-compatible.
030     */
031    boolean isPID();
032
033    /**
034     * Gets the integer value of this control mode.
035     */
036    int getValue();
037  }
038
039  /**
040   * Gets the current control mode.
041   *
042   * @return the current control mode
043   */
044  ControlMode getControlMode();
045
046  /**
047   * Sets the control mode of this speed controller.
048   *
049   * @param mode the the new mode
050   */
051  void setControlMode(int mode);
052
053  /**
054   * Set the proportional PID constant.
055   */
056  @SuppressWarnings("ParameterName")
057  void setP(double p);
058
059  /**
060   * Set the integral PID constant.
061   */
062  @SuppressWarnings("ParameterName")
063  void setI(double i);
064
065  /**
066   * Set the derivative PID constant.
067   */
068  @SuppressWarnings("ParameterName")
069  void setD(double d);
070
071  /**
072   * Set the feed-forward PID constant. This method is optional to implement.
073   */
074  @SuppressWarnings("ParameterName")
075  default void setF(double f) {
076  }
077
078  /**
079   * Gets the feed-forward PID constant. This method is optional to implement. If a subclass does
080   * not implement this, it will always return zero.
081   */
082  default double getF() {
083    return 0.0;
084  }
085
086  /**
087   * Get the current input (battery) voltage.
088   *
089   * @return the input voltage to the controller (in Volts).
090   */
091  double getBusVoltage();
092
093  /**
094   * Get the current output voltage.
095   *
096   * @return the output voltage to the motor in volts.
097   */
098  double getOutputVoltage();
099
100  /**
101   * Get the current being applied to the motor.
102   *
103   * @return the current motor current (in Amperes).
104   */
105  double getOutputCurrent();
106
107  /**
108   * Get the current temperature of the controller.
109   *
110   * @return the current temperature of the controller, in degrees Celsius.
111   */
112  double getTemperature();
113
114  /**
115   * Return the current position of whatever the current selected sensor is.
116   *
117   * <p>See specific implementations for more information on selecting feedback sensors.
118   *
119   * @return the current sensor position.
120   */
121  double getPosition();
122
123  /**
124   * Return the current velocity of whatever the current selected sensor is.
125   *
126   * <p>See specific implementations for more information on selecting feedback sensors.
127   *
128   * @return the current sensor velocity.
129   */
130  double getSpeed();
131
132  /**
133   * Set the maximum rate of change of the output voltage.
134   *
135   * @param rampRate the maximum rate of change of the votlage, in Volts / sec.
136   */
137  void setVoltageRampRate(double rampRate);
138
139  /**
140   * All CAN Speed Controllers have the same SmartDashboard type: "CANSpeedController".
141   */
142  String SMART_DASHBOARD_TYPE = "CANSpeedController";
143
144  @Override
145  default void updateTable() {
146    ITable table = getTable();
147    if (table != null) {
148      table.putString("~TYPE~", SMART_DASHBOARD_TYPE);
149      table.putString("Type", getClass().getSimpleName());
150      table.putNumber("Mode", getControlMode().getValue());
151      if (getControlMode().isPID()) {
152        table.putNumber("p", getP());
153        table.putNumber("i", getI());
154        table.putNumber("d", getD());
155        table.putNumber("f", getF());
156      }
157      table.putBoolean("Enabled", isEnabled());
158      table.putNumber("Value", get());
159    }
160  }
161
162  @Override
163  default String getSmartDashboardType() {
164    return SMART_DASHBOARD_TYPE;
165  }
166
167  /**
168   * Creates an ITableListener for the LiveWindow table for this CAN speed controller.
169   */
170  default ITableListener createTableListener() {
171    return (table, key, value, isNew) -> {
172      switch (key) {
173        case "Enabled":
174          if ((Boolean) value) {
175            enable();
176          } else {
177            disable();
178          }
179          break;
180        case "Value":
181          set((Double) value);
182          break;
183        case "Mode":
184          setControlMode(((Double) value).intValue());
185          break;
186        default:
187          break;
188      }
189      if (getControlMode().isPID()) {
190        switch (key) {
191          case "p":
192            setP((Double) value);
193            break;
194          case "i":
195            setI((Double) value);
196            break;
197          case "d":
198            setD((Double) value);
199            break;
200          case "f":
201            setF((Double) value);
202            break;
203          default:
204            break;
205        }
206      }
207    };
208  }
209
210
211}