Home Projects Philosophy Hobbies About Me Updates

Snakes on the Robot

How to control a robot with Python

Python is a very convenient language for robotics. The interactive console allows easy prototyping. The verbose traces simplify debugging. The dynamic nature of the language speeds up development. The rich libraries save work. Here's a list of different methods of getting a robot programmed with Python.

Raspberry Pi

The most straightforward way is to simply install on your robot a computer that runs Python. Raspberry Pi is one such popular example – it runs Linux, with Python installed by default and with some hardware libraries for using the GPIO pins.

There are actually two approaches. You can use the GPIO pins directly to read the sensors and control the servos (with the Servo Blaster software, for instance). Or you can connect a servo controller board over I²C or Serial connection, and use that for controlling the servos.

In case of using serial connection, make sure to disable the serial console on it first.

The down side of this approach is the need to maintain the operating system on that computer, and the relatively large size and weight of the Raspberry Pi, together with the battery it needs.

Remote Computer

If you have that servo controller board there, you can as well get rid of the Raspberry Pi, and instead just send the servo positions over a Bluetooth, WiFi or other radio link, from your stationary computer or laptop.

This makes it very convenient to debug your programs – you can run them directly in your IDE. It also lets your robot to be small and light.

However, it limits the range of your robot, requires your computer to be always on when the robot is supposed to be working, and limits the sensor data you can get.

Micropython on PyBoard

PyBoard is the first microcontroller board on which Micropython, an implementation of Python for microcontrollers, runs. The board is relatively expensive, as it sports a nice Cortex-M4 chip and some additional hardware, such as an accelerometer. It's also relatively large, with a lot of pins available. Which is all good, unless you don't actually need all that…

Compared to Raspberry Pi, the programming is greatly simplified. You no longer need to install, configure and regularly update the operating system. There is virtually no booting delay – the board comes up instantly, and you can simply switch it off at any time without having to fsck the filesystem afterwards. All the libraries for hardware access are built-in, so no need to download and install them. You get an interactive Python console over the USB connection, and the board is visible on your computer as a USB disk, on which you put all your Python files. Very simple and very convenient.

The main downside is the price, although there are now PyBoard Lite boards, which are slightly cheaper.

Hacking a Router