Introducing Interactive C Events

I have been playing around with Interactive C on the Handy Board. I have added the ability for event driven programs and the result is the Handy Board Interactive C Events (HB-ICE) source distribution. This post describes the overall design and code samples.

Event-driven programming enables you to focus on robotic behavior while the event infrastructure notifies you when something interesting happens. The event infrastructure is typically part of the operating system or application framework. In this case, HB-ICE provides the infrastructure. HB-ICE is just the minimum to monitor the Handy Board buttons and digital inputs. Other extensions will be added later.  I intend to use this to implement reactive behavior, hopefully with subsumption.

Button Events
Let’s start with an example. Here is the code that is notified when a button is released:

void handleButtonReleasedEvent(struct Event* event){
    int eventId = event->id;
    switch(eventId) {
        case EVENT_ID_STOP_BUTTON:

When a button is released, a pointer to an event is passed to the function. The id of the event is used to determine what action to take.   Events are structures like this:

struct Event {
    int type;
    int id;
    int value;
    long timestamp;

The type field helps distinguish button events from digital or analog events. The id further distinguishes the event within the type. For example, a button released event needs to indicate if the start or stop button was released. The value field is a copy of the sensor, input or button state. The time stamp field is assigned to the event when it is signaled. I plan to use that for meta-sensing in the future.

In handleButtonReleasedEvent, the program either calls start() or stop(). This is used to start and stop the robot behaviors. So how does this all get started? Let’s look at the example program main() function and supporting functions:

void initialize(){
void handleEvent(struct Event* event){
    int eventType = event->type;
void main(){
    struct Event* event = NULL;
    printf("press start\n");
        event = dequeue(eventQueue);
        if(event != NULL){

The main function calls intialize() and then enters an infinite event loop.  The initialize function initializes the event queue, digital events, motors and then starts monitoring the buttons.  The buttons are monitored in a separate process.  When a state change in a button is detected and the button is released, an event is added to the event queue. The event queue is a classic first in, first out (FIFO) queue.  The event loop in the main function takes events off the queue and calls the handleEvent function.  This function routes the event to the appropriate handler based on the type of event.

The HB-ICE distribution also supports the digital inputs. I have been using this infrastructure for the Handy Bug exercises in Fred Martin’s book entitled Robot Explorations and it appears to work.

The source is available as a zip file via MediaFire hosting or from the Source Code page.

Building Sensor Adapters from RCX Parts

I have been spending some time finding ways to reuse my old Mindstorm sensors for use with the Handy Board. Fred Martin’s book simply instructs you to cut the Lego wires and solder them to a simple header pin assembly. I was uncomfortable with that since I only have a limited number of Lego wires and the short ones would be too short if I cut them in half. I found that there is a steady supply of Mindstorm parts on e-bay, but I found another option.

My RCX died from battery acid that had leaked from batteries that I left in years ago. Since I have the Handy Board, I have no use for the RCX. The RCX has six very specialized electrical bricks. The RCX has two rows of three connectors of these electrical bricks for Lego wires. After taking apart the RCX, I found that all six are easily popped out of the RCX shell. I have hacked these bricks to provide an easy way to preserve my current Lego wires and add additional length for interfacing to the Handy Board.

The RCX electrical brick in various stage of the hack
The RCX electrical brick in various stage of the hack

The brick on the far left is the unchanged brick out of the RCX. The brick is held in place in the RCX by two tabs. I scored the tabs with a knife and easily bent and broke them off. This is shown in the middle brick. I used an old 28 gauge ribbon cable to create a custom cable for the adapter. I soldered the two wires to the metal tabs of the electrical brick. Care should be taken not to apply too much heat or the brick could melt. The results of the soldering are on the right side. Next I bent the metal tabs down and applied electrical tape as shown below.

After bending the tabs down
After bending the tabs down
After applying the electrical tape
After applying the electrical tape

Now this adapter can be connected to an existing Lego wire without cutting it. I can also extend the length by using a different length of ribbon cable.


This is how the robot from chapter one looks with the sensor adapters installed.

The sensor adapters are installed under the robot and wired to the Handy Board ports
The sensor adapters are installed under the robot and wired to the Handy Board ports