Accessing GPIO from Scratch


10 posts
by DavidMS » Tue Jun 19, 2012 10:57 pm
Can anyone tell me how difficult it would be to access the GPIO from Scratch ? I am basically looking at what it what take to create some basic robotic primary school activities for primary school age children. I am fine with the hardware bit but do not know anything about the inside workings of Scratch
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by michpern » Fri Jun 22, 2012 1:16 pm
In theory, this should possible via Python and this is exactly what I am trying to do.

I have a solution in two halves but I can't join them together yet because of an issue with Scratch on the Pi.

a) It is possible to access the GPIO from python code. I set up a basic LED circuit and wrote a couple of python scripts (one to turn the LED on and one to turn it off). This is working fine.

b) It is possible for a Python script to listen for (and send out) Scratch broadcast messages. I wrote a two-'block' Scratch program to test this - on Windows. I had a python script listening for a message and just output some text when it got the message.

Now the fun part. In order for this to all work, the "remote sensing" feature of Scratch needs to be turned on. [ To do this, select the Sensing blocks and then right-click on one of the two "sensor" blocks (normally at the bottom of the panel. One of the menu options is "enable remote sensor connections"] Unfortunately this gives an error "a primitive has failed".

I'm trying to track this down but I'm not a Smalltalk programmer. I'll report back if I find a solution.

Of course, there may be other ways to do this in Scratch

Pete
Posts: 2
Joined: Wed Dec 28, 2011 2:10 pm
by JeremyF » Fri Jun 22, 2012 1:47 pm
michpern wrote:In theory, this should possible via Python and this is exactly what I am trying to do.

I have a solution in two halves but I can't join them together yet because of an issue with Scratch on the Pi.

a) It is possible to access the GPIO from python code. I set up a basic LED circuit and wrote a couple of python scripts (one to turn the LED on and one to turn it off). This is working fine.

b) It is possible for a Python script to listen for (and send out) Scratch broadcast messages. I wrote a two-'block' Scratch program to test this - on Windows. I had a python script listening for a message and just output some text when it got the message.

Now the fun part. In order for this to all work, the "remote sensing" feature of Scratch needs to be turned on. [ To do this, select the Sensing blocks and then right-click on one of the two "sensor" blocks (normally at the bottom of the panel. One of the menu options is "enable remote sensor connections"] Unfortunately this gives an error "a primitive has failed".

I'm trying to track this down but I'm not a Smalltalk programmer. I'll report back if I find a solution.

Of course, there may be other ways to do this in Scratch

Pete

It's been a long time but I believe enable remote sensing only works with the PicoBoard...but maybe my memory fails me....


Anyway, here's what the PicoBoard is.
http://www.picocricket.com/picoboard.html
{sig} Setup: Original version Raspberry Pi (B, rev1, 256MB), Dell 2001FP monitor (1600x1200), 8GB Class 4 SD Card with Raspbian and XBMC, DD-WRT wireless bridge
Posts: 516
Joined: Wed Jan 25, 2012 9:06 pm
by DavidMS » Mon Jun 25, 2012 7:17 pm
I found this yesterday, I have not had a chance to look in detail at it but would seem to indicate variables can be passed between Scratch and Python which should be enough to use at the basis for setting the GPIO ports

http://wiki.scratch.mit.edu/wiki/Communicating_to_Scratch_via_Python_with_a_GUI
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by DavidMS » Mon Jun 25, 2012 8:46 pm
Ok managed to spend some time on this, after a bit of trial and error I now have Python communicating with Scratch. You need to make a minor mod to Scratch - see URL below.

http://scratch.mit.edu/forums/viewtopic.php?id=32046

Some points to note if try this.

- You cannot save the 'Scratch-UI-Panes' changes in the way described as right clicking the mouse seems to bring up an a LXDE shortcut mode, use alt s insted. Also I found the Rpi crashed when I tried to save a new Scratch image. Until this is fixed this means you have to make the changes each time you run Scratch.
- I tried it on a PC first to check it out.
- as sound in Scratch does not seem to work with the new Beta image I just set it up so the cat said 'hello' on screen when it received a trigger from the Python session.

What I have not been able to do is to get it working the other way the original URL suggested this should also work but I am clearly missing something, anyway will try and play more tomorrow night.
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by michpern » Wed Jun 27, 2012 6:12 pm
Interesting link.

I have managed to get this working in the way I described previously, with no changes to Scratch. I did have to use the new Wheezy Debian distribution. This has a later version of Scratch and the 'Enable Remote Sensors' functionality now works. (Enabling remote sensors just makes Scratch a server - when a 'broadcast' block is executed it publishes the message to any process listening)

On the Python side, I used a framework called Scratra http://scratch.mit.edu/forums/viewtopic.php?id=96036 and the RPi.GPIO library.

This all works well, and a student wrote a simple traffic-light simulator in Scratch (with three leds connected to GPIO pins). He then adapted it to be a Pelican crossing, with the button and red and green men as Scratch sprites.

There does seem to be one limitation in that there needs to be a short delay between sending broadcast messages.
Posts: 2
Joined: Wed Dec 28, 2011 2:10 pm
by DavidMS » Sat Jun 30, 2012 11:10 am
Ok, I have sort of got something working - well I can get python to say 'Hello world' when I broadcast hi from scratch, but I need to keep hitting return in the IDLE Python shell to see it. I guess I am being let down by my Python knowledge here.

is Scratra being called in some way - I run is and nothing seems to happen but then running

from scratra import *

@broadcast('hi')
def hi(scratch):
print 'Hello, world!'

run()

seems to sort of work accepting I need to hit 'return' to see anything ?

A some simple explanation of the Python side of things would be appreciated
Posts: 65
Joined: Fri May 25, 2012 8:48 pm
by simplesi » Sat Jul 14, 2012 4:07 pm
michpern wrote:I
I have managed to get this working in the way I described previously, with no changes to Scratch.

Any chance of an expanded explanation of the steps you used to get Scratch to talk to the GPIO please :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2046
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by simplesi » Fri Aug 03, 2012 11:28 pm
I've been playing with this (I want to use it in primary schools)

I've posted my experiences so far on a blog
http://cymplecy.wordpress.com/2012/07/2 ... pberry_pi/

is the main one but there are others and some youtube vids of my efforts
http://www.youtube.com/user/siwalters?feature=guide

hope this helps
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2046
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by DavidMS » Tue Aug 14, 2012 9:40 pm
simplesi wrote:I've been playing with this (I want to use it in primary schools)

I've posted my experiences so far on a blog
http://cymplecy.wordpress.com/2012/07/2 ... pberry_pi/

is the main one but there are others and some youtube vids of my efforts
http://www.youtube.com/user/siwalters?feature=guide

hope this helps
Simon


Thanks for posting this Simon, looking forward to trying it out myself at the weekend
Posts: 65
Joined: Fri May 25, 2012 8:48 pm