Gertboard Software in Python - Released


81 posts   Page 1 of 4   1, 2, 3, 4
by alexeames » Fri Nov 02, 2012 1:07 pm
I've started writing some of the Gertboard test programs in Python 2.7

I've tried hard to duplicate the user experience as closely as possible to the original. The aim is that the user wouldn't be able to tell whether they're running the Python or C versions just by looking at what the Gertboard or screen are doing.

So far I've managed to get a pretty good version of the leds program (using wiringpi) and a workable version of the motor program (using RPi.GPIO and software PWM).

I've written a blog article and posted a couple of Youtube videos of the programs in action compared to the original Gertboard test suite equivalents.

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

The software itself can be downloaded from...

http://raspi.tv/wp-content/uploads/2012/11/GB_Python.zip

I've started this thread, both to announce this and also to enlist support from others who might be interested in helping out. I'm not a very experienced Python programmer, so it may be that I won't be able to do all the programs in the suite. Perhaps others will be able to do the parts that I can't, or don't have time for? Or maybe people might have constructive input on how the programs can be improved? Don't just tell me it's broken - show me how to fix it :lol:

The motor program works well for me on the August Raspbian but is a bit stuttery on September Raspbian. (This may have something to do with timing changing in CPU turbo mode, or may just be due to overclock settings.)

Suggested further avenues of exploration...

It would probably be good to have versions of all the test suite programs using both RPi.GPIO and wiringpi for python. RPi.GPIO doesn't yet support hardware PWM and I'm not sure whether wiringpi for python does yet either. It would be good to get the motor program running with hardware PWM.

There's still plenty of programs to work on, so dive in and have a go. Let's have some fun and learn together. :D
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Sat Nov 03, 2012 2:54 pm
Updated download (same link as in post 1) to include second version of leds program using RPi.GPIO and also a README.txt to explain which file is which (will be more useful as more progs are completed).
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by poglad » Sat Nov 03, 2012 3:46 pm
The leds.c one has a buglet in it anyhow. Without an extra long_wait(3); after the call to start_new_pattern(p); you basically miss the first LED of the pattern as it's switched off immediately. So, for example, the first pattern appears to start at LED 2 until it plays the second time.

The code starts with the remark: "Try to strike a balance between keep code simple for novice programmers but still have reasonable quality code." IMHO as a seasoned C programmer of 23+ years it achieves neither of these aims - no offence to Gert et al, just my opinion. The "buglet" just mentioned only really occurred because of the needlessly "flexible" approach to the coding, which resulted in two different places where the first step of a pattern can be executed and just one for every other step.

If your Python translations can present something understandable to the novice and high quality then great stuff. :D
User avatar
Posts: 102
Joined: Tue Jul 31, 2012 8:47 am
Location: Aberdeen, Scotland
by pmj1979 » Sat Nov 03, 2012 4:08 pm
@poglad
Ive just got around to "fixing" the test code as mentioned here [ http://www.raspberrypi.org/phpBB3/viewtopic.php?p=204085#p204085 ] so that he LED5 lights correctly on the rev2 pi.

Can you detail what changes might be made and where to fix the LED1 issue when it script first starts?

Thanks! :)
Peter
Pi from Element14 (still not got the first Pi from RS :( )
Rev2 board | Belkin 7Port powered hub | ASUS N10 WiFi | Logitech Wired Mouse | Behavior Tech Wired KB
User avatar
Posts: 17
Joined: Tue Sep 04, 2012 12:06 pm
by poglad » Sat Nov 03, 2012 5:38 pm
pmj1979 wrote:@pmj1979
Can you detail what changes might be made and where to fix the LED1 issue when it script first starts?

You don't need to - by restructuring the code, Alex has eliminated the problem! My extra wait line was just a workaround. If someone were to translate the Python back into C, the problem would no longer be there. :)

EDIT: Well that's the case in the RG version anyhow. I don't use Wiring Pi so I haven't tried the other one!
User avatar
Posts: 102
Joined: Tue Jul 31, 2012 8:47 am
Location: Aberdeen, Scotland
by alexeames » Sat Nov 03, 2012 8:34 pm
poglad wrote:
EDIT: Well that's the case in the rg version anyhow. I don't use Wiring Pi so I haven't tried the other one!


The two versions are identical apart from the calls to the GPIO.

I've done ocol-wp as well this afternoon, but not released it yet as I've still got the ocol-rg to do - maybe this evening :)
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Sat Nov 03, 2012 8:44 pm
poglad wrote:If your Python translations can present something understandable to the novice and high quality then great stuff. :D


It would be great to get anywhere near that goal. I wonder how easily understandable to beginners the function calls are in the leds program? I'm pretty much a beginner myself, so I suppose they must be reasonably understandable.

I don't have much grasp of C. So my strategy hasn't really been to "translate" the C, more along the lines of write from scratch so the outcome looks the same to the user. It has amused me that the Python programs tend to be a lot shorter than the C ones, although a lot of that is comments. (I open the C ones to borrow the text for the instructions.) :lol:
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by poglad » Sat Nov 03, 2012 9:06 pm
Well I think you're doing a grand job, and it makes it really easy to experiment with the Gertboard demos from within Adafruit's WebIDE. I didn't really mean to be critical of the original C code - I know Gert has delivered a lot with almost no time/resources to spare - but I think his original aims of quality and accessibility are going to be more closely met by efforts like this. 8-)
User avatar
Posts: 102
Joined: Tue Jul 31, 2012 8:47 am
Location: Aberdeen, Scotland
by alexeames » Sat Nov 03, 2012 10:09 pm
Thanks. :D I've updated the uploaded file now to include the ocol open collector program in both versions. It took me slightly longer than I thought because I spotted a bug in my user input error checking. I think it's more robust now - I made that into a function too :D (it's still not perfect, but the program works if you don't try to deliberatly break it with dodgy input - seems to get stuck if you input text - will have a look at that).
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by KenT » Sun Nov 04, 2012 10:25 am
Not surprised that the software implementation of PWM is jittery although I am surprised there is a difference between the releases of Raspbian. I noticed a lot of jitter when I did that python code timing exercise reported on the MK Jam forum. If you are interested I will bring my scope to the next Milton Keynes jam and have a look at the PWM output.
Pi Presents - A toolkit to produce multi-media interactive displays for museums, visitor centres, and more
Download from http://pipresents.wordpress.com
Posts: 583
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
by alexeames » Sun Nov 04, 2012 12:38 pm
KenT wrote:Not surprised that the software implementation of PWM is jittery although I am surprised there is a difference between the releases of Raspbian. I noticed a lot of jitter when I did that python code timing exercise reported on the MK Jam forum. If you are interested I will bring my scope to the next Milton Keynes jam and have a look at the PWM output.


Thanks, that sounds like great fun Ken. I'm hoping to be at the November one. :D

August Raspbian is pretty smooth. It might simly be down to the fact that I wrote it on that one, so it was used to set the timing. Possibly the turbo mode or different overclock settings is what makes the difference, rather than the raspbian version itself. I'm rather hoping for a Python implementation of hardware PWM at some point soon. I think someone mentioned in the Python forum that Croston is planning it for an upcoming RPi.GPIO release. :)
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by croston » Sun Nov 04, 2012 1:29 pm
Yes. PWM and SPI for RPi.GPIO are still work in progress though.
User avatar
Posts: 449
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by alexeames » Sun Nov 04, 2012 2:24 pm
croston wrote:Yes. PWM and SPI for RPi.GPIO are still work in progress though.


Excellent - thanks for your work on this Ben :)
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Sun Nov 04, 2012 3:30 pm
I've got the buttons program working in Python (buttons-rg.py) with RPi.GPIO. This needs the pull-up resistors activated in order to work. I managed to figure that out in RPi.GPIO, but can't find how to do that in wiringpi for python.

Anyone got any idea how to do that or is it not something we can do with the Python wrapper for WiringPi yet?

In the meantime I'll just push out the buttons-rg.py file into the usual download location :)

http://raspi.tv/wp-content/uploads/2012/11/GB_Python.zip


As it stands now we have...
    motor.py - using sofware PWM and RPi.GPIO
    leds-rg.py - leds program using RPi.GPIO
    leds-wp.py - leds program using WiringPi
    ocol-rg.py - ocol program using RPi.GPIO - test program for relay switching
    ocol-wp.py - ocol program using WiringPi - test program for relay switching
    buttons-rg.py - buttons program using RPi.GPIO
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Mon Nov 05, 2012 11:46 am
Anyone who downloaded the new edition last night will have noticed I included the (not working) wp version of buttons by mistake. I should have put the rg version in the zip file instead. This has now been corrected.

In other news, thanks to some pointers from Texy (3 lines of code, but it was enough :lol: ), I've now implemented the undocumented, but working, hardware PWM in the new wiringpi version of the motor script. The way it's structured made it easier to make it look just like a clone of the C program on the visuals as well.

The download is in the usual place (I keep overwriting the old file - I think it's better that way so the same link will always work). And here's a link to the customary side by side video comparing the new motor-wp.py script with it's big brother written in C. :D

http://youtu.be/_6OHEQgn2wo
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Mon Nov 05, 2012 4:40 pm
I had a look at the butled program, and once I realised the basic logic was essentially the same as buttons, it practically wrote itself, with much help from Messrs Cut and Paste. :lol:

So now I've completed at least one Python version of each program in the test suite, except for the three involving the DAC and ADC.

Here's what we currently have:
    NEW motor-wp.py - using Hardware PWM and WiringPi
    motor-rg.py - using sofware PWM and RPi.GPIO
    leds-rg.py - leds program using RPi.GPIO
    leds-wp.py - leds program using WiringPi
    ocol-rg.py - ocol program using RPi.GPIO - test program for relay switching
    ocol-wp.py - ocol program using WiringPi - test program for relay switching
    buttons-rg.py - buttons program using RPi.GPIO
    butled-rg.py - butled program using RPi.GPIO

There are still some holes to fill, pending updates to the two GPIO Python systems...

    Can't do the buttons/butled programs in WiringPi without the ability to pullup
    Hardware PWM in RPi.GPIO motor prog (although software PWM version works tolerably well)
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by alexeames » Wed Nov 07, 2012 11:45 am
Well there's been over 100 downloads of the Python Gertboard software so far. I wonder if anybody else apart from the one or two I've heard from has had a chance to try any of the programs yet?
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by sportsnapper » Wed Nov 07, 2012 12:15 pm
If I had a Gertboard I'd love to try it :?
Posts: 60
Joined: Wed Sep 05, 2012 11:27 am
by thsBavR10 » Wed Nov 07, 2012 1:15 pm
alexeames wrote:Well there's been over 100 downloads of the Python Gertboard software so far. I wonder if anybody else apart from the one or two I've heard from has had a chance to try any of the programs yet?

Hi Alex,
Your work is very helpfull. I own a gertboard (got one of the kits from farnell/element 14),
assembled it myself first time working with smd parts, but it's missing "first light" under real conditions ;-)
I consider myself as a learner / refresher in Raspberry, Linux, GPIO and Python, so your transcription of the test programs will help me to understand (hopefully) that all together.
Posts: 218
Joined: Sat Jul 21, 2012 3:11 pm
by Gert van Loo » Wed Nov 07, 2012 2:18 pm
alexeames wrote:Well there's been over 100 downloads of the Python Gertboard software so far. I wonder if anybody else apart from the one or two I've heard from has had a chance to try any of the programs yet?


Sorry I have had no time.
I will be on the Electronica exhibition in Munich next week.
I will be on the Farnell stand on Wednesday-Friday.
I am very busy preparing for that.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2033
Joined: Tue Aug 02, 2011 7:27 am
by colt45 » Wed Nov 07, 2012 9:33 pm
Thanks for the hard work Alex. I'll try them out tonite hopefully.
Posts: 4
Joined: Mon Aug 06, 2012 5:22 pm
by alexeames » Fri Nov 09, 2012 10:39 am
Latest update. After I said to Wallarug in another thread about not doing a board revision checker this week, I did precisely the opposite as it sounded like a fun challenge. The board Revision changes only seem to affect the leds programs, which uses GPIO21 on Rev 1 Pi boards and GPIO27 on Rev 2 Pi boards.

So, that's been implemented in the leds program now. It works on Rev 1 boards, but I don't yet have a Rev 2 Pi. Can someone please test if it works properly with a Rev 2 Pi? (Both leds-rg.py and leds-wp.py if possible, but if one works, the other will.)

The new files are in the usual place:

http://raspi.tv/wp-content/uploads/2012/11/GB_Python.zip
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by xzanfr » Fri Nov 09, 2012 8:15 pm
Thanks for spending the time to sort these out, alexeames. I've just run through them and it's confirmed that my gertboard works despite my rubbish construction!

I'd really looking forward to using the gertboard with riscos so I'm hoping to use your helpful commenting and have a go at converting it to bbc basic as a bit of a programming practice, to see if I can poke the dusty old BBC basic memories in my head with a stick!
Posts: 8
Joined: Wed Jun 20, 2012 12:52 pm
by alexeames » Fri Nov 09, 2012 10:02 pm
Excellent. Glad they worked for you. You didn't happen to use a Rev 2 Pi did you? I'm hoping someone can test out the new board Revision checking feature of the LEDs program with a Rev 2. I'll probably buy one anyway, but not today :) Good luck with the RISCOS version. :D
Alex Eames RasPi.TV HDMIPi.com RasP.iO
User avatar
Posts: 2064
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by xzanfr » Fri Nov 09, 2012 10:59 pm
alexeames wrote:Excellent. Glad they worked for you. You didn't happen to use a Rev 2 Pi did you? I'm hoping someone can test out the new board Revision checking feature of the LEDs program with a Rev 2. I'll probably buy one anyway, but not today :) Good luck with the RISCOS version. :D

Unfortunately I'm in the same boat as you, mate - with one of the rev 1 boards (or "collectors editions" as they should be called!)
Although I'm a poor programmer, I can't see anything that should be an issue in your code so hopefully someone should be along soon with a rev 2 to confirm that all is o.k.
I'll keep you updated when I get the time to try out the BBC basic stuff and cheers again for taking the trouble for making things easier for us all :)
Posts: 8
Joined: Wed Jun 20, 2012 12:52 pm