Monday, 27 February 2017

Pimoroni Flotilla with Scratch 1.4 on the Raspberry Pi


We've been hoping that Pimoroni would add Scratch support to their fabulous Flotilla electronics range for the Raspberry Pi. We love the hardware but none of the current programming options really appeal to my kids aged 8 and 10 who really want to code in Scratch. They are just the right are for using the electronics so wanted to try and find an option that works for them.

I decided to see what I could do to get Scratch 1.4 on the Pi talking to Flotilla.



Pimoroni Flotilla for Raspberry Pi


If you want to know more about Flotilla see:
Available from Pimoroni (UK)


Approach 

It turned out to be pretty easy to connect Flotilla and Scratch 1.4 on the Raspberry Pi using scratchpy.

Scratchpy supports two-way communication between Scratch 1.4 and Python. Flotilla has a Python library.

I used this approach to:
  • map Scratch 'all sprites' variables to Flotilla outputs - e.g. set the number1 variable to 1000 and 1000 shows on the Number display. 
  • map Scratch sensor values to Flotilla inputs - e.g. set the Scratch 'dial1' sensor to the value reported from the Dial.  

For the most part the values are just passed to the Flotilla Python api as this will make it easier for kids to move from Scratch to the Python api.

It would also be possible to add a simpler interface which scales sensors to 0-100 as commonly used in Scratch or maps to the size of the stage or to directions, etc.


Flotilla Scratch 1.4 Setup

This is actually very straightforward, you just need to do a small amount of setup: 
  1. Install Flotilla Python if you haven't already.
  2. Open a terminal window
  3. Stop the Flotilla daemon if you have installed Flotilla Rockpool (required to use Python) with:
    sudo service flotillad stop
  4. Install ScratchPy using 'sudo pip install scratchpy'
  5. Go to flotillascratch on github. Download or clone flotillascratch.py and the Flotilla Scratch Demo projects. 
  6. Open flotillascratch.py with Programming->Python2 and then run it by pressing F5. (This means you can easily use F5 to restart it if you change the modules connected to the dock.)
  7. Leave flotillascratch.py running and open Programming->Scratch 1.4
  8. Click 'Sensing' then right-click on 'sensor value' and choose 'enable remote sensor connections'
  9. Whenever you change the modules connected to the dock, switch back to flotillascratch.py in IDLE and tap F5 to restart it. You don't need to restart Scratch. (I can probably find a more elegant way to do this automatically in future.)

Trying the Demos

You can download the Flotilla Scratch Demos from github

Each demo has a stage backdrop showing a photo of the modules used in that demo. If you don't get a message saying 'remote sensor connections enabled' when you open the project then go to Sensing and enable it. 

Connect the required modules to the dock and then run or restart flotillascratch.py.  

Modules are named by their type and number, e.g. dial1, to support multiple of the same type of module.

For each input module you have connected flotillascratch will create one or more sensors available in Scratch via 'sensor value'.

For each output module you will see one or more global variables ('for all sprites') starting with the name and number of the module, e.g. motor1.

Dial and Slider

The Dial and Slider modules are quite similar - they report a value between 0 and 1023.

Turn the dial to rotate the sprite and slide the slider to make it grow and shrink. 

Light 

The Light demo uses two Light sensors as there are two in the Mega Treasure Chest (the demo can be adapted if you only have one.)

Cover up both light sensors and Scratch will say 'It's dark.' You might need to adjust the thresholds. 

Try shining a torch on the Light modules or pointing them towards a light source to see the values change.

Weather

The Weather module reports the temperature and pressure. 

Try touching the temperature sensor to increase the temperature and then letting go and watching the temperature drop. 



Joystick

Use the Joystick to move a sprite around the screen.

The joystick reports x and y values. The demo maps these to up, down, left and right to move a sprite around the stage.

You can map differently if you want to hold the joystick in a different orientation. 

Motion

Access the x, y and z values of the motion sensor.

In the demo the x and y values are used to move a sprite around the stage and the z value is used to flip the sprite upside down.

There's lots of scope for doing interesting things with this module.


 Colour Sensing

Use the colour sensor to detect colours.

The red, green and blue values reported by the sensor are returned. The demo does a simple mapping to detect red, green and blue objects.

Place a brightly coloured red, green or blue object under the sensor and the sprite will change colour and tell you the colour.

There's lots of scope for doing more fancy stuff.

Number and Matrix

Number uses a global variable called 'number1' which can contain a number or up to four characters of text.

Matrix uses a global variable called 'matrix1' which contains 8 digits used to specify a bar graph. (I'll add more general support when I think of a good way to do it in Scratch.)

Try changing the number1 variable in Scratch. You can do this using the reporter blog on the stage or programmatically. The number is displayed on the Number module.

Motor

The Motor demo supports two motors as there are two in the Mega Treasure chest.

You use variables motor1 and motor2 to control the motors.

A postive value sends a motor forward at the given speed (up to 63) and a negative value sends a motor in reverse. Set a motor to speed zero to stop it.

You can press the space bar to stop both motors. 

Rainbow

The Rainbow display has 5 RGB pixels. Setting Scratch reporters on the stage to sliders works well for changing colours. 

At the moment you can change the amount of red, green and blue and the brightness, but only for all pixels at once. I'll improve this when I think of a good way to control individual pixels. 

Write Your Own Project

Now you can write your own project using Flotilla and Scratch. You might find it easier to start from one of the demo projects and use 'save as' to create a starter project. 

If you create a new project you'll need to click on 'Sensing' and then right-click on the 'sensor value' block and choose 'enable remote sensor connections'. 

You'll also need to add global variables for the output modules that you want to work with:
  • Number: number1
  • Rainbow: rainbow1.brightness, rainbow1.red, rainbow1.green, rainbow1.blue
  • Matrix: matrix1 (a string of 8 numbers to create a bar graph)
Sensor reporters for the sensors you have connected to the Flotilla Dock will appear once the flotillascratch.py is running. 

  • Slider: slider1
  • Dial: dial1
  • Touch: touch1.one, touch1.two, touch1.three, touch1.four
  • Joystick: joystick1.x, joystick1.y
  • Colour: colour1.red, colour1.green, colour1.blue, colour1.clear
  • Light: light1, light2
  • Motion: motion1.x, motion1.y, motion1.z
  • Motor: motor1, motor2
  • Weather: weather1.pressure, weather1.temperature

Corsairs of Umbar Interactive Adventure

I asked my 10 year old to come up with an example project to demonstrate Scratch working with Flotilla. Having previously not done much with Flotilla other that try the Cookbook examples and work with me on our realworld Minecraft Furnace project, he was inspired!

I suggested something pirate or ocean themed to fit with Flotilla. He's a big Tolkien fan so he came up with an interactive adventure featuring the Corsairs of Umbar. 

The goal of the adventure is to reach Mordor with men left in your army and having beaten several enemy rulers. 

This project uses the following Flotilla modules: 
  • Number: This displays the current number of men in your army
  • Touch: Use this to make a decision when options are presented
  • Rainbow: Status information on your current location or degree or peril
  • Joystick: 1-1 combat
  • Light: wait for dark

Here's a demo video:



Verdict

My kids have really enjoyed using Flotilla with Scratch. Both of them have created Scratch projects that use Flotilla and really enjoyed it and found it straightforward compared to the other options available.

They've enjoyed using the LEGO adapter for the motors to  create moving LEGO projects that can be programmed. We added a hand strap to the Motion module so you can play Scratch games using motion control by moving your hand. They've just used the Flotilla modules in a really natural way.

Of course it would be great to have browser-based support for Flotilla in Scratch 2, but when you've got everything connected to the Pi it's pretty natural to use Scratch 1.4.




More from Tech Age Kids:

Comments:

Post a Comment