Advertisement
migero

Untitled

Sep 2nd, 2023
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | Gaming | 0 0
  1. from pybricks.pupdevices import Motor, Remote, Light
  2. from pybricks.parameters import Port, Direction, Stop, Button
  3. from pybricks.tools import wait
  4.  
  5.  
  6. # Initialize the motors.
  7. steer = Motor(Port.B)
  8. light = Light(Port.C)
  9. front = Motor(Port.A, Direction.COUNTERCLOCKWISE)
  10.  
  11. front.control.limits(acceleration=500)
  12. steer.control.limits(acceleration=500)
  13. remote = Remote()
  14.  
  15. left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
  16. right_end = steer.run_until_stalled(200, then=Stop.HOLD)
  17.  
  18.  
  19. steer.reset_angle((right_end - left_end) / 2)
  20. steer.run_target(speed=200, target_angle=0, wait=False)
  21. hold = False
  22. while True:
  23.     pressed = remote.buttons.pressed()
  24.  
  25.     steer_angle = 0
  26.     if Button.RIGHT_MINUS in pressed:
  27.         steer_angle -= 75
  28.     if Button.RIGHT_PLUS in pressed:
  29.         steer_angle += 75
  30.     if Button.RIGHT in pressed:
  31.         steer_angle = 0
  32.     steer.run_target(500, steer_angle, wait=False)
  33.  
  34.     drive_speed = 0
  35.     if Button.LEFT_PLUS in pressed:
  36.         drive_speed += 5000
  37.     if Button.LEFT_MINUS in pressed:
  38.         drive_speed -= 5000
  39.     if Button.LEFT in pressed:
  40.        wait(500)
  41.        if hold == True:
  42.         hold = False
  43.        else:
  44.         hold = True
  45.        
  46.  
  47.     bright = front.speed() / 15
  48.     if hold == True:
  49.         front.control.limits(acceleration=5000)
  50.         front.run(750)
  51.         light.on(bright)
  52.        
  53.     else:
  54.         front.control.limits(acceleration=500)
  55.         front.run(drive_speed)
  56.         light.on(bright)
Tags: lego
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement