NXC_tutorial.pdf

(312 KB) Pobierz
190135044 UNPDF
Programming
LEGO NXT Robots
using NXC
(beta 30 or higher)
(Version 2.2, June 7, 2007)
by Daniele Benedettelli
with revisions by John Hansen
Preface
As happened for good old Mindstorms RIS, CyberMaster, and Spybotics, to unleash the full power of
Mindstorms NXT brick, you need a programming environment that is more handy than NXT-G, the National
Instruments Labview-like graphical language that comes with NXT retail set.
NXC is a programming language, invented by John Hansen, which was especially designed for the Lego robots.
If you have never written a program before, don't worry. NXC is really easy to use and this tutorial will lead you
on your first steps towards it.
To make writing programs even easier, there is the Bricx Command Center (BricxCC). This utility helps you to
write your programs, to download them to the robot, to start and stop them, browse NXT flash memory, convert
sound files for use with the brick, and much more. BricxCC works almost like a text processor, but with some
extras. This tutorial will use BricxCC (version 3.3.7.16 or higher) as integrated development environment (IDE).
You can download it for free from the web at the address
http://bricxcc.sourceforge.net/
BricxCC runs on Windows PCs (95, 98, ME, NT, 2K, XP, Vista). The NXC language can also be used on other
platforms. You can download it from the web page
http://bricxcc.sourceforge.net/nxc/
Most of this tutorial should also apply to other platforms, except that you loose some of the tools included in
BricxCC and the color-coding.
The tutorial has been updated to work with beta 30 of NXC and higher versions. Some of the sample programs
will not compile with versions older than beta 30.
As side note, my webpage is full of Lego Mindstorms RCX and NXT related content, including a PC tool to
communicate with NXT:
http://daniele.benedettelli.com
Acknowledgements
Many thanks go to John Hansen, whose work is priceless!
- 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 ____________________________________________________________________ 8
Summary ____________________________________________________________________________ 9
II. A more interesting program _______________________________________________ 10
Making turns ________________________________________________________________________ 10
Repeating commands _________________________________________________________________ 10
Adding comments ____________________________________________________________________ 11
Summary ___________________________________________________________________________ 12
III. Using variables_________________________________________________________ 13
Moving in a spiral ____________________________________________________________________ 13
Random numbers ____________________________________________________________________ 14
Summary ___________________________________________________________________________ 15
IV. Control structures_______________________________________________________ 16
The if statement______________________________________________________________________ 16
The do statement _____________________________________________________________________ 17
Summary ___________________________________________________________________________ 17
V. Sensors ________________________________________________________________ 18
Waiting for a sensor __________________________________________________________________ 18
Acting on a touch sensor _______________________________________________________________ 19
Light sensor_________________________________________________________________________ 19
Sound sensor ________________________________________________________________________ 20
Ultrasonic sensor_____________________________________________________________________ 21
Summary ___________________________________________________________________________ 22
VI. Tasks and subroutines ___________________________________________________ 23
Tasks ______________________________________________________________________________ 23
Subroutines _________________________________________________________________________ 24
Defining macros _____________________________________________________________________ 25
Summary ___________________________________________________________________________ 26
VII. Making music _________________________________________________________ 28
Playing sound files ___________________________________________________________________ 28
Playing music _______________________________________________________________________ 28
Summary ___________________________________________________________________________ 30
VIII. More about motors ____________________________________________________ 31
Stopping gently ______________________________________________________________________ 31
Advanced commands _________________________________________________________________ 31
PID control _________________________________________________________________________ 33
Summary ___________________________________________________________________________ 34
IX. More about sensors _____________________________________________________ 35
Sensor mode and type _________________________________________________________________ 35
The rotation sensor ___________________________________________________________________ 36
Putting multiple sensors on one input _____________________________________________________ 37
Summary ___________________________________________________________________________ 38
X. Parallel tasks ___________________________________________________________ 39
A wrong program ____________________________________________________________________ 39
Critical sections and mutex variables _____________________________________________________ 39
Using semaphores ____________________________________________________________________ 40
Summary ___________________________________________________________________________ 41
- 3 -
XI. Communication between robots ___________________________________________ 42
Master – Slave messaging ______________________________________________________________ 42
Sending numbers with acknowledgement __________________________________________________ 43
Direct commands ____________________________________________________________________ 45
Summary ___________________________________________________________________________ 45
XII. More commands _______________________________________________________ 46
Timers _____________________________________________________________________________ 46
Dot matrix display____________________________________________________________________ 46
File system _________________________________________________________________________ 47
Summary ___________________________________________________________________________ 50
XIII. Final remarks ________________________________________________________ 51
- 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 Tribot, the first rover you have been instructed to build once got
NXT set out of the box. The only difference is that you must connect right motor to port A, left motor to port C
and the grabber motor to port B.
Make sure to have correctly installed Mindstorms NXT Fantom Drivers that come with your set.
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 BricxCC. 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
the text tab).
- 5 -
190135044.001.png
Zgłoś jeśli naruszono regulamin