Technic Hub Xbox controller discrepency? #2680
-
|
I have two little bulldozer vehicles that are running the exact same code. The code lets them connect to an xbox controller and run around. However, one of them the code works perfectly fine. Connects to the controller and runs perfectly. The other one however crashes whenever it trys to connect the the controller. The xbox controller and the crashing vehicle are on the latest versions of their software. everythings connected to the correct ports. I've fully reset the vehicle and tried from there. I don't know what else I could be missing. any ideas would be greatly appreciated. Thank you! Here's the code, # ---ARTUCHU--- 4/24/26 3:27 AM
from pybricks.hubs import TechnicHub
from pybricks.iodevices import XboxController
from pybricks.parameters import Button, Color, Direction, Port
from pybricks.pupdevices import Motor,Light
from pybricks.tools import StopWatch,multitask,run_task,wait
# Set up all devices
technic_hub = TechnicHub()
xbox_controller = XboxController()
Left = Motor(Port.A, Direction.COUNTERCLOCKWISE)
Right = Motor(Port.B, Direction.CLOCKWISE)
light = Light(Port.D)
#Variables
islighton = 0
a_button_pressed_last_cycle = False
inactivity_timeout = 60000
min_voltage = 6500
max_voltage = 8400
technic_hub.light.on(Color.WHITE)
stopwatch = StopWatch()
async def handle_inputs(): #Movement
global islighton, a_button_pressed_last_cycle
while True:
buttons = xbox_controller.buttons.pressed()
triggers = xbox_controller.triggers()
# Triger Forward
if triggers[0] > 5 or triggers[1] > 5 or Button.RB in buttons or Button.LB in buttons:
stopwatch.reset()
left_speed = triggers[0]
right_speed = triggers[1]
if Button.LB in buttons: left_speed = -50
if Button.RB in buttons: right_speed = -50
if Button.LB in buttons and Button.B in buttons: left_speed = -100
if Button.RB in buttons and Button.B in buttons: right_speed = -100
Left.dc(left_speed)
Right.dc(right_speed)
# Left Joystick
elif any(abs(v) > 5 for v in xbox_controller.joystick_left()):
lx, ly = xbox_controller.joystick_left()
Left.dc(ly + lx)
Right.dc(ly - lx)
stopwatch.reset()
# Right Joystick
elif any(abs(v) > 5 for v in xbox_controller.joystick_right()):
lx, ly = xbox_controller.joystick_right()
Left.dc(ly + lx)
Right.dc(ly - lx)
stopwatch.reset()
else: #cleanup
Left.stop()
Right.stop()
#A Button (Light Toggle)
if Button.A in buttons:
if not a_button_pressed_last_cycle:
stopwatch.reset()
if islighton == 0:
light.on(100)
islighton = 1
a_button_pressed_last_cycle = True
else:
light.off()
islighton = 0
a_button_pressed_last_cycle = True
#Escape
elif Button.LJ in buttons and Button.Y in buttons and Button.GUIDE in buttons:
raise SystemExit
else:
a_button_pressed_last_cycle = False
await wait(20)
async def monitor_system(): #everythting else
global islighton
while True:
#Update battery
voltage = technic_hub.battery.voltage()
battery_percent = (voltage - min_voltage) * 100 / (max_voltage - min_voltage)
if battery_percent < 10: #Low Battery Warning
technic_hub.light.on(Color.RED)
technic_hub.light.blink()
light.on(100)
await wait(300)
light.off()
await wait(300)
light.on()
await wait(300)
light.off()
await wait(1000)
if stopwatch.time() > inactivity_timeout:
Left.stop()
Right.stop()
light.off()
islighton = 0
await wait(100)
run_task(multitask(handle_inputs(), monitor_system())) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
Maybe a longshot, but are all objects on the correct port? Another might be to write the potential exception to user storage, so you could look at it later..... Bert |
Beta Was this translation helpful? Give feedback.
-
|
We've been doing frequent updates lately and have several release channels, so "latest" is not clear. Can you please give exact version numbers? |
Beta Was this translation helpful? Give feedback.
-
|
When you use the Xbox Controller with your other hub, remember to put the controller in paring mode again. It will only remember the last one. It's best to put the controller in pairing mode before you start the hub program, or it will attempt to the unpairable controller and fail. |
Beta Was this translation helpful? Give feedback.
When you use the Xbox Controller with your other hub, remember to put the controller in paring mode again.
It will only remember the last one.
It's best to put the controller in pairing mode before you start the hub program, or it will attempt to the unpairable controller and fail.