***My Code For This Project***
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
- Breadboard
- 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.
Motion
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.
Programming
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.
Future Improvements
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.
Conclusion
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.
[…] James Poole, who recently gave us a great way to put a Raspberry Pi Zero inside a SNES controller, has branched out into robotics. He’s gone about it the old-fashioned way by using an H-bridge chip to drive two motors attached to a chassis with the Pi onboard. He’s then hooked up a USB webcam to allow him to see where he’s going. He has open-sourced all the code to do it and released it on GitHub. Take a look at his blog for more details on how to install the software and wire up the circuit. […]
Can you show your circuit if you were to add a separate h-bridge and 2 more motors? As I see it I would want to add the ability to pan and tilt the camera. I have done this with MotorAir boards and streamed to my phone but I wanted to take your concept a little further.
At first thought, if I were to add motors to bring tilt and pan functionality to the camera, I wouldn’t use DC motors because they are far too hard to control for precise movements. I would go about it using two servo motors. I’m not sure if the Pi would be able to power them all itself or if you would need external power for them also but it would be easy enough to find out. This link should get you started: http://razzpisampler.oreilly.com/ch05.html
Thanks James!
hi
me interesa este tipo de prototipo, me ayudarias a conseguirlo?
Hi there!
I actually implemented this and it works great! I am having a bit of an issue seeing the camera on the web controls (even though the motion service is running and I can connect to it separately) – but I am thrilled with the result! Just a note: It seems in your Fritzing diagram you have your ground (on the breadboard) connected to your Pi’s 3.3v.
Hi Quintin,
First off thanks for pointing out the error, Ive fixed that now and set it right!
Your issue with the camera link in the website has also alerted me to something that I hadn’t thought of before that might be the cause of your problem.
In my index.html file in the templates folder I have the link to my Pi’s local IP address and the appropriate port.
Its something easily glanced over so if you haven’t already, change that to your own Pi’s IP with the same port.
I hope that will solve your problem!
Awesome! Thank you! Now I can see the Webcam on the same page as the controls! Sorry for not going through the code more thoroughly, I did this in a bit of a rush. But now it’s all good thanks!
Hey there! I forked you and made a change to the index.html, so that it will always use the local server ip: https://github.com/qbalsdon/WebControlledRobot/blob/feature/index_use_local_ip/templates/index.html
James, congratulations for your inspiring work.
It turned out I grabbed all the components as your project, and implemented with a couple of changes (mainly using Adafruit motor shield v1 and replaced motion with MJPG-streamer). Feel free to take a look at: https://bitbucket.org/pablojr/webcontrolledrobot
Thanks a million for the feedback Pablo. That seems like a great adaption!
To do this, we’ll teach you how to use the mjpg-streamer software to stream video straight from the Raspberry Pi camera to your web browser. Once we have that in place, we’ll be converting the movement code from the previous issue’s guide so it can be executed from our little web application, and then we’ll write said web app in Python. It’s as easy as Pi!
I followed your tutorial, and got it working, but im having some issues when turning, my chassis is a tracked tank, and when turning i want one motor to go clockwise and the other counter clockwise, in the motors.py i have tried changing the code so it should work, but… after the change both motors stand still when turning, i tried many times now, copy pasting the motor2 code from turn left to the motor2 part of turn right, setting enable to HIGH and low/high
im using this h-bridge:
http://www.ebay.com/itm/DC-L298N-Stepper-Motor-Drive-Controller-Board-Module-Dual-H-Bridge-for-Arduino/361175033561?_trksid=p2047675.c100005.m1851&_trkparms=aid%3D222007%26algo%3DSIC.MBE%26ao%3D1%26asc%3D35389%26meid%3Df78e43d79eb24c419c0dd27c0c942165%26pid%3D100005%26rk%3D1%26rkt%3D6%26sd%3D221847323754
powered with 7,4v lipo 2000mah
ps did you find a solution to the 2-4 sec lag from pressing buttons? how about to implement bluetooth so one can connect a smartphone directly, wouldnt that eliminate some of the lag?
Thanks … I was looking of a way to stream from my Logitech E2500 USB camera. The only issue I came across was with the writing permissions for `motion`. In the settings file `/etc/motion/motion.conf`, I modified the parameter `target_dir /home/pi/Monitor`. I set the correct permissions to `sudo chgrp motion /home/pi/Monitor` and `chmod g+rwx /home/pi/Monitor` and `chmod -R g+w /home/pi/Monitor/`. Then make sure to restart the service.
i think these are the only Zeebo in the US, is a retro emulation console too ? http://www.ebay.com/itm/Zeebo-Console-Complete-TecToy-Brazil-Good-Condition-USA-SELLER-/131916308145?hash=item1eb6d31eb1:g:PlkAAOSwMtxXukY2
Hi James,
Many thanks for the excellent tutorial. I have one happy 5 year old son because of it ☺
Cheers,
Warren
Hi, I follow your tutorial to make a robot and I have changed a little. It worked but the page was refreshed when I clicked the button, how can i fix that, can you help me ?
Can you explain your problem a bit more? The page is supposed to refresh each time a button is pressed, but for me, it refreshed quick enough not to be a problem.
I see that there is a lag in your interface using Flask as a web server and connecting remotely.
You might want to look for the 4 part tutorial by (I think) Tony D on Adafruit’s website. adafruit.com
In that tutorial he shows how to use Flask to remotely turn an LED on/off and to read a push button. He also includes the use of a DHT22 temperature/humidity sensor and displays a graph on the page. In the beginning he sets it up as a normal web page and then goes on to show you how to have the web server and the web browser to communicate in both directions speeding this up. I didn’t have time to go through the whole tutorial, but the first few sections were great.
If you can find his tutorial I am sure you will be able to fix your lag.
Hi James, This is an awesome project and I think I will try it myself. As a newbie I have a couple questions if you don’t mind. a) Where would I wire the 9v battery? I don’t see it on your schematic. b) Do you think this project would work with a pi zero?
Thanks!
Moshe
Sorry for the delay in responding! The 9v battery would be connected to the two power rails on the breadboard. Apologies for leaving it out of the schematic!
It would be the same as this image: http://hardwarefun.com/wp-content/uploads/sites/2/2013/06/basic-bot1.png
And yeah this would definitely work on a pi zero!
Let me know how you get on!
Thanks james for sharing your project .
motion daemon is started by sudo motion start ( instead of sudo service motion start ) . now streaming and webpage working (along with scripts execution showing in terminal when giving commands on webpage ) but motor is not working , i don’t know why , i will check breadboard connection tomorrow . btw are you sure motor is working ?
and one more question , i have python adafruit script for dth11 temp and humidity sensor . How can i show temp and humidity of sensor on this project’s html file?
Thanks for your response! So I definitely got my motors working but there is something silly missing from my diagram that may be the problem. I forgot to add the 9v battery that is to be connected to the two power rails of the breadboard. Other than that though, I think it should all work. Let me know if you managed to fix it!
With regards to your temp sensor, this link here seems to have a few suggestions https://www.raspberrypi.org/forums/viewtopic.php?t=35487&p=310038 . I haven’t thought much into it myself but Im sure there are plenty of projects online that cover something similar to what you’re trying. 🙂
Again, if you make progress, let me know!
bro , i tried many things on this project like installing alexa too on rpi so that i can control through voice and voice streaming with your project , for this i have used fm transmitter script which stream voice recording on fm station . i got little success with error and i don’t have time for research on getting it right bcoz some other stuff i have to do for my career . This was just for my school project . Btw thanks bro .
Hey, I have a problem. Every time I click on a button, the page rebuilds and it takes something until I see a picture again. What can I do?
Thats a limitation of this implementation Im afraid. With every button press there is a refresh
Hey,
can i use pi cam instead of webcam.
hi…
how are you?
how to use camera with flask server or robotic car?