liveBook · Manning

Joystick

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

Joystick is a Pygame API constructor or class interface for creating an object that represents and accesses a joystick controller device.

A joystick provides sensitive analog control for a robot: each stick position can set one motor’s direction and throttle. Moving both sticks supports forward, backward, turning, spinning, and speed control. section 7.3 section 7.0

Connect a PlayStation or Xbox controller to a Raspberry Pi by USB, or pair a supported PlayStation controller over Bluetooth. A controller on a remote Linux computer can send robot movement requests across Wi-Fi or Ethernet. section 7.1

Create a Pygame joystick device

Create the first detected controller with pygame.joystick.Joystick(0), then use get_name() to confirm which device is active. Poll Pygame’s event queue continuously and handle button-down and axis-motion events by their button or axis identifier and value. section 7.4.2

For lower-level inspection, Linux exposes connected controllers as device files such as /dev/input/js0. The jstest command reports available axes and buttons, letting an application map physical controls to their numeric identifiers before interpreting events. section 7.5.1

Use joystick input as a motor-control interface:

  • Map the right stick’s vertical position to right-motor forward or backward throttle. section 7.3
  • Map the left stick’s vertical position to left-motor forward or backward throttle. section 7.3
  • Send resulting movement requests over HTTP to a robot server. section 7.7
Listing 7.2 pygame_joystick.py: Detecting specific joystick events with Pygame
#!/usr/bin/env python3
import pygame
 
FRAME_RATE = 60
WINDOW_SIZE = [100, 100]
 
def handle_event(event):
    if event.type == pygame.JOYBUTTONDOWN:
        print('button pressed', event.button)
    if event.type == pygame.JOYAXISMOTION:
        print('axis motion', event.axis, event.value)
 
def main():
    pygame.init()
    screen = pygame.display.set_mode(WINDOW_SIZE)
    clock = pygame.time.Clock()
    joystick = pygame.joystick.Joystick(0)
    print('joystick name:', joystick.get_name())
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
            handle_event(event)
        clock.tick(FRAME_RATE)
 
main()

A complete runnable Pygame loop creates the joystick device, prints its name, and reports button and axis events. section 7.4.2

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Works through Pygame device creation and event handling, while deferring more detailed event-data reading.
Examples
Demonstrates PlayStation and Xbox controllers, jstest output, and a Pygame script printing button and axis events.
What you can do after
Apply it via Pygame to identify a controller and handle button and axis events for robot movement.
How it connects
Connects Pygame events, Linux input devices, analog sticks, motor throttle, and HTTP robot control.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.