WPILib

Library for programming XRP roobts

  • Author(s): Open STEM Authors

Implementation Notes

Hardware:

  • TODO Add links to any specific hardware product page(s), or category page(s).

Software and Dependencies:

class WPILib.XRPBot(is_prototype: bool = False, ticks_per_rev: int = 288)

If you need to change the encoder counts, alter the ticksPerRev values in the following two constructors. Most robots have either 144 or 288 ticks per revolution, depending on which motors are used.

High Level

class WPILib.Drivetrain(left_encoded_motor: <module 'WPILib._encoded_motor' from '/home/docs/checkouts/readthedocs.org/user_builds/circuitpython-xrp-library/checkouts/latest/WPILib/_encoded_motor.py'>, right_encoded_motor: <module 'WPILib._encoded_motor' from '/home/docs/checkouts/readthedocs.org/user_builds/circuitpython-xrp-library/checkouts/latest/WPILib/_encoded_motor.py'>)

The drivetrain class … todo

get_left_encoder_position() float

Return the current position of the left motor’s encoder in degrees.

get_right_encoder_position() float

Return the current position of the right motor’s encoder in degrees.

set_effort(left_effort: float, right_effort: float) None

Set the raw effort of both motors individually

Parameters:
  • leftEffort (float) – The power (Bounded from -1 to 1) to set the left motor to.

  • rightEffort (float) – The power (Bounded from -1 to 1) to set the right motor to.

set_encoder_position(left_degrees: float, right_degrees: float) None

Set the position of the motors’ encoders in degrees. Note that this does not actually move the motor but just recalibrates the stored encoder value. If only one encoder position is specified, the encoders for each motor will be set to that position.

Parameters:
  • leftDegrees (float) – The distance to recalibrate the left encoder to.

  • rightDegrees (float) – The distance to recalibrate the left encoder to.

set_legacy_mode(is_legacy: bool = True)
stop() None

Stops both drivetrain motors

straight(distance: float, speed: float = 0.5, timeout: float = None) bool

Go forward the specified distance in centimeters, and exit function when distance has been reached. Speed is bounded from -1 (reverse at full speed) to 1 (forward at full speed)

Parameters:
  • distance (float) – The distance for the robot to travel (In Centimeters)

  • speed (float) – The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward

  • timeout (float) – The amount of time before the robot stops trying to move forward and continues to the next step (In Seconds)

Returns:

if the distance was reached before the timeout

Return type:

bool

turn(turn_degrees: float, speed: float = 0.5, timeout: float = None) bool

Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading. Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)

Parameters:
  • turnDegrees (float) – The number of angle for the robot to turn (In Degrees)

  • speed (float) – The speed for which the robot to travel (Bounded from -1 to 1). Default is half speed forward.

  • timeout (float) – The amount of time before the robot stops trying to turn and continues to the next step (In Seconds)

Returns:

if the distance was reached before the timeout

Return type:

bool

Outputs

class WPILib.RGBLED(pin)
set_brightness(brightness: float)

Sets the brightness of the two LEDs on the board

Parameters:

brightness (float) – The brightness to set the LEDs to [Bound from 0 (off) to 1 (max)]

set_color(red: int, green: int, blue: int)

Sets the color of the led by taking in the RGB representation of the color

Parameters:
  • red (int) – The red value (0-255)

  • green (int) – The green value (0-255)

  • blue (int) – The blue value (0-255)

class WPILib.Servo(servoPin, actuationRange: int)
set_degrees(degrees: int)

Tells the servo to move to the specified position

Parameters:

degrees (int) – The angle for the servo to move to, bound in [0,135]

Inputs

class WPILib.Buttons
is_GP20_pressed() bool

Get whether the GP20 button on the RP2040 microcontroller has been pressed : return: if the GP20 button was pressed : rtype: bool

is_GP21_pressed() bool

Get whether the GP21 button on the RP2040 microcontroller has been pressed : return: if the GP21 button was pressed : rtype: bool

class WPILib.ReflectanceWrapper

A wrapper for an object that stores either the legacy or new implementation of the reflectance sensor User can set one of two modes, and get the value of either reflectance sensor

get_left() float

Gets the the reflectance of the left reflectance sensor :return: The reflectance ranging from 0 (white) to 1 (black) :rtype: float

get_right() float

Gets the the reflectance of the right reflectance sensor :return: The reflectance ranging from 0 (white) to 1 (black) :rtype: float

set_legacy_mode(is_legacy: bool = True) None

Set whether the reflectance sensor is the old or the new analog one :param is_legacy: True if using the old version, False if using the new one :type is_legacy: bool

class WPILib.UltrasonicWrapper

A wrapper for an object that stores either the legacy or new implementation of the ultrasonic sensor User can set one of two modes, and get the value of either ultrasonic sensor

get_distance() float

Return the distance measured by the sensor in cm.

If the distance is too far, it returns a maximum value of 65535.

return:

Distance in centimeters

rtype:

float

set_legacy_mode(is_legacy: bool = True) None

Set whether the reflectance sensor is the old or the new analog one

Parameters:

is_legacy (bool) – True if using the old version, False if using the new one