ReduxLib C++ 2025.0.0-beta2
Loading...
Searching...
No Matches
redux::sensors::canandgyro::Canandgyro Class Reference

#include <Canandgyro.h>

Inheritance diagram for redux::sensors::canandgyro::Canandgyro:
redux::canand::CanandDevice

Public Member Functions

 Canandgyro (int canID)
 
frc::Quaternion GetQuaternion ()
 
frc::Rotation3d GetRotation3d ()
 
frc::Rotation2d GetRotation2d ()
 
void UseDedicatedYawAngleFrame (bool use)
 
units::turn_t GetYaw ()
 
units::turn_t GetMultiturnYaw ()
 
units::turn_t GetPitch ()
 
units::turn_t GetRoll ()
 
units::turns_per_second_t GetAngularVelocityRoll ()
 
units::turns_per_second_t GetAngularVelocityPitch ()
 
units::turns_per_second_t GetAngularVelocityYaw ()
 
units::standard_gravity_t GetAccelerationX ()
 
units::standard_gravity_t GetAccelerationY ()
 
units::standard_gravity_t GetAccelerationZ ()
 
void StartCalibration ()
 
bool IsCalibrating ()
 
bool WaitForCalibrationToFinish (units::second_t timeout)
 
bool SetPoseRPY (units::turn_t newRoll, units::turn_t newPitch, units::turn_t newYaw, units::second_t timeout=20_ms)
 
bool SetPoseR3D (frc::Rotation3d newPose, units::second_t timeout=20_ms)
 
bool SetPose (frc::Quaternion newPose, units::second_t timeout=20_ms)
 
bool SetYaw (units::turn_t yaw, units::second_t timeout=20_ms)
 
CanandgyroFaults GetStickyFaults ()
 
CanandgyroFaults GetActiveFaults ()
 
units::celsius_t GetTemperature ()
 
CanandgyroStatus GetStatus ()
 
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)
 
virtual 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)
 

Protected Attributes

redux::frames::Frame< bool > calibrating {false, 0_ms}
 
redux::frames::Frame< units::turn_t > singleYaw {0.0_tr, 0_ms}
 
redux::frames::Frame< units::turn_t > multiYaw {0.0_tr, 0_ms}
 
redux::frames::Frame< frc::Quaternion > quat {frc::Quaternion(), 0_ms}
 
redux::frames::Frame< AngularVelocityvel {AngularVelocity{0_tps, 0_tps, 0_tps}, 0_ms}
 
redux::frames::Frame< Accelerationaccel {Acceleration{0_SG, 0_SG, 0_SG}, 0_ms}
 
redux::frames::Frame< CanandgyroStatusstatus {CanandgyroStatus{0, 0, false, 30_degC}, 0_ms}
 
redux::canand::CanandSettingsManager< CanandgyroSettingsstg {*this}
 

Detailed Description

Class for the CAN interface of the canandgyro.

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 20ms 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:

Canandgyro canandgyro{0}; // instantiates with device id 0
// Reading angular position
canandgyro.GetYaw(); // gets the yaw (Z-axis) value in units::turn_t [-180 deg inclusive..180 deg exclusive)
// This is probably what you want to use for robot heading.
canandgyro.GetMultiturnYaw(); // also gets yaw, except without a wraparound
canandgyro.GetPitch(); // pitch (Y-axis) value
canandgyro.GetRoll(); // roll (X-axis) value
canandgyro.GetRotation2d(); // Z-axis Rotation2d object
canandgyro.GetRotation3d(); // Full 3d rotation object
canandgyro.GetQuaternion(); // Raw rotation quaternion object
// Reading angular velocity (all in rotations per second)
canandgyro.GetAngularVelocityYaw();
canandgyro.GetAngularVelocityPitch();
canandgyro.GetAngularVelocityRoll();
// Linear acceleration (gravitational units)
canandgyro.GetAccelerationX();
canandgyro.GetAccelerationY();
canandgyro.GetAccelerationZ();
// Updating pose:
canandgyro.SetYaw(0.25_tr); // set yaw to 0.25 rotations positive
// set roll, pitch, yaw as 0.0, 0.1, and 0.25 rotations with 20 ms timeout
canandgyro.SetPoseRPY(0.0_tr, 0.1_tr, 0.25_tr, 20_ms);
// SetPose
// Manually calibrating:
// The Canandgyro automatically calibrates on boot, but you may want to force a calibration.
// Calibration takes several seconds!!!
canandgyro.StartCalibration(); // begin calibration
canandgyro.IsCalibrating(); // check if the gyro is still calibrating
canandgyro.WaitForCalibrationToFinish(5_s); // wait up to 5 seconds for calibration to finish.
// Faults
canandgyro.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.
CanandgyroFaults faults = canandgyro.GetStickyFaults(); // fetches faults
fmt::print("Device rebooted: {}\n", faults.powerCycle);
// Timestamped data
// gets current angular position + timestamp together
auto& quatFrameData = canandgyro.GetAngularPositionFrame();
quatFrameData.GetValue(); // fetched quaternion object
quatFrameData.GetValue().W(); // fetched quaternion W component
quatFrameData.GetTimestamp(); // timestamp of the quaternion data
Definition: CanandgyroFaults.h:13
bool powerCycle
Definition: CanandgyroFaults.h:35
Definition: Canandgyro.h:110
units::turn_t GetYaw()
Definition: Canandgyro.h:190

Constructor & Destructor Documentation

◆ Canandgyro()

redux::sensors::canandgyro::Canandgyro::Canandgyro ( 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::canandgyro::Canandgyro::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.

◆ GetAccelerationX()

units::standard_gravity_t redux::sensors::canandgyro::Canandgyro::GetAccelerationX ( )
inline

Gets the linear acceleration along the X axis.

Returns
linear acceleration

◆ GetAccelerationY()

units::standard_gravity_t redux::sensors::canandgyro::Canandgyro::GetAccelerationY ( )
inline

Gets the linear acceleration along the Y axis.

Returns
linear acceleration in Gs

◆ GetAccelerationZ()

units::standard_gravity_t redux::sensors::canandgyro::Canandgyro::GetAccelerationZ ( )
inline

Gets the linear acceleration along the Z axis.

Returns
linear acceleration

◆ GetActiveFaults()

CanandgyroFaults redux::sensors::canandgyro::Canandgyro::GetActiveFaults ( )
inline

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

Returns
canandgyroFaults of the active faults

◆ GetAngularVelocityPitch()

units::turns_per_second_t redux::sensors::canandgyro::Canandgyro::GetAngularVelocityPitch ( )
inline

Gets the angular velocity along the pitch (Y) axis.

Returns
angular velocity

◆ GetAngularVelocityRoll()

units::turns_per_second_t redux::sensors::canandgyro::Canandgyro::GetAngularVelocityRoll ( )
inline

Gets the angular velocity along the roll (X) axis.

Returns
angular velocity

◆ GetAngularVelocityYaw()

units::turns_per_second_t redux::sensors::canandgyro::Canandgyro::GetAngularVelocityYaw ( )
inline

Gets the angular velocity along the yaw (Z) axis.

Returns
angular velocity

◆ GetMultiturnYaw()

units::turn_t redux::sensors::canandgyro::Canandgyro::GetMultiturnYaw ( )
inline

Gets a multi-turn yaw (Z-axis) rotation that tracks to multiple continuous rotations.

Note that this relies on the dedicated multi-turn yaw packet so if it is disabled via setYawFramePeriod it will not return fresh data.

Returns
multi-turn yaw in rotational units.

◆ GetPitch()

units::turn_t redux::sensors::canandgyro::Canandgyro::GetPitch ( )
inline

Gets the pitch (Y-axis) rotation from [-0.5 rotations inclusive..0.5 exclusive).

Returns
pitch in rotational units.

◆ GetQuaternion()

frc::Quaternion redux::sensors::canandgyro::Canandgyro::GetQuaternion ( )
inline

Gets a quaternion object of the gyro's 3d rotation from the zero point

Returns
a Quaternion of the current Canandgyro pose

◆ GetRoll()

units::turn_t redux::sensors::canandgyro::Canandgyro::GetRoll ( )
inline

Gets the roll (X-axis) rotation from [-0.5 rotations inclusive..0.5 exclusive).

Returns
roll in rotational units.

◆ GetRotation2d()

frc::Rotation2d redux::sensors::canandgyro::Canandgyro::GetRotation2d ( )
inline

Gets an frc::Rotation2d object representing the rotation around the yaw axis from the zero point If you just want Z-axis rotation use GetYaw().

Returns
a Rotation2d of the current Canandgyro yaw

◆ GetRotation3d()

frc::Rotation3d redux::sensors::canandgyro::Canandgyro::GetRotation3d ( )
inline

Gets an frc::Rotation3d object of the gyro's 3d rotation from the zero point If you just want Z-axis rotation use GetYaw().

Returns
a Rotation3d of the current Canandgyro pose

◆ GetStatus()

CanandgyroStatus redux::sensors::canandgyro::Canandgyro::GetStatus ( )
inline

Get the contents of the previous status packet, which includes active faults, sticky faults, and temperature.

Returns
device status as a status struct

◆ GetStickyFaults()

CanandgyroFaults redux::sensors::canandgyro::Canandgyro::GetStickyFaults ( )
inline

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

Returns
canandgyroFaults of the sticky faults

◆ GetTemperature()

units::celsius_t redux::sensors::canandgyro::Canandgyro::GetTemperature ( )
inline

Get onboard encoder temperature readings in degrees Celsius.

Returns
temperature in degrees Celsius

◆ GetYaw()

units::turn_t redux::sensors::canandgyro::Canandgyro::GetYaw ( )
inline

Gets the yaw (Z-axis) rotation from [-0.5 rotations inclusive..0.5 exclusive).

This is probably the function you want to use for applications like field-centric control.

Returns
yaw in rotational units.

◆ IsCalibrating()

bool redux::sensors::canandgyro::Canandgyro::IsCalibrating ( )
inline

Returns if the Canandgyro is known to be currently calibrating.

Returns
if the Canandgyro is calibrating

◆ SetPartyMode()

void redux::sensors::canandgyro::Canandgyro::SetPartyMode ( uint8_t  level)

Controls "party mode" – an encoder identification tool that blinks the onboard LED various colors if level != 0.

This function does not block.

Parameters
levelthe party level value to set.

◆ SetPose()

bool redux::sensors::canandgyro::Canandgyro::SetPose ( frc::Quaternion  newPose,
units::second_t  timeout = 20_ms 
)

Sets a new pose without recalibrating with an frc::Quaternion.

Parameters
newPosenew quaternion pose
timeoutthe timeout in seconds to wait for a pose set confirmation. Set to 0 to not check (always return true.)
Returns
true if a pose set confirmation was received (or if timeout is zero)

◆ SetPoseR3D()

bool redux::sensors::canandgyro::Canandgyro::SetPoseR3D ( frc::Rotation3d  newPose,
units::second_t  timeout = 20_ms 
)
inline

Sets a new angular position without recalibrating with an frc::Rotation3d.

Parameters
newPosenew rotation3d pose
timeoutthe timeout in seconds to wait for a pose set confirmation. Set to 0 to not check (always return true.)
Returns
true if a pose set confirmation was received (or if timeout is zero)

◆ SetPoseRPY()

bool redux::sensors::canandgyro::Canandgyro::SetPoseRPY ( units::turn_t  newRoll,
units::turn_t  newPitch,
units::turn_t  newYaw,
units::second_t  timeout = 20_ms 
)
inline

Sets a new angular position pose without recalibrating with a given roll/pitch/yaw. If you just want to set yaw, use SetYaw.

Parameters
newRollnew roll (x) pose
newPitchnew pitch (y) pose
newYawnew yaw (z) pose
timeoutthe timeout in seconds to wait for a pose set confirmation. Set to 0 to not check (always return true.)
Returns
true if a pose set confirmation was received (or if timeout is zero)

◆ SetYaw()

bool redux::sensors::canandgyro::Canandgyro::SetYaw ( units::turn_t  yaw,
units::second_t  timeout = 20_ms 
)

Sets a new yaw without recalibrating the Canandgyro. Blocks for up to 50 milliseconds by default to confirm the transaction.

Parameters
yawnew yaw angle in rotations
timeoutthe timeout in seconds to block to confirm the transaction (set 0 to not block)
Returns
true if a confirmation was received or the timeout is zero

◆ StartCalibration()

void redux::sensors::canandgyro::Canandgyro::StartCalibration ( )

Begins calibration on the Canandgyro.

This takes several seconds. To check the state of calibration, use IsCalibrating or WaitForCalibrationToFinish.

◆ UseDedicatedYawAngleFrame()

void redux::sensors::canandgyro::Canandgyro::UseDedicatedYawAngleFrame ( bool  use)
inline

Sets whether this object should use the dedicated yaw message for yaw angle instead of deriving it from the pose quaternion frame.

By default this is true, as the yaw angle frame is more precise and by default more frequent.

Parameters
useuse the yaw angle

◆ WaitForCalibrationToFinish()

bool redux::sensors::canandgyro::Canandgyro::WaitForCalibrationToFinish ( units::second_t  timeout)

Blocks the current thread until the Canandgyro has finished calibrating or until a timeout is reached.

Parameters
timeoutthe timeout in seconds to wait for a calibration confirmation.
Returns
true if the calibration has finished within the timeout, false if not.

Member Data Documentation

◆ accel

redux::frames::Frame<Acceleration> redux::sensors::canandgyro::Canandgyro::accel {Acceleration{0_SG, 0_SG, 0_SG}, 0_ms}
protected

internal Frame variable holding current acceleration state

◆ calibrating

redux::frames::Frame<bool> redux::sensors::canandgyro::Canandgyro::calibrating {false, 0_ms}
protected

internal Frame variable used to track if the device is calibrating

◆ multiYaw

redux::frames::Frame<units::turn_t> redux::sensors::canandgyro::Canandgyro::multiYaw {0.0_tr, 0_ms}
protected

internal Frame variable holding current yaw position state

◆ quat

redux::frames::Frame<frc::Quaternion> redux::sensors::canandgyro::Canandgyro::quat {frc::Quaternion(), 0_ms}
protected

internal Frame variable holding current angular position state

◆ singleYaw

redux::frames::Frame<units::turn_t> redux::sensors::canandgyro::Canandgyro::singleYaw {0.0_tr, 0_ms}
protected

internal Frame variable holding current yaw position state

◆ status

redux::frames::Frame<CanandgyroStatus> redux::sensors::canandgyro::Canandgyro::status {CanandgyroStatus{0, 0, false, 30_degC}, 0_ms}
protected

internal Frame variable holding current status value state

◆ stg

redux::canand::CanandSettingsManager<CanandgyroSettings> redux::sensors::canandgyro::Canandgyro::stg {*this}
protected

internal settings manager

◆ vel

redux::frames::Frame<AngularVelocity> redux::sensors::canandgyro::Canandgyro::vel {AngularVelocity{0_tps, 0_tps, 0_tps}, 0_ms}
protected

internal Frame variable holding current angular velocity state


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