CTRE_Phoenix 5.20.2
ctre::phoenix::sensors::CANCoder Class Reference

#include <CANCoder.h>

Inherits ctre::phoenix::CANBusAddressable.

Inherited by ctre::phoenix::sensors::WPI_CANCoder.

Public Member Functions

 CANCoder (int deviceNumber, std::string const &canbus="")
 
double GetPosition ()
 
double GetVelocity ()
 
ErrorCode SetPosition (double newPosition, int timeoutMs=0)
 
ErrorCode SetPositionToAbsolute (int timeoutMs=0)
 
double GetAbsolutePosition ()
 
ErrorCode ConfigVelocityMeasurementPeriod (SensorVelocityMeasPeriod period, int timeoutMs=0)
 
ErrorCode ConfigVelocityMeasurementWindow (int windowSize, int timeoutMs=0)
 
ErrorCode ConfigAbsoluteSensorRange (ctre::phoenix::sensors::AbsoluteSensorRange absoluteSensorRange, int timeoutMs=0)
 
ErrorCode ConfigMagnetOffset (double offsetDegrees, int timeoutMs=0)
 
ErrorCode ConfigSensorInitializationStrategy (ctre::phoenix::sensors::SensorInitializationStrategy initializationStrategy, int timeoutMs=0)
 
ErrorCode ConfigFeedbackCoefficient (double sensorCoefficient, const std::string &unitString, ctre::phoenix::sensors::SensorTimeBase sensortimeBase, int timeoutMs=0)
 
double GetBusVoltage ()
 
ctre::phoenix::sensors::MagnetFieldStrength GetMagnetFieldStrength ()
 
ErrorCode ConfigSensorDirection (bool bSensorDirection, int timeoutMs=0)
 
ErrorCode GetLastError ()
 
std::string GetLastUnitString ()
 
double GetLastTimestamp ()
 
ErrorCode ConfigSetCustomParam (int newValue, int paramIndex, int timeoutMs=0)
 
int ConfigGetCustomParam (int paramIndex, int timeoutMs=0)
 
ErrorCode ConfigSetParameter (ParamEnum param, double value, uint8_t subValue, int ordinal, int timeoutMs=0)
 
double ConfigGetParameter (ParamEnum param, int ordinal, int timeoutMs=0)
 
ErrorCode ConfigGetParameter (ParamEnum param, int32_t valueToSend, int32_t &valueReceived, uint8_t &subValue, int32_t ordinal, int32_t timeoutMs)
 
ErrorCode SetStatusFramePeriod (CANCoderStatusFrame statusFrame, uint8_t periodMs, int timeoutMs=0)
 
int GetStatusFramePeriod (CANCoderStatusFrame frame, int timeoutMs=0)
 
int GetFirmwareVersion ()
 
bool HasResetOccurred ()
 
ErrorCode GetFaults (CANCoderFaults &toFill)
 
ErrorCode GetStickyFaults (CANCoderStickyFaults &toFill)
 
ErrorCode ClearStickyFaults (int timeoutMs=0)
 
ctre::phoenix::ErrorCode ConfigAllSettings (const CANCoderConfiguration &allConfigs, int timeoutMs=50)
 
ErrorCode GetAllConfigs (CANCoderConfiguration &allConfigs, int timeoutMs=50)
 
ErrorCode ConfigFactoryDefault (int timeoutMs=50)
 
CANCoderSimCollectionGetSimCollection ()
 
- Public Member Functions inherited from ctre::phoenix::CANBusAddressable
 CANBusAddressable (int deviceNumber)
 
int GetDeviceNumber ()
 

Static Public Member Functions

static void DestroyAllCANCoders ()
 

Detailed Description

CTRE CANCoder.


// 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

Constructor & Destructor Documentation

◆ CANCoder()

ctre::phoenix::sensors::CANCoder::CANCoder ( int  deviceNumber,
std::string const &  canbus = "" 
)

Constructor.

Parameters
deviceNumberThe CAN Device ID of the CANCoder.
canbusName of the CANbus; can be a SocketCAN interface (on Linux), or a CANivore device name or serial number

Member Function Documentation

◆ ClearStickyFaults()

ErrorCode ctre::phoenix::sensors::CANCoder::ClearStickyFaults ( int  timeoutMs = 0)

Clears the Sticky Faults

Parameters
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigAbsoluteSensorRange ( ctre::phoenix::sensors::AbsoluteSensorRange  absoluteSensorRange,
int  timeoutMs = 0 
)

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
absoluteSensorRangeDesired Sign/Range for the absolute position register.
timeoutMsTimeout 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()

ctre::phoenix::ErrorCode ctre::phoenix::sensors::CANCoder::ConfigAllSettings ( const CANCoderConfiguration allConfigs,
int  timeoutMs = 50 
)

Configures all persistent settings.

Parameters
allConfigsObject with all of the persistant settings
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigFactoryDefault ( int  timeoutMs = 50)

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

Parameters
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigFeedbackCoefficient ( double  sensorCoefficient,
const std::string &  unitString,
ctre::phoenix::sensors::SensorTimeBase  sensortimeBase,
int  timeoutMs = 0 
)

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
sensorCoefficientScalar to multiply the CANCoder's native 12-bit resolute sensor. Defaults to 0.087890625 to produce degrees.
unitStringString 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"
sensortimeBaseDesired denominator to report velocity in. This impacts GetVelocity and the reported velocity in self-test in Tuner. Default is "Per Second".
timeoutMsTimeout 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()

int ctre::phoenix::sensors::CANCoder::ConfigGetCustomParam ( int  paramIndex,
int  timeoutMs = 0 
)

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
paramIndexIndex of custom parameter. [0-1]
timeoutMsTimeout 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() [1/2]

double ctre::phoenix::sensors::CANCoder::ConfigGetParameter ( ParamEnum  param,
int  ordinal,
int  timeoutMs = 0 
)

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
paramParameter enumeration.
ordinalOrdinal of parameter.
timeoutMsTimeout 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.

◆ ConfigGetParameter() [2/2]

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigGetParameter ( ParamEnum  param,
int32_t  valueToSend,
int32_t &  valueReceived,
uint8_t &  subValue,
int32_t  ordinal,
int32_t  timeoutMs 
)

Gets a parameter by passing an int by reference

Parameters
paramParameter enumeration
valueToSendValue to send to parameter
valueReceivedReference to integer to receive
subValueSubValue of parameter
ordinalOrdinal of parameter
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigMagnetOffset ( double  offsetDegrees,
int  timeoutMs = 0 
)

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
offsetDegreesOffset in degrees (unit string and coefficient DO NOT apply for this config).
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigSensorDirection ( bool  bSensorDirection,
int  timeoutMs = 0 
)

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

Parameters
bSensorDirectionFalse (default) means positive rotation occurs when magnet is spun counter-clockwise when observer is facing the LED side of CANCoder.
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigSensorInitializationStrategy ( ctre::phoenix::sensors::SensorInitializationStrategy  initializationStrategy,
int  timeoutMs = 0 
)

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
initializationStrategyThe sensor initialization strategy to use. This will impact the behavior the next time CANCoder boots up.
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigSetCustomParam ( int  newValue,
int  paramIndex,
int  timeoutMs = 0 
)

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
newValueValue for custom parameter.
paramIndexIndex of custom parameter. [0-1]
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigSetParameter ( ParamEnum  param,
double  value,
uint8_t  subValue,
int  ordinal,
int  timeoutMs = 0 
)

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
paramParameter enumeration.
valueValue of parameter.
subValueSubvalue for parameter. Maximum value of 255.
ordinalOrdinal of parameter.
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigVelocityMeasurementPeriod ( SensorVelocityMeasPeriod  period,
int  timeoutMs = 0 
)

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
periodDesired period for the velocity measurement.
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::ConfigVelocityMeasurementWindow ( int  windowSize,
int  timeoutMs = 0 
)

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

Parameters
windowSizeNumber 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.
timeoutMsTimeout 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.

◆ DestroyAllCANCoders()

static void ctre::phoenix::sensors::CANCoder::DestroyAllCANCoders ( )
static

Destructs all CANCoder objects

◆ GetAbsolutePosition()

double ctre::phoenix::sensors::CANCoder::GetAbsolutePosition ( )

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()

ErrorCode ctre::phoenix::sensors::CANCoder::GetAllConfigs ( CANCoderConfiguration allConfigs,
int  timeoutMs = 50 
)

Gets all persistant settings.

Parameters
allConfigsObject with all of the persistant settings
timeoutMsTimeout 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()

double ctre::phoenix::sensors::CANCoder::GetBusVoltage ( )

Gets the bus voltage seen by the device.

Returns
The bus voltage value (in volts).

◆ GetFaults()

ErrorCode ctre::phoenix::sensors::CANCoder::GetFaults ( CANCoderFaults toFill)

Gets the CANCoder fault status

Parameters
toFillContainer for fault statuses.
Returns
Error Code generated by function. 0 indicates no error.

◆ GetFirmwareVersion()

int ctre::phoenix::sensors::CANCoder::GetFirmwareVersion ( )

Gets the firmware version of the device.

Returns
Firmware version of device.

◆ GetLastError()

ErrorCode ctre::phoenix::sensors::CANCoder::GetLastError ( )

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()

double ctre::phoenix::sensors::CANCoder::GetLastTimestamp ( )

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

◆ GetLastUnitString()

std::string ctre::phoenix::sensors::CANCoder::GetLastUnitString ( )

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

◆ GetMagnetFieldStrength()

ctre::phoenix::sensors::MagnetFieldStrength ctre::phoenix::sensors::CANCoder::GetMagnetFieldStrength ( )

Gets the magnet's health.

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

◆ GetPosition()

double ctre::phoenix::sensors::CANCoder::GetPosition ( )

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()

CANCoderSimCollection & ctre::phoenix::sensors::CANCoder::GetSimCollection ( )
Returns
object that can set simulation inputs.

◆ GetStatusFramePeriod()

int ctre::phoenix::sensors::CANCoder::GetStatusFramePeriod ( CANCoderStatusFrame  frame,
int  timeoutMs = 0 
)

Gets the period of the given status frame.

Parameters
frameFrame to get the period of.
timeoutMsTimeout 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()

ErrorCode ctre::phoenix::sensors::CANCoder::GetStickyFaults ( CANCoderStickyFaults toFill)

Gets the CANCoder sticky fault status

Parameters
toFillContainer for sticky fault statuses.
Returns
Error Code generated by function. 0 indicates no error.

◆ GetVelocity()

double ctre::phoenix::sensors::CANCoder::GetVelocity ( )

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 ctre::phoenix::sensors::CANCoder::HasResetOccurred ( )

Returns true if the device has reset since last call.

Returns
Has a Device Reset Occurred?

◆ SetPosition()

ErrorCode ctre::phoenix::sensors::CANCoder::SetPosition ( double  newPosition,
int  timeoutMs = 0 
)

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()

ErrorCode ctre::phoenix::sensors::CANCoder::SetPositionToAbsolute ( int  timeoutMs = 0)

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()

ErrorCode ctre::phoenix::sensors::CANCoder::SetStatusFramePeriod ( CANCoderStatusFrame  statusFrame,
uint8_t  periodMs,
int  timeoutMs = 0 
)

Sets the period of the given status frame.

Parameters
statusFrameFrame whose period is to be changed.
periodMsPeriod in ms for the given frame.
timeoutMsTimeout 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.

The documentation for this class was generated from the following file: