Skip to main content

Helper class to interface robots to game controllers on the Raspberry Pi.

Project description

Pygame Controller

This project is designed to make it simple to add game controllers to python programs for controlling attached hardware. It consists of a class which maps game controllers to your own code functions and some examples and a template file to help you get started. The original design was created to assist controlling Raspberry Pi based robots using game controllers so that people learning python on Raspberry Pi computers could easily get started in mapping the joysticks, triggers and buttons on a game controller to motors, servos and lights. It has since been proven as a controller library in the prestigious Pi Wars robot competition to produce a remote controlled robot with a full GUI (see my Pi Wars 2019 github repository for details).

Installation

Pygame Controller is now available on PyPi. So you can install or upgrade the package using pip:

pip3 install --upgrade pygame-controller

To deploy the examples, clone this git repo to your Raspberry Pi:

git clone https://github.com/Footleg/pygame-controller

Install from source

If you just want the latest stable release, then you can use pip3 as detailed above. If you want to build the latest source, or build from another branch, you can build the library package using setup tools:

cd pygame-controller/library
python3 setup.py install

Controller Support

This project currently supports the following controllers:

  • Sony PS3 Dualshock Wireless controller
  • Sony PS4 Wireless controller (and compatibles)
  • 8BitDo Pro 2 controller
  • The Pi Hut wireless USB game controller
  • Rock Candy wireless USB game controller
  • Generic wireless USB game controller from Argos

Usage and Examples

There are some common problems in interfacing code to game controllers as inputs which Pygame Controller provides solutions to.

  • Different models of game controller send different information for the same controls. One might map the home button to button number 16 in the array of detected buttons, while another might map it to number 12. Pygame Controller enables the same code to respond to the user pressing a particular button, trigger or joystick regardless of which of the supported controllers is connected.
  • It is not always efficient to continually update outputs from your program to control hardware when there has been no change in the state of the control position which is mapped to that output. Pygame Controller only calls your code functions when the value of the control mapped to that code actually changes. e.g. If a button is pressed and held down then the code linked to that button will only be called once when the button is pressed, and not continually called in a loop. The code linked to the control will only be called again when the button is released.
  • When booting up the Raspberry Pi with your program configured to launch automatically and you are using a wireless bluetooth game controller, you need time to pair the controller with the pi before the program can use it. Pygame Controller provides a way to indicate that the computer OS has booted up and the program is waiting for the controller to be paired before it tries to use the controller. This enables you to provide feedback via your hardware to tell the user the status when no monitor is attached. e.g. A robot with LEDs can indicate that it is waiting for a controller to be paired, or that it has been paired successfully, or failed to be detected. By using the initialisation status callback to display information via the LEDs the user can be given feedback. See the Blinkt Controller example for an example of how this can be done.

As the name suggests, Pygame Controller is based on the Pygame engine. This means it creates a user interface which is displayed on a desktop. The application window displays the state of all the controls on the game controller, and can also be used to display additional messages from your program code. This aids in learning how the controller works, and in debugging what your program is doing.

For a high performance robot you may not want the overhead of running your program on a full desktop with a windowed application, in which case Pygame Controller may not be the right project for you. You might find the approxeng.input library more suitable. Pygame Controller works well for simple robots controlling just a few motors and servos, or if you want visual feedback on screen to help teach, or to help yourself learn. It is a great library for building robots with graphical on screen display interfaces or touch screen menus (see my Pi Wars 2019 github repository for an example using a Pimoroni Hyperpixel Touch display).

To help you start writing your own programs based on Pygame Controller, a template project file is provided. For examples using various different HATs take a look in the examples folder.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame-controller-0.2.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygame_controller-0.2.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file pygame-controller-0.2.0.tar.gz.

File metadata

  • Download URL: pygame-controller-0.2.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.7.3

File hashes

Hashes for pygame-controller-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9c8d442e186eba4067ad49411dbac66c2f002f0d36427c3861c4ac77e8a0a9a1
MD5 0ded1db1db0bbc6ee162a38fa2188615
BLAKE2b-256 e912c778a97ecbbaff9e28d9526387308c4144b065ef1a96de4c47f04a6d6710

See more details on using hashes here.

File details

Details for the file pygame_controller-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pygame_controller-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0599faa453fbd9456e13ba703b0d7c0449eedc5033521d9694ec5ef995532d3
MD5 ccefeae939f65dca21379e8905302a89
BLAKE2b-256 f13519d1dbdda3eeea712b93156bd947dd44b5d87fe12852ffdf68ad28e00fc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page