XRP software quick reference
XRP WPILib Overview
WPILib is like a special toolbox for C++ programmers in FIRST Robotics. It gives you ready-made tools to control your robot's motors, read sensors, and talk to the drivers. It makes programming your robot much easier, so you can focus on making it do cool things instead of worrying about tiny details.
for official documentation go to WPILib 2025
XRP Motor
The XRP robot has two motors: a left and a right drive motor. The motor speed range is from -1.0 to 1.0.
For more details, see the WPILib XRPMotor Class Reference.
The motor IDs are as follows:
- Left Motor: ID 0
- Right Motor: ID 1
- Open Motor 1: ID 2
- Open Motor 2: ID 3
To use the XRPMotor
class, you need to include its header file:
#include <frc/xrp/XRPMotor.h>
Constructor
You create an XRPMotor
object by specifying its device ID (0 for the left motor, 1 for the right motor).
// Create motor objects for both left and right motors
frc::XRPMotor m_left_motor{0};
frc::XRPMotor m_right_motor{1};
Common Methods
Set(double speed)
: Sets the motor speed, from -1.0 (full reverse) to 1.0 (full forward).SetInverted(bool isInverted)
: Inverts the direction of the motor. This is useful for drivetrains where motors on opposite sides need to spin in opposite directions to drive forward.Get()
: Returns the last speed set to the motor.
Usage Example
This example shows how to set up and control the motors for a simple differential drive.
// Invert the right motor so both motors spin in the same direction for forward motion
m_right_motor.SetInverted(true);
// Drive forward at half speed
m_left_motor.Set(0.5);
m_right_motor.Set(0.5);
XRP Gyro
The XRP has an onboard gyroscope that can be used to measure the robot's heading (rotation). This is useful for making precise turns or driving straight.
For more details, see the WPILib XRPGyro Class Reference.
To use the XRPGyro
class, you need to include the XRPGyro
and units
header files:
#include <frc/xrp/XRPGyro.h>
#include <units/angle.h>
#include <iostream>
Constructor
You can construct an XRPGyro
object without any parameters.
frc::XRPGyro m_gyro;
Common Methods
GetAngle()
: Returns the accumulated angle as aunits::degree_t
. Clockwise rotation is positive.Reset()
: Resets the gyro's angle to 0. This is useful for setting a new "forward" direction.
Usage Example
This example shows how to read the gyro's angle and print it to the console.
// Reset the gyro's current angle to 0
m_gyro.Reset();
// Get the current angle of the robot
units::degree_t current_angle = m_gyro.GetAngle();
// Print the angle to the console. .value() is used to get the raw number.
std::cout << "Gyro Angle: " << current_angle.value() << " deg" << std::endl;
XRP Servo
The XRP robot has two dedicated ports for standard hobby servos. These are controlled using the specific frc::XRPServo
class, which allows for precise control of angular position (typically 0-180 degrees).
For more details, see the WPILib XRPServo Class Reference.
To use a servo, you need to include the XRPServo
and units
header files:
#include <frc/xrp/XRPServo.h>
#include <units/angle.h>
#include <iostream>
Constructor
You create an XRPServo
object by specifying its device number. The XRP has two servo ports:
- Servo 1: Device Number 4
- Servo 2: Device Number 5
// Create a servo object for the servo connected to port 1 on the XRP
frc::XRPServo m_servo1{0};
Common Methods
SetAngle(units::degree_t angle)
: Sets the servo's angle. The valid range is typically 0 to 180 degrees.GetAngle()
: Returns the last angle set for the servo as aunits::degree_t
.
Usage Example
This example shows how to set a servo's angle and then read it back.
// Set the servo to its midpoint
m_servo1.SetAngle(90.0_deg);
// Read the angle back from the servo
units::degree_t current_angle = m_servo1.GetAngle();
// Print the angle to the console
std::cout << "Servo Angle: " << current_angle.value() << " deg" << std::endl;
XRP Line Sensor
The XRP has an onboard reflectance sensor array for line following. This is managed by the frc::XRPReflectanceSensor
class, which provides readings from the left and right sensors.
For more details, see the WPILib XRPReflectanceSensor Class Reference.
To use the line sensor, you need to include the XRPReflectanceSensor
header file:
#include <frc/xrp/XRPReflectanceSensor.h>
#include <iostream>
Constructor
You can construct an XRPReflectanceSensor
object without any parameters. It automatically handles both the left and right sensors.
// Create an object for the reflectance sensor array
frc::XRPReflectanceSensor m_reflectance_sensor;
Common Methods
GetLeftValue()
: Returns the left sensor's reflectance value as adouble
from 0.0 (light) to 1.0 (dark).GetRightValue()
: Returns the right sensor's reflectance value as adouble
from 0.0 (light) to 1.0 (dark).
Usage Example
This example shows how to read the values from both sensors and print them.
// Read the values from the left and right sensors
double left_value = m_reflectance_sensor.GetLeftValue();
double right_value = m_reflectance_sensor.GetRightValue();
// Print the values to the console
std::cout << "Left Sensor: " << left_value << ", Right Sensor: " << right_value << std::endl;
XRP LED and User Button
The XRP has an onboard yellow LED and USER button that can be used for status indication and user input. These are managed by the frc::XRPOnBoardIO
class.
For more details, see the WPILib XRPOnBoardIO Class Reference.
To use the LED and button, you need to include the XRPOnBoardIO
header file:
#include <frc/xrp/XRPOnBoardIO.h>
#include <iostream>
Constructor
You can construct an XRPOnBoardIO
object without any parameters. It automatically handles both the LED and user button.
// Create an object for the onboard IO (LED and button)
frc::XRPOnBoardIO m_onboard_io;
Common Methods
SetLed(bool value)
: Sets the yellow LED state. Passtrue
to turn it on,false
to turn it off.GetLed()
: Returns the current LED state as abool
(true
= on,false
= off).GetUserButtonPressed()
: Returnstrue
if the USER button is currently pressed,false
otherwise.
Usage Example
This example shows how to control the LED and read the user button state.
// Turn on the LED
m_onboard_io.SetLed(true);
// Check if the user button is pressed
bool button_pressed = m_onboard_io.GetUserButtonPressed();
if (button_pressed) {
// Toggle the LED when button is pressed
bool current_led_state = m_onboard_io.GetLed();
m_onboard_io.SetLed(!current_led_state);
std::cout << "Button pressed! LED toggled." << std::endl;
}
// Print current states
std::cout << "LED State: " << (m_onboard_io.GetLed() ? "ON" : "OFF") << std::endl;
std::cout << "Button State: " << (button_pressed ? "PRESSED" : "RELEASED") << std::endl;
Common Use Cases
- Status Indicator: Turn LED on/off to show robot state (autonomous, teleop, error conditions)
- User Feedback: Blink LED to indicate sensor readings or robot actions
- Button Control: Use the USER button to start/stop actions, toggle modes, or reset systems
- Debugging: Flash LED patterns to indicate different program states during development
XRP Rangefinder
The XRP has an onboard rangefinder (distance sensor) that can measure the distance to objects. This is useful for obstacle avoidance or precise positioning.
For more details, see the WPILib XRPRangefinder Class Reference.
To use the rangefinder, you need to include the XRPRangefinder
and units
header files:
#include <frc/xrp/XRPRangefinder.h>
#include <units/length.h>
#include <iostream>
Constructor
You can construct an XRPRangefinder
object without any parameters.
// Create an object for the rangefinder
frc::XRPRangefinder m_rangefinder;
Common Methods
GetDistance()
: Returns the measured distance as aunits::meter_t
.
Usage Example
This example shows how to read the distance from the rangefinder and print it.
// Get the distance to the nearest object
units::meter_t distance = m_rangefinder.GetDistance();
// Print the distance to the console
std::cout << "Distance: " << distance.value() << " meters" << std::endl;
Common Use Cases
- Obstacle Avoidance: Stop or turn the robot if an object is too close
- Precise Positioning: Measure distance to walls or targets for alignment
- Sensor Fusion: Combine with other sensors for better navigation