Skip to content

Commit 3fb7f2e

Browse files
committed
42140-app-controlled-transformation-vehicle: Add extra demo.
- Add additional program. - Apply formatting. - Fix image URL. - Reduce image size. Fixes #89
1 parent c5d25b9 commit 3fb7f2e

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed
-480 KB
Loading

sets/technic/42140-app-controlled-transformation-vehicle/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: "App-Controlled Transformation Vehicle"
33
number: 42140
44
image:
5-
local: "42131-app-controlled-transformation-vechicle.jpg"
5+
local: "42140-app-controlled-transformation-vehicle.jpg"
66
credit: "LEGO"
77
layout: set
88
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"
9-
---
9+
---

sets/technic/42140-app-controlled-transformation-vehicle/powered-up-remote/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ Up Remote.
2222
```python
2323
{% include_relative main.py %}
2424
```
25+
26+
Additional program submitted by [@mikeyupol](https://github.com/mikeyupol):
27+
28+
```python
29+
{% include_relative main2.py %}
30+
```

sets/technic/42140-app-controlled-transformation-vehicle/powered-up-remote/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
SPEED = speed_levels[current_speed_index] # Update SPEED
3939

4040
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
41+
current_speed_index = min(
42+
len(speed_levels) - 1, current_speed_index + 1
43+
) # Increase speed index
4244
SPEED = speed_levels[current_speed_index] # Update SPEED
4345

4446
# Store the current state for the next iteration
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from pybricks.pupdevices import Motor, Remote
2+
from pybricks.hubs import TechnicHub
3+
from pybricks.parameters import Button, Color, Direction, Port
4+
from pybricks.tools import wait, StopWatch
5+
6+
# This drives like a car.
7+
# Left +/- control going forwards/backwards, and right +/- turn.
8+
# When the vehicle is moving turn is gradual, when it's stationary turn is faster.
9+
10+
remote = Remote()
11+
12+
hub = TechnicHub()
13+
14+
# Set very high speed limits to ensure maximum is reached.
15+
DRIVE_SPEED = 10000
16+
TURN_SPEED = 650
17+
DRIVE_ACCELERATION = 7000
18+
19+
# Initialize the motors.
20+
right_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
21+
left_motor = Motor(Port.B)
22+
23+
left_motor.control.limits(acceleration=DRIVE_ACCELERATION)
24+
right_motor.control.limits(acceleration=DRIVE_ACCELERATION)
25+
26+
while True:
27+
# Check which buttons are pressed
28+
wait(10)
29+
pressed = remote.buttons.pressed()
30+
31+
# Choose forward/backward and turn right/left based on pressed buttons
32+
left_speed = 0
33+
right_speed = 0
34+
pitch, roll = hub.imu.tilt()
35+
sign = -1 if abs(roll) > 90 else 1
36+
speed = sign * DRIVE_SPEED
37+
turn = sign * TURN_SPEED
38+
if Button.LEFT_PLUS in pressed or Button.LEFT_MINUS in pressed:
39+
# Going forwards/backwards
40+
left_speed = speed
41+
right_speed = speed
42+
43+
# Turn slowly when moving
44+
turn_right = Button.RIGHT_PLUS in pressed
45+
turn_left = Button.RIGHT_MINUS in pressed
46+
if (turn_right and sign > 0) or (turn_left and sign < 0):
47+
right_speed = turn
48+
elif turn_right or turn_left:
49+
left_speed = turn
50+
51+
if Button.LEFT_MINUS in pressed:
52+
left_speed *= -1
53+
right_speed *= -1
54+
else:
55+
# Not moving, fast turn
56+
if Button.RIGHT_PLUS in pressed:
57+
left_speed = DRIVE_SPEED
58+
right_speed = -DRIVE_SPEED
59+
if Button.RIGHT_MINUS in pressed:
60+
left_speed = -DRIVE_SPEED
61+
right_speed = DRIVE_SPEED
62+
63+
# Activate the driving motors.
64+
left_motor.run(left_speed)
65+
right_motor.run(right_speed)

0 commit comments

Comments
 (0)