Add PIDInterface for PIDController and CAN devices.

Added a PIDInterface for the PIDContrller, CANJaguar, and CANTalon to
inherit from.

Change-Id: I88d4943159476a44400009703db5e79d8cd4b5a9
This commit is contained in:
James Kuszmaul
2015-06-15 13:48:53 -04:00
parent 27e4509676
commit 4013402134
11 changed files with 295 additions and 105 deletions

View File

@@ -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, CANSpeedController, LiveWindowSendable {
public class CANJaguar implements MotorSafety, PIDOutput, PIDInterface, CANSpeedController, LiveWindowSendable {
public static final int kMaxMessageDataSize = 8;
@@ -304,6 +304,27 @@ public class CANJaguar implements MotorSafety, PIDOutput, CANSpeedController, Li
return m_value;
}
/**
* Equivalent to {@link #get()}.
*/
@Override
public double getSetpoint() {
return get();
}
/**
* Get the difference between the setpoint and goal in closed loop modes.
*
* Outside of position and velocity modes the return value of getError()
* has relatively little meaning.
*
* @return The difference between the setpoint and the current position.
*/
@Override
public double getError() {
return get() - getPosition();
}
/**
* Sets the output set-point value.
*
@@ -387,6 +408,20 @@ public class CANJaguar implements MotorSafety, PIDOutput, CANSpeedController, Li
set(value, (byte)0);
}
/**
* Equivalent to {@link #set(double)}. Implements PIDInterface.
*/
@Override
public void setSetpoint(double value) {
set(value);
}
@Override
public void reset() {
set(m_value);
disableControl();
}
/**
* Check all unverified params and make sure they're equal to their local
* cached versions. If a value isn't available, it gets requested. If a value
@@ -834,6 +869,11 @@ public class CANJaguar implements MotorSafety, PIDOutput, CANSpeedController, Li
disableControl();
}
// PIDInterface interface
public void enable() {
enableControl();
}
// PIDOutput interface
@Override
public void pidWrite(double output) {
@@ -1061,6 +1101,15 @@ public class CANJaguar implements MotorSafety, PIDOutput, CANSpeedController, Li
enableControl(0.0);
}
/**
* Return whether the controller is enabled.
*
* @returns true if enabled.
*/
public boolean isEnabled() {
return m_controlEnabled;
}
/**
* Disable the closed loop controller.
*

View File

@@ -1,11 +1,3 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.11
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.CanTalonSRX;
@@ -20,7 +12,7 @@ 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, PIDSource, CANSpeedController, LiveWindowSendable {
public class CANTalon implements MotorSafety, PIDOutput, PIDSource, PIDInterface, CANSpeedController, LiveWindowSendable {
private MotorSafetyHelper m_safetyHelper;
public enum TalonControlMode {
PercentVbus(0), Follower(5), Voltage(4), Position(1), Speed(2), Current(3), Disabled(15);
@@ -79,8 +71,10 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
private CanTalonSRX m_impl;
TalonControlMode m_controlMode;
private TalonControlMode m_controlMode;
private static double kDelayForSolicitedSignals = 0.004;
private double m_minimumInput;
private double m_maximumInput;
int m_deviceNumber;
boolean m_controlEnabled;
@@ -98,6 +92,7 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
setProfile(m_profile);
applyControlMode(TalonControlMode.PercentVbus);
}
public CANTalon(int deviceNumber,int controlPeriodMs) {
m_deviceNumber = deviceNumber;
m_impl = new CanTalonSRX(deviceNumber,controlPeriodMs); /* bound period to be within [1 ms,95 ms] */
@@ -183,6 +178,40 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
set(outputValue);
}
/**
* Resets the accumulated integral error and disables the controller.
*
* The only difference between this and {@link PIDController#reset} is that
* the PIDController also resets the previous error for the D term, but the
* difference should have minimal effect as it will only last one cycle.
*/
public void reset() {
disable();
clearIAccum();
}
/**
* Return true if Talon is enabled.
*
* @return true if the Talon is enabled and may be applying power to the motor
*/
public boolean isEnabled() {
return isControlEnabled();
}
/**
* Returns the difference between the setpoint and the current position.
*
* @return The error in units corresponding to whichever mode we are in.
* @see #set(double) set() for a detailed description of the various units.
*/
public double getError() {return getClosedLoopError();}
/**
* Calls {@link #set(double)}.
*/
public void setSetpoint(double setpoint) { set(setpoint); }
/**
* Flips the sign (multiplies by negative one) the sensor values going into
*the talon.
@@ -487,6 +516,9 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
changeControlMode(m_controlMode);
m_controlEnabled = true;
}
public void enable() {
enableControl();
}
public void disableControl() {
m_impl.SetModeSelect(TalonControlMode.Disabled.value);
@@ -652,13 +684,6 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
return CanTalonJNI.intp_value(fp);
}
/**
* Clear the accumulator for I gain.
*/
public void ClearIaccum()
{
SWIGTYPE_p_CTR_Code status = m_impl.SetParam(CanTalonSRX.param_t.ePidIaccum, 0);
}
/**
* Set the proportional value of the currently selected profile.
*
@@ -743,6 +768,14 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
m_impl.SetRampThrottle(rate);
}
/**
* Clear the accumulator for I gain.
*/
public void ClearIaccum()
{
SWIGTYPE_p_CTR_Code status = m_impl.SetParam(CanTalonSRX.param_t.ePidIaccum, 0);
}
/**
* Sets control values for closed loop control.
*
@@ -816,6 +849,9 @@ public class CANTalon implements MotorSafety, PIDOutput, PIDSource, CANSpeedCont
}
// TODO: Documentation for all these accessors/setters for misc. stuff.
public void clearIAccum() {
SWIGTYPE_p_CTR_Code status = m_impl.SetParam(CanTalonSRX.param_t.ePidIaccum, 0);
}
public void setForwardSoftLimit(int forwardLimit) {
m_impl.SetForwardSoftLimit(forwardLimit);
}