PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Monday, March 10, 2014

On 4:09 PM by Anonymous

Overview

Python is a powerful fast programming language, that can be run interactively, or from a file. Easy to use, no programming experience is required. Python is object oriented and similar to many other languages, but much more fun! Let's get started.

Python: Command line Mode

Python is pre-loaded with the BeagleBone Linux Distribution. To start python, simply open a terminal session and enter "python". Commands are interpreted as they are entered. Control-C will exit Python command mode.

Python command line

Go ahead and try some simple commands

print 'hello world'

{displays} hello world

print 1+2

{displays} 3

Control+c  { to exit }
(Hold down the CTRL key while pressing the C key.)

Python: From a file

For this example we will make a simple three LED light chaser.

What you will need

STEP 1

Wire the circuit as shown in the diagram

Inspire Python Tutorial LEDS

STEP 2

Create a simple file with your favorite text editor (nano, vim, emac etc).

nano rgy.py

{ this will create a empty file named rgy.py for our light chaser}

STEP 3

Go ahead and enter this program (or copy-paste).
Ctrl-o followed by Ctrl-x will write (save)  your file and exit the editor.


#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P9_14", GPIO.OUT)
GPIO.setup("P9_16", GPIO.OUT)
GPIO.setup("P8_19", GPIO.OUT)

while True:
        GPIO.output("P9_14", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P9_14", GPIO.LOW)
        GPIO.output("P9_16", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P9_16", GPIO.LOW)
        GPIO.output("P8_19", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P8_19", GPIO.LOW)

Note in Python indentations are very important. This organizes the code sections and makes the program easier to read.

What does this mean?

Line 1 tells the computer that this is a python file.
Line 2 asks to use the python library that contains function to make programming easier
Line 3 asks to use the library for "time"

We then setup the GPIO (General Purpose Input and Output) pins on the BeagleBone, that we want to use as Outputs to turn on the LEDs. We do this by calling setup from the library GPIO
Finally we create a loop that set the output pins ON, take a short nap for 0.5 seconds, and then change the LEDS to create the light-chaser pattern. This loop repeats until Control-C is pressed to exit.

STEP 4

Before running this program, we need to download a library (if not already loaded) to control the input and output pins of the BeagleBone.

/usr/bin/ntpdate -b -s -u pool.ntp.org

opkg update && opkg install python-pip python-setuptools

pip install Adafruit_BBIO


The first line sets the time, the second line gets the library, the third line installs the library.

STEP 5

To run the program enter

python rgy.py 


(remember: Ctrl-C to exit)

STEP 6

You can make your python script executable. (so you won't need to type python every time)

On the first line of your script enter:

 #!/usr/bin/python

Then set the permission to executable

 chmod +x myscript.py

Finally run your script

 ./myscript.py

Python Tip

Sometimes you would like python to exit gracefully (Turn OFF LEDs before exiting
Using the "try-except" accomplishes this task.


try:
   while True:

         //do something
 
except KeyboardInterrupt:

        // cleanup
        // get ready to exit

The cleanup section might be resetting an attached device, moving a servo motor to rest position, turning OFF leds etc.

Improved version (using try-except)


#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P9_14", GPIO.OUT)
GPIO.setup("P9_16", GPIO.OUT)
GPIO.setup("P8_19", GPIO.OUT)

print "Simple LED chaser"
print "Press Ctrl+C to exit"

try:
  while True:
        GPIO.output("P9_14", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P9_14", GPIO.LOW)
        GPIO.output("P9_16", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P9_16", GPIO.LOW)
        GPIO.output("P8_19", GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output("P8_19", GPIO.LOW)

except KeyboardInterrupt:
        GPIO.output("P9_16", GPIO.LOW)
        GPIO.output("P9_14", GPIO.LOW)
        GPIO.output("P8_19", GPIO.LOW)

Once the keyboard interrupt is detected, we turn OFF the LEDs and exit gracefully.



Python: Using Cloud9 IDE {new}

The new debian distribution, pre-installed on BeagleBone Black revison C, includes Python support within the Cloud9 IDE (Integrated Development Environment). Simply copy-paste your python code in the IDE workspace and click 'RUN'.

Additional Resources

Beginner's guide for Non-Programmer