NBC_tutorial.pdf

(386 KB) Pobierz
190134945 UNPDF
Programming Lego Robots
using NBC
(Version 0.02, Oct 31, 2006)
by Ross Crawford
Preface
The Lego MindStorms NXT robot is a wonderful new toy 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 robot, although visually attractive and much more powerful than the RIS software for the RCX, is still
somewhat limited in its functionality. To unleash the full power of your robot, you need a different programming
environment. NBC is a programming language, written by John Hansen, which is especially designed for the
Lego robots. If you have never written a program before, don't worry. NBC is really easy to use and this tutorial
will tell you all about it. Actually, programming robots in NBC 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 Bricx Command Center. This utility helps you to write your
programs, to send them to the robot, and to start and stop the robot. Bricx Command Center works almost like a
text processor, but with some extras. This tutorial will use Bricx Command Center (version 3.3.7.15 or higher) as
programming environment. You can download it for free from the web at the address
http://bricxcc.sourceforge.net/
Bricx Command Center runs on Windows PC’s (95, 98, ME, NT, 2K, XP). The language NBC can also be
downloaded from the web at address
http://bricxcc.sourceforge.net/nbc/
Acknowledgements
I would like to thank John Hansen for developing NBC. Also many thanks to Mark Overmars for writing his
NQC tutorial, on which this is heavily based.
- 2 -
Contents
Preface ___________________________________________________________________ 2
Acknowledgements ____________________________________________________________________ 2
Contents __________________________________________________________________ 3
I. Writing your first program __________________________________________________ 5
Building a robot ______________________________________________________________________ 5
Starting Bricx Command Center __________________________________________________________ 5
Writing the program ___________________________________________________________________ 6
Running the program __________________________________________________________________ 7
Errors in your program _________________________________________________________________ 8
Changing the speed ____________________________________________________________________ 9
Adding comments _____________________________________________________________________ 9
Summary ___________________________________________________________________________ 10
II. Using variables _________________________________________________________ 11
Moving in different ways ______________________________________________________________ 11
Displaying results on the screen _________________________________________________________ 13
Random numbers ____________________________________________________________________ 13
Summary ___________________________________________________________________________ 14
III. Flow Control __________________________________________________________ 15
The cmp and tst statements _____________________________________________________________ 15
The brcmp and brtst statements__________________________________________________________ 16
The jmp statement ____________________________________________________________________ 16
Loops – repeating code ________________________________________________________________ 18
Summary ___________________________________________________________________________ 19
IV. Sensors _______________________________________________________________ 20
Waiting for a sensor __________________________________________________________________ 20
Acting on a touch sensor _______________________________________________________________ 21
Light sensors ________________________________________________________________________ 21
Summary ___________________________________________________________________________ 23
V. Making music___________________________________________________________ 24
Playing tones ________________________________________________________________________ 24
Playing files ________________________________________________________________________ 24
Creating your own sound files __________________________________________________________ 25
Summary ___________________________________________________________________________ 26
VI. Threads and subroutines _________________________________________________ 27
Threads ____________________________________________________________________________ 27
Subroutines _________________________________________________________________________ 27
Defining macros _____________________________________________________________________ 28
Summary ___________________________________________________________________________ 29
VII. More about motors _____________________________________________________ 30
Stopping gently ______________________________________________________________________ 30
Synchronising motors _________________________________________________________________ 30
Regulating the motor speed_____________________________________________________________ 30
Rotating a specific angle _______________________________________________________________ 31
More advanced motor control ___________________________________________________________ 31
Summary ___________________________________________________________________________ 32
VIII. More about sensors ____________________________________________________ 33
Sensor type _________________________________________________________________________ 33
Sensor mode ________________________________________________________________________ 33
Sound sensor ________________________________________________________________________ 34
- 3 -
Motor as a rotation sensor ______________________________________________________________ 34
Ultrasonic sensor_____________________________________________________________________ 35
More advanced sensor control __________________________________________________________ 35
Putting it all together__________________________________________________________________ 36
Summary ___________________________________________________________________________ 37
IX. Parallel threads ________________________________________________________ 38
A wrong program ____________________________________________________________________ 38
Using mutexes_______________________________________________________________________ 38
Summary ___________________________________________________________________________ 39
X. Communication between robots ____________________________________________ 40
Communication with other NXT bricks ___________________________________________________ 40
Communication with a PC _____________________________________________________________ 40
Communication with other Bluetooth devices ______________________________________________ 40
Summary ___________________________________________________________________________ 40
XI. More commands ________________________________________________________ 41
System calls ________________________________________________________________________ 41
System clock ________________________________________________________________________ 41
Arrays _____________________________________________________________________________ 42
Type declarations ____________________________________________________________________ 43
Type aliases_________________________________________________________________________ 44
Summary ___________________________________________________________________________ 45
- 4 -
I. Writing your first program
In this chapter I will show you how to write an extremely simple program. We are going to program a robot to
move forwards for 4 seconds, then backwards for another 4 seconds, and then stop. Not very spectacular but it
will introduce you to the basic idea of programming. And it will show you how easy this is. But before we can
write a program, we first need a robot.
Building a robot
The robot we will use throughout this tutorial is the “Tribot”, the instructions for which are included with your
NXT set. If you are new to LEGO robotics, I recommend doing the tutorials that come with the LEGO software,
to get acquainted with what your NXT can do. Your robot should look like this:
(Note that you may already have added sensors and a grabber – if so, you should remove them temporarily, as
some of the examples may not work correctly with them attached.)
Starting Bricx Command Center
We write our programs using Bricx Command Center. Start it by double clicking on the icon BricxCC. (I assume
you already installed Bricx Command Center. If not, download it from the web site (see the preface), and install
it in any directory you like.) The program will ask you where to locate the robot. Switch the robot on and press
OK . The program will (most likely) automatically find the robot. Now the user interface appears as shown below
(without a window).
- 5 -
190134945.001.png
Zgłoś jeśli naruszono regulamin