The Raspberry Pi is unquestionably the most versatile piece of tech ever devised. This bare-bones, credit-card sized computer serves as a tool for teaching the fundamentals of computer science. Engineers, tinkerers and techies have used it to make robots, games consoles, art installations - Raspberry Pi have even gone into space! But this time we use Raspberry Pi back to our initial cause, to bring more useful application to STEAM education.
Raspbian is a Debian-based computer operating system for Raspberry Pi, the newest version has already integrated the Scratch2 offline version, so we can easily use graphical programming in Rasbian. In this project we used Raspberry Pi3, and install the newest image of Raspbian. For installing the Rasbian tutorial, you can refer to this https://www.raspberrypi.org/downloads/raspbian/ , it has detailed steps.
Interesting, right? But we are more concern how it integrate with hardware. Press the "More blocks" catalog then "Add extension", in the popup modal select "Pi GPIO extension" and you may find two more blocks shown in the toolbox. These two blocks may control the IOs on the external port.
I know you are expecting more as I do. We can't make a fancy robot merely based on GPIOs, we need a cerebel to perform all the actions or reading sensor values while RaspberryPi act as the brain.
Rosbot mainboard could be direct stack on your PI, and communicate via the serial port. Such design to make Raspberry Pi the brain and Rosbot mainboard the cerebrum, which only responsible for movement executive or drive and collect external sensors.
We assume you installed or upgrade to the lastest Raspbian and we use VNC to ease the remote access to PI, you can refer to https://www.raspberrypi.org/documentation/remote-access/vnc/ for how to set-up VNC server on Raspberry Pi.
The rosbot mainboard may stack directly on raspberry pi while communicate via the serial port, aka ttyAMA0 for raspberry pi 2 or ttyS0 for version 3. We need to enable serial port for communication access and disable kernel console output via serial.
- sudo raspi-config
Navigate to Interfacing Options and select serial port. Disable Login on serial then enable the hardware access to serial port.
- curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
- sudo apt-get install -y nodejs
- sudo apt-get install -y build-essential
Then we need install gyp environment to compile native module.
- sudo npm install -g node-gyp
- sudo npm install -g node-pre-gyp
For now we have nodejs installed, check if installed successful.
- node -v
- npm -v
Here is the tricky part, It take me a whole night to figure out how the scratch2 binding works. The scratch2 on raspberrypi is a modified version from scratchX and using electron to provide the runtime context. You may find the scratch2 in the usr library.
As we metioned the scrach2 is using a electron context, so we have to build native modules for electron not the nodejs we have just installed. But meanwhile we need nodejs to handle all the building procedure. Go to scratch2 app directory
- cd /usr/lib/scratch2
We have to add an .npmrc file to let gyp know that we are building for a specified target.
- nano .npmrc
runtime = electron target = 1.6.2 target_arch =arm disturl = http://atom.io/download/atom-shell
Then we need to install serialport module
- sudo npm install serialport
If everything is fine you may find it is compiling from source code, and finally got a serialport module in folder ''node_modules\serialport\build". This moudle could be used for scratch2 extension.
Enter Scratch2 panel, long press 'Shift' and click 'File' in the same time, you will see 'import experimental extension' show up.
Enter your extension's name and file path, click load to add the extension to your Scratch2.
Then goes to Scripts -> More Blocks, you will see the blocks you wrote before.
Drag various kinds of blocks to the area for programming, click the blocks and the electronic modules will turn on.
Try to write extensions for different external hardware. In theory, you may port scratch-x extensions to scratch2 the same way.