Skip to content

Joystick Examples

Local Controller Query

examples/joystick/local_query.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import time

from kevinbotlib.joystick import LocalJoystickIdentifiers

while True:
    count = LocalJoystickIdentifiers.get_count()
    names = LocalJoystickIdentifiers.get_names()
    guids = LocalJoystickIdentifiers.get_guids()
    print(f"{count} joysticks present")
    print(f"Joystick Names: {names}")
    print(f"Joystick GUIDs: {guids}")
    time.sleep(1)

Local Raw Polling

examples/joystick/local_polling.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import time

from kevinbotlib.joystick import RawLocalJoystickDevice
from kevinbotlib.logger import Logger, LoggerConfiguration

logger = Logger()
logger.configure(LoggerConfiguration())

controller = RawLocalJoystickDevice(0)
controller.start_polling()

try:
    while True:
        print("Buttons:", controller.get_buttons())
        print("POV:", controller.get_pov_direction())
        print("Axes:", controller.get_axes())
        time.sleep(0.1)
except KeyboardInterrupt:
    controller.stop()

Local Named Polling

examples/joystick/local_named_polling.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import time

from kevinbotlib.joystick import LocalNamedController
from kevinbotlib.logger import Logger, LoggerConfiguration

logger = Logger()
logger.configure(LoggerConfiguration())

controller = LocalNamedController(0)
controller.start_polling()

try:
    while True:
        print("Held buttons:", [btn.name for btn in controller.get_buttons()])
        print("POV Direction:", controller.get_pov_direction())
        print("Trigger Values:", controller.get_triggers())
        print("Left Stick Values:", controller.get_left_stick())
        print("Right Stick Values:", controller.get_right_stick())
        time.sleep(0.1)
except KeyboardInterrupt:
    controller.stop()

Sending Joystick Data with a RedisCommClient

examples/joystick/sender.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time

from kevinbotlib.comm.redis import RedisCommClient
from kevinbotlib.joystick import JoystickSender, LocalNamedController
from kevinbotlib.logger import Logger, LoggerConfiguration

logger = Logger()
logger.configure(LoggerConfiguration())

controller = LocalNamedController(
    0
)  # it doesn't matter what type of controller is being sent - all values will be converted to raw anyway
controller.start_polling()

client = RedisCommClient()
client.connect()
client.wait_until_connected()

sender = JoystickSender(client, controller, "joysticks/0")
sender.start()

while True:
    time.sleep(1)

Receiving Raw Joystick Data with a RedisCommClient

examples/joystick/rx_raw.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import time

from kevinbotlib.comm.redis import RedisCommClient
from kevinbotlib.joystick import RemoteRawJoystickDevice
from kevinbotlib.logger import Logger, LoggerConfiguration

logger = Logger()
logger.configure(LoggerConfiguration())

client = RedisCommClient()
client.connect()
client.wait_until_connected()

controller = RemoteRawJoystickDevice(client, "joysticks/0")
controller.start_polling()

try:
    while True:
        print("Buttons:", controller.get_buttons())
        print("POV:", controller.get_pov_direction())
        print("Axes:", controller.get_axes())
        print("Connected:", controller.is_connected())
        time.sleep(0.1)
except KeyboardInterrupt:
    controller.stop()

Receiving Named Joystick Data with a RedisCommClient

examples/joystick/rx_named.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import time

from kevinbotlib.comm.redis import RedisCommClient
from kevinbotlib.joystick import RemoteNamedController
from kevinbotlib.logger import Logger, LoggerConfiguration

logger = Logger()
logger.configure(LoggerConfiguration())

client = RedisCommClient()
client.connect()
client.wait_until_connected()

controller = RemoteNamedController(client, "joysticks/0")
controller.start_polling()

try:
    while True:
        print("Buttons:", controller.get_buttons())
        print("POV:", controller.get_pov_direction())
        print("Trigger Values:", controller.get_triggers())
        print("Left Stick Values:", controller.get_left_stick())
        print("Right Stick Values:", controller.get_right_stick())
        print("Connected:", controller.is_connected())
        time.sleep(0.1)
except KeyboardInterrupt:
    controller.stop()

Robot Controller with Joystick Listening

examples/robot/joystick_robot.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from kevinbotlib.joystick import RemoteNamedController
from kevinbotlib.logger import Level
from kevinbotlib.robot import BaseRobot


class DemoRobot(BaseRobot):
    def __init__(self):
        super().__init__(
            opmodes=[
                "TestOp1",
            ],  # robot's operational modes
            log_level=Level.TRACE,  # lowset logging level
            enable_stderr_logger=True,
            cycle_time=20,  # loop our robot code 20x per second - it is recommended to run much higher in practice
            metrics_publish_timer=0,  # the test robot doesn't use metrics - see the metrics_robot.py example for a metrics usage example
        )

        self.joystick1 = RemoteNamedController(self.comm_client, "%ControlConsole/joystick/0")

    def robot_start(self) -> None:  # runs once as the robot starts
        super().robot_start()
        print(
            "Starting robot..."
        )  # print statements are redirected to the KevinbotLib logging system - please don't do this in production

    def robot_periodic(self, opmode: str, enabled: bool) -> None:
        super().robot_periodic(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'}... {opmode}")
        print(self.joystick1.is_connected())
        print(self.joystick1.get_buttons())

    def opmode_init(self, opmode: str, enabled: bool) -> None:
        super().opmode_init(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'} init... {opmode}")

    def opmode_exit(self, opmode: str, enabled: bool) -> None:
        super().opmode_exit(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'} exit... {opmode}")

    def robot_end(self) -> None:  # runs as the robot propares to shutdown
        super().robot_end()
        print("Ending robot...")


if __name__ == "__main__":
    DemoRobot().run()