diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java index c16295a4c7..22a1ff3862 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java @@ -23,7 +23,7 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException; * Texas Instruments Jaguar Speed Controller as a CAN device. * @author Thomas Clark */ -public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveWindowSendable { +public class CANJaguar implements MotorSafety, PIDOutput, CANSpeedController, LiveWindowSendable { public static final int kMaxMessageDataSize = 8; @@ -217,7 +217,7 @@ public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveW // Not all Jaguar firmware reports a hardware version. m_hardwareVersion = 0; } - + // 3330 was the first shipping RDK firmware version for the Jaguar if (m_firmwareVersion >= 3330 || m_firmwareVersion < 108) { diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANSpeedController.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANSpeedController.java new file mode 100644 index 0000000000..09ecca4a65 --- /dev/null +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANSpeedController.java @@ -0,0 +1,122 @@ +package edu.wpi.first.wpilibj; + +public interface CANSpeedController extends SpeedController { + /** + * Mode determines how the motor is controlled, used internally. + * + * Note that the Jaguar does not support follower mode. + */ + public enum ControlMode { + PercentVbus((byte)0), + Current((byte)1), + Speed((byte)2), + Position((byte)3), + Voltage((byte)4), + Follower((byte)5); // Not supported by Jaguar. + + public byte value; + + public static ControlMode valueOf(byte value) { + for(ControlMode mode : values()) { + if(mode.value == value) { + return mode; + } + } + + return null; + } + + private ControlMode(byte value) { + this.value = value; + } + } + + /** + * Return the current setpoint. + * + * @return the current setpoint, as passed to {@link #set}. + */ + public double get(); + + /** + * Set the output for whichever mode we are currently in. + * + * @param value the setpoint, in some units depending on the mode. + */ + public void set(double value); + + /** + * Disables to motor controller output. + */ + public void disable(); + + /** + * Set the proportional PID constant. + */ + public void setP(double p); + + /** + * Set the integral PID constant. + */ + public void setI(double i); + + /** + * Set the derivative PID constant. + */ + public void setD(double d); + + /** + * Get the current input (battery) voltage. + * + * @return the input voltage to the controller (in Volts). + */ + public double getBusVoltage(); + + /** + * Get the current output voltage. + * + * @return the output voltage to the motor in volts. + */ + public double getOutputVoltage(); + + /** + * Get the current being applied to the motor. + * + * @return the current motor current (in Amperes). + */ + public double getOutputCurrent(); + + /** + * Get the current temperature of the controller. + * + * @return the current temperature of the controller, in degrees Celsius. + */ + public double getTemperature(); + + /** + * Return the current position of whatever the current selected sensor is. + * + * See specific implementations for more information on selecting feedback + * sensors. + * + * @return the current sensor position. + */ + public double getPosition(); + + /** + * Return the current velocity of whatever the current selected sensor is. + * + * See specific implementations for more information on selecting feedback + * sensors. + * + * @return the current sensor velocity. + */ + public double getSpeed(); + + /** + * Set the maximum rate of change of the output voltage. + * + * @param rampRate the maximum rate of change of the votlage, in Volts / sec. + */ + public void setVoltageRampRate(double rampRate); +} diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java index c987885368..6f8a0f5bd7 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java @@ -20,14 +20,14 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable; import edu.wpi.first.wpilibj.tables.ITable; import edu.wpi.first.wpilibj.tables.ITableListener; -public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWindowSendable { +public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedController, LiveWindowSendable { private MotorSafetyHelper m_safetyHelper; - public enum ControlMode { + public enum TalonControlMode { PercentVbus(0), Follower(5), Voltage(4), Position(1), Speed(2), Current(3), Disabled(15); public int value; - public static ControlMode valueOf(int value) { - for(ControlMode mode : values()) { + public static TalonControlMode valueOf(int value) { + for(TalonControlMode mode : values()) { if(mode.value == value) { return mode; } @@ -36,7 +36,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi return null; } - private ControlMode(int value) { + private TalonControlMode(int value) { this.value = value; } } @@ -79,7 +79,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi private CanTalonSRX m_impl; - ControlMode m_controlMode; + TalonControlMode m_controlMode; private static double kDelayForSolicitedSignals = 0.004; int m_deviceNumber; @@ -96,7 +96,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi m_profile = 0; m_setPoint = 0; setProfile(m_profile); - applyControlMode(ControlMode.PercentVbus); + applyControlMode(TalonControlMode.PercentVbus); } public CANTalon(int deviceNumber,int controlPeriodMs) { m_deviceNumber = deviceNumber; @@ -106,13 +106,13 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi m_profile = 0; m_setPoint = 0; setProfile(m_profile); - applyControlMode(ControlMode.PercentVbus); + applyControlMode(TalonControlMode.PercentVbus); } @Override public void pidWrite(double output) { - if (getControlMode() == ControlMode.PercentVbus) { + if (getControlMode() == TalonControlMode.PercentVbus) { set(output); } else { @@ -120,6 +120,11 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi } } + @Override + public double pidGet() { + return getPosition(); + } + public void delete() { m_impl.delete(); } @@ -366,7 +371,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi /** * Returns temperature of Talon, in degrees Celsius. */ - public double getTemp() { + public double getTemperature() { long tempp = CanTalonJNI.new_doublep(); // Create a new swig pointer. m_impl.GetTemp(new SWIGTYPE_p_double(tempp, true)); return CanTalonJNI.doublep_value(tempp); @@ -446,7 +451,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi return CanTalonJNI.intp_value(speedp); } - public ControlMode getControlMode() { + public TalonControlMode getControlMode() { return m_controlMode; } /** @@ -455,16 +460,16 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi * @param controlMode Control mode to ultimately enter once user calls set(). * @see #set */ - private void applyControlMode(ControlMode controlMode) { + private void applyControlMode(TalonControlMode controlMode) { m_controlMode = controlMode; - if (controlMode == ControlMode.Disabled) + if (controlMode == TalonControlMode.Disabled) m_controlEnabled = false; // Disable until set() is called. - m_impl.SetModeSelect(ControlMode.Disabled.value); + m_impl.SetModeSelect(TalonControlMode.Disabled.value); UsageReporting.report(tResourceType.kResourceType_CANTalonSRX, m_deviceNumber + 1, controlMode.value); } - public void changeControlMode(ControlMode controlMode) { + public void changeControlMode(TalonControlMode controlMode) { if(m_controlMode == controlMode){ /* we already are in this mode, don't perform disable workaround */ }else{ @@ -484,7 +489,7 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWi } public void disableControl() { - m_impl.SetModeSelect(ControlMode.Disabled.value); + m_impl.SetModeSelect(TalonControlMode.Disabled.value); m_controlEnabled = false; } diff --git a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CANTalonTest.java b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CANTalonTest.java index d17d17dd94..506fdb708d 100644 --- a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CANTalonTest.java +++ b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CANTalonTest.java @@ -87,7 +87,7 @@ public class CANTalonTest extends AbstractComsSetup { @Test public void SetGetPID() { CANTalon talon = new CANTalon(0); - talon.changeControlMode(CANTalon.ControlMode.Position); + talon.changeControlMode(CANTalon.TalonControlMode.Position); double p = 0.05, i = 0.098, d = 1.23; talon.setPID(p, i , d); assertTrue(errorMessage(talon.getP(), p), Math.abs(p - talon.getP()) < 1e-5); @@ -106,7 +106,7 @@ public class CANTalonTest extends AbstractComsSetup { @Test public void positionModeWorks() { CANTalon talon = new CANTalon(0); - talon.changeControlMode(CANTalon.ControlMode.Position); + talon.changeControlMode(CANTalon.TalonControlMode.Position); talon.setFeedbackDevice(CANTalon.FeedbackDevice.AnalogPot); Timer.delay(0.2); double p = 1.0, i = 0.0, d = 0.00;