diff --git a/sets/technic/42099-off-roader/powered-up-remote/index.md b/sets/technic/42099-off-roader/powered-up-remote/index.md new file mode 100644 index 0000000..c21a8fe --- /dev/null +++ b/sets/technic/42099-off-roader/powered-up-remote/index.md @@ -0,0 +1,26 @@ +--- +title: "Powered Up Remote Control" +maintainer: + user: "pybricks" + name: "The Pybricks Team" +image: + local: "powered-up-remote-truck.jpg" +description: + "Control LEGO Technic vehicles with the Powered Up Remote." +video: + youtube: "kN47P6mksj4" +building_instructions: + external: https://www.lego.com/cdn/product-assets/product.bi.core.pdf/6314518.pdf +code: "#program" +--- + + +# Program + +This program expands the [basic driving program](../driving) with the Powered +Up Remote. + +{% include copy-code.html %} +```python +{% include_relative main.py %} +``` diff --git a/sets/technic/42099-off-roader/powered-up-remote/main.py b/sets/technic/42099-off-roader/powered-up-remote/main.py new file mode 100644 index 0000000..640a340 --- /dev/null +++ b/sets/technic/42099-off-roader/powered-up-remote/main.py @@ -0,0 +1,54 @@ +from pybricks.pupdevices import Motor, Remote +from pybricks.parameters import Port, Direction, Stop, Button +from pybricks.tools import wait + +# Initialize the motors. +steer = Motor(Port.C) +front = Motor(Port.A, Direction.COUNTERCLOCKWISE) +rear = Motor(Port.B, Direction.COUNTERCLOCKWISE) + +# Lower the acceleration so the car starts and stops realistically. +front.control.limits(acceleration=1000) +rear.control.limits(acceleration=1000) + +# Connect to the remote. +remote = Remote() + +# Find the steering endpoint on the left and right. +# The middle is in between. +left_end = steer.run_until_stalled(-200, then=Stop.HOLD) +right_end = steer.run_until_stalled(200, then=Stop.HOLD) + +# We are now at the right. Reset this angle to be half the difference. +# That puts zero in the middle. +steer.reset_angle((right_end - left_end) / 2) +steer.run_target(speed=200, target_angle=0, wait=False) + +# Now we can start driving! +while True: + # Check which buttons are pressed. + pressed = remote.buttons.pressed() + + # Choose the steer angle based on the left controls. + steer_angle = 0 + if Button.LEFT_PLUS in pressed: + steer_angle -= 75 + if Button.LEFT_MINUS in pressed: + steer_angle += 75 + + # Steer to the selected angle. + steer.run_target(500, steer_angle, wait=False) + + # Choose the drive speed based on the right controls. + drive_speed = 0 + if Button.RIGHT_PLUS in pressed: + drive_speed += 1000 + if Button.RIGHT_MINUS in pressed: + drive_speed -= 1000 + + # Apply the selected speed. + front.run(drive_speed) + rear.run(drive_speed) + + # Wait. + wait(10) diff --git a/sets/technic/42099-off-roader/powered-up-remote/powered-up-remote-truck.jpg b/sets/technic/42099-off-roader/powered-up-remote/powered-up-remote-truck.jpg new file mode 100644 index 0000000..59aa012 Binary files /dev/null and b/sets/technic/42099-off-roader/powered-up-remote/powered-up-remote-truck.jpg differ