This is my first attempt at building a robot with a Raspberry Pi so this was going to be an interesting encounter. I’ve created similar autonomous robots using Arduino but I wanted to see how I could utilise the advantages that having a full Linux computer in the driving seat. In this post I will be documenting this project to build a simple remote control robot that I can control over a website hosted on the Pi. As an extra feature, I wanted to be able to see the robot’s surroundings in real-time, so I hooked a standard USB webcam up to it and had it stream directly to my controller website. I will go through this step by step to show how I went about this build.
Components You Will Need (I worked it out to be about 70€ for everything but I had most of the bits already):
- Raspberry Pi (I used an RPi 2)
- USB Webcam
- H-Bridge ( https://www.adafruit.com/products/807 )
- 2 Motors
- Chassis For The Robot ( https://www.fasttech.com/product/1993602 )
- WiFi Dongle
- Jumper Cables
- 9v Battery
- Battery Pack For RPi (I used the battery pack for my phone)
Setting Up the Raspberry Pi
For the operating system that I used for this project, I used Ubuntu Mate. I initially tried using Raspbian but for some reason I just couldn’t get the motion daemon working with it so I tried this instead with success. I had it installed, expanded the filesystem and had it boot straight to the command line.
Once I had the operating system up and running, the first thing I did was install and get a program called motion running. With one command, motion starts streaming live video to a specified port on your Pi’s local IP address. This would make up the eyes of my robot. To install it run:
sudo apt-get install motion
Once it was installed I made a few adjustments to the motion.conf file:
sudo nano /etc/motion/motion.conf
The changes were as following:
- daemon on
- framerate 90
- stream_maxrate 100
- stream_localhost off
And the last change happened with this next command:
sudo nano /etc/default/motion
and here I just changed start_motion_daemon to yes.
So now this is motion set up. To execute the program just make sure your webcam is plugged into the Pi and run:
sudo service motion start
Now go to a browser on your local network visit your_Pis_IP:8081 and you will see your webcam stream in a straightforward window.
Building The Circuit
Next thing I did was build my circuit that would allow the Pi to power the motors.
I used a standard H-Bridge IC to operate the two motors separately and wired them up as shown in the diagram above. The specific Pin numbers are shown in my Python code. The Pi supplied the operating voltage for the IC and a 9v battery powers the motors themselves.
I programmed this using Python and a library called Flask to set up the server. My code is supplied in the link above.
To pull this code onto your RPi, you can use this command (if git isnt installed, first run sudo apt-get install git):
git clone https://github.com/JamesPoole/WebControlledRobot
There is a simple tutorial to get Flask running on the Pi at http://mattrichardson.com/Raspberry-Pi-Flask/. It is really just 2 commands and you are away.
sudo apt-get install python-pip
sudo pip install Flask
Move the code into a directory of its own and once you are in that directory run the following to set up your Web Server:
sudo python app.py
Once you have run that, if you go to a browser on your network again and enter: your_pi_ip:8000, you will see the web server up and running in all of its glory.
If all of that has gone to plan, you should see the live stream at the top of the window and the grid of buttons below. If you have the hardware built, if you press any of these buttons it will trigger the robot to head off in the direction that you have specified.
Auto Start Up Upon Login
First thing you need to do is set up the Pi to boot straight to the CLI and log in without prompt. To do this, go to the Raspberry Pi Configuration tool (raspi-config) and tick the box to auto log in as the user pi.
Now finally you can go ahead and put the commands that need to be executed in the .bashrc file.
sudo nano ~/.bashrc
In here, go to the bottom of the file and add this line:
sudo service motion start & python location_where_you_put_the_code_/WebControlledRobot/app.py
Now whenever you log in, you will be asked for your sudo password and it will kick into action. The quickest way I do this is using ssh on my phone or computer.
In Raspbian, there is an easy way to boot up straight to the command line without having to put in any passwords at all which would mean that you could just plug in and it will go straight into robot mode. However, even after searching the internet for ages, I couldn’t find a successful way of doing this on Ubuntu so that small feature will be left for another day. If anyone could in fact enlighten me on this, let me know!
Another one would be to be able to input commands without the webpage needing to refresh with each button press. This current system works but it can lag behind a bit if there’s a few quick, consecutive button presses which can lead to poor accuracy.
I’m thrilled with how this fellow turned out and how well he works. It took a while but it was definitely worth it! Now it’s time to start driving him around the house and annoying the housemates!
If you have any feedback leave a comment, email or find me on Twitter, all is appreciated!
Edit: Previously I had my ground rail connected to the 3.3v pin in the connection diagram above and I have corrected this in the current version. Thanks to Quintin for pointing out this error.