connecting/disconnecting to XBOX controller while the program is already running #1598
Replies: 5 comments
-
It has always done that, same as the powered up remote.
We could consider this for Python, but I think I'd like to keep it simple in block coding, where all initialization just happens at the start. Does that make sense? Alternatively, we could add the |
Beta Was this translation helpful? Give feedback.
-
Adding a timeout is enough.
D
Il dom 14 apr 2024, 12:30 laurensvalk ***@***.***> ha scritto:
… As of today, the program stops and waits for the XBOX controller to
connect to the hub when I instantiate the class XboxController.
It has always done that, same as the powered up remote.
I was wondering if it would be possible to connect asynchronously with the
controller at any time while the main program is already running
We could consider this for Python, but I think I'd like to keep it simple
in block coding, where all initialization just happens at the start. Does
that make sense?
Alternatively, we could add the timeout parameter just like we have for
the Powered Up remote. Then you could choose to do something else if it
doesn't find the remote/controller.
—
Reply to this email directly, view it on GitHub
<#1598 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALJJ3TPKKRABNUGITSTV6EDY5JLDRAVCNFSM6AAAAABGFQXA5GVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TCMBYGU2TI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
What I think would be helpful is if the XboxController constructor took an That way you could write code for menus, for example, that could use either the controller or the hub buttons to control the robot. With just a connection timeout parameter, I think we'd have to create a wrapper class around the controller instead of using it directly. It seems like an |
Beta Was this translation helpful? Give feedback.
-
Somewhat related. Due to the program disconnecting it is also very hard to debug. So I created a dummy controller class in python, that can be used as a stand in for the XboxController class. Thought I'd share it in case other people find this useful. # controller_simulation.py
from pybricks.tools import StopWatch
class ReadingsTimeline:
def __init__(self, readings):
self.readings = readings
self.stop_watch = StopWatch()
def current(self):
elapsed_time = self.stop_watch.time()
total_duration = 0
for reading in self.readings:
total_duration += reading.duration
if elapsed_time < total_duration:
return reading.value
return self.readings[-1].value
def is_empty(self):
return not self.readings
class SimulatedController:
def __init__(
self,
joystick_left_readings=[],
joystick_right_readings=[],
trigger_readings=[],
button_readings=[],
):
self.joystick_left_readings_timeline = ReadingsTimeline(joystick_left_readings)
self.joystick_right_readings_timeline = ReadingsTimeline(joystick_right_readings)
self.trigger_readings_timeline = ReadingsTimeline(trigger_readings)
self.buttons = SimulatedKeypad(button_readings)
self.stop_watch = StopWatch()
def joystick_left(self):
if self.joystick_left_readings_timeline.is_empty():
raise IndexError("No readings available for left joystick.")
return self.joystick_left_readings_timeline.current()
def joystick_right(self):
if self.joystick_right_readings_timeline.is_empty():
raise IndexError("No readings available for right joystick.")
return self.joystick_right_readings_timeline.current()
def triggers(self):
if self.trigger_readings_timeline.is_empty():
raise IndexError("No readings available for triggers.")
return self.trigger_readings_timeline.current()
class SimulatedKeypad:
def __init__(self, button_readings=[]):
self.button_readings_timeline = ReadingsTimeline(button_readings)
self.stop_watch = StopWatch()
def pressed(self):
if self.button_readings_timeline.is_empty():
raise IndexError("No readings available for buttons.")
return self.button_readings_timeline.current()
class Reading:
def __init__(self, value, duration):
self.value = value
self.duration = duration Example usage: from controller_simulation import SimulatedController, Reading
controller = SimulatedController(
joystick_left_readings=[
Reading((0, 50), 1000),
Reading((50, 0), 1000),
Reading((0, -50), 1000),
Reading((-50, 0), 1000),
Reading((0, 0), 1000),
],
joystick_right_readings=[
Reading((50, 0), 1000),
],
button_readings=[Reading([], 6000), Reading([Button.B], 0)],
) The Reading class takes two argument, the value and the duration in milliseconds. After that duration the next reading will be returned for the corresponding method ( while Button.B not in controller.buttons.pressed():
x1, y1 = controller.joystick_left()
x2, y2 = controller.joystick_right() |
Beta Was this translation helpful? Give feedback.
-
This suggestion has been converted to an actionable issue in #1800. Thanks everyone! |
Beta Was this translation helpful? Give feedback.
-
Hello.
As of today, the program stops and waits for the XBOX controller to connect to the hub when I instantiate the class XboxController.
I was wondering if it would be possible to connect asynchronously with the controller at any time while the main program is already running, and let the robot check whether the controller is connected and behave differently when it is (remote-controlled) or not (autonomous).
If not, I'll stick to writing two different programs for each robot.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions