This commit is contained in:
2022-06-15 10:29:21 +02:00
parent b5011bbf28
commit 84c7286660
@@ -1,56 +1,84 @@
from pybricks.pupdevices import Motor, Remote ################################################################################
from pybricks.parameters import Port, Direction, Stop, Button # #
from pybricks.tools import wait # pybricks script for 42109 - RC Truck v.3 by ufotografol #
# #
# Initialize the motors. # This script enables the LEGO® Powered Up Technic Hub to be used with the #
steer = Motor(Port.B) # LEGO® Powerd UP 88010 Remote. #
front = Motor(Port.D, Direction.COUNTERCLOCKWISE) # #
# Configuration of the Technic Hub: #
# Connect to the remote. # Port A: Not used #
remote = Remote() # Port B: Steering with left and right limited #
# Port C: Not used #
# Read the current settings # Port D: Drive motor #
old_kp, old_ki, old_kd, _, _ = steer.control.pid() # #
################################################################################
# Set new values # Rebrickable: https://rebrickable.com/mocs/MOC-57612/ #
steer.control.pid(kp=old_kp*4, kd=old_kd*0.4) # Script base: https://racingbrick.com/2021/08/remote-control-for-control-sets #
# -without-an-app-or-smartphone-pybricks/ #
# Find the steering endpoint on the left and right. ################################################################################
# The middle is in between. # #
left_end = steer.run_until_stalled(-200, then=Stop.HOLD) # Changelog #
right_end = steer.run_until_stalled(200, then=Stop.HOLD) # #
################################################################################
# We are now at the right. Reset this angle to be half the difference. # v0.0.1 15-06-2022 #
# That puts zero in the middle. # Changed variable namings. #
steer.reset_angle((right_end - left_end)/2) # Changelog added. #
steer.run_target(speed=200, target_angle=0, wait=False) # Comment header added. #
# Changed the way spped is handled within the while loop. #
# Set steering angle for the buggy # v0.0.0 14-06-2022 #
steer_angle = (((right_end - left_end)/2)-5) # First version. #
print('steer angle:',steer_angle) ################################################################################
while True: from pybricks.pupdevices import Motor, Remote
# Check which buttons are pressed. from pybricks.parameters import Port, Direction, Stop, Button
pressed = remote.buttons.pressed() from pybricks.tools import wait
# Choose the steer angle based on the right controls. # Initialize the motors.
steering = Motor(Port.B)
if Button.RIGHT_MINUS in pressed: driving = Motor(Port.D, Direction.COUNTERCLOCKWISE)
steer.run_target(1400, -steer_angle, Stop.HOLD, False)
elif Button.RIGHT_PLUS in pressed: # Connect to the remote.
steer.run_target(1400, steer_angle, Stop.HOLD, False) remote = Remote()
else:
steer.track_target(0) # Read the current settings
old_kp, old_ki, old_kd, _, _ = steering.control.pid()
# Choose the drive speed based on the left controls.
drive_speed = 0 # Set new values
if Button.LEFT_MINUS in pressed: steering.control.pid(kp=old_kp*4, kd=old_kd*0.4)
drive_speed += 100
if Button.LEFT_PLUS in pressed: # Find the steering endpoint on the left and right.
drive_speed -= 100 # The middle is in between.
left_end = steering.run_until_stalled(-200, then=Stop.HOLD)
# Apply the selected speed. right_end = steering.run_until_stalled(200, then=Stop.HOLD)
front.dc(drive_speed)
# We are now at the right. Reset this angle to be half the difference.
# Wait. # That puts zero in the middle.
steering.reset_angle((right_end - left_end)/2)
steering.run_target(speed=200, target_angle=0, wait=False)
# Set steering angle for the buggy
steer_angle = (((right_end - left_end)/2)-5)
print('steer angle:',steer_angle)
while True:
# Check which buttons are pressed.
pressed = remote.buttons.pressed()
# Choose the steering angle based on the right controls.
if Button.RIGHT_MINUS in pressed:
steering.run_target(1400, -steer_angle, Stop.HOLD, False)
elif Button.RIGHT_PLUS in pressed:
steering.run_target(1400, steer_angle, Stop.HOLD, False)
else:
steering.track_target(0)
# Choose the drive speed based on the left controls.
if Button.LEFT_MINUS in pressed:
driving.dc(100)
elif Button.LEFT_PLUS in pressed:
driving.dc(-100)
else:
driving.dc(0)
# Wait.
wait(10) wait(10)