Raspberry PI, Python, Scratch and Minecraft

We were recently lucky enough to win 5 Raspberry PIs from the Coderdojo Foundation so figuring out what to do with them was our first challenge. In this post I will detail how I set them up so that the ninjas at the Naas-Sallins dojo could use them to improve their coding skills. They came with Raspbian installed but instructions for getting started with the PI are on the website. Knowing that the ninjas love minecraft I decided to install the special edition for the PI by following the instructions here.

Typically people use python to control the Pi edition of minecraft via its api and there is some great information on how to do that on the Stuff About Code website but after some digging about I found that it would be possible to use the Scratch extension mechanism to allow ninjas access minecraft on a remote Pi from their laptops.

The comprehensive instructions are here and you should have a read of them but in general you run a python program which talks http with scratch and translates them to instructions that the remote Pi running minecraft will understand. You then load a .s2e file which adds the minecraft blocks to scratch.

So once we got everything working with the mcpi-scratch extension it was time to see how well it works.

Using Scratch with the mcpi-scratch extension.

Using Scratch with the mcpi-scratch extension.

This screen shot shows the minecraft blocks which are found in the more blocks section and it also shows a couple of the custom blocks that are going to draw the letter they are named after in minecraft blocks.

When we run this scratch program with our mcpi-scratch helper application and extension running we get the following result.

This just gives a quick idea of what is possible, but the main advantage is that we can have multiple ninjas working on different laptops connecting to a single Pi running minecraft.

This ability to write helper apps and extensions for scratch open up a wide range of possibilities. I used the mcpi-scratch.py code as the basis for a quick test to allow scratch to tweet. I added the code from our earlier python program which uses the tweepy library.

""" messing about with Scratch2 helper app for twitter thanks to mcpi-scratch for the idea"""
import sys, traceback
import argparse, urllib
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
import tweepy
import logging

logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

class GetHandler(BaseHTTPRequestHandler):

    """code to hit twitter"""
    def postToTwitter(self, params):
        tweet = urllib.unquote(params[0])
        api.update_status(status=tweet)
        return ''

    def pollEvents(self, params):
        global pollInc, pollLimit, prevPosStr, posStr
        pollInc += 1
        log.debug('poll: {} {}'.format(pollInc, prevPosStr))
        if (prevPosStr != "") and (pollInc % pollLimit != 0):
            log.debug("don't call mc")
            return prevPosStr

        posStr = ''
        prevPosStr = posStr
        return posStr

    def do_GET(self):
        global api
        cmds = {
            "poll" : self.pollEvents,
            "postToTwitter" : self.postToTwitter,
        }
        parsed_path = urlparse.urlparse(self.path)

        message_parts = []
        message_parts.append('')
        cmdpath = parsed_path[2].split('/')
        print cmdpath
        handler = cmds[cmdpath[1]]
        pollResp = handler(cmdpath[2:])
        log.debug ("pollResp: {0}".format(pollResp))
        message_parts.append(pollResp)
        message = '\r\n'.join(message_parts)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(message)
        return

parser = argparse.ArgumentParser(description='scratch_tweet is a Scratch2 extension helper app to allow Scratch programs to manipulate twitter via tweepy')

#enter the corresponding information for your Twitter application:
CONSUMER_KEY = 'sjkfhskjhfkjshfkjdsh'
CONSUMER_SECRET = 'fkslfjskljfklsjflkjsdldfkjslkfjlksjfkls'
ACCESS_KEY = 'sjdfklsjfkljsklfjlskjfklsjfklsjlfkjslkfjskljfkls'
ACCESS_SECRET = 'skfljslkfjskljfklsjflkjslkfjskljflksjflksjfklj'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

args = parser.parse_args()
log.info(args)

pollInc = 0
pollLimit = 15
prevPosStr = ""

try:
    api = tweepy.API(auth)
except:
    e = sys.exc_info()[0]
    log.exception('cannot connect to twitter')
    traceback.print_exc(file=sys.stdout)
    sys.exit(0)

from BaseHTTPServer import HTTPServer
server = HTTPServer(('localhost', 4715), GetHandler)
log.info('Starting server, use <Ctrl-C> to stop')
server.serve_forever()</pre>
<pre>

The scratch extension associated with this program is as follows this should be saved as a .s2e file

{
 "extensionName": "scratch_tweet Py",
 "extensionPort": 4715,
 "blockSpecs": [

 [" ", "post tweet %s", "postToTwitter", "Tweet"],
 ],

 }
 }

So now you run the python program, launch scratch, hold down shift and click file and then pick import experimental http extension and open the extension above. The post tweet block should now be available to use.
scratchtweet

scratchtweetxx
This just shows what is possible with the http extension in scratch.

Advertisements

Scratch (well almost) and the ISS

This is just a very short post for the Naas-Sallins Coder Dojo about a smaltalk clone of  Scratch called Phratch. As the website says “ It includes two interesting categories: Files and colors. Files allows one to manipulate local and remote files, and Colors to manipulate color features”. So in this quick post we are going to have a look at the files extension.

Phratch Files Section

Phratch Files Section

The first item here is the one we are going to use by pointing the url at the open-notify api from our previous posts. Our very short 8 line program is as follows

Phratch ISS Tracker Code

Phratch ISS Tracker Code

Inside a standard ‘When Green Flag is clicked-Forever loop’

we set the variable mydata to the contents of our api call

we set the variables lat and lon to a particular range of characters in my data. I did this by trial and error and its a bit rough and ready.

we then set x and y to the lat and lon adjusted to match the dimensions of the display window size (360×480) to standard latitude and longitude (180×360)

finally we sleep for a minute

The sprite to represent the ISS position is a simple blue dot with the stage background set to a map of the world. The map projection I used was from a python basemap example with a quick photoshop adjustment to crop it to size. The Equidistant Cylindrical Projection matches the requirements of our display window and our result turns out to be pretty decent

Real-Time ISS Tracker

Real-Time ISS Tracker

So as you can see the ability that phratch has to interact with a file or a url opens up a whole new set of interesting opportunities.

If you are interested you can download phratch here.

Python Vs Scratch II

This is the third post in a series for the Naas-Sallins Coder Dojo about using the Python programming language.  This time we are going to have a look at how we break up a program into small sections which perform particular tasks and how we connect those sections together. We are going to use our lotto example from the last time

""" Lotto number generator for dojo"""
import random
balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]
picks = random.sample(balls,6)
print picks

but we are going to extend it so that we ask our user to enter their 6 numbers and then use our random number selection code to simulate the lotto draw. We will keep going until the draw matches our numbers and then we will tell the user how many goes it took.

So we are going to introduce the concept of a function which is a small chunk of code that we use to do a particular thing.  An equivalent is a block which is available in Scratch 2 and BYOB/SNAP. We start off by defining its name and then we write the code that we want the function to perform. An example makes more sense so amending our code above to put it in a function we get

def draw_random():
picks = random.sample(balls,6)
    return(picks)

now anytime we want to use the code in the function we just call it by using its name draw_random(). For example if we want to print out two sets of numbers our program would look like this

import random
def draw_random():
    """get 6 randoms from the pot"""
    picks = random.sample(balls,6)
    return(picks)

balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]

print draw_random()
print draw_random()

So whats important to get from this is the structure of a function, we give it a name and then all the code that is part of the function is indented and when its finished it returns a value to the code that called the function. In this case it returns our list of 6 numbers to the print statement which displays the value returned. Our output would be something like

[2, 7, 19, 24, 28, 40]
[7, 18, 24, 31, 39, 45]

Now we want to get the user to enter the numbers they want to pick. In scratch we might have something like

scratchask6

where we ask the user for a number and add that number to our list.

In python we can do this as follows

for repeat in range(6):
    answer = raw_input("enter a number between 1 and 45")
    selection.append(int(answer))

Here we are saying that we want to do something 6 times,
that we will make the variable answer equal to whatever the user enters
and then we add the answer to our list called selection (we actually convert answer to an integer number with the int(answer) part so make sure the user entered a number)

Now if we put together what we have and pop the code to get user input into a function we can come up with the following program

""" Lotto number generator 2 for dojo"""
import random

def draw_random():
    """get 6 randoms from the pot"""
    picks = random.sample(balls,6)
    picks.sort()
    return(picks)

def get_selection():
    """get 6 numbers from the user"""
    selection = []
    for i in range(6):
        number = raw_input('Enter a number ')
        selection.append(int(number))
    selection.sort()
    print "You Picked"
    print selection
    return(selection)

balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]
count = 0
myselection = get_selection()
while True:
    draw = draw_random()
    count += 1
    if draw == myselection:
        print "You won on draw"
        print count
        print "The number of years it would have taken for your numbers to come up is"
        print count/104 #--2 draws per week
        break

So lets go through this to see what we are doing. Our first function draw_random is the same as above except for the line picks.sort() which takes our list and sorts it in ascending order. We do this to make it easier to compare out list of random numbers with our list of picked numbers. Our second function get_selection is where we get the 6 numbers from the user, again we sort our list before we return it.

Our main section of code starts when we make our list containing the 45 balls and then make a variable called count which we set to zero. The next line myselection = get_selection() is where we call the get_selection function and myselection gets the value of the list returned. Next we start a loop with the while True: statement. This is similar to

scratchmain

a forever loop in Scratch. We call our draw_random function and it returns a list of 6 random numbers to draw. After that we add one to our variable count and then check if our 6 random numbers are equal to the 6 numbers that the user picked. If they are we tell the user how many times we picked the random numbers before they matched and how many years it would have taken to do that in the real bi-weekly lotto draw. If they didn’t match then we tray again until they do.

The main thing to take away from this is the general structure of our python program. It is worth typing the commands into the python editor as you get the hang of the python syntax. Also if you are running on an older computer be prepared for your program to run for a while. As usual don’t worry if you get stuck or have problems, I will give you a hand at the next dojo session but it is worth giving this or a similar sized program a try so you can get the hang of python and how it works.

Python Vs Scratch

This is the second post in a series for the Naas-Sallins Coder Dojo about using the Python programming language. What I want to talk about here is how a more traditional programming language like Python compares with Scratch, by having a quick look at what they have in common and whats different.

In Scratch we are used to the different coloured sections which contain the various commands.

scratchlibraryThese commands are grouped together by the type of function they perform so we can see that all the commands to move a sprite are in the motion section. In Python we have the concept of libraries which we can import into our program to allow us to do particular things. For example we might import urllib2 to enable us to open a webpage. Again don’t worry about the details of this now, we will cover this as we go along.

Now in Scratch we are used to dragging our various commands for a sprite onto our scripts tab and putting them together to get the desired output. In Python however we need to type each command into our editor and we need to understand that how we layout those commands will change how our program runs. In Python the layout of the code matters more than in most other traditional programming languages since Python uses indentation to group statements together. Let me show you what I mean.

In Scratch we might have scratchifblock so we can see that if our variable x is equal to one we will move ten steps and than the sprite will say “this is an if block in Scratch”. We can see that the orange if-then includes the two other statements.

To do something similar in a Python program we would do something like this

if x ==1:
    currentpos = currentpos + 1
    print "if block in Python"

So what is important here is not the syntax of the Python commands but the fact that the two lines we want to run if the value of x are equal to one must be indented so that Python knows that they are all part of the same condition that needs to be run when the value of x is one. Some other languages enclose all the commands to be run when a condition is true in {} brackets but in Python its how the lines of code are indented that controls this.

Okay so that’s enough talk, how about some real code. Lets make a program to select 6 lotto numbers from 1 to 45 inclusive. If we were doing this in scratch we might do something like

scratchlottoWhich would give us 6 random numbers but wouldn’t check if we had the same number twice.

Now to try this in Python we need to start our editor so we can type in our commands so search for and run pythonwin.exe. It is a free editor that comes with Activestate Python which allows us to access the help documentation,  as well as enter and run our code.

Okay so the first thing we are going to do is set up our pot of 45 balls to choose from, in this case we are going to use a list. A list in Python is pretty much the same as our list from Scratch so we enter the following.

balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]

Here we set out list called balls to all the numbers from 1 to 45 inclusive, note the use of the [ ] square brackets to denote a list. Now we need the code that will pick our 6 balls at random from the 45. Finding the right information can be tricky but the we can search for that information both in the help files and by using Google.

Click on Help-Python Manuals and enter random in the search tab, If you select the first result you should get all the details of the functions built into the random library. As you can see it is a bit complicated and as you gain more experience you will be able to understand this but for the moment trust me when I say (a bit of example code will make it clearer) that the bit we want is

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.”

Step one is to tell Python that we want to use the random library by using the import command

import random

then we need to use the random.sample command in our code so our full program becomes

""" Lotto number generator for dojo"""
import random
balls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]
picks = random.sample(balls,6)
print picks

Here we have

A comment describing the program

Our import statement so we can use the methods associated with random

Our setup of the pot by putting the numbers 1 to 45 in a list called balls

Our selection of a set of picks by getting a sample of 6 items at random from the list called balls

Our print statement which displays the 6 random items from the list.

When you run the code hopefully you should get something like [32, 3, 38, 12, 25, 5]

Again, don’t worry if you didn’t get everything/anything just let me know where you got stuck and we will give you a hand at the next dojo session.