PyQuadStick: An open-source Python package for quadcopter flight-simulator / remote autopilot control on Linux, Windows, and Mac OS X
My students and I had been writing a Quadcopter simulator in Python, and we needed a way to control the vehicle from a joystick or R/C transmitter. Naturally we turned to PyGame, but we soon ran into the complications resulting from using different controllers on different operating systems. To avoid having to deal with this complexity directly, I bundled the code for various controllers into a package, which requires only a few lines of code to use. For example:
from quadstick.rc.frsky import Taranis as Controller controller = Controller(('Altitude hold', 'Position hold', 'Autopilot')) while (controller.running()): pitch, roll, yaw, climb, switch = controller.poll()
where pitch, roll, yaw, and climb are in the interval [-1,+1] and switch is 0, 1, or 2. This example works with the FrSky Taranis transmitter. The idea is that you can specify the labels associated with the three-position switch on a typical R/C transmitter like this. There are also classes to support a Spektrum DX8 transmitter with an adapter cable, as well as a Logitech Extreme 3D Pro joystick and a Sony PS3 controller. For the joystick you change the switch setting by pulling the trigger, and for the PS3, by hitting the buttons (triangle, square, circle).
You will need:
- The ability to program in Python
- The PyGame package installed on your computer
- The PyQuadStick package installed on your computer
- Administrator (root) privileges on your computer
- An adapter cable if you want to use a Spektrum transmitter
Once you’ve installed the PyQuadStick package, use a terminal (Linux) or command shell (Windows) to change to the directory where you put it, and issue the command
python setup.py install
On Linux you may need to issue this command as root:
sudo python setup.py install
Next, open the qstest.py script and edit it so that the import line for your controller is the only one not commented-out. Then run the script as you would any other (IDLE, command-line, etc.).
This package can also be used in conjunction with PyGCSUDP, to fly an actual quadcopter using the OpenPilot Ground Control Station. This arrangements allows you to prototype different autopilot algorithms in Python on your MAV.
- On Linux PyGame will print out debugging values for the joystick when you run it from a console. The solution is to compile and install PyGame from source.
Kevin P (wazari972) has added keyboard support as of 18 October 2015. Thanks, Kevin!
Adding support for new controllers
If your controller isn’t supported, it shouldn’t be too difficult to subclass one of the existing classes to add support for it. If you do this, please send me your code so I can add it to the package. To figure out the mappings for a new controller, you can run the little jstest.py script included in the package, which will report the values for the axes, buttons, and “hats”.
If you have trouble or questions, let me know.
This work was supported in part by a Commonwealth Research Commercialization Fund grant from the Center for Innovative Technology (CRCF #MF14F-011-MS) and a Lenfest summer research grant from Washington and Lee University.