Tutorial Details:
Difficulty Level:
Basic
Topics Covered: Motor
commands for NQC.
Assumed Knowledge:
THe Basics, My
first program
Written By: BILL LANE
BACK
In My First Program we
saw just how easy it was to make our Spybot spin. This is
what the script looked like:
task main()
{
On(OUT_A);
}
So lets have a closer look at that command line; On
(OUT_A);. Spybots have two motor outputs. NQC refers
to the right hand motor as OUT_A
and the left hand motor as OUT_B.
The On command simply
tells the stated motor to run. If we wanted to run both motors
we could have two commands one turning on OUT_A
and one turning on OUT_B.
But NQC makes it even easier than that. We can control both
motors in the one command by using +.
It looks like this:
On(OUT_A + OUT_B);
If you download and run that your Spybot it will move forward
until it hits something or you turn it off. Which is great!
But it would be much better if we could stop it too! It probably
won't surprise you to discover that we can stop our Spybot
by substituting the word Off
for the word On. The problem
is that if the first line tells it to start and the second
line tells it to stop we'll never see it do anything. So what
we really want it to do is to wait for a little while before
executing the Off command,
so we can see it move. NQC let's us do exactly that. Let's
have a look:
On(OUT_A + OUT_B);
Wait(100);
Off(OUT_A + OUT_B);
Using Wait makes the
program pause for the stated period of time. The time is in
100ths of a second. Which means that 100 is equal to 1 second.
So in our example the robot moves forward for a second and
then stops. We could also substitute Off
with Float. Off
acts like a break making our robot stop with a jerk. Float
allows the motor to roll to a halt.
So what next? Shall we go backward? OK! To move backwards
we need to change the direction of the motors. We have a few
options to achieve this. The motors are currently stopped
so we could reverse their direction on one line and then turn
them back on with the next. Like so:
Rev(OUT_A + OUT_B);
On(OUT_A + OUT_B);
We can also combine both commands using OnRev(OUT_A
+ OUT_B); (there is an equivalent command for setting
the direction to forwards and turning on the motors; OnFwd).
Once again you'll want to add a Wait
and then turn both motors Off.
You saw at the start that we could turn by turning just one
motor On. But that's not
the best way to turn. You'll get a much zippier turn by using
both motors. Simply run one motor forward and one motor backwards.
Here's an example:
Fwd(OUT_A);
Rev(OUT_B);
On(OUT_A + OUT_B);
Wait(100);
There's another command we can use that will allow us to
make out Spybot suddenly spin in the opposite direction. Using
Toggle makes the stated
motors switch direction. Using it now makes our forward motor
run in reverse and our reverse motor run forward. Hence it
will spin in the opposite direction. Adding a short pause
and an Off command here's the completed script:
task main()
{
On(OUT_A + OUT_B); //move
forward for 1 second
Wait(100);
Off(OUT_A + OUT_B); //stop
then reverse for 1 second
OnRev(OUT_A + OUT_B);
Wait(100);
Off(OUT_A + OUT_B); //stop
then spin left for 1 second
Fwd(OUT_A);
Rev(OUT_B);
On(OUT_A + OUT_B);
Wait(100);
Toggle(OUT_A + OUT_B); //spin right for
1 sec
Wait(100);
Off(OUT_A + OUT_B); //stop
all motors
}
These aren't the only commands for moving Spybots. But they'll
certainly get you started. To learn about the rest take a
look at Dave Baum's NQC Programmers
Guide. It's part of the standard NQC download so you
mayalready have it . If not it can be downloaded from the
NQC homepage (check the links page for the address).
|