42140-app-controlled-transformation-vehicle: Add extra demo.

- Add additional program.
- Apply formatting.
- Fix image URL.
- Reduce image size.

Fixes https://github.com/pybricks/pybricks-projects/issues/89
This commit is contained in:
Laurens Valk
2023-11-24 21:04:12 +01:00
parent c5d25b973f
commit 3fb7f2e27c
5 changed files with 76 additions and 3 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 612 KiB

After

Width:  |  Height:  |  Size: 132 KiB

@@ -2,8 +2,8 @@
title: "App-Controlled Transformation Vehicle"
number: 42140
image:
local: "42131-app-controlled-transformation-vechicle.jpg"
local: "42140-app-controlled-transformation-vehicle.jpg"
credit: "LEGO"
layout: set
description: "Fast-action fun in a model that flips! Build and play with 2 vehicles in 1 model with this App-Controlled Transformation Vehicle that flips when it hits a wall"
---
---
@@ -22,3 +22,9 @@ Up Remote.
```python
{% include_relative main.py %}
```
Additional program submitted by [@mikeyupol](https://github.com/mikeyupol):
```python
{% include_relative main2.py %}
```
@@ -38,7 +38,9 @@ while True:
SPEED = speed_levels[current_speed_index] # Update SPEED
if right_button_pressed and not prev_right_button_pressed:
current_speed_index = min(len(speed_levels) - 1, current_speed_index + 1) # Increase speed index
current_speed_index = min(
len(speed_levels) - 1, current_speed_index + 1
) # Increase speed index
SPEED = speed_levels[current_speed_index] # Update SPEED
# Store the current state for the next iteration
@@ -0,0 +1,65 @@
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()
# Set very high speed limits to ensure maximum is reached.
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)