colt45
Posts: 4
Joined: Mon Aug 06, 2012 5:22 pm

Re: Gertboard Software in Python - Released

Sat Nov 10, 2012 6:51 am

Alex, your scripts have ignited a fire, I haven't had any time to fully test everything but I have wrote my own python script to operate a stepping motor via the open collector driver. I really didn't know where to start but your scripts really sparked an idea of how to proceed. Thank You! This is my first full script in Python, It took me about 3 hours to get it this close. I've modified some scripts on my CNC machine that runs EMC linux but never anything of this nature. Anyway, guys be gentle, I'm really looking for the correct way to handle the arrays and reduce the iterations of code. Any positive input will be greatly appreciated. Here is the link:

https://dl.dropbox.com/u/58178325/step-wp_v2.py

Thanks guys!

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Sat Nov 10, 2012 9:50 am

Fantastic - I'll have a good look later (I laughed out loud when I saw the credit though) :lol:
Alex Eames RasPi.TV, RasP.iO

thelukemccarthy
Posts: 4
Joined: Tue Mar 13, 2012 2:43 pm

Re: Gertboard Software in Python - Released

Sat Nov 10, 2012 11:16 am

Hi colt45

I'm not a Python programming but the code looks good to me.

I found a good explanation of Python arrays here http://effbot.org/zone/python-list.htm
and if statements here http://effbot.org/pytut/node6.htm

Good luck and well done.

colt45
Posts: 4
Joined: Mon Aug 06, 2012 5:22 pm

Re: Gertboard Software in Python - Released

Sat Nov 10, 2012 5:28 pm

Thanks gents. At the speed its operating at in the vid there is some minor jitter. I would realistically want to use the ATMEGA to do something like this IRL but, I learned quite a bit in writing it. Here is the link to the vid: (Please excuse the quality, it is HD though.)

https://www.youtube.com/watch?v=Z3FzOYgYlfY

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Sat Nov 10, 2012 6:06 pm

Well done Colt. Most importantly of all - it works :)

I had a look at your code. The only comment I had was that whenever I'm doing an operation over more than one or two things, I try and put it in a loop to minimise code lines. It won't affect functionality though. (For example, lines 45-53 are screaming out to go in a loop - have a look at reset_ports() for the leds-wp.py prog to see what I mean - that handles the resetting of 12 ports in 3 lines of code).

You would also need to use a list for the variables and slice notation to access them. But if your program works, that's what matters most. And I'm no expert anyway :lol:
Alex Eames RasPi.TV, RasP.iO

DirkS
Posts: 9941
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Gertboard Software in Python - Released

Sun Nov 11, 2012 1:17 am

[quote="alexeames"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.)[/quote]
Tested both progs on a Rev 2 board (revision is '0004') and works fine.

Personally I would change the detection slightly. If a board comes out with a higher revision id (e.g. '0007') the result is undefined. The following should work (at least it did for me):

Code: Select all

def PiRevCheck():               # Function to check for which Pi Board revision we have
    revcheck = open('/proc/cpuinfo')
    boardRev = -1
    while True: 
        line = revcheck.readline()
        if not line: break              # end if reached the end of the file
        if "Revision" in line:
            rev = line[-5:-1]
            if rev <= "0003":
                boardRev = 1
            else:
                boardRev = 2
    revcheck.close()
    return boardRev
NB: I have programmed in a whole range of languages, but it's my first go at Python so no guarantees the above is 100% correct...

Gr.
Dirk.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Sun Nov 11, 2012 8:25 am

Thanks for doing the Rev 2 test :D and for the idea Dirk. I like the idea, although the way my script handled "the future" was with the else below (outside the function).

Code: Select all

boardRev = PiRevCheck() # check board Revision so we can set port 21/27 correctly
if boardRev == 1:
    ports = [25, 24, 23, 22, 21, 18, 17, 11, 10, 9, 8, 7]   # define ports list Rev 1
else:
    ports = [25, 24, 23, 22, 27, 18, 17, 11, 10, 9, 8, 7]   # define ports list all others
Changing it to inside the function might be more robust if the function was to be used in another program (in case we forget). My script will define ports as the correct ones for Rev 1 boards if it detects a Rev 1 code, and in all other cases (including a -1 return) it will define ports for Rev 2 boards - this seems relatively future-proof unless they change the ports again. In which case an update would have been needed anyway. :D

The main problem with your modification is that the variable rev is a string, not an integer. That could be solved by wrapping it with int().

So

Code: Select all

 if rev <= "0003":
will fail unless we've previously made rev an integer like this...

Code: Select all

rev = int(line[-5:-1])
And once we've done that, we'd probably have to change the line to...

Code: Select all

 if rev <= 3:
Your suggestion makes the code a bit tidier though, so I think I'll implement it. :D
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Sun Nov 11, 2012 9:04 am

I've uploaded the above change and also updated the README.txt file to include some simple installation and usage instructions (for example how to install wiringpi).

http://raspi.tv/wp-content/uploads/2012 ... Python.zip
Alex Eames RasPi.TV, RasP.iO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

Re: Gertboard Software in Python - Released

Tue Nov 13, 2012 5:40 pm

You've probably found this already, but my recent 512Mb rev 2 board has the revision ID "000f", which doesn't parse well with int(line[-5:-1]) !!

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 13, 2012 5:58 pm

ejfield wrote:You've probably found this already, but my recent 512Mb rev 2 board has the revision ID "000f", which doesn't parse well with int(line[-5:-1]) !!
Thanks - I haven't come across that. I was under the impression that only digits were in that field. Looks like it might be hex, so will need some further investigation. Million dollar question though. Did it work or did it error out as f can't be an int? It would have worked with the previous "string" version LOL I should have left it alone. :lol: I might revert it.

Edit - Have reverted to the 9th November version which I think should work fine. Even though it's less elegant, function over form. :lol:
Alex Eames RasPi.TV, RasP.iO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

Re: Gertboard Software in Python - Released

Tue Nov 13, 2012 10:32 pm

Yes I think it will be hex, a quick google suggests that a few other values are kicking about. And yes it failed as you suggested - something like "invalid literal for int with base 10", which seem fair!

Keep up the good work.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 13, 2012 10:57 pm

ejfield wrote:Yes I think it will be hex, a quick google suggests that a few other values are kicking about. And yes it failed as you suggested - something like "invalid literal for int with base 10", which seem fair!

Keep up the good work.
Thanks. Can you/have you tried the current (reverted) version to see if it works? I don't have a Rev 2 pi yet so am relying on reports from people like your good self. I put it in the zip file in the usual place. I think it's best if the current version always has the same URL.

http://raspi.tv/wp-content/uploads/2012 ... Python.zip
Alex Eames RasPi.TV, RasP.iO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 12:46 pm

Yup that seems to work. I was slightly confused at first sight though, so here's a suggestion. If the only codes for rev1 are 0002 and 0003, why not set the default for boardRev to 2 (instead of -1), then you only need the test for 0002 and 0003, which will set boardRev to 1.

cheers.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 1:23 pm

ejfield wrote:Yup that seems to work.
Excellent - thanks :)
ejfield wrote:I was slightly confused at first sight though, so here's a suggestion. If the only codes for rev1 are 0002 and 0003, why not set the default for boardRev to 2 (instead of -1), then you only need the test for 0002 and 0003, which will set boardRev to 1.
I did consider it at one point, but although a bit messy, this way leaves the door open for further GPIO changes in Rev 3 or any future Revs. I could still be persuaded to eliminate a couple of (currently) unnecessary lines in the name of tidier code though. :D For now, what matters most to me is that it works :lol:
Alex Eames RasPi.TV, RasP.iO

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 1:26 pm

ejfield wrote:Yup that seems to work. I was slightly confused at first sight though, so here's a suggestion. If the only codes for rev1 are 0002 and 0003, why not set the default for boardRev to 2 (instead of -1), then you only need the test for 0002 and 0003, which will set boardRev to 1..
RPi.GPIO defaults to rev 2 unless a rev 1 board is detected.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 4:56 pm

croston wrote:
ejfield wrote:Yup that seems to work. I was slightly confused at first sight though, so here's a suggestion. If the only codes for rev1 are 0002 and 0003, why not set the default for boardRev to 2 (instead of -1), then you only need the test for 0002 and 0003, which will set boardRev to 1..
RPi.GPIO defaults to rev 2 unless a rev 1 board is detected.
Ooh that's posh :) Will it do substitutions even with BCM pin settings? Surely not?
Alex Eames RasPi.TV, RasP.iO

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 5:02 pm

alexeames wrote:
croston wrote:
ejfield wrote:Yup that seems to work. I was slightly confused at first sight though, so here's a suggestion. If the only codes for rev1 are 0002 and 0003, why not set the default for boardRev to 2 (instead of -1), then you only need the test for 0002 and 0003, which will set boardRev to 1..
RPi.GPIO defaults to rev 2 unless a rev 1 board is detected.
Ooh that's posh :) Even with BCM pin settings?
The variable GPIO.RPI_REVISION will give you the revision number. If you choose to use BCM numbers then you have to adjust the numbering yourself. If you use BOARD numbers then these will be adjusted for you.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Thu Nov 15, 2012 7:25 pm

croston wrote:The variable GPIO.RPI_REVISION will give you the revision number. If you choose to use BCM numbers then you have to adjust the numbering yourself. If you use BOARD numbers then these will be adjusted for you.
Excellent. Thanks Ben. That means the RPi.GPIO based LED script can certainly be tidied up. :D
Alex Eames RasPi.TV, RasP.iO

nr.
Posts: 144
Joined: Wed Oct 03, 2012 8:51 am
Location: The Fens
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 20, 2012 3:21 pm

xzanfr wrote: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!
Here goes - a bit of BASIC for flashing the LEDs on RISC OS:

Code: Select all

REM check we're on a Pi
SYS "GPIO_GetBoard" TO a%
IF a% = 11 OR a% = 12 THEN
 REM build array to map LEDs to GPIO
 DIM b%(11)
 REM loop through all twelve LEDs
 b%() = 7,8,9,10,11,17,18,21,22,23,24,25
 FOR x% = 0 TO 11
  g%=b%(x%)
  PRINT "Now playing with pin ";g%
  REM read the current pin mode
  SYS "GPIO_ReadMode" ,g% TO a%
  PRINT "Pin Mode ";~a%
  REM is it output ?
  IF a% <>1 THEN
   REM no - set it as output
   SYS "GPIO_WriteMode" ,g%,1 TO a%
   PRINT "Set Mode To GPIO Output"
   REM read it again, just to be sure
   SYS "GPIO_ReadMode" ,g% TO a%
   PRINT "Pin Mode now "; ~a%
  ENDIF
  REM flash LED 3 times
  FOR z% = 1 TO 3
   REM turn LED on
   SYS "GPIO_WriteData" ,g%,1
   FOR y% = 1 TO 500000: NEXT
   REM turn LED off
   SYS "GPIO_WriteData" ,g%,0
   FOR y% = 1 TO 500000: NEXT
  NEXT
 NEXT
ENDIF
END
As discussed in the RISC OS section, the code isn't great. But it works [1]. To use it, strap up the gertboard as if for the LED.c test program, and load the GPIO module as discussed again in the RISC OS section of this forum.
If this is useful, please feel free to fiddle with it and do something useful. It's just something I kicked together this afternoon (first BASIC program since, well, probably since puberty in fact) and as such, it's probably a bit rubbish.

[1] On a rev. A anyway. Not tested on a rev. B

Ta,
--
nr.

nr.
Posts: 144
Joined: Wed Oct 03, 2012 8:51 am
Location: The Fens
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 20, 2012 4:25 pm

Dammit, I *knew* this would happen. I just wanted to flash the LEDs as a proof of concept, but couldn't help fiddling with the code. Here's a slightly easier to read version:

Code: Select all

REM check we're on a Pi, just bail out if otherwise
SYS "GPIO_GetBoard" TO a%
IF a% = 11 OR a% = 12 THEN
 REM build array to map LEDs to GPIO
 DIM b%(11)
 IF a% = 12 THEN
  REM set pin to LED mappings for rev A
  b%() = 7,8,9,10,11,17,18,21,22,23,24,25
 ELSE
  REM Set pin to LED mappings for rev B
  b%() = 7,8,9,10,11,17,18,27,22,23,24,25
 ENDIF
 REM loop through LEDs one way, flashing them
 FOR x% = 0 TO 11
  g%=b%(x%)
  REM read the current pin mode
  SYS "GPIO_ReadMode" ,g% TO a%
  REM is it output ?
  IF a% <>1 THEN
   REM no - set it as output
   SYS "GPIO_WriteMode" ,g%,1 TO a%
  ENDIF
  REM flash LED 3 times
  FOR z% = 1 TO 3
   PROC_ledon(x%)
   PROC_delay(300000)
   PROC_ledoff(x%)
   PROC_delay(300000)
  NEXT
 NEXT
 REM loop through LEDs the other way, flashing them
 REM no need to check mode this time through
 FOR x% = 11 TO 0 STEP -1
  REM flash LED 3 times
  FOR z% = 1 TO 3
   PROC_ledon(x%)
   PROC_delay(300000)
   PROC_ledoff(x%)
   PROC_delay(300000)
  NEXT
 NEXT
ENDIF
END

DEF PROC_delay(delay%)
  FOR y% = 1 TO delay%:NEXT
ENDPROC

DEF PROC_ledon(led%)
  SYS "GPIO_WriteData" ,b%(led%),1
ENDPROC

DEF PROC_ledoff(led%)
  SYS "GPIO_WriteData", b%(led%),0
ENDPROC
This *should* work on a rev B as well. I can't test this, as I don't have one. Right. Now I *really* need to get on with some work this afternoon.

Ta,
--
nr.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 20, 2012 9:41 pm

nr that's awesome - well done. :D

You should start a new thread for that so other Riscos users will be drawn in - otherwise they'll never find it - and this one's about Python after all :)

I looked at your code before reading the text and thought

"REM? What the BH is REM doing in a Python script?" :lol:
Alex Eames RasPi.TV, RasP.iO

sundancer
Posts: 2
Joined: Mon Nov 26, 2012 1:55 pm

Re: Gertboard Software in Python - Released

Mon Nov 26, 2012 8:11 pm

i tried to download your software directly to raspberry pi but i got this error

$ wget http://raspi.tv/wp-content/uploads/2012 ... Python.zip
--2012-11-26 20:08:48-- http://raspi.tv/wp-content/uploads/2012 ... Python.zip
Resolving raspi.tv (raspi.tv)... 94.76.218.131
Connecting to raspi.tv (raspi.tv)|94.76.218.131|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2012-11-26 20:08:48 ERROR 403: Forbidden.

since this software is for raspi only it would be very appropriate if you fix this :)

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Mon Nov 26, 2012 8:57 pm

sundancer wrote:i tried to download your software directly to raspberry pi but i got this error

$ wget http://raspi.tv/wp-content/uploads/2012 ... Python.zip
--2012-11-26 20:08:48-- http://raspi.tv/wp-content/uploads/2012 ... Python.zip
Resolving raspi.tv (raspi.tv)... 94.76.218.131
Connecting to raspi.tv (raspi.tv)|94.76.218.131|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2012-11-26 20:08:48 ERROR 403: Forbidden.

since this software is for raspi only it would be very appropriate if you fix this :)
You're right. I'll look into it. First time it's come up. :D

It might be my wordpress install or the security plugin, but the file is 644, the directory is 755, so I don't know what the problem is. It downloads perfectly well from a browser. I'll dig around and see if wget is excluded somewhere. Might need help from a Linux guru here :D

Try again - I think I've found and fixed it. wget was excluded in a config file :lol:
It works for me on one of my Pis now. Thanks for letting me know
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard Software in Python - Released

Tue Nov 27, 2012 8:30 am

Further to last night's "wget" download issue (now fixed - over-zealous security plugin), I decided to make a download page on RasPi.TV, which is the one I will quote in future for downloading the Python Gertboard test suite. It's easier to remember too...

http://raspi.tv/downloads

It has a link to the original download URL, but if we quote the new URL, it would enable me to relocate the download file should that become necessary/desirable.

So far in November, we've had about 400 downloads and very few reported issues (I'm glad to say :lol: )
Alex Eames RasPi.TV, RasP.iO

sundancer
Posts: 2
Joined: Mon Nov 26, 2012 1:55 pm

Re: Gertboard Software in Python - Released

Tue Nov 27, 2012 7:55 pm

It would be really cool if you could maintain your code on github.com for example so whenever one needs to download your code he/she would always get latest version from github url. That way people can follow and clone your project to fit their specific needs. Even contributions can be made and it would be easy for you to merge them into your project.

Return to “HATs and other add-ons”