Skip to content

Instantly share code, notes, and snippets.

@haylinmoore
Created February 24, 2022 22:14
Show Gist options
  • Save haylinmoore/bf9dc470c24b49a0102b11983fa75f49 to your computer and use it in GitHub Desktop.
Save haylinmoore/bf9dc470c24b49a0102b11983fa75f49 to your computer and use it in GitHub Desktop.
from pybricks.pupdevices import Remote, Motor
from pybricks.parameters import Port, Button, Direction, Icon
from pybricks.robotics import DriveBase
from pybricks.hubs import PrimeHub
from pybricks.tools import wait
hub = PrimeHub()
# Connect to the remote.
my_remote = Remote(timeout=None)
# Display a heart at half brightness.
hub.display.image(Icon.HEART / 2)
left_motor = Motor(Port.D, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.C)
drive = DriveBase(left_motor, right_motor, 185, 80)
def wait_button_release(b):
pressed = my_remote.buttons.pressed()
while b in pressed:
pressed = my_remote.buttons.pressed();
wait(50)
drive_angle = 0;
drive_speed = 0;
angle_diff = 70
angle_perc = 2.3
move_perc = 2.3
move_speed = 200
while True:
# Check which buttons are pressed.
pressed = my_remote.buttons.pressed()
# Check a specific button.
if Button.LEFT in pressed:
drive_speed = 0
drive.drive(drive_speed, drive_angle)
if Button.CENTER in pressed:
drive_speed = 0
drive_angle = 0
drive.drive(drive_speed, drive_angle)
if Button.LEFT_PLUS in pressed:
if drive_speed < 0:
drive_speed -= (drive_speed/move_perc)
drive_speed += move_speed
if Button.LEFT_MINUS in pressed:
if drive_speed > 0:
drive_speed -= (drive_speed/move_perc)
drive_speed -= move_speed
if Button.RIGHT_MINUS in pressed:
if drive_angle == 0:
drive_angle += angle_diff
if drive_angle < 0:
drive_angle -= (drive_angle/angle_perc)
drive_angle += angle_diff
if Button.RIGHT_PLUS in pressed:
if drive_angle == 0:
drive_angle -= angle_diff
if drive_angle > 0:
drive_angle -= (drive_angle/angle_perc)
drive_angle -= angle_diff
if Button.RIGHT in pressed:
drive_angle = 0
drive.drive(drive_speed, drive_angle)
if drive_speed != 0 or drive_angle != 0:
drive.drive(drive_speed, drive_angle)
wait(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment