> For the complete documentation index, see [llms.txt](https://docs.andymark.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.andymark.com/frc-electronics/can-sensors/can-hex-bore-absolute-encoder-am-5200_can/examples.md).

# 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.

{% hint style="warning" %}
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.
{% endhint %}

{% hint style="info" %}
The snippets use CAN bus 2 and device ID 0. Change both values to match the controller and encoder configuration.
{% endhint %}

## Setup

Install the [AndyMark WPILib vendor library](/frc-electronics/wpilib-am-vendor-library-setup.md) before using CAN. See [Device API](/frc-electronics/can-sensors/can-hex-bore-absolute-encoder-am-5200_can/device-api.md) for all available methods.

{% tabs %}
{% tab title="CAN Java" %}

```java
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
);
```

{% endtab %}

{% tab title="CAN C++" %}

```cpp
#include "AM_CAN_HexBoreEncoder.h"
#include <frc/smartdashboard/SmartDashboard.h>

// Initialize device
// The device's default CAN is 0. Change it using AndyMark IPK
// SystemCore Bus 2 with device ID 0
AM_CAN_HexBoreEncoder encoder{2, 0};

// Restore the default 10 ms report period.
encoder.ResetReportPeriod();

// Run once, only after establishing the mechanical reference.
encoder.SetZeroHere();

// All data the HexBore Encoder provides can be retrieved by using these two lines
AM_EncoderTelemetry 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
frc::SmartDashboard::PutNumber(
    "Encoder/AngleDegrees",
    degrees
);
frc::SmartDashboard::PutNumber(
    "Encoder/VelocityDegreesPerSecond",
    degreesPerSecond
);
```

{% endtab %}

{% tab title="PWM" %}
The absolute PWM output can be read with a duty-cycle input. Scale the measured duty cycle using the pulse timing listed on [Specifications](/frc-electronics/can-sensors/can-hex-bore-absolute-encoder-am-5200_can/specifications.md).

* [WPILib DutyCycleEncoder Java API](https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj/DutyCycleEncoder.html)
* [WPILib DutyCycleEncoder C++ API](https://github.wpilib.org/allwpilib/docs/release/cpp/classfrc_1_1_duty_cycle_encoder.html)
  {% endtab %}

{% tab title="Analog" %}
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.

* [WPILib AnalogInput Java API](https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj/AnalogInput.html)
* [WPILib AnalogInput C++ API](https://github.wpilib.org/allwpilib/docs/release/cpp/classfrc_1_1_analog_input.html)
  {% endtab %}
  {% endtabs %}

## 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.
