|
| 1 | +from pybricks.pupdevices import Motor, Remote |
| 2 | +from pybricks.parameters import Port, Direction, Stop, Button |
| 3 | +from pybricks.tools import wait, StopWatch |
| 4 | +import pybricks.hubs |
| 5 | + |
| 6 | +# This program provides controls for the Audi e-tron Technic set. |
| 7 | +# It provides basic throttle + steering and the red buttons |
| 8 | +# increase / decrease speed by three levels: slow, medium, fast. |
| 9 | +# The hub will shut down after two minutes of inactivity to |
| 10 | +# save power. |
| 11 | +# |
| 12 | +# Controls: |
| 13 | +# Left remote + / - = Throttle: Forward & Reverse |
| 14 | +# Right remote + / - = Steering |
| 15 | +# Red left button = Decrease speed |
| 16 | +# Red right button = Increase speed |
| 17 | +# |
| 18 | + |
| 19 | +# Speed levels |
| 20 | +speed_levels = [500, 1000, 2000] # Slow, Medium, Fastest |
| 21 | +current_speed_index = 1 # Start with Medium speed |
| 22 | +SPEED = speed_levels[current_speed_index] # Initialize SPEED with the medium speed |
| 23 | + |
| 24 | +# Initialize the motors. |
| 25 | +steer = Motor(Port.D) # Steering motor |
| 26 | +front = Motor(Port.B, Direction.COUNTERCLOCKWISE, profile=300) # Front wheel drive |
| 27 | +rear = Motor(Port.A, Direction.COUNTERCLOCKWISE, profile=300) # Rear wheel drive |
| 28 | + |
| 29 | +# Set the acceleration and speed limits for the motors. |
| 30 | +front.control.limits(speed=2000, acceleration=2000) |
| 31 | +rear.control.limits(speed=2000, acceleration=2000) |
| 32 | + |
| 33 | +# Connect to the powered up remote. |
| 34 | +remote = Remote() |
| 35 | + |
| 36 | +# Calibrate the steering motor. |
| 37 | +left_end = steer.run_until_stalled(-200, then=Stop.HOLD) |
| 38 | +right_end = steer.run_until_stalled(200, then=Stop.HOLD) |
| 39 | +steer.reset_angle((right_end - left_end) / 2) |
| 40 | +steer.run_target(speed=1000, target_angle=0, wait=False) |
| 41 | + |
| 42 | +# Variables to track the previous state of the red buttons. |
| 43 | +prev_left_button_pressed = False |
| 44 | +prev_right_button_pressed = False |
| 45 | + |
| 46 | +# Initialize inactivity timer |
| 47 | +inactivity_timer = StopWatch() |
| 48 | + |
| 49 | +# Now we can start driving! |
| 50 | +while True: |
| 51 | + pressed = remote.buttons.pressed() |
| 52 | + |
| 53 | + # Reset the timer if there's any button pressed |
| 54 | + if pressed: |
| 55 | + inactivity_timer.reset() |
| 56 | + |
| 57 | + # Shut down if inactive for 2 minutes (120 seconds) |
| 58 | + if inactivity_timer.time() > 120000: # 2 minutes in milliseconds |
| 59 | + hub.system.shutdown() |
| 60 | + |
| 61 | + # Detect single presses on the red buttons. |
| 62 | + left_button_pressed = Button.LEFT in pressed |
| 63 | + right_button_pressed = Button.RIGHT in pressed |
| 64 | + |
| 65 | + if left_button_pressed and not prev_left_button_pressed: |
| 66 | + current_speed_index = max(0, current_speed_index - 1) # Decrease speed index |
| 67 | + SPEED = speed_levels[current_speed_index] # Update SPEED |
| 68 | + |
| 69 | + if right_button_pressed and not prev_right_button_pressed: |
| 70 | + current_speed_index = min(len(speed_levels) - 1, current_speed_index + 1) # Increase speed index |
| 71 | + SPEED = speed_levels[current_speed_index] # Update SPEED |
| 72 | + |
| 73 | + # Store the current state for the next iteration |
| 74 | + prev_left_button_pressed = left_button_pressed |
| 75 | + prev_right_button_pressed = right_button_pressed |
| 76 | + |
| 77 | + # Steering. |
| 78 | + steer_angle = 0 |
| 79 | + if Button.RIGHT_MINUS in pressed: |
| 80 | + steer_angle += 75 |
| 81 | + if Button.RIGHT_PLUS in pressed: |
| 82 | + steer_angle -= 75 |
| 83 | + |
| 84 | + steer.run_target(500, steer_angle, wait=False) |
| 85 | + |
| 86 | + # Throttle: Forward & Reverse. |
| 87 | + drive_speed = 0 |
| 88 | + if Button.LEFT_PLUS in pressed: |
| 89 | + drive_speed -= SPEED |
| 90 | + if Button.LEFT_MINUS in pressed: |
| 91 | + drive_speed += SPEED |
| 92 | + |
| 93 | + front.run(drive_speed) |
| 94 | + rear.run(drive_speed) |
| 95 | + |
| 96 | + wait(25) |
0 commit comments