Reading Position and Velocity
Frames
Section titled “Frames”For reading values like position and velocity, there are two ways of reading data: the value directly and from a Frame object. Frames contain both the value and the timestamp when it was received, whereas reading the value directly will carry no timestamp information.
Position
Section titled “Position”Position in the Helium Canandmag refers to the relative position of the encoder, which resets to 0 on power on. Position is returned in the units of revolutions.
Absolute Position refers to the absolute position of the encoder, which does not reset to 0 on power on, and is wrapped between 0 and 1.
// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag = new Canandmag(0);
// Get the relative position of the encoderdouble position = canandmag.getPosition();
// Get the absolute position of the encoderdouble abspos = canandmag.getAbsPosition();
// Get a timestamped value of the relative positionFrameData<Double> relativePositionData = canandmag.getPositionFrame().getFrameData();
// Get the timestamp of the position frame data (seconds, FPGA time)relativePositionData.getTimestamp();
// The actual data within the framedouble framePosition = relativePositionData.getValue();
// Get the absolute position frame object// You can pass this into Frame.waitForFrames or use .addCallback on this// to get timestamped data synchronously or asynchronously, respectively.Frame<Double> absPositionFrame = canandmag.getAbsPositionFrame();// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag{0};
// Get the relative position of the encoderunits::turn_t position = canandmag.GetPosition();
//Get the absolute position of the encoderunits::turn_t abspos = canandmag.GetAbsPosition();
// Get a timestamped value of the relative positionFrameData<units::turn_t> relativePositionData = canandmag.GetPositionFrame();
// Get the timestamp of the position frame data (seconds, FPGA time)relativePositionData.GetTimestamp();
// The actual data within the frameunits::turn_t framePosition = relativePositionData.GetValue();
// Get the absolute position frame object// You can pass this into Frame.WaitForFrames or use .AddCallback on this// to get timestamped data synchronously or asynchronously, respectively.Frame<units::turn_t> absPositionFrame = canandmag.GetAbsPositionFrame();Zeroing Position and Offsets
Section titled “Zeroing Position and Offsets”Both the absolute and relative position of the encoder can be offset, or reset to zero. The relative position offset is not saved on a reboot, whereas the absolute position offset is saved on reboot (by default).
When setting the position, the offset is calculated such that the current value of the relative or absolute position becomes the passed position. For example, an encoder at position 10, which has its position set to 20, applies an offset of +10 internally, so that the reported position becomes 20.
Using the zero function is equivalent to pressing the onboard zero button: it sets both the relative and absolute position of the encoder to 0.
// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag = new Canandmag(0);
// Set the relative position to 100 revolutionscanandmag.setPosition(100);
// Set the absolute position to 0.5 revolutionscanandmag.setAbsPosition(0.5);
// Set the absolute position to 0.3 revolutions, with no timeout to check for success (0 seconds),// and but do not persist the zero offset on a device reboot (false)canandmag.setAbsPosition(0.3, 0, false);
// Zero both the relative and absolute position// (overrides the two offsets we set before)canandmag.zeroAll();// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag{0};
// Set the relative position to 100 revolutionscanandmag.SetPosition(100_tr);
// Set the absolute position to 0.5 revolutionscanandmag.SetAbsPosition(0.5_tr);
// Set the absolute position to 0.3 revolutions, with no timeout to check for success (0 seconds),// and but do not persist the zero offset on a device reboot (false)canandmag.SetAbsPosition(0.3_tr, 0_s, false);
// Zero both the relative and absolute position// (overrides the two offsets we set before)canandmag.ZeroAll();Velocity
Section titled “Velocity”Velocity in the Helium Canandmag is returned in the units of revolutions per second.
// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag = new Canandmag(0);
// Get the current velocity in revolutions per seconddouble velocity = canandmag.getVelocity();
// Get the velocity Frame.// Acts similarly to the position frame.Frame<Double> velocityFrame = canandmag.getVelocityFrame();
// Frames also expose the last data timestamp directly.velocityFrame.getTimestamp();// Creates a Canandmag object referencing a Canandmag with CAN ID 0Canandmag canandmag{0};
// Get the current velocity in revolutions per secondunits::turns_per_second_t velocity = canandmag.GetVelocity();
// Get the velocity Frame.// Acts similarly to the position frame.Frame<units::turns_per_second_t> velocityFrame = canandmag.GetVelocityFrame();
// Frames also expose the last data timestamp directly.velocityFrame.GetTimestamp();