Basics

The beginners guide to Raspberry Pi Robotics


Learn the fundamentals of robotics - Part 1

The Robot revolution is coming. Robots are no longer just machines of science fiction, from self-driving cars, to flying drones, robots are on the march. Over the next few years robots are going to be seen all over the place and will be increasingly used in agriculture, manufacture, medicine, education, as well as in our own homes. The amazing thing is that now almost anyone can become a roboticist and if you have a Raspberry Pi you’re already half way there.



Before we get into the what and how-to of any robotics, the first question may be why would you want to build a robot with a Raspberry Pi in the first place? Also, what kind of things will a Raspberry Pi robot be able to do?


The great thing about robots is that they are physical and immersive. Instead of providing output to pixels on a screen, a robot is in your personal space and its movements, lights and sounds go way beyond the limits of a screen. With the Raspberry Pi and some low cost hardware, not only can you make a robot move, you can make a robot that can speak, dance and a whole lot more besides! Why wouldn't you want to turn a Raspberry Pi into a robot?


What makes the Raspberry Pi so good for robotics (as well as so many other projects) is its special GPIO port (General Purpose Input Output). This allows the Raspberry Pi to connect to all kinds of electronics and hardware. The fundamental requirements for the most interesting robots are the ability to both sense and interact with their environment, and it is the GPIO port of the Raspberry Pi that makes this possible.


Before going into further details for building a simple robot, let's first consider some robot fundamentals.


What makes a robot robotic?

Wikipedia defines a robot as a machine which can be electronically programmed to carry out a variety of physical tasks or actions". In addition to this requirement to perform physical actions, a proper robot should also have an autonomous ability. Autonomy allows a robot to act independently in the world and this makes them different from things like radio controlled vehicles that are not able to function by themselves. The Raspberry Pi's processing ability (both CPU and GPU), along with its GPIO port, gives it lots of potential for developing autonomous behaviour. As a simple example let's take a look at the classic line following robot.


A line following robot has the ability to move forward, change direction and detect a line on the ground. This is a classic example of autonomous behaviour. No matter where the line leads, the robot is programmed to follow the line without the need for any external control.


While autonomous behavior can be very useful in a robot it can also be a lot of fun to control it directly. Some of the most interesting applications happen when a robot combines the two. Imagine a flying robot that can fly around at your command but can also be programmed never to crash into walls and other obstacles.


Anatomy of a Raspberry Pi robot

Let us go through all the things you need to build your own robot using a Raspberry Pi. I will detail a minimal possible Raspberry Pi robot step by step. These basic steps are:


Connecting to the Raspberry Pi via SSH

Before we connect to the Raspberry Pi over SSH, we need to set up our network settings to allow us to do this over a network, here I'll be connecting the Raspberry Pi to a network.

Read more

Portable robot power

It's essential to get the robot running on portable power. One of the simplest ways to power our Raspberry Pi is through a 4xAA battery pack with a 5V voltage regulator.

Read more

Building a robot chassis

While it is possible to use an existing chassis to build your robot, it can be fun to make your own. Even some stiff cardboard can be used for this.

Read more

Making your robot sensational

To keep things simple we are going to add just one IR sensor to our robot. The way this works is that it has one LED that is an IR emitter and one that is an IR receiver. The receiver measures the light reflected from the ground and the amount received will depend on the reflectivity of the ground.

Read more

Making the robot move

The simplest and probably cheapest way to give your robot motion is by connecting two continuous rotation servos. The servos are designed to turn just 90 or 180 degrees, though modified versions that provide continuous rotation exist. With two wheels the robot requires a third wheel for support. For a DIY design, you could use a ball bearing castor or furniture wheels but to keep it simple you could also use a smooth sliding support made from a paperclip.

Read more

As you can see it's really simple to get started on robotics, all you need is a few basic materials, enthusiasm about the topic and some spare time. Nowadays everyone can became a roboticist.

Reliable remote access for the Raspberry Pi

Wifi networking can be pretty flakey and most Rapsberry Pi users will have experienced some hair pulling or at least head-scratching as a result. Remote access is fundamental to the PiBot so finding robust ways of remote access are going to be useful.



Communication Fallback

When programming and playing with your robot a more solid alternative to wifi communication is a must. See the following guide for how to do this with an ethernet cable and without needing a network:Raspberry Pi Remote Connections Without A Network! Other ways can be with a wireless radio module. These have been found to have a great range and great stability.


Go for a decent Wifi Module

I've now played with several different wifi modules and I have found that the Edimax EW-7811Un to be the best. Along with having the drivers built into Raspian and being power efficient it also has glowing blue leds for feedback.


Protocols and Interfaces

Even with a single WiFi module it is possible to use a number of interfaces/protocols. The most useful are SSH, web interfaces, and media streaming protocols. Configuring a convenient SSH connection is the best place to start and I'll go through how I've done this over my local network. If you want to use the PiBot direct with smartphones or without needing a local network this is also possible though I wont cover this here.


Remote convenience?

When configuring the system I wanted to end up with a really easy way to connect to the system this included:

  • DHCP so that the systems network address is dynamic
  • Using an SSH Key so terminal session initialisation won't require password input each time
  • The avahi daemon package so that the system can be found over the network by a name only

Configuring /etc/network/interfaces

This is the main network configuration file for Debian linux systems and in my system I change it to:

auto lo
iface lo inet loopback
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "Network-name-here"
wpa-psk "network-password-here"

You'll notice that I've removed the wpa_supplicant reference as I personally find this unnecessary and I like to the configure things in the /etc/network/interfaces file only.


Restarting hte network services

sudo service networking restart

Installing avahi daemon

avahi daemon is a linux implementation of zeroconf that uses a set of techniques aimed at making network connections with the minimum of configuration. It will enable us address the system over the network as pibot.local instead of a less memorable identifier.

sudo apt-get install avahi-daemon

Then

sudo insserv avahi-daemon
sudo vim /etc/avahi/services/multiple.service

Then use something like the following:

sudo /etc/init.d/avahi-daemon restart

Now we can use:

ssh pi@pibot.local

Great now that's easier to contact our system over the local network.


SSH key based authentication

On the local system check whether you already have a public key in the defualt location

ls ~/.ssh/id_rsa.pub ssh pi@192.168.0.22 "cat >> ~/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub

Change hostname

The host name is configured in the following files: /etc/hosts ,/etc/hostname

sudo /etc/init.d/hostname.sh
sudo reboot

Connecting to the Raspberry Pi via SSH

Before we connect to the Raspberry Pi over SSH, we need to set up our network settings to allow us to do this over a network, here I'll be connecting the Raspberry Pi to a network, then connecting my computer to that same network to be able to SSH into the Pi. If you know the IP address of your RPi, we can skip to connecting



The settings for your IP address are kept in /etc/network/interfaces/. Use the code below to open the file in the correct directory.

sudo nano /etc/network/interfaces

In this file you'll need to input the code below:

auto lo
iface lo inet loopback
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.101
netmask 255.255.255.0
network 192.168.0.1
broadcast 192.168.0.254
gateway 192.168.0.48
wpa-ssid "PiBot"
wpa-psk "raspberry"

On start up, your RPi will use this file to use the wifi dongle plugged in (I used the Edimax EW-7811Un). Because we have the piece of code 'iface wlan0 inet static' we have to define the address, netmask, network, broadcast and gateway manually, because we want to have static settings and not dynamic, this means we won't be able to use the global internet. After defining these, we need to define the network we want to connect work on start up, here I have used 'PiBot' for the SSID (network name) and 'raspberry' for the PSK (network password). This file will set us up on a static IP 192.168.0.101 to connect to the 'PiBot' network on start up.


Before we start anything else, we need to restart our network interface using this command:

sudo service network restart

Connecting

Next you'll need to get the computer you'll use to SSH into the RPi set up with SSH, if you're using a Mac or another Linux machine, this is already setup, on windows I recommend using Putty as your SSH client. You'll need to connect to the same network that your Raspberry Pi is connected to, to be able to access it. Once connected we can type:

ssh pi@192.168.0.101

Which allows us to access ‘pi’ (The default user name on a Raspberry Pi) at the IP address 192.168.0.101 so we access the Raspberry Pi. From here you can use the command line to edit, move and run files using the command line.

If you are needing help before the articles are completed please see the useful links for some great resources on the web for learning more about various topics.

© 2014 The Pi Club - Raspberry Pi is a trademark of the Raspberry Pi foundation.