Practical exercises on Lego Mindstorm robots
IntroductionFrom Tobias Amnell, Uppsala Universiteit (original).
The LEGO Mindstorms Robotics Invention System consists of a bunch of LEGO pieces and the RCX (Robotics command System) unit (see picture on the right). The RCX unit is an autonomous programmable microcomputer, using a Hitachi H8/3932 microcontroller. The RCX brick can be used to control actuators, like a sound generator, lights, and motors, and read input from various sensors, like light sensors, pressure sensors, rotation sensors, and temperature sensors. The RCX brick also has an LCD display (useful for printing information) and an IR-transceiver (for downloading programs and communicating with other bricks). The RCX unit is built for the easy attachment of LEGO building blocks and pieces.
BrickOS (formarly known as LegOS) is a small Operating System for the LEGO Mindstorms RCX unit. BrickOs is cross-compiled on a PC under Linux or Windows 95/98/2000/NT environments. BrickOS programs are written in C and compiled to native code for the Hitachi H8 (most other programming environments for the RCX depends on the virtual machine that is the default programming environment from Lego). BrickOS offers preemptive multitasking, dynamic memory management, POSIX semaphores, as well as native access to display, buttons, IR communication, motors and sensors. Observe that BrickOS is not a hard Real-Time Operating system but does offer functionality similar to that of most soft real-time embedded OSes on the
The assignment should be solved in groups of two or three persons. It is not recommended that you work alone, since there is a limited number of lab equipments available.
Before you start make sure that you have the following materials available:
Installing and testing the software
Goto the BrickOs installation site for installation instructions. You need a pc with a serial port.
As explained on the site mentioned above, windows users need to install cygwin. Please note that the standard installation will not do. You need to specify manually to download additional files. The packages make, gcc, flex, automake, autoconf and patch are certainly not included in the default installation but required. Please make sure to select all required packages. A detailed list is available from the above mentioned website.
The LegOS Command Reference 10-0.2.4 contains a short summary of commands available for programming the RCX unit. If you want details you can investigate the system header files in the PATH_TO_LEGOS_DIR/include/ directory or you can check the API.
A handy editor for editing the C source code is TextPad.
Assignment 1: Study the rover program
Investigate the rover.c program in the demo folder. Try to understand how the rover program works. Do you observe - or can you imagine - situations that the rover program can not recover from? How can this be solved? Change the implementation to overcome this problem. Are there problems that can not be solved by your program but that require a mechanical solution?
Assignment 2: Threads
Reimplement the rover.c program using threads. Use one thread for the left sensor / wheel and one thread for the right sensor / wheel.
Assignment 3: A simple light detector
Attach a light sensor to the RCX unit. Write a short program (one task, coded into the main function) which works as a Geiger counter for light, ie. when the light sensor receives a lot of light it should be generating a very frequent sound (ticking). When little light is received the sound (ticking) should be less frequent. The light value should also be displayed on the LCD.
Investigate the PATH_TO_LEGOS_DIR/demo/robots.c file and the LegOS Command Reference 10-0.2.4 on how to play sounds, read the light sensor, and write something on the LCD display.
Note: To be able to use the lcd_int() function you should include the conio.h include file.
Assignment 4 : Braitenberg Vehicles
In this exercise you will compare the behaviour you predicted in exercise session "Sensor-motor control" on Braitenberg vehicles with a working implementation.
a. Replace the two bumpers by two light
sensors. Program your vehicle such that it drives towards the light
or away from the light. What are the problems you encounter? Are
there fundamental differences with the behaviour you expected?
Assignment 5 : An illustration of emergent behaviour
Prepare your robot such that it has two light sensors and a bumper at the front. Make sure that the light sensors are not parallel, but slightly oriented away from each other. Make your robot exploring the arena and make sure that it detects and recovers collisions with the wall, other robots, ... The arena will be filled with small obstacles that are too light to be detected by the bumper, but can be seen by the light sensors. If this behaviour is shared by all robots, an interesting emergent behaviour might be observed. What will be the expected behaviour if there is only one robot / if there are many robots? What's happening in the real world?
Assignment 6: Q-learning
In this exercise we will build a robot that will learn a task using reinforcement learning, specifically we will opt for the Q-learning algorithm.
Suppose Mr. Jones purchased a lawn mower robot. Unwrapping the box he finds a robot and a charging station. He needs to place the charging station somewhere in the garden, close to a wall outlet of course. Now, the robot does not know Jones’ garden. The mowing behaviour of the robot is just random navigating. But as soon as the batteries are low, the robot needs to find the charging station quickly without getting stuck against obstacles. Now, let us write a Q-learning algorithm which guides the robot to the charging station.
Build a robot with 2 light sensors, left and right. Build a bumper in front of the robot: when the bumper hits an obstacle, the robot should be able to detect this. The charging station will be a bright light, to which the robot has to navigate.
Define states for the robot. We have 3 sensors: the left sensor (L), the right sensor (R ) and the bumper (B).
Define actions for the robot. We have two actuators, the left and right motor.
Now, let’s define some rewards for the robot. Make sure to have a negative reward for bumping against obstacles. And of course, a positive reward for reaching the charging station. You will need some other rewards as well, but that you will figure out for yourself (or if you paid attention during the lecture, you might remember which rewards you still need).
Assignment 7 : RoboTag
A popular game in robotics is RoboTag. Two or more
robots drive around in a simple arena, the edges of the arena are marked with a
line on the ground. When a robot bumps into another robot, it shouts
"Tag!" by sending this message out its IR port. The tagged robot
is punished and has to sit still for 3 seconds. The tagged robot must immediately
reply by sending "Ok". When the bumping robot receives the
"Ok", it raises it's score by one, turns around and starts exploring
An example of a communication program that uses the IR port for sending and receiving messages is available here.