# Examples

### Use-Case

This example program demonstrates how to use the AndyMark FRC Hex Bore Encoder in an FRC robot program. It initializes the encoder from the robot’s configuration and continuously reads position data while the robot is enabled. The program can report the encoder’s **absolute position** through multiple output options — **CAN**, **PWM**, and **analog** — so teams can choose the interface that best fits their robot wiring and control system. It also shows how the encoder’s **zero/reset button** can be used to set the current shaft position as **0** (useful for quickly re-indexing mechanisms like arms, turrets, or elevators during setup). The measured position values are then displayed on the Driver Station in real time, allowing users to verify the encoder is installed correctly, confirm direction/offset behavior, and troubleshoot signal integrity or mechanical alignment. This example is useful for quick bring-up and testing, and it can be used without needing in-depth programming knowledge.

### Code Example

To use this encoder via CAN, you must first install the AndyMark vendor libraries. Instructions can be found [here](https://docs.andymark.com/frc-electronics/wpilib-am-vendor-library-setup)

View [Device API](https://docs.andymark.com/frc-electronics/can-sensors/can-hex-bore-absolute-encoder-am-5200_can/device-api) for a comprehensive list of commands

{% 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;

//Initialize Device
//The device's default CAN is 0. Change it using AndyMark CAN interface utility
AM_CAN_HexBoreEncoder hexEncoder = new AM_CAN_HexBoreEncoder(0);
//Reset Report Period to the default of 10ms
hexEncoder.resetReportPeriod();

//This will set the 0 of the encoder to the current position
hexEncoder.setZeroHere();

//All data the HexBore Encoder provides can be retrieved by using these two lines
AM_Encoder_Telemetry telemetryData = hexEncoder.getTelemetry();
AM_EncoderStatus statusData = hexEncoder.getStatus();

//Retrieves position and velocity in terms of degrees and degrees per second
double degrees = hexEncoder.getAngleDegrees();
double degreesPerSec = hexEncoder.getVelocityDegPerSec();
SmartDashboard.putNumber("angleDegrees", degrees);
SmartDashboard.putNumber("Velocity (DegPerSec)", degreesPerSec);
```

{% endtab %}

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

```cpp
//File to include
#include "AM_CAN_HexBoreEncoder.h"

//Initialize device
//The device's default CAN is 0. Change it using AndyMark CAN interface utility
AM_CAN_HexBoreEncoder hexBoreEncoder{0};
//Reset Report Period to the default of 10ms
hexBoreEncoder.ResetReportPeriod();

//This will set the 0 of the encoder to the current position
hexBoreEncoder.setZeroHere();

//All data the HexBore Encoder provides can be retrieved by using these two lines
AM_EncoderTelemetry telemetryData = hexBoreEncoder.GetTelemetry();
AM_EncoderStatus encoderStatus = hexBoreEncoder.GetStatus();

//Retrieves position and velocity in terms of degrees and degrees per second
double degrees = hexBoreEncoder.GetAngleDegrees();
double degreesPerSec = hexBoreEncoder.GetVelocityDegPerSec();
frc::SmartDashboard::PutNumber("angleDegrees",degrees);
frc::SmartDashboard::PutNumber("Velocity (DegPerSec)",degreesPerSec);
```

{% endtab %}

{% tab title="PWM" %}

```
https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html
```

{% endtab %}

{% tab title="Analog" %}

```
https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.andymark.com/frc-electronics/can-sensors/can-hex-bore-absolute-encoder-am-5200_can/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
