using NQC (Version 3.03, Oct 2, 1999) by Mark Overmars Utrecht University P.O. Box 80.089, 3508 TB Utrecht the NetherlandsDepartment of Computer ScienceProgramming Lego Robots Preface The Lego MindStorms and CyberMaster robot s are wonderful new toys from which a wide variety of robots can be constructed, that can be programmed to do all sorts of complicated tasks. Unfortunately, the software that comes with the robots is, although visually attractive, rather limited in functio nality. Hence, it can only be used for simple tasks. To unleash the full power of the robots, you need a different programming environment. NQC is a programming language, written by Dave Baum, that was especially designed for the Lego robots. If you have n ever written a program before, don't worry. NQC is really easy to use and this tutorial will tell you all about it. Actually, programming the robots in NQC is a lot easier than programming a normal computer, so this is a chance to become a programmer in an To make writing programs even easier, there is the RCX Command Center. This utility helps you to write your programs, to send them to the robot, and to start and stop the robot. RCX Command Center works almost like a text processor, but with some extras. This tutorial will use RCX Command Center (version 3.0 or higher) as programming environment. You can download it for free from the web at the address http:/ ...
by Mark Overmars Department of Computer Science Utrecht University P.O. Box 80.089, 3508 TB Utrecht the Netherlands
Preface The Lego MindStorms and CyberMaster robots are wonderful new toys from which a wide variety of robots can be constructed, that can be programmed to do all sorts of complicated tasks. Unfortunately, the software that comes with the robots is, although visually attractive, rather limited in functionality. Hence, it can only be used for simple tasks. To unleash the full power of the robots, you need a different programming environment. NQC is a programming language, written by Dave Baum, that was especially designed for the Lego robots. If you have never written a program before, don't worry. NQC is really easy to use and this tutorial will tell you all about it. Actually, programming the robots in NQC is a lot easier than programming a normal computer, so this is a chance to become a programmer in an easy way. To make writing programs even easier, there is the RCX Command Center. This utility helps you to write your programs, to send them to the robot, and to start and stop the robot. RCX Command Center works almost like a text processor, but with some extras. This tutorial will use RCX Command Center (version 3.0 or higher) as programming environment. You can download it for free from the web at the address http://www.cs.uu.nl/people/markov/lego/RCX Command Center runs on Windows PC’s (’95, ’98, NT). (Make sure that you ran the software that comes with the Lego set at least once, before using RCX Command Center. The Lego software installs certain components that RCX Command Center uses.) The language NQC can also be used on other platforms. You can download it from the web at address http://www.enteract.com/~dbaum/lego/nqc/Most of this tutorial also applies to the other platforms (assuming you use NQC version 2.0 or higher), except that you loose some of the tools and the color-coding. In this tutorial I assume that you have the MindStorms robot. Most of the contents also applies to the CyberMaster robots although some of the functionality is not available for those robots. Also the names of e.g. the motors are different so you will have to change the examples a little bit to make them work. AcknowledgementsI would like to thank Dave Baum for developing NQC. Also many thanks to Kevin Saddi for writing a first version of the first part of this tutorial.
- 2 -
ContentsPrf_________________________________________________________________2e ace ledgemenAcknow ts 2 ____________________________________________________________________ Contents 3________________________________________________________________ I. Writing your first program ________________________________________________ 5Buildinga_______________________________________________________________________ robot 5 ngCXCommandC___________________________________________________________ Starti R enter 5 Writingtheprogram____________________________________________________________________6ngtheprog___________________________________________________________________ Runni ram 7 your program __________________________________________________________________ Errors in 7 Changing the speed 8 ____________________________________________________________________ Sumry____________________________________________________________________________ ma 8 II.Amoreinterestingprogram_______________________________________________ 9Makingturns_________________________________________________________________________9Repeatingmman___________________________________________________________________ co ds 9 ddingcomment_____________________________________________________________________ A 10 ry___________________________________________________________________________11Summa III. Using variables _____________________________ 12__________________________ Movinginaspiral____________________________________________________________________12_____________________________________________________________________ Random numbers 13 Summay___________________________________________________________________________13r ________________________________ _____________________ IV. Control structures 14The if statement 14 ______________________________________________________________________ _____________________________________________________________________ The do statement 15 Summary_____________________________________________________________________15______ ________________________________ ______________________________ V. Sensors 16ngora___________________________________________________________________16Waiti f sensor g_______________________________________________________________16Actin on a touch sensor Lightsensors________________________________________________________________________17ummay___________________________________________________________________________18S r _________________________________________________ VI. Tasks and subroutines 19Tasks 19 ______________________________________________________________________________ Subroutines 20 __________________________________________________________________________ _______________________________________________________________________ Inline functions 20 Definingmacro’s________________________________________________21 _______ ______________ y___________________________________________________________________________22Summar VII. Making music ________ 23_ ______________________________________________ Built-in sounds 23 _______________________________________________________________________ Playingsic________________________________________________________________________23mu y___________________________________________________________________________ Summar 24 VIII. More about motors 25__________________________________________________ Stopping gently __ ________ 25 ____________________________________________________________ Advanced commands 25 __________________________________________________________________ Varyingmotorsp__________________________________________________________________26eed Summary ________________26 ___________________________________________________________ IX. More about sensors 27___________________________________________________ Sensormodeandtype__________________________________________________________________27____________________________________________________________________ The rotation sensor 28 Puttingmultiplesensorsononeinput______________________________________________________28Makingaproximitysensor______________________________________________________________29