segunda-feira, dezembro 10, 2012

Gertboard software in Python 2.7 – part 1 – motor and leds programs

Download Python Gertboard Programs


I’ve been having a go at producing Python 2.7 versions of some of the Gertboard test suite. It’s partly as a learning experience for me and one which I hope will help others too.


Which GPIO? Why not both?


There’s two different ways to handle GPIO (General Purpose Input Output) in Python at the moment…



  • Ben Croston’s RPi.GPIO

  • Gordon Henderson’s WiringPi (Python Wrapper by Gadgetoid)


The ultimate aim is to have versions of the Python programs using both systems. As a file naming convention, we’ll take the name from the original Gertboard program and add -rp.py for RPi.GPIO and -wp.py for WiringPi.


I had a bit of trouble getting WiringPi for Python up and running, so when I wrote the motor program, I used RPi.GPIO, which I’ve used before. It’s easy to use and is now included in the Raspbian image. So if you’ve got a recent image, you’ve already got RPi.GPIO installed.


When I’d got that working reasonably well, I thought I’d have a go at installing WiringPi for Python again. This time the installation worked (but not as easily as would be nice). So I decided to test out the use of WiringPi for Python with a slightly easier program. I’ve replicated the leds program as faithfully as I can.


The idea with both of these Python programs, was to make the user’s experience as identical as possible to what would happen if they ran the original Gertboard test program. I’ve tested both programs against the originals and they are as close to identical as I can make them.


Fair Comment


I’m sure people will have comments about programming style, etc. (people always do) and I’m also sure that there is plenty of room for improvement. I welcome that, as long as you’re nice and constructive. Smile


I’ve put lots of my own explanatory comments in the script to try to explain what each section does. This is with a view to other people learning too.


It’s very interesting to me that the Python leds program can be written in ~40 lines, whereas the leds.c C source code takes a lot more lines than that. I don’t know if that’s the way it is between C and Python, or if the C program was written to be friendlier for newcomers to understand, but it looks the same at the other end, from the point of view of the user.


Fully functional


I’ve used functions, which is something fairly new to me. I’m starting to see the power of them. When I first wrote the scripts, I used several consecutive loops, but then thought “these loops are basically the same. What about if I had one common function and used ‘function arguments’ to handle the basic differences in the loops?” It seemed to work out OK, but obviously it makes the code slightly harder to read and understand.


leds-wp.py


leds-wp.py is as close a copy as I can make it, uses wiringpi for python. To make use of it, you will need to have wiringpi for python installed. You can install it with…


sudo apt-get update


sudo apt-get install python-dev python-pip


sudo pip install wiringpi



If you don’t have WiringPi installed, or have trouble with it, use leds-rg.py, which is the RPi.GPIO version.


motor.py


motor-rg.py uses RPi.GPIO and software PWM, which is not quite as smooth as the hardware PWM used by the original, but it works. It works better in the August 2012 Raspbian than the September version. Possibly to do with the flexible overclocking messing around with the timings. I hope to do another version with hardware PWM once I work out how to do it.


Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py



You can download these files from here…

Download Python Gertboard Programs


And if you want to discuss these programs or porting others in the Gertboard test suite into Python, then either leave a comment below or you might like to visit this thread on the Raspberry Pi forums.


Update 3 November 2012 ~15:00: Just added leds-rg.py – leds program using RPi.GPIO

Update 3 November 2012 22:02: Just added ocol-rg.py and ocol-wp.py – open collector darlington array relay triggering program for both GPIO methods.

Update 4 November 2012 15:30: Just added buttons-rg.py – buttons program with RPi.GPIO

Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py







via RasPi.tv http://raspi.tv/2012/gertboard-software-in-python-2-7-motor-and-leds-programs

Sem comentários:

LinkWithin

Related Posts Widget for Blogs by LinkWithin