v0.1.0
This commit is contained in:
@@ -12,13 +12,19 @@
|
|||||||
# Port D: Not used #
|
# Port D: Not used #
|
||||||
# #
|
# #
|
||||||
################################################################################
|
################################################################################
|
||||||
# Script base: https://racingbrick.com/2021/08/remote-control-for-control-sets #
|
|
||||||
# -without-an-app-or-smartphone-pybricks/ #
|
|
||||||
################################################################################
|
|
||||||
# #
|
# #
|
||||||
# Changelog #
|
# Changelog #
|
||||||
# #
|
# #
|
||||||
################################################################################
|
################################################################################
|
||||||
|
# v0.1.0 05-07-2022 #
|
||||||
|
# restructured to the version at pybricks-projects on Github #
|
||||||
|
# https://github.com/pybricks/pybricks-projects/blob/master/sets/technic/ #
|
||||||
|
# 42124-off-road-buggy/powered-up-remote/main.py #
|
||||||
|
# Changed gearbox part. #
|
||||||
|
# Button left can only shift down #
|
||||||
|
# Button right can only shift up #
|
||||||
|
# New variables to easily change the number of gears #
|
||||||
|
# Added battery indicator with hub LED. #
|
||||||
# v0.0.0 04-07-2022 #
|
# v0.0.0 04-07-2022 #
|
||||||
# First version. #
|
# First version. #
|
||||||
# Based on v0.2.0 02-07-2022 of 42109-1 - App-Controlled Top Gear Rally Car #
|
# Based on v0.2.0 02-07-2022 of 42109-1 - App-Controlled Top Gear Rally Car #
|
||||||
@@ -26,15 +32,19 @@
|
|||||||
|
|
||||||
from pybricks.pupdevices import Motor, Remote
|
from pybricks.pupdevices import Motor, Remote
|
||||||
from pybricks.parameters import Port, Direction, Stop, Button, Color
|
from pybricks.parameters import Port, Direction, Stop, Button, Color
|
||||||
|
from pybricks.hubs import TechnicHub
|
||||||
from pybricks.tools import wait
|
from pybricks.tools import wait
|
||||||
|
|
||||||
# Initialize the motors.
|
# Initialize the motors.
|
||||||
steering = Motor(Port.B)
|
steering = Motor(Port.B, Direction.CLOCKWISE)
|
||||||
driving = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
driving = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
||||||
|
|
||||||
# Connect to the remote.
|
# Connect to the remote.
|
||||||
remote = Remote()
|
remote = Remote()
|
||||||
|
|
||||||
|
# Initialize the hub.
|
||||||
|
hub = TechnicHub()
|
||||||
|
|
||||||
# Read the current settings
|
# Read the current settings
|
||||||
old_kp, old_ki, old_kd, _, _ = steering.control.pid()
|
old_kp, old_ki, old_kd, _, _ = steering.control.pid()
|
||||||
|
|
||||||
@@ -51,73 +61,84 @@ right_end = steering.run_until_stalled(200, then=Stop.HOLD)
|
|||||||
steering.reset_angle((right_end - left_end)/2)
|
steering.reset_angle((right_end - left_end)/2)
|
||||||
steering.run_target(speed=200, target_angle=0, wait=False)
|
steering.run_target(speed=200, target_angle=0, wait=False)
|
||||||
|
|
||||||
# Set steering angle for the buggy
|
# Set steering angle
|
||||||
steer_angle = (((right_end - left_end)/2)-5)
|
steering_angle = (((right_end - left_end)/2)-5)
|
||||||
|
|
||||||
# Set variable for gear
|
# Set variable for gear
|
||||||
gear_old = 1
|
gear_total = 3 # Total number of gears.
|
||||||
gear = 1
|
gear_old = None # Empty variable to set later on (keep
|
||||||
speed = 33
|
# empty!).
|
||||||
|
gear = 1 # Gear at start (must be higher than 0 and
|
||||||
|
# lower or eaqual to gear_total).
|
||||||
|
gear_color = ["BLUE", "WHITE", "RED"] # Number of colors should at least be
|
||||||
|
# equal to gear_total.
|
||||||
|
|
||||||
|
# Battery variables
|
||||||
|
voltage_current = hub.battery.voltage()
|
||||||
|
voltage = voltage_current
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Check which buttons are pressed.
|
# Check which buttons are pressed.
|
||||||
pressed = remote.buttons.pressed()
|
pressed = remote.buttons.pressed()
|
||||||
|
|
||||||
# Check if the right middle button is pressed to change gear
|
# Choose the steering angle based on the right controls.
|
||||||
if Button.RIGHT in pressed:
|
if Button.RIGHT_PLUS in pressed:
|
||||||
if gear_old is 1:
|
steering.run_target(1400, -steering_angle, Stop.HOLD, False)
|
||||||
gear = 2
|
elif Button.RIGHT_MINUS in pressed:
|
||||||
elif gear_old is 2:
|
steering.run_target(1400, steering_angle, Stop.HOLD, False)
|
||||||
gear = 3
|
else:
|
||||||
else:
|
steering.track_target(0)
|
||||||
gear = 1
|
|
||||||
while Button.RIGHT in pressed:
|
|
||||||
# Button debounce
|
|
||||||
wait(10)
|
|
||||||
pressed = remote.buttons.pressed()
|
|
||||||
|
|
||||||
# Check if the left middle button is pressed to change gear
|
# Check if the left middle button is pressed to change gear
|
||||||
if Button.LEFT in pressed:
|
if Button.LEFT in pressed:
|
||||||
if gear_old is 3:
|
if gear_old > 1:
|
||||||
gear = 2
|
gear -= 1
|
||||||
elif gear_old is 2:
|
|
||||||
gear = 1
|
|
||||||
else:
|
|
||||||
gear = 3
|
|
||||||
while Button.LEFT in pressed:
|
while Button.LEFT in pressed:
|
||||||
# Button debounce
|
# Button debounce
|
||||||
wait(10)
|
wait(10)
|
||||||
pressed = remote.buttons.pressed()
|
pressed = remote.buttons.pressed()
|
||||||
|
|
||||||
if gear is not gear_old:
|
# Check if the right middle button is pressed to change gear
|
||||||
if gear is 1:
|
if Button.RIGHT in pressed:
|
||||||
remote.light.on(Color.BLUE)
|
if gear_old < gear_total:
|
||||||
speed = 33
|
gear += 1
|
||||||
gear_old = 1
|
while Button.RIGHT in pressed:
|
||||||
elif gear is 2:
|
# Button debounce
|
||||||
remote.light.on(Color.WHITE)
|
wait(10)
|
||||||
speed = 67
|
pressed = remote.buttons.pressed()
|
||||||
gear_old = 2
|
|
||||||
else:
|
|
||||||
remote.light.on(Color.RED)
|
|
||||||
speed = 100
|
|
||||||
gear_old = 3
|
|
||||||
|
|
||||||
# Choose the steering angle based on the right controls.
|
# Set speed according to the choosen gear.
|
||||||
if Button.RIGHT_PLUS in pressed:
|
if gear is not gear_old:
|
||||||
steering.run_target(1400, -steer_angle, Stop.HOLD, False)
|
remote.light.on(Color[gear_color[gear-1]])
|
||||||
elif Button.RIGHT_MINUS in pressed:
|
speed = (100/gear_total)*gear
|
||||||
steering.run_target(1400, steer_angle, Stop.HOLD, False)
|
gear_old = gear
|
||||||
else:
|
|
||||||
steering.track_target(0)
|
|
||||||
|
|
||||||
# Choose the drive speed based on the left controls.
|
# Choose the drive speed based on the left controls.
|
||||||
|
drive_speed = 0
|
||||||
if Button.LEFT_PLUS in pressed:
|
if Button.LEFT_PLUS in pressed:
|
||||||
driving.dc(speed)
|
drive_speed += speed
|
||||||
elif Button.LEFT_MINUS in pressed:
|
elif Button.LEFT_MINUS in pressed:
|
||||||
driving.dc(-speed)
|
drive_speed -= speed
|
||||||
|
|
||||||
|
# Apply the selected speed.
|
||||||
|
driving.dc(drive_speed)
|
||||||
|
|
||||||
|
# Show battery status by hub LED.
|
||||||
|
# Get current voltage and gradually up or down the value.
|
||||||
|
voltage_current = hub.battery.voltage()
|
||||||
|
if voltage_current < voltage:
|
||||||
|
voltage -= 1
|
||||||
|
elif voltage_current > voltage:
|
||||||
|
voltage += 1
|
||||||
|
# Show the correct color.
|
||||||
|
if voltage > 9000:
|
||||||
|
hub.light.on(Color.GREEN)
|
||||||
|
elif voltage > 8100:
|
||||||
|
hub.light.on(Color.YELLOW)
|
||||||
|
elif voltage > 7200:
|
||||||
|
hub.light.on(Color.ORANGE)
|
||||||
else:
|
else:
|
||||||
driving.dc(0)
|
hub.light.on(Color.RED)
|
||||||
|
|
||||||
# Wait.
|
# Wait.
|
||||||
wait(10)
|
wait(10)
|
||||||
|
|||||||
Reference in New Issue
Block a user