ReduxLib C++ 2024.1.1-beta0
Loading...
Searching...
No Matches
redux::sensors::canandcoder::Canandcoder Class Reference

#include <Canandcoder.h>

Inheritance diagram for redux::sensors::canandcoder::Canandcoder:
redux::canand::CanandDevice

Public Member Functions

 Canandcoder (int canID)
 
units::turn_t GetPosition ()
 
units::turn_t GetAbsPosition ()
 
bool SetPosition (units::turn_t newPosition, units::second_t timeout=50_ms)
 
bool SetAbsPosition (units::turn_t newPosition, units::second_t timeout=50_ms, bool ephemeral=false)
 
bool ZeroAll (units::second_t timeout=50_ms)
 
units::turns_per_second_t GetVelocity ()
 
bool MagnetInRange ()
 
CanandcoderFaults GetStickyFaults ()
 
CanandcoderFaults GetActiveFaults ()
 
units::celsius_t GetTemperature ()
 
void ClearStickyFaults ()
 
void SetPartyMode (uint8_t level)
 
- Public Member Functions inherited from redux::canand::CanandDevice
virtual void HandleMessage (CanandMessage &msg)=0
 
virtual CanandAddressGetAddress ()=0
 
bool IsConnected (units::second_t timeout=2_s)
 
virtual std::string GetDeviceClassName ()
 
std::string GetDeviceName ()
 
virtual void PreHandleMessage (CanandMessage &msg)
 
void CheckReceivedFirmwareVersion ()
 
virtual CanandFirmwareVersion GetMinimumFirmwareVersion ()
 
bool SendCANMessage (uint8_t apiIndex, uint8_t *data, uint8_t length)
 
template<std::size_t len>
requires (len < 8U)
void SendCANMessage (uint8_t msgId, std::span< std::byte, len > data)
 

Detailed Description

Class for the CAN interface of the Canandcoder.

If you are using a Canandcoder with Spark Max or Talon with the PWM output, see our Spark Max docs or our Talon SRX docs for information on how to use the encoder with the Rev and CTRE APIs.

The C++ vendordep uses the units library for all dimensioned values, including settings.

Operations that receive data from the device (position, velocity, faults, temperature) generally do not block. The object receives data asynchronously from the CAN packet receive thread and reads thus return the last data received.

Operations that set settings or change offsets will generally wait for up to 50ms by default as they will usually wait for a confirmation packet to be received in response – unless the blocking timeout is set to zero, in which case the operation swill not block.

Example code:

Canandcoder canandcoder{0}; // instantiates with encoder id 0
// Reading the Canandcoder
canandcoder.GetPosition(); // returns a multi-turn relative position, in rotations (turns)
canandcoder.GetAbsPosition(); // returns an absolute position bounded from [0..1) over one rotation
canandcoder.GetVelocity(); // returns measured velocity in rotations per second
// Updating position
canandcoder.SetPosition(-3.5_tr); // sets the relative position to -3.5 turns (does not persist on reboot)
canandcoder.SetAbsPosition(330_deg, 0_s); // sets the absolute position to 330 degrees without blocking for confirmation (persists on reboot)
canandcoder.ZeroAll(); // sets both the relative and absolute position to zero
// Changing configuration
settings.SetVelocityFilterWidth(25_ms); // sets the velocity filter averaging period to 25 ms
settings.SetInvertDirection(true); // make positive be clockwise instead of ccw opposite the sensor face
canandcoder.SetSettings(settings, 50_ms); // apply the new settings to the device, with maximum 50 ms timeout per settings operation
// Faults
canandcoder.ClearStickyFaults(); // clears all sticky faults (including the power cycle flag). This call does not block.
// this flag will always be true on boot until the sticky faults have been cleared,
// so if this prints true the encoder has rebooted sometime between ClearStickyFaults and now.
CanandcoderFaults faults = canandcoder.GetStickyFaults(); // fetches faults
fmt::print("Encoder rebooted: {}\n", faults.powerCycle);
// Timestamped data
redux::frames::FrameData<units::turn_t> posFrameData = canandcoder.GetPositionFrame().GetFrameData(); // gets current position + timestamp together
posFrameData.GetValue(); // fetched position in rotations
posFrameData.GetTimestamp(); // timestamp of the previous position
Definition: Frame.h:21
units::second_t GetTimestamp()
Definition: Frame.h:42
T GetValue()
Definition: Frame.h:35
Definition: CanandcoderFaults.h:13
bool powerCycle
Definition: CanandcoderFaults.h:34
Definition: CanandcoderSettings.h:54
void SetVelocityFilterWidth(units::millisecond_t widthMs)
Definition: Canandcoder.h:91

Constructor & Destructor Documentation

◆ Canandcoder()

redux::sensors::canandcoder::Canandcoder::Canandcoder ( int  canID)

Constructor with the device's id. This object will be constant with respect to whatever CAN id assigned to it, so if a device changes id it may change which device this object reads from.

Parameters
canIDthe device id to use

Member Function Documentation

◆ ClearStickyFaults()

void redux::sensors::canandcoder::Canandcoder::ClearStickyFaults ( )

Clears sticky faults.

It is recommended to clear this during initialization, so one can check if the encoder loses power during operation later.

This call does not block, so it may take up to the next status frame (default every 1000 ms) for the sticky faults to be updated.

◆ GetAbsPosition()

units::turn_t redux::sensors::canandcoder::Canandcoder::GetAbsPosition ( )

Gets the current absolute position of the encoder, in a scaled value from 0 inclusive to 1 exclusive. By default, higher values are in the counter-clockwise direction from the sensor face.

This value will persist across encoder power cycles making it appropriate for swerves/arms/etc.

Returns
absolute position in fraction of a rotation [0..1)

◆ GetActiveFaults()

CanandcoderFaults redux::sensors::canandcoder::Canandcoder::GetActiveFaults ( )

Fetches active faults. Active faults are only active for as long as the error state exists.

Returns
CanandcoderFaults of the active faults

◆ GetPosition()

units::turn_t redux::sensors::canandcoder::Canandcoder::GetPosition ( )

Gets the current integrated position in rotations.

This value does not wrap around, so turning a sensed axle multiple rotations will return multiple sensed rotations of position. By default, positive is in the counter-clockwise direction from the sensor face.

On encoder power-on, unlike the absolute value, this value will always initialize to zero.

Returns
signed relative position in rotations (range [-131072.0..131071.999938396484])

◆ GetStickyFaults()

CanandcoderFaults redux::sensors::canandcoder::Canandcoder::GetStickyFaults ( )

Fetches sticky faults. Sticky faults are the active faults, except once set they do not become unset until ClearStickyFaults() is called.

Returns
CanandcoderFaults of the sticky faults

◆ GetTemperature()

units::celsius_t redux::sensors::canandcoder::Canandcoder::GetTemperature ( )

Get onboard encoder temperature readings in degrees Celsius.

Returns
temperature in degrees Celsius

◆ GetVelocity()

units::turns_per_second_t redux::sensors::canandcoder::Canandcoder::GetVelocity ( )

Returns the measured velocity in rotations per second.

Returns
velocity, in rotations (turns) per second

◆ MagnetInRange()

bool redux::sensors::canandcoder::Canandcoder::MagnetInRange ( )

Returns whether the encoder magnet is in range of the sensor or not. This can be seen visually on the sensor – a green LED is in range, whereas a red LED is out of range.

Returns
whether the output shaft magnet is in range.

◆ SetAbsPosition()

bool redux::sensors::canandcoder::Canandcoder::SetAbsPosition ( units::turn_t  newPosition,
units::second_t  timeout = 50_ms,
bool  ephemeral = false 
)

Sets the new absolute position value for the encoder which will (by default) persist across reboots.

Parameters
newPositionnew absolute position in fraction of a rotation (acceptable range [0..1))
timeoutmaximum time to wait for the operation to be confirmed (default 0.050 seconds). Set to 0 to not check (and not block).
ephemeralif true, set the setting ephemerally – the new zero offset will not persist on power cycle.
Returns
true on success, false on timeout

◆ SetPartyMode()

void redux::sensors::canandcoder::Canandcoder::SetPartyMode ( uint8_t  level)

Controls "party mode" – an encoder identification tool that blinks the onboard LED various colors at a user-specified strobe period. The strobe period of the LED will be (50 milliseconds * level). Setting this to 0 disables party mode.

This function does not block.

Parameters
levelthe party level value to set.

◆ SetPosition()

bool redux::sensors::canandcoder::Canandcoder::SetPosition ( units::turn_t  newPosition,
units::second_t  timeout = 50_ms 
)

Sets the new relative (multi-turn) position of the encoder to the given value.

Note that this does not update the absolute position, and this value is lost on a power cycle. To update the absolute position, use Canandcoder::SetAbsPosition

Parameters
newPositionnew position in rotations
timeoutmaximum time to wait for the operation to be confirmed (default 0.050 seconds). Set to 0 to not check (and not block).
Returns
true on success, false on timeout

◆ ZeroAll()

bool redux::sensors::canandcoder::Canandcoder::ZeroAll ( units::second_t  timeout = 50_ms)

Sets both the current absolute and relative encoder position to 0 – generally equivalent to pressing the physical zeroing button on the encoder.

Parameters
timeoutmaximum time in seconds to wait for each operation to be confirmed (there are 2 ops for zeroing both absolute and relative positions, so the wait is up to 2x timouet). Set to 0 to not check (and not block).
Returns
true on success, false on timeout

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