I was looking for a good project for my kids to play with robotics where they could use their preferred way of coding, Scratch. If you’re not familiar with Scratch, it’s a visual coding environment designed for kids. For instance,
will turn the sprite toward the top of the screen and move it 10 steps. You can actually create pretty sophisticated software using these tools.
For the robotics piece, I bought an OWI-535 Robotic Arm Edge kit to put together with the kids. We worked on it 3 nights in a row, but got it assembled. Using the 5 switch controller, I was able to use it as a utensil to eat a breakfast sausage. Innovation! Putting it together was a great experience because the kids got to see and understand the inner workings. The only downside was that the switches were a bit fiddly and sometimes require adult-strength fingers to control it.
So, I did some research and found a USB adapter for the arm. Installation was easy, but the software that comes with it only runs on Windows. I could have run a VM and installed the software there, but since my goal was to get to the point where my kids could program it, I decided to find a more open source way to control it. Some Googling unearthed a python package designed to do just that: Roboarm. So, I fired up the Raspberry Pi 3 and opened the terminal. Installation was as easy as:
pip install roboarm
It automatically handles the connection to the OWI and the syntax is pretty straight forward:
from roboarm import Arm
arm = Arm()
arm.wrist.up(1)
arm.grips.open(1)
arm.led.on(2)
This will move the wrist joint up for 1 second, move the grips open for a second, and turn the LED on for 2 seconds then turn it off. You can view the documentation at the git site. This took care of the connection from Python to the OWI, but now we needed to connect Scratch to Python. Fortunately, the offline editor comes pre-installed on the flavor of Debian installed on the RPi, otherwise, you can follow these instructions to install it on Linux.
Google to the rescue again! I was able to find a Python library for communicating with the Scratch offline editor. You can find scratchpy on github. Once again, installation was a breeze:
pip install scratchpy
was all it took, and we were off and running. We did the tests outlined in the readme, then it was time to put it to work. First, we created a Python script to listen for broadcasts from Scratch and execute whatever actions Scratch fed it:
def listen():
while True:
try:
yield s.receive()
except scratch.ScratchError:
raise StopIteration
for msg in listen():
if msg[0] == 'broadcast':
exec(msg[1])
Saved this script as roboarm.py and, then, we just needed Scratch to tell Python what to tell the OWI:
Would fire off a bunch of broadcasts in the form of {“broadcast”: “arm.base.rotate_counter(1)”} that roboarm.py was listening for. When it received them, the exec() command would execute them as if we were typing them into the python prompt.
Now, I’ve challenged the kids to come up with clever ways to use this – maybe a game that involves the robot arm moving something, maybe a robot dance… I’m leaving it up to them, but we’ll find something cool to do with it.