A few weeks ago, we showed how easy it is to control our new Formula AllCode robot buggy with Python on a Windows PC. To take this further, this article will explain the steps necessary to get the robot working on a Raspberry Pi (RPi).
The hardware required is a Raspberry Pi (I used a version 2 model B) and a Bluetooth USB dongle. Of course, you’ll need a keyboard, mouse and monitor too. Alternatively, the RPi can be set-up to be remotely accessed from a PC or MAC using something like TightVNC.
This article is not limited to a Raspberry Pi and should apply to most Linux-based computers – for example, I used these same instructions on and old laptop running Ubuntu.
Pairing and Configuring Bluetooth
Setting up Bluetooth is relatively easy on a Raspberry Pi and can be done in a number of ways. The following steps is perhaps a more complex way of setting it up, but it should work in all situations.
Step 1 – Get your Bluetooth settings
Open a command-line terminal and type the command “hciconfig”. This will bring up a list of Bluetooth devices available on your RPi. The important thing to note is the identifier of the Bluetooth module – in my case it is “hci0”:
Step 2 – Detect the Formula AllCode
Switch on the robot and then type “hcitool scan”. When I did this, it showed two devices. Mine was the latter (“API_B”) and you will need to take note of the 6 pairs of hexadecimal numbers that are the MAC address, a unique identifier to the robot – in my case, “00:BA:55:23:1C:20”.
Step 3 – Pair the robot with the RPi
To pair, you can use the “bluez-simple-agent” command using the “hci0” and MAC address found in the previous steps.
Step 4 – Making the change permanent
The final step is to make this pairing happen automatically when the RPi is next used. This can be done by editing the “/etc/bluetooth/rfcomm.conf” file (e.g. using nano) and entering the following code. Again, you will need to ensure you use the correct MAC address that was found earlier.
# Automatically bind the device at startup
# Bluetooth address of the device
# RFCOMM channel for the connection
# Description of the connection
comment "Formula AllCode";
Step 5 – Testing the connection
Once you are paired, you can test the connection by using the following command:
echo "PlayNote 100,100\n" > /dev/rfcomm1
If all goes well, you should here a beep from the Formula AllCode.
If this does not work and you get “permission denied” message, you may need to add yourself to the “dialout” group. To see if this is the case, use the “id” command with your username as a parameter to check which groups you belong to. If the group “dialout” is not listed, you can add yourself to the group using the following command (remember to substitute your username in place of “steve”!):
sudo usermod -a -G dialout steve
You will then need to logout and log back in to see this change, and the “PlayNote” command above should now work ok.
Setting up Python and the “FA.py” library
I used Python 3 in this blog, which was already installed on my RPi, but Python 2 should be fine also. If you have neither on your device then you should install that first.
You will then need to install pySerial if it is not already on your RPi. This can be done with the following command in the terminal window:
sudo apt-get install python-serial
We will now leave the terminal window behind – everything else can be done from the desktop.
The next step is to add the Formula AllCode library to your RPi. At the moment we are still working on completing this file and it will eventually have a dedicated installation procedure, but for now simply create a new directory (folder) where you will be saving your Python programs and copy the FA.py library file there.
A preliminary version of this file can be downloaded here: FA.py module
Our first programs
Open the Python IDE, select “File…New Window” from the menu:
We have developed the Python library so that it can be used on various platforms without needing to alter your programs. The only thing that needs to change is the number of the COM port you are using.
Type the following into the new Python window, save it in the same directory where you saved the “FA.py” library and then press F5 (or select “Run…Run Module” from the menu). You should hear some notes played on the robot!
#Example 1: simple sounds on Formula AllCode
If you read the previous blog about Python, you’ll notice that there is an important change. Previously, the commands to interact with the robot were called directly using the FA module (e.g. FA.PlayNote) whereas we are now creating a specific object (“fa”). The reason for this change is to allow us to control multiple robots at the same time with the same program. For example:
#Example 2: dancing robots
loop = 4
while loop > 0:
loop -= 1
#don't forget to stop!
Now you have the basics of using Formula AllCode on the RPi, you can try more complex programs such as reading the IR sensors to avoid collisions.
In fact, you are not limited to Python. The first half of this blog shows how to set up the connection between the RPi and the robot and once this is done then any programming language that can access the COM port can be used.
As a quick test, I just spent 10 minutes finding out how to communicate via a COM port using Wolfram (which I found pre-loaded on my RPi). I’ve never used this language before, and probably never even heard of it, but within 10 minutes I’ve managed to get the robot to beep at me! Here’s how:
This is what our Formula AllCode robot is all about – helping learn robotics and programming concepts using an interesting and motivating device in any language you like.
Now, what language should I play with next… Scratch or Haskell?
18,763 total views, 4 views today