CANTalon

class wpilib.CANTalon(deviceNumber, controlPeriodMs=10)[source]

Bases: wpilib.MotorSafety

Talon SRX device as a CAN device

The TALON SRX is designed to instrument all runtime signals periodically. The default periods are chosen to support 16 TALONs with 10ms update rate for control (throttle or setpoint). However these can be overridden with setStatusFrameRate().

Likewise most control signals are sent periodically using the fire-and-forget CAN API.

Signals that are not available in an unsolicited fashion are the Close Loop gains. For teams that have a single profile for their TALON close loop they can use either the webpage to configure their TALONs once or set the PIDF,Izone,CloseLoopRampRate,etc... once in the robot application. These parameters are saved to flash so once they are loaded in the TALON, they will persist through power cycles and mode changes.

For teams that have one or two profiles to switch between, they can use the same strategy since there are two slots to choose from and the ProfileSlotSelect is periodically sent in the 10 ms control frame.

For teams that require changing gains frequently, they can use the soliciting API to get and set those parameters. Most likely they will only need to set them in a periodic fashion as a function of what motion the application is attempting. If this API is used, be mindful of the CAN utilization reported in the driver station.

Encoder position is measured in encoder edges. Every edge is counted (similar to roboRIO 4X mode). Analog position is 10 bits, meaning 1024 ticks per rotation (0V => 3.3V). Use setFeedbackDevice() to select which sensor type you need. Once you do that you can use getSensorPosition() and getSensorVelocity(). These signals are updated on CANBus every 20ms (by default). If a relative sensor is selected, you can zero (or change the current value) using setSensorPosition().

Analog Input and quadrature position (and velocity) are also explicitly reported in getEncPosition(), getEncVelocity(), getAnalogInPosition(), getAnalogInRaw(), getAnalogInVelocity(). These signals are available all the time, regardless of what sensor is selected at a rate of 100ms. This allows easy instrumentation for “in the pits” checking of all sensors regardless of modeselect. The 100ms rate is overridable for teams who want to acquire sensor data for processing, not just instrumentation. Or just select the sensor using setFeedbackDevice() to get it at 20ms.

Velocity is in position ticks / 100ms.

All output units are in respect to duty cycle (throttle) which is -1023(full reverse) to +1023 (full forward). This includes demand (which specifies duty cycle when in duty cycle mode) and rampRamp, which is in throttle units per 10ms (if nonzero).

When in (default) PercentVBus mode, set() and get() are automatically scaled to a -1.0 to +1.0 range to match other motor controllers.

Pos and velocity close loops are calc’d as:

err = target - posOrVel
iErr += err
if IZone != 0 and abs(err) > IZone:
    ClearIaccum()
output = P * err + I * iErr + D * dErr + F * target
dErr = err - lastErr

P, I, and D gains are always positive. F can be negative.

Motor direction can be reversed using reverseOutput() if sensor and motor are out of phase. Similarly feedback sensor can also be reversed (multiplied by -1) using reverseSensor() if you prefer the sensor to be inverted.

P gain is specified in throttle per error tick. For example, a value of 102 is ~9.9% (which is 102/1023) throttle per 1 ADC unit(10bit) or 1 quadrature encoder edge depending on selected sensor.

I gain is specified in throttle per integrated error. For example, a value of 10 equates to ~0.99% (which is 10/1023) for each accumulated ADC unit(10bit) or 1 quadrature encoder edge depending on selected sensor. Close loop and integral accumulator runs every 1ms.

D gain is specified in throttle per derivative error. For example a value of 102 equates to ~9.9% (which is 102/1023) per change of 1 unit (ADC or encoder) per ms.

I Zone is specified in the same units as sensor position (ADC units or quadrature edges). If pos/vel error is outside of this value, the integrated error will auto-clear:

if IZone != 0 and abs(err) > IZone:
    ClearIaccum()

This is very useful in preventing integral windup and is highly recommended if using full PID to keep stability low.

CloseLoopRampRate is in throttle units per 1ms. Set to zero to disable ramping. Works the same as RampThrottle but only is in effect when a close loop mode and profile slot is selected.

class ControlMode[source]

Bases: builtins.object

Current = 3
Disabled = 15
Follower = 5
PercentVbus = 0
Position = 1
Speed = 2
Voltage = 4
class CANTalon.FeedbackDevice[source]

Bases: builtins.object

AnalogEncoder = 3
AnalogPot = 2
EncFalling = 5
EncRising = 4
QuadEncoder = 0
class CANTalon.StatusFrameRate[source]

Bases: builtins.object

enumerated types for frame rate ms

AnalogTempVbat = 3
Feedback = 1
General = 0
QuadEncoder = 2
CANTalon.changeControlMode(controlMode)[source]
CANTalon.clearIaccum()[source]

Clear the accumulator for I gain.

CANTalon.clearStickyFaults()[source]
CANTalon.configFwdLimitSwitchNormallyOpen(normallyOpen)[source]

Configure the fwd limit switch to be normally open or normally closed. Talon will disable momentarilly if the Talon’s current setting is dissimilar to the caller’s requested setting.

Since Talon saves setting to flash this should only affect a given Talon initially during robot install.

Parameters:normallyOpen – True for normally open. False for normally closed.
CANTalon.configRevLimitSwitchNormallyOpen(normallyOpen)[source]
  • Configure the rev limit switch to be normally open or normally closed.
  • Talon will disable momentarilly if the Talon’s current setting
  • is dissimilar to the caller’s requested setting.
  • Since Talon saves setting to flash this should only affect
  • a given Talon initially during robot install.
  • @param normallyOpen true for normally open. false for normally closed.
CANTalon.disable()[source]
CANTalon.disableControl()[source]
CANTalon.enableBrakeMode(brake)[source]
CANTalon.enableControl()[source]
CANTalon.enableForwardSoftLimit(enable)[source]
CANTalon.enableLimitSwitch(forward, reverse)[source]
CANTalon.enableReverseSoftLimit(enable)[source]
CANTalon.free()[source]
CANTalon.get()[source]

Gets the current status of the Talon (usually a sensor value).

In Current mode: returns output current.

In Speed mode: returns current speed.

In Position omde: returns current sensor position.

In Throttle and Follower modes: returns current applied throttle.

Returns:The current sensor value of the Talon.
CANTalon.getAnalogInPosition()[source]

Get the current analog in position, regardless of whether it is the current feedback device.

Returns:The 24bit analog position. The bottom ten bits is the ADC (0 - 1023) on the analog pin of the Talon. The upper 14 bits tracks the overflows and underflows (continuous sensor).
CANTalon.getAnalogInRaw()[source]

Get the current analog in position, regardless of whether it is the current feedback device. :returns: The ADC (0 - 1023) on analog pin of the Talon.

CANTalon.getAnalogInVelocity()[source]

Get the current encoder velocity, regardless of whether it is the current feedback device.

Returns:The current speed of the analog in device.
CANTalon.getBrakeEnableDuringNeutral()[source]

Returns True if break is enabled during neutral. False if coast.

CANTalon.getBusVoltage()[source]
Returns:The voltage at the battery terminals of the Talon, in Volts.
CANTalon.getCloseLoopRampRate()[source]

Get the closed loop ramp rate for the current profile.

Limits the rate at which the throttle will change. Only affects position and speed closed loop modes.

Returns:rampRate Maximum change in voltage, in volts / sec.
See:#setProfile For selecting a certain profile.
CANTalon.getClosedLoopError()[source]

Get the current difference between the setpoint and the sensor value.

Returns:The error, in whatever units are appropriate.
CANTalon.getControlMode()[source]
CANTalon.getD()[source]
CANTalon.getDescription()[source]
CANTalon.getDeviceID()[source]
CANTalon.getEncPosition()[source]

Get the current encoder position, regardless of whether it is the current feedback device.

Returns:The current position of the encoder.
CANTalon.getEncVelocity()[source]

Get the current encoder velocity, regardless of whether it is the current feedback device.

Returns:The current speed of the encoder.
CANTalon.getF()[source]
CANTalon.getFaultForLim()[source]
CANTalon.getFaultForSoftLim()[source]
CANTalon.getFaultHardwareFailure()[source]
CANTalon.getFaultOverTemp()[source]
CANTalon.getFaultRevLim()[source]
CANTalon.getFaultRevSoftLim()[source]
CANTalon.getFaultUnderVoltage()[source]
CANTalon.getFirmwareVersion()[source]
Returns:The version of the firmware running on the Talon
CANTalon.getI()[source]
CANTalon.getIZone()[source]
CANTalon.getIaccum()[source]
CANTalon.getNumberOfQuadIdxRises()[source]

Get the number of of rising edges seen on the index pin.

Returns:number of rising edges on idx pin.
CANTalon.getOutputCurrent()[source]

Returns the current going through the Talon, in Amperes.

CANTalon.getOutputVoltage()[source]
Returns:The voltage being output by the Talon, in Volts.
CANTalon.getP()[source]

Get the current proportional constant.

Returns:double proportional constant for current profile.
CANTalon.getPinStateQuadA()[source]
Returns:IO level of QUADA pin.
CANTalon.getPinStateQuadB()[source]
Returns:IO level of QUADB pin.
CANTalon.getPinStateQuadIdx()[source]
Returns:IO level of QUAD Index pin.
CANTalon.getPosition()[source]
CANTalon.getSensorPosition()[source]
CANTalon.getSensorVelocity()[source]
CANTalon.getSetpoint()[source]
Returns:The latest value set using set().
CANTalon.getSpeed()[source]
CANTalon.getStickyFaultForLim()[source]
CANTalon.getStickyFaultForSoftLim()[source]
CANTalon.getStickyFaultOverTemp()[source]
CANTalon.getStickyFaultRevLim()[source]
CANTalon.getStickyFaultRevSoftLim()[source]
CANTalon.getStickyFaultUnderVoltage()[source]
CANTalon.getTemp()[source]

Returns temperature of Talon, in degrees Celsius.

CANTalon.handle[source]
CANTalon.isControlEnabled()[source]
CANTalon.isFwdLimitSwitchClosed()[source]

Returns True if limit switch is closed. False if open.

CANTalon.isRevLimitSwitchClosed()[source]

Returns True if limit switch is closed. False if open.

CANTalon.kDelayForSolicitedSignals = 0.004
CANTalon.pidWrite(output)[source]
CANTalon.reverseOutput(flip)[source]

Flips the sign (multiplies by negative one) the throttle values going into the motor on the talon in closed loop modes.

Parameters:flip – True if motor output should be flipped; False if not.
CANTalon.reverseSensor(flip)[source]

Flips the sign (multiplies by negative one) the sensor values going into the talon.

This only affects position and velocity closed loop control. Allows for
situations where you may have a sensor flipped and going in the wrong direction.
Parameters:flip – True if sensor input should be flipped; False if not.
CANTalon.set(outputValue, syncGroup=0)[source]

Sets the appropriate output on the talon, depending on the mode.

In PercentVbus, the output is between -1.0 and 1.0, with 0.0 as stopped.

In Follower mode, the output is the integer device ID of the talon to
duplicate.

In Voltage mode, outputValue is in volts.

In Current mode, outputValue is in amperes.

In Speed mode, outputValue is in position change / 10ms.

In Position mode, outputValue is in encoder ticks or an analog value,
depending on the sensor.
Parameters:outputValue – The setpoint value, as described above.
CANTalon.setCloseLoopRampRate(rampRate)[source]

Set the closed loop ramp rate for the current profile.

Limits the rate at which the throttle will change. Only affects position and speed closed loop modes.

Parameters:rampRate – Maximum change in voltage, in volts / sec.
See:#setProfile For selecting a certain profile.
CANTalon.setD(d)[source]

Set the derivative constant of the currently selected profile.

Parameters:d – Derivative constant for the currently selected PID profile.
See:#setProfile For selecting a certain profile.
CANTalon.setF(f)[source]

Set the feedforward value of the currently selected profile.

Parameters:f – Feedforward constant for the currently selected PID profile.
See:#setProfile For selecting a certain profile.
CANTalon.setFeedbackDevice(device)[source]
CANTalon.setForwardSoftLimit(forwardLimit)[source]
CANTalon.setI(i)[source]

Set the integration constant of the currently selected profile.

Parameters:i – Integration constant for the currently selected PID profile.
See:#setProfile For selecting a certain profile.
CANTalon.setIZone(izone)[source]

Set the integration zone of the current Closed Loop profile.

Whenever the error is larger than the izone value, the accumulated integration error is cleared so that high errors aren’t racked up when at high errors.

An izone value of 0 means no difference from a standard PIDF loop.

Parameters:izone – Width of the integration zone.
See:#setProfile For selecting a certain profile.
CANTalon.setP(p)[source]

Set the proportional value of the currently selected profile.

Parameters:p – Proportional constant for the currently selected PID profile.
See:#setProfile For selecting a certain profile.
CANTalon.setPID(p, i, d, f=0, izone=0, closeLoopRampRate=0, profile=None)[source]

Sets control values for closed loop control.

Parameters:
  • p – Proportional constant.
  • i – Integration constant.
  • d – Differential constant.
  • f – Feedforward constant.
  • izone – Integration zone – prevents accumulation of integration error with large errors. Setting this to zero will ignore any izone stuff.
  • closeLoopRampRate – Closed loop ramp rate. Maximum change in voltage, in volts / sec.
  • profile – which profile to set the pid constants for. You can have two profiles, with values of 0 or 1, allowing you to keep a second set of values on hand in the talon. In order to switch profiles without recalling setPID, you must call setProfile().
CANTalon.setPosition(pos)[source]
CANTalon.setProfile(profile)[source]

Select which closed loop profile to use, and uses whatever PIDF gains and the such that are already there.

CANTalon.setReverseSoftLimit(reverseLimit)[source]
CANTalon.setSensorPosition(pos)[source]
CANTalon.setStatusFrameRateMs(stateFrame, periodMs)[source]

Change the periodMs of a TALON’s status frame. See StatusFrameRate enum for what’s available.

CANTalon.setVoltageRampRate(rampRate)[source]

Set the voltage ramp rate for the current profile.

Limits the rate at which the throttle will change. Affects all modes.

Parameters:rampRate – Maximum change in voltage, in volts / sec.
CANTalon.stopMotor()[source]

Common interface for stopping a motor.