Sending messages between Raspberry Pi using ScratchGPIO

NB Only available in dev version
https://cymplecy.wordpress.com/2013/11/26/scratchgpio-development-testing/

For a while I’ve wanted one computer running a Traffic Light program to easily talk  to a  Pi running as robot vehicle so that the robot can respond easily to the traffic light  sequence.

And extending this to another robots crossing the lights as vehicles or pretend pedestrians!

Link1I’ve added in a basic syntax for this

Link is used to setup connection to any other computer running Scratch with Remote Sensor Connections (RSC) enabled.  (All ScratchGPIO instances running on Pi are and it can be simply enabled on other computers by users)

Then simply, prefix any broadcasts that you want to send to the other computer with Send.  The other computer will receive the broadcast without the Send prefix e.g  Stop or Go

Ideas to enhance/change

Let Link work with just last octet of IP (ie. assume on the same LAN and automatically determine first 3 octets. (easy)

Try and come up with using last 4 digits of serial num like SID uses (hard)

Or something to make it less geeky if possible (hard)

Add a prefix/identifier to the broadcast so that receiving computer can identify/filter/differentiate non-local broadcasts (to prevent advanced Scratchers from taking control of a remote Scratch device without permission and make it possible to have more 1 computer sending messages into a remote computer)

The code is in the dev version (but liable to change) but please have a play and pass on your ideas/thoughts

Please remember my target audience are 7-11 year olds and their teachers 🙂

Simon

 

Change to Ultrasonic behaviour

Dev Version only

My ultrasonic handling code now runs in its own separate thread and defaults to returning an update distance measurement every 1 sec.

This can be change by creating an UltraDelay variable and using

set UltraDelay to xxx

If xxx = 0 then it will run as fast as possible which is currently about 5 times/sec or equivalent to xxx =0.2

 

You can also use

Broadcast UltraDelayxxx

if you prefer

Raspberry Pi SID testing using LAN->Laptop cable

Today I took my 6 Pi into one of my schools and connected them up to the school Win7 Pro laptops using a direct CAT5 cable and powered them from USB charger batteries.

They all performed fine technically and it was a great success (if I say so myself) 🙂

[I could only get 5 to work in the lesson as the last SD card imaging took over and hour instead of the usual 15 mins but that’s a separate issue ]

By connecting them direct to laptops via cat5 cable means they didn’t need WiFi adaptors and therefore no configuring of the laptop was needed.

This is because I used this post to set up the Pi to automatically try and use an IP addressing the same range that Windows machines default to (169.254.0.0 to 169.254.255.255).

The reason for doing it this way is that I only have 1 school left with pupil desktop machines – all the rest use laptops/net-books/IPads so there is no chance of plugin any Pi into any monitors/main network ports.

The Pi are also set up to be SIDs  and therefore for the pupils to get controlling the addon boards plugged into them (mixture of PiDie,Pibrella,PiStep and Ladderboards) all they had to do was run the Handshake.py program (which I’ve wrapped up into a windows .exe file), saved it to a network folder and stuck a shortcut toit on the desktop.

Once they’d handshaked with SID running on their Pi, They simply loaded their normal Windows Scratch 1.4 and loaded in a basic script to switch all the LEDs off and On to test that everythgin was working.

From the start of a 1 hours lesson (and considering this was the first time we’d done it) 3 out of the 5 groups (only a 15 intake class luckily) had got a full Traffic Light –  Green/Amber/Red/Red and Amber/Green sequence going.

Lessons Learnt

All the AddOns responded to LED1..n On/Off commands except for the PiStep (as its normally used as a stepper) and that group needed to use Pin11,12 and 13 and it meant separate instructions for them so better to make sure all boards can respond to same commands of make sure individual help-sheets for each board are printed out.

Once group didn’t push their CAT5 cable in until it clicked (obviously as a tech I knew this and didn’t bother telling them – so I’ll make sure that’s in the instructions next time)

Also, I’m going to make a tick sheet of plug in sequence just to make sure its all done in a good order.

Laptop on and logged in, cat 4 plugged into laptop,cat 5 into Pi, power lead into Pi (this is the fiddly bit they found) and then power lead into battery supply.

 

LMC Emulator in Scratch

Updated 14May14 to combine instruction and data in one array value

There is a basic microprocessor concept called the LMC http://www.yorku.ca/sychen/research/LMC/ which introduces the concepts of basic assembly language programming using only 10 instructions.

I’ve made a Scratch version that is a working copy of the simulator.

The way to use it is to enter your instructions into the list called Memory and then press the run button.

The main difference between this version and the original LMC is in he entering of pure data.  To just enter a number into memory you would use:

DAT23

The LMC computer will then ignore the DAT instruction and move the program counter to the next instruction

The INP instruction will pause and let you enter a value into accumulator.

The OUT instruction will cause the cat to say the value of the accumulator

All other LMC instructions (LDA,STA,ADD,SUB,BRA,BRZ,BRP and HLT ) should work the same as the original simulator.

The simulator is available here

Any feedback greatly appreciated 🙂

 

Version 5 updated to 5.1.26

New features

Support for Dr Simon Monk Raspirobotboard 2

Support for 4Tronix Pizazz Robot Kit

Input handling now responds to short lived events – very useful for Line Following Robots and Burglar Alarms etc

Camera Support courtesy of Matt Venn

Broadcast photo

This will take a photo and stick it in /home/pi/photos using Raspberry Pi camera

Basic Minecraft support

Broadcast MinecraftStart to connect to running Minecraft program

set MineX setMineY and setMineZ followed my MinecraftMove should move you to a the co-ordinates defined

or you can do Broadcast MineCraftCamMove to move the camera to those co-ords instead

Thats all it does so far

Other changes,

ADAFruit and 4Tronix I2C PWM and analogue input boards bow supported at a ll times wit ha ll add-ons

ScratchGPIO 5.1.15

OLD VERSION RESTORED AS THIS ONE DIDN’T WORK!!!!!

This version made it to the stable version tonight.

New features

broadcast photo

This will take a photo and stick it in /home/pi/photos using Raspberry Pi camera

broadcast MinecraftStart to connect to running Minecraft program

set MineX setMineY and setMineZ followed my MinecraftMove should move you to a the co-ordinates defined

or you can do Broadcast MineCraftCamMove to move the camera to thos co-ords instead

Thats all it does so far

Other changes,

ADAFruit and 4Tronix I2C PWM and analogue input boards bow supported at a ll times wit ha ll add-ons

 

Uniqueness of Rasperry Pi Serial Numbers

My SID project needs the user to type in the last 4 digits of a PI’s serial num in order to connect remotely.

I chose 4 as I thought (intuitively) that it was enough digits to almost certainly be unique in a class of 15 Pi.

Of course,  if it takes one year 7 child x attempts in t time to get it right then imagine the number of attempts a class of 30 children working in “teams”  of two takes to do this.  then make it 2 digits – then 3 – then 4.

…….. so 30 mins later, all pupils have connected their laptops to their Pi 🙂

OK – lets try and me more scientific and find the probability of 1/2/3 or 4 digits letting us uniquely identify a Pi.

Turns outs to be one of those great maths problems called the “birthday problem” and that the probability of any 2 Pi out of 15 having same last digits of serial number is (in %) where r = num of Pi and d = number of hex digits is given by

a = (16^d)!
b = (16^d)^r)
c = ((16^d)-r)!

P(%) = 100*(1 – a/(b * c)

Substituting r = 15 and d =2 gives 34%

Substituting r = 15 and d =3 gives 2.5%

Substituting r = 15 and d = 4 gives 0.2%

So maybe we could get away with 3 digits but with say 30 Pi the numbers change to

Substituting r = 30 and d = 3 gives 10%

Substituting r = 30 and d = 4 gives 0.7%

So in a class of 30 you’d have to use 4 digits and so for compatibility for rich schools  – I’ve stuck with 4 digits 🙂

Just for an exercise, I wondered if I converted the serial num into Base 30 (using 0,1,2….x,y,z) for digits I get this

Substituting r =15 and d =2 gives 11%

Substituting r =15 and d =3 gives 0.4%

and even for a class of 30

Substituting r =30 and d =2 gives 38%

Substituting r =30 and d =3 gives 1.6%

So I’d say we could get away with three Base 30 digits.

But since its a lot of trouble to convetr from Base 16 to Base 30, I’m going to stick with 4 hex digits and stick them on the side of each Pi 🙂