CANCoder

class phoenix5.sensors.CANCoder(deviceNumber: int, canbus: str = '')

Bases: CANBusAddressable

CTRE CANCoder.

{@code
// Example usage of a CANCoder
CANCoder cancoder{0}; // creates a new CANCoder with ID 0

CANCoderConfiguration config;
// set units of the CANCoder to radians, with velocity being radians per second
config.sensorCoefficient = 2 * M_PI / 4096.0;
config.unitString = "rad";
config.sensorTimeBase = SensorTimeBase::PerSecond;
cancoder.ConfigAllSettings(config);

std::cout << cancoder.GetPosition() << std::endl; // prints the position of the CANCoder
std::cout << cancoder.GetVelocity() << std::endl; // prints the velocity recorded by the CANCoder

ErrorCode error = cancoder.GetLastError(); // gets the last error generated by the CANCoder
CANCoderFaults faults;
ErrorCode faultsError = cancoder.GetFaults(faults); // fills faults with the current CANCoder faults; returns the last error generated

cancoder.SetStatusFramePeriod(CANCoderStatusFrame_SensorData, 10); // changes the period of the sensor data frame to 10ms
}

If the Phoenix 5 API must be used for this device, the device must have 22.X firmware. This firmware is available in Tuner X after selecting Phoenix 5 in the firmware year dropdown.

Deprecated:

This device’s Phoenix 5 API is deprecated for removal in the 2025 season. Users should update to Phoenix 6 firmware and migrate to the Phoenix 6 API. A migration guide is available at https://v6.docs.ctr-electronics.com/en/stable/docs/migration/migration-guide/index.html.

Constructor.

Parameters:
  • deviceNumber – The CAN Device ID of the CANCoder.

  • canbus – Name of the CANbus; can be a SocketCAN interface (on Linux), or a CANivore device name or serial number

clearStickyFaults(timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Clears the Sticky Faults

Parameters:

timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configAbsoluteSensorRange(absoluteSensorRange: phoenix5._ctre.sensors.AbsoluteSensorRange, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the signage and range of the “Absolute Position” signal. Choose unsigned for an absolute range of [0,+1) rotations, [0,360) deg, etc… Choose signed for an absolute range of [-0.5,+0.5) rotations, [-180,+180) deg, etc…

Parameters:
  • absoluteSensorRange – Desired Sign/Range for the absolute position register.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configAllSettings(allConfigs: phoenix5._ctre.sensors.CANCoderConfiguration, timeoutMs: int = 50) phoenix5._ctre.ErrorCode

Configures all persistent settings.

Parameters:
  • allConfigs – Object with all of the persistant settings

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configFactoryDefault(timeoutMs: int = 50) phoenix5._ctre.ErrorCode

Configures all persistent settings to defaults (overloaded so timeoutMs is 50 ms).

Parameters:

timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configFeedbackCoefficient(sensorCoefficient: float, unitString: str, sensortimeBase: phoenix5._ctre.sensors.SensorTimeBase, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Choose what units you want the API to get/set. This also impacts the units displayed in Self-Test in Tuner. Depending on your mechanism, you may want to scale rotational units (deg, radians, rotations), or scale to a distance (inches, centimeters).

Parameters:
  • sensorCoefficient – Scalar to multiply the CANCoder’s native 12-bit resolute sensor. Defaults to 0.087890625 to produce degrees.

  • unitString – String holding the unit to report in. This impacts all routines (except for ConfigMagnetOffset) and the self-test in Tuner. The string value itself is arbitrary. The max number of letters will depend on firmware versioning, but generally CANCoder supports up to eight letters. However, common units such as “centimeters” are supported explicitly despite exceeding the eight-letter limit. * Default is “deg”

  • sensortimeBase – Desired denominator to report velocity in. This impacts GetVelocity and the reported velocity in self-test in Tuner. Default is “Per Second”.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configGetCustomParam(paramIndex: int, timeoutMs: int = 0) int

Gets the value of a custom parameter. This is for arbitrary use.

Sometimes it is necessary to save calibration/duty cycle/output information in the device. Particularly if the device is part of a subsystem that can be replaced.

Parameters:
  • paramIndex – Index of custom parameter. [0-1]

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Value of the custom param.

configGetParameter(*args, **kwargs)

Overloaded function.

  1. configGetParameter(self: phoenix5._ctre.sensors.CANCoder, param: phoenix5._ctre.ParamEnum, ordinal: int, timeoutMs: int = 0) -> float

Gets a parameter. Generally this is not used. This can be utilized in - Using new features without updating API installation. - Errata workarounds to circumvent API implementation. - Allows for rapid testing / unit testing of firmware.

Parameters:
  • param – Parameter enumeration.

  • ordinal – Ordinal of parameter.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Value of parameter.

  1. configGetParameter(self: phoenix5._ctre.sensors.CANCoder, param: phoenix5._ctre.ParamEnum, valueToSend: int, ordinal: int, timeoutMs: int) -> tuple[phoenix5._ctre.ErrorCode, int, int]

Gets a parameter by passing an int by reference

Parameters:
  • param – Parameter enumeration

  • valueToSend – Value to send to parameter

  • valueReceived – Reference to integer to receive

  • subValue – SubValue of parameter

  • ordinal – Ordinal of parameter

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configMagnetOffset(offsetDegrees: float, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Adjusts the zero point for the absolute position register. The absolute position of the sensor will always have a discontinuity (360 -> 0 deg) or (+180 -> -180) and a hard-limited mechanism may have such a discontinuity in its functional range. In which case use this config to move the discontinuity outside of the function range.

Parameters:
  • offsetDegrees – Offset in degrees (unit string and coefficient DO NOT apply for this config).

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configSensorDirection(bSensorDirection: bool, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Choose which direction is interpreted as positive displacement. This affects both “Position” and “Absolute Position”.

Parameters:
  • bSensorDirection – False (default) means positive rotation occurs when magnet is spun counter-clockwise when observer is facing the LED side of CANCoder.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configSensorInitializationStrategy(initializationStrategy: phoenix5._ctre.sensors.SensorInitializationStrategy, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Pick the strategy on how to initialize the CANCoder’s “Position” register. Depending on the mechanism, it may be desirable to auto set the Position register to match the Absolute Position (swerve for example). Or it may be desired to zero the sensor on boot (drivetrain translation sensor or a relative servo).

TIP: Tuner’s self-test feature will report what the boot sensor value will be in the event the CANCoder is reset.

Parameters:
  • initializationStrategy – The sensor initialization strategy to use. This will impact the behavior the next time CANCoder boots up.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configSetCustomParam(newValue: int, paramIndex: int, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the value of a custom parameter. This is for arbitrary use.

Sometimes it is necessary to save calibration/duty cycle/output information in the device. Particularly if the device is part of a subsystem that can be replaced.

Parameters:
  • newValue – Value for custom parameter.

  • paramIndex – Index of custom parameter. [0-1]

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configSetParameter(param: phoenix5._ctre.ParamEnum, value: float, subValue: int, ordinal: int, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets a parameter. Generally this is not used. This can be utilized in - Using new features without updating API installation. - Errata workarounds to circumvent API implementation. - Allows for rapid testing / unit testing of firmware.

Parameters:
  • param – Parameter enumeration.

  • value – Value of parameter.

  • subValue – Subvalue for parameter. Maximum value of 255.

  • ordinal – Ordinal of parameter.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configVelocityMeasurementPeriod(period: phoenix5._ctre.sensors.SensorVelocityMeasPeriod, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Configures the period of each velocity sample. Every 1ms a position value is sampled, and the delta between that sample and the position sampled kPeriod ms ago is inserted into a filter. kPeriod is configured with this function.

Parameters:
  • period – Desired period for the velocity measurement.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

configVelocityMeasurementWindow(windowSize: int, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the number of velocity samples used in the rolling average velocity measurement.

Parameters:
  • windowSize – Number of samples in the rolling average of velocity measurement. Valid values are 1,2,4,8,16,32. If another value is specified, it will truncate to nearest support value.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

static destroyAllCANCoders() None

Destructs all CANCoder objects

getAbsolutePosition() float

Gets the absolute position of the sensor. The absolute position may be unsigned (for example: [0,360) deg), or signed (for example: [-180,+180) deg). This is determined by a configuration. The default selection is unsigned. The units are determined by the coefficient and unit-string configuration params, default is degrees. Note: this signal is not affected by calls to SetPosition().

Returns:

The position of the sensor.

getAllConfigs(allConfigs: phoenix5._ctre.sensors.CANCoderConfiguration, timeoutMs: int = 50) phoenix5._ctre.ErrorCode

Gets all persistant settings.

Parameters:
  • allConfigs – Object with all of the persistant settings

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.

getBusVoltage() float

Gets the bus voltage seen by the device.

Returns:

The bus voltage value (in volts).

getFaults(toFill: phoenix5._ctre.sensors.CANCoderFaults) phoenix5._ctre.ErrorCode

Gets the CANCoder fault status

Parameters:

toFill – Container for fault statuses.

Returns:

Error Code generated by function. 0 indicates no error.

getFirmwareVersion() int

Gets the firmware version of the device.

Returns:

Firmware version of device.

getLastError() phoenix5._ctre.ErrorCode

Call GetLastError() generated by this object. Not all functions return an error code but can potentially report errors.

This function can be used to retrieve those error codes.

Returns:

The last ErrorCode generated.

getLastTimestamp() float

*Get the timestamp of the CAN frame retrieved in the last called get routine.

getLastUnitString() str

Get the units for the signal retrieved in the last called get routine.

getMagnetFieldStrength() phoenix5._ctre.sensors.MagnetFieldStrength

Gets the magnet’s health.

Returns:

The magnet health code (red/orange/green).

getPosition() float

Gets the position of the sensor. This may be relative or absolute depending on configuration. The units are determined by the coefficient and unit-string configuration params, default is degrees.

Returns:

The position of the sensor.

getSimCollection() phoenix5._ctre.sensors.CANCoderSimCollection
Returns:

object that can set simulation inputs.

getStatusFramePeriod(frame: phoenix5._ctre.sensors.CANCoderStatusFrame, timeoutMs: int = 0) int

Gets the period of the given status frame.

Parameters:
  • frame – Frame to get the period of.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Period of the given status frame.

getStickyFaults(toFill: phoenix5._ctre.sensors.CANCoderStickyFaults) phoenix5._ctre.ErrorCode

Gets the CANCoder sticky fault status

Parameters:

toFill – Container for sticky fault statuses.

Returns:

Error Code generated by function. 0 indicates no error.

getVelocity() float

Sets the position of the sensor. The units are determined by the coefficient and unit-string configuration params, default is degrees.

Parameters:

newPosition

Returns:

ErrorCode generated by function. 0 indicates no error.

hasResetOccurred() bool

Returns true if the device has reset since last call.

Returns:

Has a Device Reset Occurred?

setPosition(newPosition: float, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the position of the sensor. The units are determined by the coefficient and unit-string configuration params, default is degrees.

Parameters:

newPosition

Returns:

ErrorCode generated by function. 0 indicates no error.

setPositionToAbsolute(timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the position of the sensor to match the magnet’s “Absolute Sensor”. The units are determined by the coefficient and unit-string configuration params, default is degrees.

Returns:

ErrorCode generated by function. 0 indicates no error.

setStatusFramePeriod(statusFrame: phoenix5._ctre.sensors.CANCoderStatusFrame, periodMs: int, timeoutMs: int = 0) phoenix5._ctre.ErrorCode

Sets the period of the given status frame.

Parameters:
  • statusFrame – Frame whose period is to be changed.

  • periodMs – Period in ms for the given frame.

  • timeoutMs – Timeout value in ms. If nonzero, function will wait for config success and report an error if it times out. If zero, no blocking or checking is performed.

Returns:

Error Code generated by function. 0 indicates no error.