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.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
- BeagleBone Black
- BeagleBone expandable case Orange or Black (recommended)
- AC adapter 5 Volts, 2 Amp (optional)
- Medium Breadboard
- Jumper Wire Pack for Breadboards
- Proto Parts Kit
- (1) Green LED
- (1) Red LED
- (1) Yellow LED
- (3) Resistors 330 Ohms
These components are included in the parts kit (PTK-027)
STEP 1
Wire the circuit as shown in the diagramSTEP 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 enterpython 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 exitingUsing 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.