|
| 1 | +from pybricks.hubs import TechnicHub |
| 2 | +from pybricks.pupdevices import Motor, Remote |
| 3 | +from pybricks.parameters import Port, Stop, Direction, Button, Color, Side |
| 4 | +from pybricks.tools import wait, StopWatch |
| 5 | + |
| 6 | +# Motor speed |
| 7 | +# Speed levels |
| 8 | +speed_levels = [400, 800, 1470] # Slow, Medium, Fastest |
| 9 | +current_speed_index = 1 # Start with Medium speed |
| 10 | +SPEED = speed_levels[current_speed_index] |
| 11 | + |
| 12 | +# Initialize the hub, motors, and remote |
| 13 | +hub = TechnicHub() |
| 14 | +left_motor = Motor(Port.B) |
| 15 | +right_motor = Motor(Port.A) |
| 16 | +remote = Remote() |
| 17 | +inactivity_timer = StopWatch() |
| 18 | + |
| 19 | +# Increase motor acceleration |
| 20 | +left_motor.control.limits(speed=1470, acceleration=4000) |
| 21 | +right_motor.control.limits(speed=1470, acceleration=4000) |
| 22 | + |
| 23 | +# Variables to track the previous state of the red buttons |
| 24 | +prev_left_button_pressed = False |
| 25 | +prev_right_button_pressed = False |
| 26 | + |
| 27 | +# Main control loop |
| 28 | +while True: |
| 29 | + # Check which buttons are pressed |
| 30 | + buttons_pressed = remote.buttons.pressed() |
| 31 | + |
| 32 | + # Detect single presses on the red buttons |
| 33 | + left_button_pressed = Button.LEFT in buttons_pressed |
| 34 | + right_button_pressed = Button.RIGHT in buttons_pressed |
| 35 | + |
| 36 | + if left_button_pressed and not prev_left_button_pressed: |
| 37 | + current_speed_index = max(0, current_speed_index - 1) # Decrease speed index |
| 38 | + SPEED = speed_levels[current_speed_index] # Update SPEED |
| 39 | + |
| 40 | + if right_button_pressed and not prev_right_button_pressed: |
| 41 | + current_speed_index = min(len(speed_levels) - 1, current_speed_index + 1) # Increase speed index |
| 42 | + SPEED = speed_levels[current_speed_index] # Update SPEED |
| 43 | + |
| 44 | + # Store the current state for the next iteration |
| 45 | + prev_left_button_pressed = left_button_pressed |
| 46 | + prev_right_button_pressed = right_button_pressed |
| 47 | + |
| 48 | + # Determine hub orientation |
| 49 | + # Note: Slight bugginess when flipping from Orange to Blue side |
| 50 | + # Might be improved by including FRONT here |
| 51 | + invert_direction = hub.imu.up() == Side.BOTTOM |
| 52 | + |
| 53 | + # Control motors with remote |
| 54 | + left_speed = 0 |
| 55 | + right_speed = 0 |
| 56 | + |
| 57 | + if Button.LEFT_PLUS in buttons_pressed: |
| 58 | + left_speed = SPEED |
| 59 | + elif Button.LEFT_MINUS in buttons_pressed: |
| 60 | + left_speed = -SPEED |
| 61 | + |
| 62 | + if Button.RIGHT_PLUS in buttons_pressed: |
| 63 | + right_speed = -SPEED |
| 64 | + elif Button.RIGHT_MINUS in buttons_pressed: |
| 65 | + right_speed = SPEED |
| 66 | + |
| 67 | + # Swap motor direction if the hub is upside down |
| 68 | + if invert_direction: |
| 69 | + left_speed, right_speed = right_speed, left_speed # Swap the motor controls |
| 70 | + |
| 71 | + # Run motors |
| 72 | + left_motor.run(left_speed) |
| 73 | + right_motor.run(right_speed) |
| 74 | + |
| 75 | + # Update light color based on orientation |
| 76 | + hub.light.on(Color.ORANGE if invert_direction else Color.BLUE) |
| 77 | + remote.light.on(Color.ORANGE if invert_direction else Color.BLUE) |
| 78 | + |
| 79 | + # Check for inactivity - Power off if left untouched to save batteries |
| 80 | + # Reset the timer if there's any button pressed |
| 81 | + if buttons_pressed: |
| 82 | + inactivity_timer.reset() |
| 83 | + |
| 84 | + if inactivity_timer.time() > 120000: # 2 minute in milliseconds |
| 85 | + hub.system.shutdown() |
| 86 | + |
| 87 | + # Small delay to prevent overloading the CPU |
| 88 | + wait(10) |
0 commit comments