Tutorial Details:
Difficulty Level:
Basic
Topics Covered: Basic
motor commands for Mindscript.
Assumed Knowledge:
THe Basics, My
First Program
Written By: BILL LANE
BACK
In My First Program we saw how easy
it was to make our robot move. In this tutorial we want to
take things a bit further. We'll be looking at all the basics
of movement. Moving forward, stopping, reversing, amd we'll
also spend some time improving our turns. To start with let's
have a look at our original script:
program myFirst
{
output left
on 1
main
{
on [left]
}
}
There are a few things we'll want to take note of in this
context. The first is the output
left on 1. Here we're telling the program that we'll
be using output 1 and
that we'll be calling it left.
In Mindscript it is essential that you declare any outputs
(or inputs) before you use them.
The second thing to note is within the main command block,
which is where we'll be putting all our commands for today.
It quite simply says on[left],
and it turns on the motor
called left. Note the
square brackets around the output name. You need to use these
if you want to control more than one output at a time (e.g
you wanted to turn on left and right). While the brackets
were no essential in this example, they are a good habit to
maintain. Here's the example modified to run both motors (and
therefore move forward):
program myFirst
{
output left
on 1
output right on 2
main
{
on [left right]
}
}
N.B. The use of square
brackets is quite different from NQC where you would use round
braces "( )". Also note the absence of a semi-colon
after the command to seperate different commands, which is
required in NQC.
This is great! Now if only it would stop! We can stop it
easily enough using Off.
But if we use it as the next command then it will turn on
and then turn off again straight away and we won't see much
of anything. Therefore, we would like it to pause between
commands so we can see it move. We do this using Wait:
on[left right]
wait 100
off[left right]
The number after wait
indicates how long to wait. It is stated in 100ths of a second
(i.e 100 = 1 second, 200 = 2 seconds). So in our example the
robot will move forward for 1 second and then stop. Mindscript
also offers a simpler version of this sequence. We can use
on for, it looks like
this:
on [left right] for 100
This will produce exactly the same result as the previous
sequence of commands. The next thing we'd like to be able
to do is to change direction. We can do this by using forward
or backward to set the
motors direction. It looks like this:
on[left right]
wait 100
backward [left right]
wait 100
off [left right]
Both backward and forward
have abbreviated forms that work just as well; for forward
use fd; for backward
use bk. It is also possible
to replace off with float.
Off creates a sudden braked
stop while float allows
the robot to coast to a halt.
At the start I mentioned that we'd look at a better turn.
The original turn used only one motor and produces a slow
jerky turn. But we can produce a much better turn by using
two motors. We simply need to get our motors to run in opposite
directions. Heres the code:
fd[left]
bk[right]
on [left right] for 100
To make it turn in the opposite direction we could use reverse.
In NQC reverse tells the
motor to go backwards. But in Mindscript
reverse tells the motor
to go in the opposite direction. Which means if we use it
at the end of our left-hand spin; our forward motor will go
backwards and; our backwards motor will go forwards. The end
result being a right - hand spin. Try this:
fd [ left ]
bk [ right ]
on [ left right ]
wait 100
reverse [ left right ]
wait 100
off [ left right ]
We can simplify the first two lines of our code using direction
(abrreviated form dir
). Direction allows us to set the direction of more than one
motor at a time, by listing them after the command name. The
first output named will go forward and the second output named
will go backward. Allowing us to replace the top two lines
of the previous code block as follows:
dir left right
We can also use direction to set both motors forward or backward
as in the following examples:
dir [left right] [ ] //both
motors forward
dir [ ] [left right] //both
motors backward
Substituting that into our code heres the completed script
for moving our robot forward for 1 second, backwards for 1
second, spin left for 1 second, spin right for one second:
program test{
output left on 1
output right on 2
main{
dir [left right] [ ]
on [ left right ] for 100
dir [ ] [left right]
on [ left right ] for 100
dir left right
on [ left right ] for 100
reverse [left right]
on [ left right ] for 100
}
}
Mindscript offers us even more scope for controlling our
Spybots movement by using some of it's inbuilt programs. But
that's a bit beyond the scope of a Basic tutorial. Have a
look in the Intermediate topics to learn more about controlling
you Spybot's movement.
|