MIT's Scratch is brilliant. I think it is mainly for kids to learn to program. The GUI is pretty cool and I've actually used it on an EDX course on game design. You can even add you own extensions like the Lego WeDo and the PicoBoard. So,... maybe writing one for the Raspberry PI might be interesting. It has been Githubed under the permissive MIT license, so you might just want to go there and rip your own. MrYsLabs has one as well here.
This project replicates Rover to some extent. The PiFace board was used instead of using the Raspberry Pi's IO directly. It does however copy most of the hardware. You can probably use this and the Rover project and not worry about the PI Face board.
Originally I got this working with a Gertbot board, but I blew it up, so I had to use the L298N board instead. I've included the source to the Gertbot board but it has been commented out. Anyway just as well since I needed the Uart for remote communications using Bluetooth. I don't have a Official Raspberry PI Wifi dongle, so I had to make do with the Bluetooth.
Really this project is about extending onto Scratch rather than building a wheeled robot, so this this project focuses on that.
To do this, the latest Insider Preview of Windows IoT 10 is required. Yay, Windows IoT has Uart support.
How Scratch Extensions work
Scratch communicates to the outside world using HTTP GETs to a arbitrary port on localhost (loop back). You give it a JSON file that specifies a port and a description of the blocks.
In this case, there are 5 blocks. Two to read and write digital IO, one to set the motor direction, one to set the motor speed and the last to get the ultrasonic distance.
For the read blocks, Scratch sets a poll request about 30 times per second. It is expecting the state to be returned. So the PI is sending back the state of the I/O pins and the ultrasonic distance.
For the write blocks (such as motor control), it also sends a GET with the motor state.
Since Scratch communicates to localhost only, a port forwarder app was created that ran on the same PC as Scratch. The port forwarder either forwards via sockets (over wifi) or Bluetooth serial profile to the Raspberry PI. The sockets was tested using Ethernet as no Wifi was available. Interestingly this port forwarder app can not be written as a Universal App/Store App since Microsoft explicitly prohibits communication via localhost (there is a reference url in the code).
Using GETs makes the whole thing stateless which means you can bring the Raspberry PI on and offline and everything is cool. You could even has multiple Scratches going on, each trying to get control of the robot - that would be an interesting two player game.
Potentially you could port forward over the internet, but for this to work, you would need to encrypt traffic to get around proxy funnies (they cache stuff).
Scratch Program
The Scratch program is very simple, turn 90 degrees, take a look. If there is nothing there, move forward 1 second at full power. Go back to turning 90 degrees again.
Very simple, but surprisingly amusing to watch.
Raspberry PI
In the code, you can see how to communicate to the Uart, SPI and TCP IP sockets. The code has been documented so, it should not amazing hard to follow.
With the Uart, you can see that a separate task was spawned just to look for incoming traffic from Scratch. This is different to sockets where you just get an event to say a socket has opened.
The ultrasound sensor is triggered every 500 milliseconds. Windows IoT only has a resolution of a millisecond, so the ranges that are returned are multiples of 17cm. Good enough really for my demo program.
One of the relays was used on the PiFace board to control the power to the motor L298N driver. This relay is normally off and is only powered on once everything is set up. This is because the PiFace uses open collector outputs that are pulled high to 5V when everything is off.
A connector was uses as a make do power block. Half the pins are connected together and the whole thing hot glue gunned down.
The black box that supplies power to the Raspberry Pi is one of those harden ones you get for providing USB power in cars. The are quite cheap and seem to protect the Raspberry PI. Is has a standard USB connector at one so it makes it easy to power the Raspberry PI.
Comments
Please log in or sign up to comment.