Skip to content

Commit 3c3d7f4

Browse files
tensafefrogslaurensvalk
authored andcommitted
42160-audi-rs-q-e-tron: Powered up remote code.
1 parent 87db494 commit 3c3d7f4

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
139 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Audi RS Q e-tron"
3+
number: 42160
4+
image:
5+
local: "42160-audi-rs-q-e-tron.webp"
6+
credit: "LEGO"
7+
layout: set
8+
description: "Build an Audi rally car. Inspire kids to build a remote-controlled rally car with this LEGO® Technic™ Audi RS Q e-tron model kit."
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: "../42160-audi-rs-q-e-tron.webp"
8+
credit: "LEGO"
9+
description: "Control the Technic Audi RS Q e-tron with the Powered Up Remote."
10+
code: "#program"
11+
---
12+
13+
In this project we'll show you how to control the Audi RS Q e-tron with the Powered Up remote.
14+
15+
# Program
16+
17+
This program enables control of the vehicle with the Powered
18+
Up Remote.
19+
20+
{% include copy-code.html %}
21+
22+
```python
23+
{% include_relative main.py %}
24+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from pybricks.pupdevices import Motor, Remote
2+
from pybricks.parameters import Port, Direction, Stop, Button
3+
from pybricks.tools import wait, StopWatch
4+
import pybricks.hubs
5+
6+
# This program provides controls for the Audi e-tron Technic set.
7+
# It provides basic throttle + steering and the red buttons
8+
# increase / decrease speed by three levels: slow, medium, fast.
9+
# The hub will shut down after two minutes of inactivity to
10+
# save power.
11+
#
12+
# Controls:
13+
# Left remote + / - = Throttle: Forward & Reverse
14+
# Right remote + / - = Steering
15+
# Red left button = Decrease speed
16+
# Red right button = Increase speed
17+
#
18+
19+
# Speed levels
20+
speed_levels = [500, 1000, 2000] # Slow, Medium, Fastest
21+
current_speed_index = 1 # Start with Medium speed
22+
SPEED = speed_levels[current_speed_index] # Initialize SPEED with the medium speed
23+
24+
# Initialize the motors.
25+
steer = Motor(Port.D) # Steering motor
26+
front = Motor(Port.B, Direction.COUNTERCLOCKWISE, profile=300) # Front wheel drive
27+
rear = Motor(Port.A, Direction.COUNTERCLOCKWISE, profile=300) # Rear wheel drive
28+
29+
# Set the acceleration and speed limits for the motors.
30+
front.control.limits(speed=2000, acceleration=2000)
31+
rear.control.limits(speed=2000, acceleration=2000)
32+
33+
# Connect to the powered up remote.
34+
remote = Remote()
35+
36+
# Calibrate the steering motor.
37+
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
38+
right_end = steer.run_until_stalled(200, then=Stop.HOLD)
39+
steer.reset_angle((right_end - left_end) / 2)
40+
steer.run_target(speed=1000, target_angle=0, wait=False)
41+
42+
# Variables to track the previous state of the red buttons.
43+
prev_left_button_pressed = False
44+
prev_right_button_pressed = False
45+
46+
# Initialize inactivity timer
47+
inactivity_timer = StopWatch()
48+
49+
# Now we can start driving!
50+
while True:
51+
pressed = remote.buttons.pressed()
52+
53+
# Reset the timer if there's any button pressed
54+
if pressed:
55+
inactivity_timer.reset()
56+
57+
# Shut down if inactive for 2 minutes (120 seconds)
58+
if inactivity_timer.time() > 120000: # 2 minutes in milliseconds
59+
hub.system.shutdown()
60+
61+
# Detect single presses on the red buttons.
62+
left_button_pressed = Button.LEFT in pressed
63+
right_button_pressed = Button.RIGHT in pressed
64+
65+
if left_button_pressed and not prev_left_button_pressed:
66+
current_speed_index = max(0, current_speed_index - 1) # Decrease speed index
67+
SPEED = speed_levels[current_speed_index] # Update SPEED
68+
69+
if right_button_pressed and not prev_right_button_pressed:
70+
current_speed_index = min(len(speed_levels) - 1, current_speed_index + 1) # Increase speed index
71+
SPEED = speed_levels[current_speed_index] # Update SPEED
72+
73+
# Store the current state for the next iteration
74+
prev_left_button_pressed = left_button_pressed
75+
prev_right_button_pressed = right_button_pressed
76+
77+
# Steering.
78+
steer_angle = 0
79+
if Button.RIGHT_MINUS in pressed:
80+
steer_angle += 75
81+
if Button.RIGHT_PLUS in pressed:
82+
steer_angle -= 75
83+
84+
steer.run_target(500, steer_angle, wait=False)
85+
86+
# Throttle: Forward & Reverse.
87+
drive_speed = 0
88+
if Button.LEFT_PLUS in pressed:
89+
drive_speed -= SPEED
90+
if Button.LEFT_MINUS in pressed:
91+
drive_speed += SPEED
92+
93+
front.run(drive_speed)
94+
rear.run(drive_speed)
95+
96+
wait(25)

0 commit comments

Comments
 (0)