Files
davecparker b60589abf4 tutorials/wireless: Added remote control techniques.
Added new tutorial category for Remote Control, and the Basic Button Techniques tutorial

Spelling fixes

Added DriveBase remote control tutorial

Added another remote control tutorial, for a basic DriveBase

Fix typos and formatting

Change remote image from PNG to JPG

Use "not in" instead of "not (..in..)"

Use "not in" instead of "not (..in..)" for better Python style

Fixed tutorial md snippet to match the py update

from the "not in" style fix

Moved Remote Control tutorial category to be under Wireless

Moved the Remote Control category to be under the existing Wireless top-level category, and changed the Remote Control category md file to specify
   layout: set
so that the entire structure parallels that of wireless/hub-to-device
2023-01-09 09:29:40 +01:00

58 lines
2.0 KiB
Python

from pybricks.hubs import TechnicHub # change hub type as necessary
from pybricks.pupdevices import Motor, Remote
from pybricks.parameters import Button, Color, Port
# Define the hub object.
# For other hub types, change TechnicHub here and on the line 1 import
hub = TechnicHub()
# This tutorial uses two motors on ports A and B
left_motor = Motor(Port.A)
right_motor = Motor(Port.B)
# Define the max and current motor speed to use
MAX_SPEED = 720 # deg/sec
speed = MAX_SPEED
# Try to connect to the remote
hub.light.on(Color.YELLOW) # turn hub light yellow while trying to connect
rc = Remote() # will stop the program if it can't connect
# Set status light on both hub and remote to green to indicate connection
hub.light.on(Color.GREEN)
rc.light.on(Color.GREEN)
# Get the set of buttons on the remote that start out pressed
pressed = rc.buttons.pressed()
# The main loop repeatedly tests the remote buttons and reacts (forever)
while True:
# Update the set of buttons that are currently pressed,
# and also remember which ones were pressed the last time we checked.
was_pressed = pressed
pressed = rc.buttons.pressed()
# 1. Press and hold Left Plus to spin the left motor
if Button.LEFT_PLUS in pressed:
left_motor.run(speed)
else:
left_motor.stop()
# 2. Tap Left Minus to turn the left motor exactly 720 degrees backwards
if Button.LEFT_MINUS in pressed and Button.LEFT_MINUS not in was_pressed:
left_motor.run_angle(speed, -720)
# 3. Press and hold Right Plus to spin the right motor forward,
# or Right Minus to spin it backward, or neither to stop it.
if Button.RIGHT_PLUS in pressed:
right_motor.run(speed)
elif Button.RIGHT_MINUS in pressed:
right_motor.run(-speed)
else:
right_motor.stop()
# 4. Press and hold Right Center to reduce the motor speed while doing 1-3
if Button.RIGHT in pressed:
speed = MAX_SPEED / 4
else:
speed = MAX_SPEED