Examples
Use case
The CAN example reads absolute position, velocity, telemetry, and status from the Hex Bore Encoder. It also shows the method used to set the current mechanical position as the CAN zero reference.
Call the zero-setting method only when the mechanism is intentionally positioned at its established reference. Do not call it repeatedly from a periodic loop.
Setup
Install the AndyMark WPILib vendor library before using CAN. See Device API for all available methods.
import com.andymark.jni.AM_CAN_HexBoreEncoder;
import com.andymark.jni.AM_CAN_HexBoreEncoder.AM_EncoderStatus;
import com.andymark.jni.AM_CAN_HexBoreEncoder.AM_Encoder_Telemetry;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
// Initializes the device
// The device's default CAN is 0. Change it using the AndyMark IPK
// SystemCore Bus 2 with device ID 0
AM_CAN_HexBoreEncoder encoder =
new AM_CAN_HexBoreEncoder(2, 0);
// Restore the default 10 ms report period
encoder.resetReportPeriod();
// Run once, only after establishing the mechanical zero reference
encoder.setZeroHere();
// All data the HexBore Encoder provides can be retrieved by using these two lines
AM_Encoder_Telemetry telemetry = encoder.getTelemetry();
AM_EncoderStatus status = encoder.getStatus();
// Retrieves position and velocity in terms of degrees and degrees per second
double degrees = encoder.getAngleDegrees();
double degreesPerSecond = encoder.getVelocityDegPerSec();
// Prints the retreived data to the SmartDashboard
SmartDashboard.putNumber("Encoder/AngleDegrees", degrees);
SmartDashboard.putNumber(
"Encoder/VelocityDegreesPerSecond",
degreesPerSecond
);The absolute PWM output can be read with a duty-cycle input. Scale the measured duty cycle using the pulse timing listed on Specifications.
The absolute analog output can be read with a robot-controller analog input. Confirm the measured voltage at known shaft positions before applying an application-specific scale and offset.
Validation steps
Rotate the shaft by hand through one complete revolution when the mechanism allows it.
Confirm direction, wraparound, offset, and repeatability.
Power-cycle the robot and verify that absolute position returns correctly.
Confirm the measured position before enabling closed-loop motion.
Last updated
Was this helpful?

