42124-off-road-buggy: Add remote project

* Create index.md

* Add files via upload

* Delete 42124-off-road-buggy directory

* Create index.md

* Add files via upload

* Create index.md

* Create main.py

* Add files via upload

* Add files via upload

* Update index.md

* Update index.md

* Update index.md

* Update index.md
This commit is contained in:
Jorge Pereira
2021-07-23 16:11:11 +02:00
committed by GitHub
parent 21a0c6f28d
commit a4a0f9bfe4
5 changed files with 95 additions and 0 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

@@ -0,0 +1,9 @@
---
title: "Off-Road Buggy"
number: 422124
image:
local: "42124-off-road-buggy.jpg"
credit: "LEGO"
layout: set
description: "Add set description"
---
@@ -0,0 +1,35 @@
---
title: "Powered Up Remote Control"
maintainer:
user: "JorgePe"
name: "Jorge Pereira"
image:
local: "powered-up-remote-buggy.jpg"
description:
"Control LEGO Technic vehicles with the Powered Up Remote."
video:
youtube: st_lcMgz618
building_instructions:
external: https://www.lego.com/cdn/product-assets/product.bi.core.pdf/6351188.pdf
code: "#program"
---
# How it works
Your MicroPython programs can connect to the Powered Up Remote so you can detect when
any of the buttons are pressed and react to it.
# Program
This program expands the [basic driving program](../driving) with the Powered
Up Remote.
After the program starts it connects to your remote (just press the green button)
then it makes a quick calibration of the steering and you are ready to drive.
Left Channel controls the steering and Right Channel drives the car.
{% include copy-code.html %}
```python
{% include_relative main.py %}
```
@@ -0,0 +1,51 @@
from pybricks.pupdevices import Motor, Remote
from pybricks.parameters import Port, Direction, Stop, Button
from pybricks.tools import wait
# Initialize the motors.
steer = Motor(Port.B)
front = Motor(Port.A, Direction.COUNTERCLOCKWISE)
# Lower the acceleration so the car starts and stops realistically.
front.control.limits(acceleration=1000)
# Connect to the remote.
remote = Remote()
# Find the steering endpoint on the left and right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)
# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle.
steer.reset_angle((right_end - left_end) / 2)
steer.run_target(speed=200, target_angle=0, wait=False)
# Now we can start driving!
while True:
# Check which buttons are pressed.
pressed = remote.buttons.pressed()
# Choose the steer angle based on the left controls.
steer_angle = 0
if Button.LEFT_PLUS in pressed:
steer_angle -= 75
if Button.LEFT_MINUS in pressed:
steer_angle += 75
# Steer to the selected angle.
steer.run_target(500, steer_angle, wait=False)
# Choose the drive speed based on the right controls.
drive_speed = 0
if Button.RIGHT_PLUS in pressed:
drive_speed += 1000
if Button.RIGHT_MINUS in pressed:
drive_speed -= 1000
# Apply the selected speed.
front.run(drive_speed)
# Wait.
wait(10)
Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB