Strict Standards: Non-static method timezone::is_dst() should not be called statically in /home4/sabre/public_html/textpattern/lib/txplib_misc.php on line 2355

Strict Standards: Non-static method timezone::is_supported() should not be called statically in /home4/sabre/public_html/textpattern/lib/txplib_misc.php on line 2523
Blog: SABRE: Code Blocks, Electronic Bricks, and LEGO Beams

Code Blocks, Electronic Bricks, and LEGO Beams

Wouldn’t it be cool if you were to be able to write a program for the Arduino in Scratch, using code something like this:

Catenary Initialization Sample
Simple control loop with Catenary

or, how about if you could program an NXT robot with code sort of like this:

sample scratch script for Chelonius

Well, you can … sort of. This was what I worked on for the Western Canadian Robotics Society annual games — I entered an Art Bot that did just that, using an Arduino and some Seeed Studio Electronic Bricks and an NXT robot called Chelonius. I do have some code to share, but it’ll take more effort before it is usable by the target audience.

A Bridge Between Scratch and the Outside World

The lifelong kindergarten group at MIT who created Scratch added a really cool and useful feature: Enable Remote Sensor Connections, also known as Scratch Connections. Basically, you can right-click on the “[slider] sensor value” block (in the “Sensing” palette in Scratch) and an advanced option comes up: “enable remote sensor connections”. When you do this, Scratch opens a network socket and uses a simple protocol to communication messages. You can easily write a program in the language of your choice that will receive broadcast messages from Scratch, changes to global variables, and can tell Scratch that a remote sensor exists and when its value changes. The Scratch Connections site has details on how to activate the feature and communicate with Scratch from Python, Processing, or Java.

Chalkmarrow wrote one of these helper applications that bridges Scratch and the Arduino. The application is called Catenary. Unfortunately, to use it from Scratch, you have to create some ugly broadcast commands which are not easily editable at all.

And that’s where BYOB (Build Your Own Blocks) saves the day. Jens Mönig has taken the source code for Scratch and extended it so that you can create your own code blocks. With Brian Harvey, he’s been working towards BYOB version 3.0, which will be used to support an introductory computer science course at Berkeley. (That is so cool! I can’t say enough good things about Scratch, and BYOB really extends the possibilities of what can be done with it.)

Throw In Some Custom Blocks

The beautiful thing about this is that I can now create code blocks for Scratch that look like regular code blocks, but make it way easier to communicate with Catenary (or any other program like it). I have a sample application designed to run in BYOB version 2.99.007. As BYOB is undergoing rapid changes and is still in alpha, and so that you can better experiment with creating blocks of your own, I’d like to show you the blocks I’ve created — both what the block looks like, and what the procedure is inside the block.

The core block is this one:

"send command with filter" block

and (although the end user really doesn’t need to know or care), the procedure inside looks like this:

"send command with filter" procedure

It takes a list of commands and a filter character. Catenary filters out (ignored) all broadcast messages, unless they start with a caret (’^’) character, in which case, Catenary knows it is a special command for it to act upon. Thus, the caret is the filter character.

At a slightly higher level of abstraction, we have this block:

"tell arduino" block

which simply takes the list of commands supplied and puts the appropriate filter character in front, like this:

"tell arduino" procedure

[Below, you’ll see that I used a very similar command, with a different filter, for sending instructions to a custom robot.]

Here, then, are all the blocks that I’ve created for use with the Arduino, via Catenary.

Block Procedure
"Control arduino on port" block "control arduino on port" procedure
"reset arduino" block "reset arduino" procedure
"make arduino pin an output" block "make arduino pin an output" procedure
"make arduino pin an input" block "make arduino pin an input" procedure
"tell arduino pin on" block "tell arduino pin on" procedure
"tell arduino pin off" block "tell arduino pin off" procedure
"set arduino pin to percent" block "set arduino pin to percent" procedure

All in all, it is pretty slick. I did want to note that I’ve had trouble reading inputs. When you, say, set pin 10 to be an input, then you are supposed to be able to read the value via a sensor block:

Pin10 sensor value

I had some success with digital inputs, but not with analog inputs. It is not clear to me if I’ve made a mistake or need to study the Catenary code further.

Five other points with using Catenary:

1. Before using it, you’ll need to load the StandardFirmata sketch onto your Arduino board.

Choose the StandardFirmata Sketch from the Arduino Menus

2. You will need to know the port number that Catenary uses to address the Arduino. You can either run Catenary within Processing, where it prints a list of the serial ports, or, look at the menu within the Arduino environment, and count up from zero until you find your serial port (as indicated by the numbers in red). For me, the Arduino shows up on port 1 (and hence my programs all start with “control arduino on port 1”).

Determine the serial port to use for talking to the Arduino

3. After you’ve uploaded the StandardFirmata sketch and figured out which port you are using, close the Arduino environment. (This frees up the serial port so that Catenary can talk to it.)

4. Finally, make sure that BYOB is running and then run Catenary. It will say that it has connected to Scratch and that the Arduino port has not been specified. (If it does not say that it connected to Scratch, make sure that Remote Sensor Connections are enabled).

5. I’ve hard better success running Catenary within Processing than from running the stand-alone executables that come with it. (I’ve only tried on a machine running OS X 10.5, mind you). If you opt to run it that way, you’ll need to download Processing (which is what the Arduino environment is based on, so it shouldn’t be too unfamiliar), and download and install the library that let’s Processing talk to an Arduino.

You should now be able to double-click on blocks and have things happen on your Arduino.

The NeXT Step

I also wanted to be able to control my LEGO Mindstorms NXT from this Scratch-like environment, and the LeJOS environment — which allows you to program your NXT using Java, and allows you to communicate with it from your computer in Java — is an excellent way to go.

Chelonius

The project had three main parts:

1. The BYOB Code Tiles for the robot
2. A custom Java program, called “TalkToBot” which uses Dr. Andrew Davison’s library for talking to Scratch from Java, and
3. A custom LeJOS application, called ArtBot, running on my robot, Chelonius.

By all means, download the source code. I make no claims that the code is any good, but I did want to get it up here for anyone else who wanted to explore. Also note that it is not general purpose; it only works with a robot running the specific program that is included. You will need to have LeJOS NXJ installed on your computer and the LeJOS firmware on your robot, to make any use of this code.

As my time was very limited, I developed a simple communication method: all of the blocks would issue a command and specify two (floating-point) numbers. They’d also move the sprite on screen a little bit.

There, we have the following code tiles:

"tell robot to" block "tell robot to" procedure
"move robot __ cm" block "move robot __ cm" procedure
"robot turn left" block "turn robot left" procedure
"turn robot right" block "turn robot right" procedure
"robot pen down" block "robot pen down" procedure
"robot pen up" block "robot pen up" procedure
"robot play tone" block "robot play tone" procedure
"robot stop all" block "robot stop all" procedure

One thing I really like about the LeJOS libraries for the NXT is that they easily allow you to tell the robot to go a certain distance or turn a certain amount. (Mind you, it really requires some tweaking to get things exact.)

How To Run The Application

These instructions assume:

1. You have BYOB 2.99.007 (or newer?) set up on your computer.
2. You have LeJOS NXJ 0.85 (or newer?) set up on your computer and NXT.
3. You aren’t scared of using a command prompt
4. You have paired your NXT and computer for bluetooth communication.

Here, then, are the instructions.

1. Start BYOB and load the script found in BYOB/Base Commands.ypr in the source archive.

2. Compile and upload the program for the robot.

Issue the following commands, at a command prompt, starting in the root directory of the source archive. (Skip any lines starting with a #).

# Go into the NXT code directory
cd NXT
# Compile the ArtBot program
nxjc ArtBot.java
# Upload the ArtBot program to your robot (via USB or Bluetooth)
nxj ArtBot
# Return to the main directory
cd ..

3. Run the computer program.

From a command prompt,

cd PC
# compile the TalkToBot application and Scratch connection libraries
nxjpcc *.java
# run the TalkToBot program
nxjpc TalkToBot

The program should indicate that it has connected to Scratch, and then it’ll try to make a Bluetooth connection to the NXT.

After that happens, every time you double-click on a block with ‘robot’ in the name in the Scratch-like environment, you’ll see a message in your terminal window and on the robot, and the robot will execute the command.

I’m sorry that I don’t have a video to post, as seeing it in action is way better than taking my word for it. Unfortunately, my robot Chelonius has already been dismantled for use in my next project.

In my next post, I’ll discuss where things go from here.

Chelonius, showing the pen

Posted by Clinton Blackmore - Tuesday May 25, 2010.
Posted in .

Comment

  1. Great job. I originally came up with Catenary as a quick program that would (hopefully) allow more advanced scratchers to control the Arduino. I think your BYOB blocks idea is a better way to go (though built-in serial communications would make things even easier :)

    — Chalkmarrow · May 28, 09:15 AM · #

  2. Boy, it is good to hear from you, Chalkmarrow. I sure appreciate the work you put into Catenary. Direct serial communication is a good idea — I can’t wait to figure out how to make my own blocks right within Squeak so as to make that possible.

    Clinton Blackmore · May 28, 09:29 AM · #

  3. Hi Clinton, hi Southern Alberta Robotics Enthusiasts,
    That’s really great! But it don’t work with my arduino.

    Catenary works: It says, Scratch is connected and the Arduino Port ist setting is shown as COM 9.
    In Catenary I see the pin Nr. 13 blinking, if I program it with your blocks in BYOP:

    When clicked,
    control arduino
    on port 0,
    wait 1 secs,
    reset arduino,
    wait 1 secs,
    make arduino pin: 13 an output,
    forever:
    turn arduino pin: 13 on,
    wait 1 secs,
    turn arduino pin: 13 off,
    wait 1 secs

    On the Arduino I can see some blinking Rx. But my LED Nr. 13 don’t blink. Somebody wrote I need a special firmata 3.3.4. Is this correct? How I have to install this firmata?

    Please can you help me.
    Michael

    — Michael · Jul 30, 06:03 AM · #

  4. hi clinton,
    now it works. I don’t know, what the mistake was. I have one further question: After sending the program from BYOB to the Arduino – is it burnt on the Atmega? Or must it all times triggered from the PC by BYOB? Can I cut the line and the arduino works on?
    Thanks Michael

    — Michael · Jul 30, 01:53 PM · #

  5. Hi Michael.

    I’m glad to hear you got it to work.

    I’m afraid that this approach through Catenary uses the computer to issue commands to the Arduino; it does not compile and upload a program that the Arduino can run independantly of the computer.

    I’m actually working on making Enchanting export code for compilation right now, but my immediate aim is to do so for the NXT, with the Arduino as something to target in the future.

    There is another project in the works called modkit which also aims to let you use a Scratch-like interface to program your Arduino, for use as an independant program.

    Cheers,
    Clinton

    Clinton Blackmore · Jul 30, 05:05 PM · #