Skip to content

Commit c5d25b9

Browse files
tensafefrogslaurensvalk
authored andcommitted
42140-app-controlled-transformation-vehicle: Add remote demo.
Add Powered up Remote support for App-Controlled Transformation Vechicle Add variable speed settings via red buttons
1 parent 465d95f commit c5d25b9

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
613 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "App-Controlled Transformation Vehicle"
3+
number: 42140
4+
image:
5+
local: "42131-app-controlled-transformation-vechicle.jpg"
6+
credit: "LEGO"
7+
layout: set
8+
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+
---
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: "Powered Up Remote Control"
3+
maintainer:
4+
user: "tensafefrogs"
5+
name: "Geoff Stearns"
6+
image:
7+
local: "../42140-app-controlled-transformation-vehicle.jpg"
8+
credit: "LEGO"
9+
description: "Control the Technic App-Controlled Transformation Vechicle with the Powered Up Remote."
10+
code: "#program"
11+
---
12+
13+
In this project we'll show you how to control the App-Controlled Transformation Vehicle with the Powered Up remote.
14+
15+
# Program
16+
17+
This program enables control of the flip vehicle with the Powered
18+
Up Remote.
19+
20+
{% include copy-code.html %}
21+
22+
```python
23+
{% include_relative main.py %}
24+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)