Sie sind auf Seite 1von 28

Lego: A Gentle Introduction to Robotics

Linto Antony
lintoa@rediffmail.com
Lego: A Gentle Introduction to Robotics
by Linto Antony

Copyright © 2003 by Linto Antony

Revision History
Revision 1.0 2 Nov 2003 Revised by: LA
Lego: A Gentle Introduction to Robotics
Table of Contents
Acknowledgement............................................................. i
1. Introduction .................................................................1
What is Lego? .................................................................. 1
What is a Lego Mindstorm? .............................................. 1
LEGO Mindstorms Robotics Invention System.................. 1
Bricks that think ............................................................. 2
2. The Hardware................................................................5
Overview .......................................................................... 5
Hardware ......................................................................... 5
A Brief description of the Programmable Brick ........... 5
RCX........................................................................... 5
IR Transceiver ........................................................... 6
Cables ....................................................................... 6
3. LegOS ...........................................................................9
LegOS .............................................................................. 9
The LegOS kernel............................................................. 9
Starting the kernel..................................................... 9
Timing ..................................................................... 10
Notions of kernel image, user programs, and tasks .. 10
Memory management .............................................. 10
Semaphore facility ................................................... 11
IR Networking .......................................................... 11
4. How to program Lego ..................................................13
Code Examples .............................................................. 13
Hello, World ............................................................. 13
Controlling Motors ................................................... 13
Touch Sensor .......................................................... 14
Light Sensor ............................................................ 15
Basic Threading....................................................... 15
Playing Sound ......................................................... 16
5. Conclusion..................................................................19
A. References ..................................................................21

iii
iv
Acknowledgement
This document was prepared to give an overview about the new
trend in the Robotic world; Lego Mindstorms. I’m indebtedly
grateful to the Head Of The Computer Science DepartmentDr.
Agnisarman Namboodiri for his support. I’m grateful to Lect.
Sminesh for his valuable guidance and suggestions. I’m also
grateful to all other faculty members of Computer Science De-
partment for their support. I gracefully remeber all students of
S7 CSE-A.

i
Acknowledgement

ii
Chapter 1. Introduction
The Lego Mindstorm Robotic Invention System is one of the
most fascinating technological innovations in recent years.The
LEGO Group is a major sponsor of research at the Media Lab,
and it is fair to say that the LEGO RCX Brick is inspired by
the MIT(Massachusetts Institute Of Technology) Programmable
Brick and related research. However, the RCX Brick was de-
signed "from the ground up" by the LEGO Group and is not
based on any of the technical work in the MIT Programmable
Brick. The two devices use different CPUs and have entirely dif-
ferent software systems behind their operation.
Lego robotic systems are used to teach programming, artificial
intelligence and control theory in some of the best universities
in the world - including our own IIT’s (IIT Kanpur is currently
coordinating an interesting project called BRICS).

What is Lego?
Lego is the world’s largest toy manufacturing company. They
make ‘building blocks’ using which kids construct things out
of their imagination - the sheer number, variety and ingenious
design of the blocks makes it possible to build virtually anything
we can think of.

What is a Lego Mindstorm?


Building ‘intelligent robots’ is one of the most fascinating of all
human endeavours - it is a result of our desire to embed a part
of ourselves in inanimate objects and breathe ‘life’ into them.
Robot building was confined to university research departments
and big industrial houses, as few individuals had the resources
to design and build their own systems.
Lego, together with researchers at MIT’s Media Lab, changed
all that in the year 1998 with the introduction of their
‘programmable brick’. The ‘programmable brick’ is a small
Hitachi microcontroller with 3 motor outputs and 3 sensor
inputs. This, combined with standard Lego building blocks
and additional parts like wheels, pulleys, gears etc (all made of
plastic) can be used to build fascinating programmable robotic
systems. The user builds the robot, writes a program on the PC
and downloads it onto the ‘brick’ - the robot now becomes fully
autonomous and moves and acts according to the program
stored in the brick.

1
Chapter 1. Introduction

LEGO Mindstorms Robotics Invention System


When LEGO released their robotics series, it caused a huge stir
both with children and also with adults. The appeal of bringing
your creations alive has been central to LEGOs strategy in the
toy market - now, take a good idea and then add a complete
new layer of flexibility, a progammable microcontroller, the ap-
peal just soars! Now, take an excellent software package, and a
clearly illustrated book to help the user start, and you have a
sure winner. Indeed, I’ve never had more fun with a toy in my
life. RIS revolves around the RCX microcontroller (shown right).
This little beauty can store up to five programs simultaneously,
and can have programs downloaded to it via an IR transmitter. It
can even communicate with other RCX controllers! In your kit,
you get one of these controllers, two touch sensors, two motors,
and a light sensor. Also included are a huge variety of LEGO
pieces, axes, bolts, wheels, wires and other related building ma-
terials. It is then your choice whether to build and program the
LEGO challenges, or explore everything yourself.
The inventors are not part of a big research university or at some
forward-looking computer company, but a loosely knit group of
engineers who build gizmos out of Lego bricks. Developed by
the Billund-based Danish toy company, some bricks contain
microcomputer-controlled sensors and motors, and all are com-
ponents of a three-year-old product called Lego Mindstorms.
Originally intended for use by children at home or in the class-
room, Mindstorms has instead enraptured tens of thousands
of adults. Engineers, academic researchers, and hobbyists have
reverse-engineered its firmware, hacked together new program-
ming environments, and shared recipes for unanticipated ways
of connecting it to the outside world.

Bricks that think


Lego and the engineering community share a long history. As
far back as the early 1980s, automotive engineers reportedly
used the brightly colored interlocking bricks and hinges--then
years away from built-in microcomputers--to make scale models
of proposed vehicle assembly lines. In 1984, the company be-
gan working with researchers at the Massachusetts Institute of
Technology (MIT), in Cambridge, to adapt its motors, switches,
and other parts for computer control. Indeed, the RCX controller
that serves as the brain of every Mindstorms robotic gizmo is
based on what the company learned by sponsoring the develop-
ment at the MIT Media Laboratory of a "programmable brick."
RCX stands for robotic command explorer. It is an oversized
Lego brick [left], a little larger than two stacked decks of play-
ing cards. On the outside are a liquid-crystal display (LCD), four
2
Chapter 1. Introduction

control buttons, and six wiring connectors. Inside are a Hitachi


H8 central processing unit (CPU) with 32KB of RAM, space for
batteries, and an infrared transmitter and receiver, which com-
municate with a base station plugged into a PC port.
Three of the external wiring connectors are outputs, each sup-
plying a user-controlled voltage to a motor, a lamp, or anything
else that will run off half a dozen AA batteries. The remaining
connectors can read data from a variety of sensors (one light
and two touch sensors, all housed in Lego-style bricks, are in-
cluded with the basic kit, along with two stepper motors). The
LCD can display motor settings, sensor readings, or diagnostic
information about the status of the RCX and its software.
To construct a working gizmo, a Mindstormer typically builds
the mechanism out of the RCX, motors, sensors, wire, and the
roughly 700 bricks, beams, gears, axles, and wheels included
with the basic kit, which lists for US $199. Then, to program
the RCX, an aspiring inventor runs a graphical environment
provided by Lego for the PC. By connecting blocks representing
commands such as READ SENSOR LEVEL or TURN MOTOR
ON, he or she creates a program, downloads it to the RCX by
an infrared link, and pushes the appropriate control button to
start it running.
The PC sends the program to the RCX in the form of byte-codes.
These are sequences of 8-bit instructions that specify such ac-
tions as turning on a motor, waiting for input from a sensor, or
jumping to another point in the program. A little more than half
the RCX’s 32 KB of RAM is taken up by firmware--downloaded
from the PC when the RCX is first powered on--that interprets
these byte codes and executes the H8 machine code that actu-
ally makes things happen.
From these relatively simple parts, hobbyists have managed
to build a mind-boggling array of widgets. The list includes
robotic trash trucks that pick up and deposit miniature
dumpsters, rolling and walking robots, a light-bulb changer,
and maze-following robots. Hundreds of Web pages put up by
fans around the globe supply the details for building these
creations.
That Mindstorms is such a hit with adults does not strike Fred
Martin as strange. Said the former MIT researcher, who played
a pivotal role in the programmable brick development, "It’s the
toy I would have wanted to have" when he was younger, and
he still wants it today. The goal of MIT’s research, Martin said,
was to create "materials that make ideas accessible," and the
programmable brick does exactly that for robotics and for com-
puter programming. People who use Mindstorms learn by build-
ing something tangible.
Above all, Mindstorms makes ideas about robotics accessible to
3
Chapter 1. Introduction

people without sophisticated mechanical-engineering skills or


access to a machine shop. With Mindstorms, anyone can build
computer-controlled mechanisms by simply snapping parts to-
gether. Where an earlier generation of hobbyists struggled to
build parts to the submillimeter tolerances required for a work-
ing robot arm, "Lego parts are made to exact lengths," with an
accuracy measured in tens of micrometers.

4
Chapter 2. The Hardware

Overview
The base system for using the RCX consists of the RCX itself,
an infrared transceiver, and a PC. Additional components, such
as motors, sensors, and other building elements, combine with
the base system to allow the creation of functional autonomous
robotic devices.
At the core of the RCX is a Hitachi H8 microcontroller with 32K
of external RAM. The microcontroller is used to control three
motors, three sensors, and an infrared serial communications
port. An on-chip, 16K ROM contains a driver that is run when
the RCX is first powered up. The on-chip driver is extended by
downloading 16K of firmware to the RCX. Both the driver and
firmware accept and execute commands from the PC through
the IR communications port. Additionally, user programs are
downloaded to the RCX as byte code and are stored in a 6K
region of memory. When instructed to do so, the firmware in-
terprets and executes the byte code of these programs.

Hardware

A Brief description of the Programmable Brick


The Programmable Brick, which forms the heart of the robotic
system, is nothing but a Hitachi microcontroller and 32KB of
RAM, with some motor driver and sensor/IR circuits, packed
in a small yellow coloured Lego block. Programs and data are
stored on the RAM. When the brick is powered up, it executes
code stored in about 16K of ROM. This code either transfers
control to an operating system (called ‘firmware’ in Lego Litera-
ture) loaded in RAM, or, if firmware is not present, it gets ready
to download one through an IR link from the PC. The RAM is
battery backed - even when the unit is powered off, the battery
keeps powering the RAM.

RCX
To remove the circuit board, open the RCX, remove the four
screws, remove the IR shield, then separate the front cover from
the circuit board. Release the two battery contacts from the bat-
tery side, then slide the circuit board free. Do this at your own
risk; if you’re not careful there’s a chance you’ll break some-
thing. You can also twist the circuit board free without first re-
moving the battery contacts.
5
Chapter 2. The Hardware

The negative battery contact is somewhat easier to release than


the positive one. By removing the negative contact first, then
sliding the circuit board free, you will be able to remove the
positive contact from the circuit board side by inserting a small
screwdriver in the slot between the contact and the battery case
and gently prying the contact outward and upward.
The parts, from left to right, starting with the top row: circuit
board, front cover; IR shield, screws, battery contacts; battery
case, battery cover. Two removable plastic ribs are sitting on the
battery case. The top of the circuit board. Two IR LEDs and an
IR receiver are on the left, an LCD is in the center, and several
capacitors and an adapter socket are on the right. The LCD is
covering an LCD controller and a speaker; above and below the
LCD are the contacts for the four rubber keys. Twelve clamps
rise from the circuit board to mate with the input and output
ports on the cover. Flops, the small chip above that is a bank
of NAND gates. The large square chip is the microcontroller, the
chip below that is a RAM. The large chip in the middle is a bank
of flip.

IR Transceiver
It is also possible to disassemble the IR transceiver. Simply re-
move the four screws and lift off the back cover, making sure
to move the battery spring out of the way. From left to right,
starting with the top row: front cover, back cover; circuit board,
screws. The front cover doubles as an IR filter. A switch and a
6.7 ohm resistor are on the left, the two chips in the middle are
banks of NAND gates, and the grey circle in the lower right is a
voltage regulator.
The white rectangle below the chip in the upper middle is a
green LED, and hanging off the right side of the chip are the IR
receiver and two identical IR LEDs. The DB-9 serial connector
is on the opposite side of the circuit board.

Cables
The cable to connect the IR transceiver to your PC is a null mo-
dem cable that contains six wires, of which only five are used.
With the larger row on top, the holes of each connector are num-
bered from right to left starting with the top row. Pins inserted
into these holes are joined by the cable to pins inserted into the
holes of the opposite connector as follows:
Pin To Name Description
2 3 RD Receive Data
3 2 TD Transmit Data
5 5 SG Signal Ground
6
Chapter 2. The Hardware

7 8 RTS Ready To Send


8 7 CTS Clear To Send

Pin 4 connects to pin 4 but is unused; pins 1, 6, and 9 have no


connection. The RTS/CTS signals are not used for flow control.
Instead, they are used by the PC to check whether or not the
transceiver is connected. The transceiver wires CTS and RTS
together; the PC checks for the transceiver by asserting and de-
asserting RTS. If it sees that CTS tracks RTS, then it assumes
that the device sitting on the serial port is the transceiver.

7
Chapter 2. The Hardware

8
Chapter 3. LegOS
Remaining part of this document is an introduction to a reason-
ably powerful embedded operating system called LegOS which
makes Lego coding lots of fun for the serious Linux/C hacker.
Through some very simple (and interesting!) programming ex-
amples, I demonstrate how Operating System concepts like con-
currency and multithreading are employed to control real world
mechanical systems.

LegOS
It is possible to set up a cross development environment under
Linux to develop Lego applications. A port of the GNU compiler
to the Hitachi controller is available. Once that is installed, you
can get a copy of a small embedded operating system called ‘Le-
gOS’ and compile it. The compiled LegOS can be downloaded to
the programmable brick by using the command ‘firmdl3’ (which
comes with the LegOS distribution). Once the OS (firmware) is
downloaded, you can write C programs, compile them into .lx
files (that’s the extension which LegOS uses - the C programs
can’t be compiled into native Linux format because they are not
going to be executed on Linux) and download them onto the Lego
brick by using the ‘dll’ command. There are 8 slots available for
storing programs on the Lego brick - using an option to the ‘dll’
command, you can store your program in whichever slot you
like. Execution of the program is done by first using the ‘Pgm’
button on the Lego brick to choose a program and then pressing
the ‘Run’ button.
LegOS supports multiple, preemptible threads. A 16 bit timer
generates an interrupt once every 1ms or so - the interrupt han-
dler sets motor speed and direction, checks sensor values, looks
at whether the time consumed by the currently executing thread
has exceeded the set quantum, and if so, stops that thread and
chooses a new one for execution. Knowing this much of archi-
tectural detail is sufficient to becoming productive with LegOS.
Let’s look at some code.

The LegOS kernel


The LegOs kernel is monolithic, as all of the source are compiled
together in a system binary image. The interface for the user
programs are given in a dynamic linker script, which contains
all the linker scripts.

9
Chapter 3. LegOS

Starting the kernel


The kernel starts when kmain() is called by the ROM. This func-
tion initialises the kernel, before it is started in either single
tasking or multitasking mode. During kernel initialization, 3
tasks are started. The first is the idle task, which is a dummy
task with the lowest priority.
The next two tasks implement the dynamic program loading ca-
pabilities. They are called packet_consumer() and key_handler()
and are run with the highest priority. The packet_consumer()
handles the activity on the IR-port, and the key_handler() han-
dles activity on the buttons of the RCX brick. The task manager
is then started, and the kernel starts switching between the exe-
cution among the 3 tasks. New tasks are started by downloading
and starting userprograms.

Timing
LegOS is driven by the interrupts from the 16-bit timer, which
is configured to make an interrupt every millisecond. The
timer interrupt is handled by a ROM function pointed to by
the ocia_vector. This vector points to the systime_handler()
function, which in turn polls all the various subsystems
in turn, by calling their handlers. This means that LegOS
is polling instead of interrupts, to communicate with the
enviornment.

Notions of kernel image, user programs, and tasks


The kernel is a separetly compiled binary image, which exports
a set of symbols in a linker script called legos.lds. The kernel
image is located in the beggining of the RAM.
User programs are compiled separetly, and linked with the ker-
nel symbol table, creating the relocatable format .lx. The final
linking of the absolute are done when downloading the program
with dll, after the absolute address of the text segment of the
program is known. User programs are downloaded to the part
of RAM following the kernel image part.
Tasks are started by a user program as threads in the address
space of the user program. As user programs are compiled in
seperate address spaces, user programs cannot start threads in
other user programs.

Memory management
LegOS uses a straightforward continuous allocation scheme for
10
Chapter 3. LegOS

managing the memory. There is no support in the RCX hard-


ware for advanced memory management schemes such as pag-
ing or segmentation. Furthermore, the memory is a scarce re-
source, so advanced memory management scheme would imply
much overhead.
The memory is divided into the kernel part and user program
part. The kernel code and static kernel data are located in the
lower part from address 0x8000 up to address mm_start, which
is a global variable. The memory manager has the responsibility
of all the memory from address mm_start to address 0xFFFF.
The memory is organised in memory blocks which consist of a
4-byte header and an even number of data bytes. The first two
bytes of the header contain the process id of the process which
has allocated the block. If the block is unallocated, the value
is MM_FREE which is defined to be zero. The last two bytes of
the header hold the size of the data block following the header.
The unit of memory used by the memory manager internally is
2 byte words.
As memory is initialized the memory is divided into blocks for
different devices and some free blocks. Memory is allocated
using the function malloc(). Malloc() will search for the first
free block of suitable size. Memory is deallocated by adding
the memory block to the adjacent free block suing the free()
function.

Semaphore facility
LegOS provides a semaphore facility which implements the clas-
sical Dijkstra definition of semaphores. The semaphores are
counting semaphores. Besides the usual wait and post opera-
tions, a non-blocking wait called sem_trywait() and an operation
for reading the semaphore value, sem_getvalue() are provided.
The wait operation is implemented using the wait_event() func-
tion, which puts the process in the sleep mode, and specifies a
wake-up function.The wake-up function used with semaphores
is sem_event_wait(), which checks the value of the semaphore.

IR Networking
LNP is the LegOS Network protocol which is used by user
programs to communicate over the IR port. In LegOS two kinds
of LNP packets can be used. The first one is called the integrity
packets, which contain no addressing information, and is used
for broadcasting packets. The second one is the addressing
packet, which provides a UDP like service.

11
Chapter 3. LegOS

To communicate with a PC running Linux, a daemon called lnpd


is used. Lnpd allows multiple clients to connect to an RCX run-
ning LegOS. Furthermore a library called liblnp exists, so appli-
cations can connect with remote lnpd daemons.

12
Chapter 4. How to program Lego
Here in this chapter I have tried out some sample codes that
can be run on Lego using LegOS. I have given it over here to
innovate you to think freely about different devices and Artificial
Intelligence.

Code Examples

Hello, World

#include <conio.h>
#include <unistd.h>
int main()
{
/* Space on the LCD display
* to print only ‘hello’ (ie,5 char’s).*/

lcd_clear();
cputs("Hello");
while(1);
return 0;
}

The classical Hello, World. The message is visible on the LCD


screen of the Lego Brick.

Controlling Motors

#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>

int main()
{
motor_c_speed(40);
motor_c_dir(fwd);
sleep (10);
motor_c_speed(off);
return 0;
}

The motors can be connected to any of the three input ports


A, B or C. The speed can vary from 0 to 255. Only when the
direction is set does the motor start turning. The direction can
be one of the enumerated values fwd, rev, off or brake.
13
Chapter 4. How to program Lego

Touch Sensor
The kit comes with two touch sensors. Here is a small program
which monitors the state of the touch sensor connected to sen-
sor input 1.
#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>

/* When depressed, it is 1, else


* it is zero
*/

int main()
{
while(1) {
lcd_clear();
lcd_int(TOUCH_1);
/* 500 milli sec sleep */
msleep(500);
}
}

Here is another program:


#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>
#define MIN_ENV_SPEED 40
/* Turn off motors when either
* touch sensor is touched
*/
int main()
{
motor_a_speed(MIN_ENV_SPEED);
motor_c_speed(MIN_ENV_SPEED);
motor_a_dir(fwd);
motor_c_dir(fwd);
while(1) {
if (TOUCH_1 || TOUCH_2) {
motor_a_speed(off);
motor_c_speed(off);
break;
}
}
return 0;
}

14
Chapter 4. How to program Lego

Instead of turning off the motors, we can also think of reversing


the direction.

Light Sensor
The unit comes equipped with one light sensor, which operates
in either active or passive mode. In active mode, the light sensor
operates under the glow of a small LED - it is ideal to find out
reflectivity changes of near objects. In passive mode, the sensor
operates under environmental illumination.
#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>
#define MIN_ENV_SPEED 40
int main()
{
/* My desk shows a reading of
* around 87. The dark side of
* a floppy around 40 and the
* metallic part of a floppy
* around 124.
*/
/* Get into active mode */
ds_active(&SENSOR_2);
while(1) {
msleep(750);
lcd_int(LIGHT_2);
}
}

Basic Threading
A new thread is started by the execi call, which is given the
address of the function to be executed as the new thread, an
argument count and and argument vector, priority (maximum
20) and a stack size, which is set to a default value.
#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>
#define PRI 10
/* Main and fun1 execute in
* parallel
*/

int c = 0;

15
Chapter 4. How to program Lego

int fun1(int argc, char **argv)


{
while(1) {
cputs("Fun1");
msleep(250);
}
return 0;
}

int main()
{
execi(&fun1, 0, 0, PRI, DEFAULT_STACK_SIZE);
while(1) {
cputs("Main");
msleep(450);
}
return 0;
}

Does the termination of the main thread result in the stopping


of the ‘child’ thread? No, that’s what I gather from the output of
the following program. It keeps on counting.
#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>
#define PRI 10
/* What happens when main dies?
* Child thread does not stop, it
* keeps on running. */

int c = 0;
int fun1(int argc, char **argv)
{
while(1) {
lcd_int(c++);
msleep(700);
}
return 0;
}
int main()
{
execi(&fun1, 0, 0, PRI,DEFAULT_STACK_SIZE);
sleep(5);
return 0;
}

16
Chapter 4. How to program Lego

Playing Sound
Playing musical notes is simple.
#include <conio.h>
#include <unistd.h>
#include <dmotor.h>
#include <dsensor.h>
#include <dsound.h>
note_t music[] = { {PITCH_D4, 5}, {PITCH_END, 0} };
int main()
{
cputs("start");
dsound_play(music);
wait_event(dsound_finished, 0);
cputs("end");
return 0;
}

We define an array of structures, each structure having two


fields, a pitch and duration (seconds). The pitch is defined
in standard musical notation (and is internally converted
to real frequency). The array should end with the structure
{PITCH_END, 0}. All the pitches in the array are played, till
{PITCH_END, 0}. The standard function dsound_finished
returns true only when the hardware finishes playing the
tunes.
All these examples are to be compiled using a gcc cross compiler
for the Hitachi H8/300 platform. The programs are compiled
and should be downloaded into Lego using an Infrared port and
a command called dll.

17
Chapter 4. How to program Lego

18
Chapter 5. Conclusion
The potential of using robots in the classroom has intrigued
educators for as long as robots have been a part of our culture.
Economic constraints, however, have demanded patience on our
part; robots were initially too expensive and too difficult to work
for large-scale classroom use.
Over time, however, technology becomes better, cheaper, and
easier to use. If we are patient, devices originally suited for re-
search labs and highly trained proffesionals may become cheap
enough and user-friendly enough for classroom use. Lego Mind-
storms have contributed such an innovation in the computing
world.
Since Robotics has now becoming so important in the world of
computers. It has now grown into a seperate discipline in west-
ern countries. I hope that the future curriculum of our univer-
sities will contain Robotics. Then there will be a sure place for
the Lego Mindstorms in our colleges.

19
Chapter 5. Conclusion

20
Appendix A. References

• IEEE Robotics and Automation Magazine.


• http://www.legomindstorms.com/
• http://www.generation5.org/
• www.cs.uu.nl
• www.usafa.af.mil

21
Appendix A. References

22

Das könnte Ihnen auch gefallen