pybricks / pybricks-projects

Example programs for Pybricks
MIT License
94 stars 42 forks source link

New project for LEGO Transformation Vehicle 42140 with powered up remote #89

Closed mikeyupol closed 9 months ago

mikeyupol commented 1 year ago

Hi,

I have put together some code to allow the powered-up remote to control the LEGO transformation vehicle (item #42140). I used the Cat bulldozer project as a template. It's configured to drive like a car, i.e. left PLUS/MINUS control moving forward/backward and right PLUS/MINUS controls turning. This was more intuitive for me and my son than making each side control the respective track as is done in the app.

When the vehicle is moving either forward or backward, turns are more gradual. If the vehicle is stopped, turns are quicker (tracks are rotated in opposite direction with the max speed). If the vehicle is flipped the motors are run in reverse direction so that controls are unchanged (pressing forward still moves the vehicle forward).

I hope that this helps people to use the remote with this LEGO set. Maybe it could be added to the projects page.

from pybricks.pupdevices import Motor, Remote
from pybricks.hubs import TechnicHub
from pybricks.parameters import Button, Color, Direction, Port
from pybricks.tools import wait, StopWatch

# This drives like a car.
# Left +/- control going forwards/backwards, and right +/- turn.
# When the vehicle is moving turn is gradual, when it's stationary turn is faster.

remote = Remote()

hub = TechnicHub()

# TODO: I'm not sure what the max speed is, below is definitely a gross over
# exaggeration.  The only thing that's important is that TURN_SPEED be the same
# percentage of the max speed.
DRIVE_SPEED = 10000
TURN_SPEED = 650
DRIVE_ACCELERATION = 7000

# Initialize the motors.
right_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
left_motor = Motor(Port.B)

left_motor.control.limits(acceleration=DRIVE_ACCELERATION)
right_motor.control.limits(acceleration=DRIVE_ACCELERATION)

while True:
    # Check which buttons are pressed
    wait(10)
    pressed = remote.buttons.pressed()

    # Choose forward/backward and turn right/left based on pressed buttons
    left_speed = 0
    right_speed = 0
    pitch, roll = hub.imu.tilt()
    sign = -1 if abs(roll) > 90 else 1
    speed = sign*DRIVE_SPEED
    turn = sign*TURN_SPEED
    if Button.LEFT_PLUS in pressed or Button.LEFT_MINUS in pressed:
        # Going forwards/backwards
        left_speed = speed
        right_speed = speed

        # Turn slowly when moving
        turn_right = Button.RIGHT_PLUS in pressed
        turn_left = Button.RIGHT_MINUS in pressed
        if (turn_right and sign > 0) or (turn_left and sign < 0):
            right_speed = turn
        elif turn_right or turn_left:
            left_speed = turn

        if Button.LEFT_MINUS in pressed:
            left_speed *= -1
            right_speed *= -1
    else:
        # Not moving, fast turn
        if Button.RIGHT_PLUS in pressed:
            left_speed = DRIVE_SPEED
            right_speed = -DRIVE_SPEED
        if Button.RIGHT_MINUS in pressed:
            left_speed = -DRIVE_SPEED
            right_speed = DRIVE_SPEED

    # Activate the driving motors.
    left_motor.run(left_speed)
    right_motor.run(right_speed)
laurensvalk commented 1 year ago

Thanks for sharing!

Maybe it could be added to the projects page.

Thank you. It may take a while, but we're happy to do that.

antonymorrish commented 1 year ago

Thankyou for the sample.

I have a 42140 Transformation Vehicle. This code ran fine for me. Control was very easy to use.

Edit: I did find the forward/backward acceleration a bit jerky for me. Dropped the DRIVE_ACCELERATION down to 5000 and was a lot smoother.

tensafefrogs commented 9 months ago

Doh, I didn't think to look here first, but I just submitted a separate pull request to control the transformation vehicle.

Perhaps we can merge the two?

See Pull request: https://github.com/pybricks/pybricks-projects/pull/92

laurensvalk commented 9 months ago

Thanks both of you for submitting a project. We've been pretty busy, so this has been on our backlog. Thanks for the ready made PR!

Any objections if we list both projects? I can take @tensafefrogs's PR as is and add @mikeyupol's solution on the same page (each credited by their own user name.)

tensafefrogs commented 9 months ago

Sounds great to me, awesome to have more options.

mikeyupol commented 9 months ago

Sounds good to me too. Thanks.

On Fri, Nov 24, 2023, 2:39 PM Geoff Stearns @.***> wrote:

Sounds great to me, awesome to have more options.

— Reply to this email directly, view it on GitHub https://github.com/pybricks/pybricks-projects/issues/89#issuecomment-1826035587, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA5WU5LCIH6GZMAMN7VYUZDYGDZX3AVCNFSM6AAAAAAYLHIX2GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRWGAZTKNJYG4 . You are receiving this because you were mentioned.Message ID: @.***>

laurensvalk commented 9 months ago

Thanks for the quick response! Both of your examples are now live at https://pybricks.com/projects/sets/technic/42140-app-controlled-transformation-vehicle/powered-up-remote/