loading
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.    

About This Instructable

535,900views

387favorites

License:

More by ZRob314:Piano Pi - Play a Duet With Your Pi! Raspberry Pi Touchscreen Setup Times Square in your living room!   Turn your Sense Hat LED into a news ticker. 
Add instructable to: