bjpirt
Posts: 7
Joined: Thu May 01, 2014 10:50 am

Scratch and robotics

Thu May 01, 2014 10:54 am

Hello,
Thought you might be interested to see some work I did yesterday in getting Scratch running on a Pi talking to a WiFi kids robotics kit I've been developing:

http://mirobot.io/blog/2014/04/29/raspb ... d-mirobot/

It uses the remote sensor connection, though I wish there was a better way. I was curious about any issues other users have had with this - I was finding that I needed some delays in the scratch code otherwise it wasn't picking up messages that the robot was broadcasting back to Scratch. I was also wondering about making it work with BYOB / Snap! because it looks like that might be more flexible with regards to external communication. Any thoughts.
Cheers,
Ben

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: Scratch and robotics

Thu May 01, 2014 10:55 pm

Hi Ben,

From experiments with scratchpy on the Raspberry Pi, the maximum stable sensor update rate is about 2Hz. This was achieved by using a trigger message from the Python side, to minimise the CPU usage in Scratch. The Python code and examples used for the BrickPi can be found in Issue 23 of the MagPi.

Best regards,

Will

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Fri May 02, 2014 1:41 pm

Hi Ben,
I like your "ready" variable concept. - very handy for stepper motors :)

One way to tidy it up would be for your script to keep its own record of ready value - set it 0 when it receives an appropriate broadcast and then at the right time - set it to 1 and use sensor-update msg type (sensor-update "ready" 1) instead of broadcast to send the value back to Scratch.

That way you could throw some blocks away (and the need for the wait 0.1) and be left with

Code: Select all

repeat 5
   broadcast mirobot-forward-100
   say forward
   wait until ready sensor value = 1
   ...
There's a quite a bit of exisiting Scratch/Arduino setups about and at least one Scratch 2 one https://github.com/MrYsLab/s2a_fm using Firmata running on the Arduino

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Fri May 02, 2014 1:56 pm

Just looked at the hardware design :)

I'll need to checkout the concept of WiFi module into DIY Arduino :)

Tim Moore has run his Shrimpbot using Bluetooth modules

http://www.thingiverse.com/thing:36062

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Fri May 02, 2014 2:02 pm

What WiFi module is it BTW?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

bjpirt
Posts: 7
Joined: Thu May 01, 2014 10:50 am

Re: Scratch and robotics

Fri May 02, 2014 6:01 pm

@williamhbell the 2hz limit is good to know, that explains what I was seeing

@simplesi that's much better just updating the ready variable from the node script - cuts out quite a few blocks. Top tip!

I think it's a real shame that there's no real way of integrating things with custom blocks in 1.4. I get the whole compatibility thing, but the ability for remote devices to be able to register new blocks would be fantastic. I've been playing about with BYOB / Snap! and that looks like it will be easier to modify, but it's also quite a bit more complex. Shame there's not a "Scratch Mode" where most of the more complex functions are hidden.

Cheers,
Ben

bjpirt
Posts: 7
Joined: Thu May 01, 2014 10:50 am

Re: Scratch and robotics

Fri May 02, 2014 6:03 pm

simplesi wrote:What WiFi module is it BTW?
It's made by a Chinese company called USR. It's pretty neat and quite low cost. More details here:

https://www.kickstarter.com/projects/bj ... nt-6655117

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Sat May 03, 2014 4:56 pm

Can't get anything via that KS link - have you another one please?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

bjpirt
Posts: 7
Joined: Thu May 01, 2014 10:50 am

Re: Scratch and robotics

Mon May 05, 2014 11:37 am

Weird, just worked for me now - nevermind. Here's a copy paste of that comment:
The WiFi module is a neat little device. Aside from being low cost, it's also got a few tricks up its sleeve:
- Has dual networks so you can join its access point and it can join yours
- Socket to serial converter
- Very small
- Room on-board for your own web pages
It's also quite easy to embed in designs because it's got 2mm header pins unlike the previous version which needed surface mounting.
You can find more details here:
http://en.usr.cn/Tiny-size-low-power-ua ... ifi-module
The only down-side is that the manual is sometimes written ahead of actual product functionality. Apart from that the company have been very supportive. Give me a shout if you have any other questions.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Thu May 08, 2014 8:19 am

I think it's a real shame that there's no real way of integrating things with custom blocks in 1.4.
1.4 file format is fixed. Compatibility means sticking with it.

It can be forked into another product not called Scratch (many have done this - S4A in Italy, Open University have done it with their Sense version)

But you lose the branding/traction of it being called Scratch.

Once Tim Rowledge has got his speeded up version into a fully releasable state, then the descsion time will come as to whether to fork away from 1.4 file format and have a derivative that knows about I/O

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

bjpirt
Posts: 7
Joined: Thu May 01, 2014 10:50 am

Re: Scratch and robotics

Thu May 08, 2014 11:45 am

I get it, but it's still a shame external integrations are so clunky :-)

Is the 1.x branch of scratch still being developed? Or is it all 2.x now? I just wish 2.x wasn't Flash. There's no easy solution, it seems.

It's got me thinking about how you could achieve what we want to without losing compatibility. Obviously defining custom blocks is going to break compatibility, but I wonder if it could be done in a slightly different way using more basic building blocks.

If you could do something similar to the remote sensor connection functionality that would block on a response (let's say an http request), then it would be quite easy to hook this in to a sketch so that it was waiting on an external action to complete (this could be turning a pin on or off or controlling a robot). I imagine that being able to define subroutines / functions has probably already been decided against in core scratch (otherwise I guess they'd be there already - Snap! feels a bit too complex), but they would then make it quite easy to define your own functions for controlling things, which you could then use like a library within a project.

Ah well, maybe I'm just dreaming :-)

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Thu May 08, 2014 12:51 pm

1.4 file format is fixed - the program itself is being improved to give better performance on the Pi but 100% same file format/behaviour.

Snap! is the most actively developed alternative but that doesn't run on Pi so not really something worth discussing here.

BYOB is THE cross-platform tool but suffers from not being enhanced to the same degree as Scratch to run on the Pi so its slow and unlikely to receive any effort to develop it from anyone.

But its fine on Win (and I assume Mac/Linux)

It will read 1.4 files and pre-programmed named functions can be setup to do hide the broadcasts from the user and allow you to easily write blocking requests (I'll test that out just to make sure it can be done)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

ghp
Posts: 1408
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch and robotics

Fri May 09, 2014 1:32 pm

Hello, I thought a while about this coding problem. Problems like this where a 'success feedback' is needed typically need a 'hardware abstraction layer' HAL, handling communication to external device and hiding communication details, timeouts and more.
In scratch 1.4, the only way I know to implement is by the use of synchronous "broadcast and wait".
robot-example.gif
robot-example.gif (14.45 KiB) Viewed 3242 times
The sample code uses 'robot_command' and 'robot_parameter' to define what to execute, and broadcast 'robot_execute' to call the abstraction layer. This is like calling a method with parameters. On top of screenshot, you see two examples of how this could look like. You will notice that these statements are abstract, there is no dependency of what type of hardware is connected. The variables FORWARD and RIGHT are helping to avoid typos in this case.

The method aka HAL is here in same object, but if the 'robot-'-variables are global, this script could live in a different object, too.

The abstraction layer is called by the robot-execute broadcast synchronously. It sets the command values for the external device, and sends a mirobot-execute. This is needed, as remote sensor network will not send out same value twice.
The remaining code is to handle timeouts and some sort of status to help with analysis. This script needs to be written along the capabilities of external 'remote sensor-network-client. I assumed here that the client code is able to receive command parameters by variables. Also the ready variable is set directly from the robot handling code.

Regards, Gerhard

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Scratch and robotics

Fri May 09, 2014 2:13 pm

I had a quick play with BYOB (on a PC) and its not too hard to create a blocking type function
https://www.dropbox.com/s/041pv2w8kqoku ... .01.02.png

So I think any complex action could be wrapped up in a nice callable block function that the users would'nt normally see :)

Your original code would come down to this for the user

repeat 5
say forward
MiroBot Forward 100
...

Simon

@gerhard - I like your native Scratch workaround - I'll have to try it out with pupils to see if they can easily use it :)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: Scratch and robotics

Sat May 17, 2014 2:36 pm

ghp wrote:Hello, I thought a while about this coding problem. Problems like this where a 'success feedback' is needed typically need a 'hardware abstraction layer' HAL, handling communication to external device and hiding communication details, timeouts and more.
In scratch 1.4, the only way I know to implement is by the use of synchronous "broadcast and wait".
Hi Gerhard,

Another way of solving this is to start an external DAQ loop that sends messages back to Scratch. This can be used to trigger Scratch functions with minimal overhead. The second example in the BrickPi article in Issue 23 of the MagPi uses this idea, to reduce the reaction time down to a fraction of a second.

Regards,

Will

Return to “Scratch”