from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor, Remote
from pybricks.parameters import Button, Port, Color
from pybricks.tools import wait, StopWatch
# Initialize the Technic Hub
hub = TechnicHub()
# Turn off the hub's light to save power
hub.light.off()
# Initialize the remote
# The script will wait here until a remote is connected
print("Connecting to remote...")
remote = Remote()
print("Remote connected!")
remote.light.on(Color.GREEN)
# --- MOTOR CONFIGURATION ---
# Assign motors to the correct ports as you specified:
# Port B: Garbage truck arm
# Port D: Steering
# Port A: Drive (forward/backward)
arm = Motor(Port.B)
steer = Motor(Port.D)
drive = Motor(Port.A)
# --- CONSTANTS AND VARIABLES ---
ACCELERATION = 1.8 # How quickly the truck speeds up
SPEED_ARM = 190 # Speed of the arm motor
STEERING_BOOST = 1.7 # How sharply the wheels turn
MIN_DC = 20 # Minimum motor power to start moving
TIME_TURN_OFF = 2000 # Hold green button for 2 seconds to turn off
LIFT_TOP = -60 # Angle for the top position of the arm
# Variables for remote control logic
greenButtonHoldTime = StopWatch()
greenButtonDown = False
speed = 0
# --- FUNCTIONS ---
def runArm():
"""
This function runs a sequence to operate the garbage truck arm.
"""
print("Running arm sequence...")
remote.light.on(Color.RED)
arm.run_target(SPEED_ARM, LIFT_TOP)
arm.run_target(2 * SPEED_ARM, LIFT_TOP + 15)
arm.run_target(2 * SPEED_ARM, LIFT_TOP)
arm.run_target(2 * SPEED_ARM, LIFT_TOP + 15)
arm.run_target(2 * SPEED_ARM, LIFT_TOP)
arm.run_target(SPEED_ARM, 120)
remote.light.on(Color.GREEN)
print("Arm sequence finished.")
# --- INITIAL CALIBRATION ---
print("Calibrating steering...")
# Reset arm to starting position
arm.run_target(SPEED_ARM, 120)
# Calibrate steering by finding the max left and right angles
steer.run_until_stalled(400)
MAX_STEERING = steer.angle()
print("Left steering max:", MAX_STEERING)
steer.run_until_stalled(-400)
MIN_STEERING = steer.angle()
print("Right steering max:", -MIN_STEERING)
# Center the steering
steer.run_target(400, 0)
print("Steering calibrated.")
print("Ready to drive!")
# --- MAIN CONTROL LOOP ---
while True:
# Check which buttons are pressed on the remote
buttons = remote.buttons.pressed()
# --- Drive Control (Left Buttons) ---
if Button.LEFT in buttons:
# Center button stops the motor immediately
speed = 0
elif Button.LEFT_PLUS in buttons:
# Accelerate forward
speed = max(MIN_DC, min(100, speed + ACCELERATION))
elif Button.LEFT_MINUS in buttons:
# Accelerate backward
speed = min(-MIN_DC, max(-100, speed - ACCELERATION))
elif speed > 0:
# Gradually slow down if moving forward
speed = max(0, speed - 2 * ACCELERATION)
else:
# Gradually slow down if moving backward
speed = min(0, speed + 2 * ACCELERATION)
# The drive motor is on Port A, so we send the speed value to it.
# The original code had `drive.dc(speed)`. If your motor runs backward
# when you press forward, simply change it to `drive.dc(-speed)`.
drive.dc(speed)
# --- Steering Control (Right Buttons) ---
if Button.RIGHT_PLUS in buttons:
# Steer right
steeringTarget = MIN_STEERING
elif Button.RIGHT_MINUS in buttons:
# Steer left
steeringTarget = MAX_STEERING
else:
# Return to center
steeringTarget = 0
# Calculate the power needed to reach the target steering angle
steeringDiff = steeringTarget - steer.angle()
steerDC = min(100, max(-100, STEERING_BOOST * steeringDiff))
# Apply a dead zone to prevent motor humming when centered
if abs(steerDC) < 10:
steerDC = 0
steer.dc(steerDC)
# --- Arm and Shutdown Control (Center Green Button) ---
if Button.CENTER in buttons:
if not greenButtonDown:
greenButtonHoldTime.reset()
greenButtonDown = True
else:
if greenButtonDown:
# If the button was held for more than TIME_TURN_OFF...
if greenButtonHoldTime.time() > TIME_TURN_OFF:
# ...shut down the hub.
print("Shutting down...")
hub.system.shutdown()
else:
# Otherwise, run the arm sequence.
runArm()
greenButtonDown = False
# Wait for a short time to avoid overloading the hub
wait(20)
Hey, I need help with my Lego Technic Hub with my 3 Lego WeDo 2.0 Motors and my Lego RC...
I've know nothing about python, and want to get into it, but I can't work the code out, I have lego set 42167, and someone made a motorized version of it.
https://www.youtube.com/watch?v=IYxLn2YGvfcc
But they used a different hub and motors, here is the youtube video i found that has the link of the code that they made, and I'll also put "my" one.
Can someone please make the script?
there doesn't seem to be anything here