TLDR, see it in action:

Scratch Robot on Github.

The goal of this proof of concept was to play with blockly.

This library allows to let users create a program using scratch language. This way, you don’t need to know any programming language, but plug logic blocks together.

Blockly allows to define and program our own blocks.

For example, here is how I defined “forward” block:

Blockly.defineBlocksWithJsonArray([
  {
    "type": "move_forward",
    "message0": "avance",
    "previousStatement": null,
    "nextStatement": null,
    "colour": "%{BKY_PROCEDURES_HUE}",
    "tooltip": "%{BKY_ROBOT_FORWARD_TOOLTIP}"
  }
]);
Forward block
Generated block

And here is how I program it:

Blockly.JavaScript['move_forward'] = function (block) {
  return 'Robot_forward();\n';
};

It is a “generator”, and you can also define one for python, php…

I developed 3 blocks: “forward”, “turn left/right”, “wait N seconds”. Each block return generated Javascript that send and an order to the Raspberry Pi robot.

I also used Js-Interpreter to not run all the generated Javascript directly, but halt the generated Javascript, and wait the robot to move.

Finally, for the robot, I re-used an API I already worked on for another project: Darkmira/drop-robotapi. The readme contains the wiring schema to mount your own robot.

Scratch Robot on Github.

Licence Creative Commons

Julien Maulny

Leave a comment