Lego MindStorms: Programming with NQC
by Jonathan Knudsen |
Pages: 1, 2, 3
Program flow and waiting
|
Parts of this article: |
If you've programmed in C, you'll be very comfortable with NQC. You get
the standard for and while loops. NQC also supports if
and if else. The syntax is just like C. If you haven't programmed
with C, it's not hard to learn.
NQC supports two commands that allow you to delay execution of a program.
The Wait() command is a simple delay, where you specify the time
in hundredths of a second. If you want to wait for a certain condition to
become true, use until() instead. This is actually a kind of loop,
with this form:
until([condition])
[statements]
The condition is evaluated; as long as it is false, the statements are
executed repeatedly. The until() loop follows C syntax, such that
multiple statements can be enclosed in brackets. In the next section, we'll
write a program with a simple until() loop.
Reading inputs
You can make your robot dance now, but to program interesting behavior you'll need to respond to RoboTag's sensors. Here's another program that allows RoboTag to avoid obstacles. Save the following program as "Bounce.nqc":
task main() {
SetSensor(SENSOR_1, SENSOR_TOUCH);
while (true) {
// Move forward.
OnFwd(OUT_A + OUT_C);
// Wait for a bump.
until (SENSOR_1 == 1)
;
// Spin left.
Rev(OUT_A);
Fwd(OUT_C);
Wait(50);
}
}
There are several new things in this program. Let's look at the sensor-reading
stuff first. The RCX needs to know what kind of sensors are attached to
its inputs in order to interpret their signals correctly. In this program,
we use SetSensor() to tell the RCX that a touch sensor is attached
to input 1:
SetSensor(SENSOR_1, SENSOR_TOUCH);
NQC includes constants that represent each input: SENSOR_1, SENSOR_2,
and SENSOR_3. There are also constants representing all the basic
sensor types, like SENSOR_LIGHT, SENSOR_ROTATION, SENSOR_CELSIUS
for temperature sensors, and others. If you build your own sensors, there
are more flexible commands for configuring the inputs, but I won't get
into them here.
Once you configure an input, you can read its value by using SENSOR_1,
SENSOR_2,
and SENSOR_3. In this program, we configured input 1 to have a
touch sensor. It will have a value of either 0 or 1.
The algorithm in this program is simple: move forward until the bumper
is hit. Then spin left for half a second. The whole thing is enclosed in
an infinite while() loop.
Variables
NQC supports 31 integer variables. To use a variable, just declare it as in C. Here's a simple example:
int x;
task main() {
x = 14;
x++;
}
Multitasking
It gets even better. The default RCX firmware supports multitasking programs,
i.e. programs that do more than one thing at a time. Up until now, all
of the sample programs have had one task, main. Every program
must have a main; it's the task that is executed when you press
the Run button on the RCX. However, you're free to start other tasks
from main:
task main() {
start secondTask;
start thirdTask;
}
task secondTask() {
}
task thirdTask() {
}
You can stop tasks, too, using stop. Programs can have up to ten tasks. To communicate information between tasks, you'll have to use variables. NQC supports several flavors of subroutines too, but I'm not going to cover them here.
To close, I'll detail a program that instructs RoboTag to roam around avoiding obstacles, until it finds a nice dark place to hide.




