Beruflich Dokumente
Kultur Dokumente
Linto Antony
lintoa@rediffmail.com
Lego: A Gentle Introduction to Robotics
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.
1
Chapter 1. Introduction
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
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
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
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.
9
Chapter 3. LegOS
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.
Memory management
LegOS uses a straightforward continuous allocation scheme for
10
Chapter 3. LegOS
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
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;
}
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;
}
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>
int main()
{
while(1) {
lcd_clear();
lcd_int(TOUCH_1);
/* 500 milli sec sleep */
msleep(500);
}
}
14
Chapter 4. How to program Lego
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 main()
{
execi(&fun1, 0, 0, PRI, DEFAULT_STACK_SIZE);
while(1) {
cputs("Main");
msleep(450);
}
return 0;
}
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;
}
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
21
Appendix A. References
22