Reading Position and Velocity

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

Position in the Helium Canandcoder 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 Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder = new Canandcoder(0);

// Get the relative position of the encoder
double position = canandcoder.getPosition();

// Get the absolute position of the encoder
double abspos = canandcoder.getAbsPosition();

// Get a timestamped value of the relative position
FrameData<Double> relativePositionData = canandcoder.getPositionFrame().getFrameData();

// Get the time stamp of the position frame data (seconds, FPGA time)
relativePositionData.getTimestamp();

// The actual data within the frame
double 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 = canandcoder.getAbsPositionFrame();
// Creates a Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder{0};

// Get the relative position of the encoder
units::turn_t position = canandcoder.GetPosition();

//Get the absolute position of the encoder
units::turn_t abspos = canandcoder.GetAbsPosition();

// Get a timestamped value of the relative position
FrameData<units::turn_t> relativePositionData = canandcoder.GetPositionFrame();

// Get the time stamp of the position frame data (seconds, FPGA time)
relativePositionData.GetTimestamp();

// The actual data within the frame
units::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 = canandcoder.GetAbsPositionFrame();

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 Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder = new Canandcoder(0);

// Set the relative position to 100 revolutions
canandcoder.setPosition(100);

// Set the absolute position to 0.5 revolutions
canandcoder.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)
canandcoder.setAbsPosition(0.3, 0, false);

// Zero both the relative and absolute position
// (overrides the two offsets we set before)
canandcoder.zeroAll();
// Creates a Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder{0};

// Set the relative position to 100 revolutions
canandcoder.SetPosition(100_tr);

// Set the absolute position to 0.5 revolutions
canandcoder.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)
canandcoder.setAbsPosition(0.3_tr, 0_s, false);

// Zero both the relative and absolute position
// (overrides the two offsets we set before)
canandcoder.ZeroAll();

Velocity

Velocity in the Helium Canandcoder is returned in the units of revolutions per second.

// Creates a Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder = new Canandcoder(0);

// Get the current velocity in revolutions per second
double velocity = canandcoder.getVelocity();

// Get the velocity Frame.
// Acts similarly to the position frame.
Frame<Double> velocityFrame = canandcoder.getVelocityFrame();

// Frames also expose the last data timestamp directly.
velocityFrame.getTimestamp();
// Creates a Canandcoder object referencing a Canandcoder with CAN ID 0
Canandcoder canandcoder{0};

// Get the current velocity in revolutions per second
units::turns_per_second_t velocity = canandcoder.GetVelocity();

// Get the velocity Frame.
// Acts similarly to the position frame.
Frame<units::turns_per_second_t> velocityFrame = canandcoder.GetVelocityFrame();

// Frames also expose the last data timestamp directly.
velocityFrame.GetTimestamp();