Introduction: Building Robots With Raspberry Pi and Python

The goal of this tutorial is to help you begin programming with Python to control your Arduino robots with a Raspberry PI. We will go over the basics like installation and some simple examples to control your robot from a Python GUI from Raspberry PI.

 

Step 1: Parts List

In this tutorial we are going to use the following items :

1.   Raspberry PI ( with monitor, USB hub, keyboard, mouse,and WIFI Dongle)
http://www.zagrosrobotics.com/shop/item.aspx?itemid=895

2.   Arduino UNO or compatiable (USB cable)
http://www.zagrosrobotics.com/shop/item.aspx?itemid=868

3.   Wifi Router

Now when it comes to robot bases there are many kits available in size and complexity. In this tutorial I have decided on using this setup with a 12v battery that will give us long run times and high load capacity. And a power distribution board with regulated 12v and 5v power outputs that makes it easy to deliver power to our robot.

4.   Robot Base 
http://www.zagrosrobotics.com/shop/item.aspx?itemid=523

5. Motor Driver
http://www.zagrosrobotics.com/shop/item.aspx?itemid=782

6. Power Distribution Board
http://www.zagrosrobotics.com/shop/item.aspx?itemid=765

7. 12volt  7amp hr Battery
http://www.zagrosrobotics.com/shop/item.aspx?itemid=766

Step 2: Introduction to Python

Python is a general purpose interpreted, interactive, object-oriented, high-level programming language that was created by Guido van Rossum in the late eighties. Python allows users to write code with clear, readable syntax with a growing number of support libraries for various tasks that is open source and has a community based around its development. Here are some helpful links that might come in handy when starting out.


Cheat Sheet

Python Howto 

Step 3: Python Installation and Setup

In order to get our Python programs running we need to ensure first that Python is installed on our system along with the proper dependencies and verify the version we are using. Now If you are using " 2013-02-09-wheezy-raspbian.img "  image or later on your Raspberry Pi, Python should be pre-installed.

2013-02-09-wheezy-raspbian.img

We can check our python version by using the terminal to verify.

Copy and paste in the LXterm

     python --version

For the examples we are going to go over it is recommended that you use Python versions 2.6 or later. If Python is not installed on your system you can use apt-get granted your Raspberry Pi is connected to the Internet.

Copy and paste in the LXterm 

     sudo apt-get install python2.6

Install some of the the dependencies  

     sudo apt-get install python-serial

Copy paste in the LXterm 

sudo apt-get install python python-tk idle python-pmw python-imaging

Next download and place the following files to your pi folder.

scanports-arduino.py
term.py
Buttons.py

Now we have to make the programs executable with a chmod command 

Copy and paste in LXterm 

     sudo chmod +x /home/pi/scanports-arduino.py
     sudo chmod +x /home/pi/term.py
     sudo chmod +x /home/pi/Buttons.py

The programs should now be executable and we can run them by double clicking them or via terminal. The last step is to download and load the sketch with the Arduino IDE.

Arduino Robot Base Sketch

  If you are unfamiliar with Arduino and its IDE you can read up on it here.

Arduino 


And lastly refer to the wiring diagram above for setting up the power connections to the robot base.



Step 4: Examples

I have chosen some useful and simple programs that may help you begin to understand Python, and get you connected to your Arduino. There are many useful tutorials on Youtube and online that you should investigate if you want to develop a stronger understanding of Python. I am only going to briefly explain the nature of the code by including some comments within the code. So if your curious about how Python works or its rules, syntax, expressions. You can also try some of the links below.

Beginners Guide
Youtube Tutorial 
Arduino - Python

The first program we are going to execute is the scanports_arduino.py This little program looks for likely ports that the Arduino is typically mounted to "UNO"  /dev/ttyACM0 , "others"  /dev/ttyUSB0. We are going to need this information for the next program.

Copy and paste in LXterm

     python /home/pi/scanports_arduino.py

Since I we using Arduino UNO scanports-arduino.py should give us a terminal like window with the output of /dev/tttyACM0.

Now we are going to use that information to designate our port and baudrate to start the next program term.py.

Copy and paste in LXterm

     python /home/pi/term.py /dev/ttyACM0 115200


When the program is executed properly we are able to use the keyboard to send data to the Arduino over serial to control our robot.


And the last program is a simple button interface that displays clickable buttons to send data to Arduino to drive the robot.
You can either double click Buttons.py or use the following terminal command

Copy and paste into LXterm

     python /home/pi/Buttons.py

You can always modify the program and sketch to add more buttons and new commands.

Step 5: Teleoperation

VNC (Virtual Network Computing) is one way we can control and monitor a computers desktop from another computer over a network. Which in our case is going to be useful for wireless remote teleoperation of the robot and basic control of Raspberry Pi. In order to complete this task we will need a wireless router and a wifi dongle connected to the Raspberry PI as well as a separate computer to control the robot through VNC.  

Setting up your Raspberry pi with VNC .. 

Copy and paste into LXterm

     sudo apt-get install tightvncserver

After it is installed we can start the service with 

Copy and paste into LXterm

     vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565

The first time you run the vncserver you may be asked to create a password to authenticate connection if not use the following command. 

Copy and Paste into LXterm

     vncpasswd

After the password is set its time to login to the sever with the other computer.There are a many VNC Client programs for most OS's, but I will be using Ultravnc on a Windows machine. You can download the program here

Ultravnc Download

Once the program is installed and running we just need to input the raspberry Pi's IP and verify our password to connect to it. The IP can be found by using the command line on the Raspberry Pi.

Copy and paste into LXterm

    ip addr show

It will be the number before the / after the topic " inet "

***Remember that the Raspberry Pi and the windows machine need to be on the same network/router.***

Once you are logged in to the Raspbery Pi you can now interact with the desktop as you normally would but with a very slight delay. With this option you can now interact with the Raspberry Pi wirelessly to perform all sorts of tasks from teleoperation to writing code and reprogramming the robot. You can also use SSH (Secure Shell ) over Raspberry Pi to start the VNCServer that way you don't have to launch it form the Raspberry PI you can just power on your Raspberry Pi without the need for the monitor and keyboard connected to the robot. To achieve this you can follow this great tutorial on setting up Putty and VNC on Raspberry Pi.

Tutorial Link 
     



Step 6: Conclusion

Hopefully this tutorial has helped you get your robot moving and inspired you to think about creating your own programs with Python. Here is a short video of the Robot being controlled with Python and VNC.    

Comments

author
M Tanzeel Khalid (author)2017-07-21

***Remember that the Raspberry Pi and the windows machine need to be on the same network/router.**

Sir,is any server available on google by which i connect raspberry pi 3 and window machine with different network.

author
BastienC (author)2016-09-22

Cool will finish it soon but using ROS :)

author
OktafK (author)BastienC2016-10-11

ROS??

author
HumanoidRobotEngineer (author)2016-02-28

do you need an arduino uno R3 board module?

author
RebeccaC44 (author)2015-12-04

Second, are you using any type of fuse?

author
RebeccaC44 (author)2015-12-04

Hey. This is fantastic! I've always wanted to build a robot. Thank you. I'm sooo close. Just have a couple of questions that I'd love if you could help with. First What's the small piece connected to the switch section of the board in this picture?

Screen Shot 2015-12-04 at 2.09.54 PM.png
author
googleclone (author)2015-05-21

Nice work! I'm trying to wrap my mind around how the R-Pi communicates to the Arduino though

author
jscottb (author)googleclone2015-06-03

In this case, the writer has the Arduino and RPI connected via USB. The pyserial library is used for the communication on the RPI side and the Serial library (hardware in this case) is used on the Arduino side.

author
caffewmilk (author)2015-03-20

Thank you for sharing. I did a search for raspberry pi robot tutorial and your instructable was at the top 3 links. I have a more genera unrelated question and I apologize for it: how do you guys come up with the funds for all these fun projects?

I'm looking forward to building a raspberry pi robot. Thanks.

author
Epic121 (author)2014-11-14

Do u have the circuit diagram of this robot? If you have please share.

author
jamesthequack (author)2014-09-06

Very very nice! I like your style!

author
ecommgeek (author)2014-06-29

What kind of wire do I need to hook this all up?

author
ZRob314 (author)ecommgeek2014-07-16

We normally just use jumper wires similar to these:

http://www.zagrosrobotics.com/shop/item.aspx?itemi...

You will probalby want some male-male and male-female

author
MoserLabs (author)2014-05-16

Nice work! I'm trying to wrap my mind around how the R-Pi communicates to the Arduino though. From the one image it's got a 5V connection to the Arduino, but there aren't any data connections from Pi to Arduino?

author
dhill29 (author)2014-01-11

Hi Fells, there was a problem with the sketch provided. To make it work you will need to change

int r_motor_pwm = 10;

to

int r_motor_pwm = 3;

author
ZRob314 (author)dhill292014-02-09

This depends on the version of the motor driver listed above or if a different motor drive is used. Always double check the data sheets or specs on your components/devices against your code to avoid any confusion during setup/operation.

author
dhill29 (author)ZRob3142014-02-09

Thanks ZRob, That is very good advice. I had bought the exact parts linked to from you and it just is a little thing that tripped me up as a beginner. I wanted to make sure any other inexperienced people would be able to find the problem easily.

author
epierce (author)2013-11-12

This works sweet! Have you tried to run it with NetIO on an Android or Iphone device?

author
epierce (author)2013-10-29

Zrob thanks for helping a 50 year old realize his bot dreams!! I have been hitting a brickwall with this project for 6 months!! Unfortunatley my wife is screaming at me for yelling ITS ALIVE!!! at 1:00 am!!LOL II now realize why the villagers actually destroyed Dr. Frankenstein's lab. Any way to program the button control to an android tablet or phone?

author
purpulhaze (author)2013-10-14

Great instructable! I think I'm gonna try this as I have all the parts on hand. Do you know how one would go about adding live video streaming to something like this? I have a camera module on the way and also a compatible webcam. Though I wasn't impressed with the usb webcam performance.

author
nerd7473 (author)2013-08-31

cool

author
Selenae (author)2013-08-20

Thank you for your post, I was blocked with the python part of my project and you give me some robust base to begin my learn :-)

author
uavguy (author)2013-07-25

Wow this is so awesome! I just got a Raspberry Pi and I have been wanting to use it for robotics. Thank you for taking the time to put this together I know it will be a big help to me!