Advertisement
Guest User

Untitled

a guest
Apr 29th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. from pybricks.pupdevices import Motor, Remote
  2. from pybricks.parameters import Port, Direction, Stop, Button
  3. from pybricks.hubs import TechnicHub
  4. from pybricks.tools import wait
  5.  
  6. # Initialize the motors.
  7. steer = Motor(Port.B)
  8. front = Motor(Port.A, Direction.COUNTERCLOCKWISE)
  9.  
  10. # Connect to the remote.
  11. remote = Remote()
  12.  
  13. # Initialize the hub.
  14. hub = TechnicHub()
  15.  
  16. # Read the current settings
  17. #old_kp, old_ki, old_kd, _, _ = steer.control.pid()
  18.  
  19. # Set new values
  20. #steer.control.pid(kp=old_kp*4, kd=old_kd*0.4)
  21.  
  22. # Find the steering endpoint on the left and right.
  23. # The middle is in between.
  24. left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
  25. right_end = steer.run_until_stalled(200, then=Stop.HOLD)
  26.  
  27. # We are now at the right. Reset this angle to be half the difference.
  28. # That puts zero in the middle.
  29. steer.reset_angle((right_end - left_end)/2)
  30. steer.run_target(speed=200, target_angle=0, wait=False)
  31.  
  32. # Set steering angle for the buggy
  33. steer_angle = (((right_end - left_end)/2)-5)
  34. print('steer angle:',steer_angle)
  35.  
  36. # Now we can start driving!
  37. while True:
  38. # Check which buttons are pressed.
  39. pressed = remote.buttons.pressed()
  40.  
  41. # Choose the steer angle based on the right controls.
  42. if Button.LEFT_PLUS in pressed:
  43. steer.run_target(1400, -steer_angle, Stop.HOLD, False)
  44. elif Button.LEFT_MINUS in pressed:
  45. steer.run_target(1400, steer_angle, Stop.HOLD, False)
  46. else:
  47. steer.track_target(0)
  48.  
  49. # Top speed controls
  50. top_speed = 50
  51. if Button.LEFT in pressed:
  52. top_speed = 100
  53.  
  54. # Choose the drive speed based on the left controls.
  55. drive_speed = 0
  56. if Button.RIGHT_PLUS in pressed:
  57. drive_speed += top_speed
  58. if Button.RIGHT_MINUS in pressed:
  59. drive_speed -= top_speed
  60. if Button.RIGHT in pressed:
  61. print('Battery voltage:',(hub.battery.voltage())/1000,"V")
  62. wait(100)
  63.  
  64. # Apply the selected speed.
  65. front.dc(drive_speed)
  66.  
  67. # Wait.
  68. wait(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement