Additional Programming Highlights

advertisement
Additional Programming Highlights
This section contains information on Robotics Invention System programming concepts that can be
introduced at different points in the sequenced activities. When appropriate, these highlights are crossreferenced in the Teachers Guide pages.
Repeat For The repeat block is useful when blocks in a
stack form a repetitive pattern. In this case, the
repeating unit can be embedded within the two parts of
the repeat block, and the number of times for the blocks
to be executed can be specified.
Be on the lookout
R/W-2
When students identify a repeating pattern in
a stack, they sometimes include in the repeat
command blocks that are not part of the
pattern. Here is an example:
These four commands
repeat themselves.
These four
commands repeat
themselves two
times.
In this example, the
Wait For and
Beep blocks are
not part of the
Here the repeating unit is inserted
within the repeat block, and the
counter is set for 2 repetitions.
repeating pattern.
Therefore, they
should not be
included in the
repeat block.
What you can do: Use Transparency #10
as a guide, showing students how a repeating
© 2002 The LEGO Group. Used here with permission.
pattern can be embedded in a repeat block
8-1
The counter in the repeat block can also select a
number randomly from a range you specify. Each
time the program runs, the repeat counter will
randomly select a number from within that range.
you insert a stack in the repeat
block, it could be repeated any
number of times between 2
and 10.
© 2002 The LEGO Group. Used here with permission.
Here the range is 2 to 10. If
Note: Additional blocks can be attached to the
repeat block. The program will run top-down,
looping through the repeat block, then dropping to
the stack that follows.
Be on the lookout
Repeat Forever A repeat forever block does what
it says, it repeats a stack embedded in it forever
(well, almost). In fact, it repeats the stack as long as
the program is running. If stacked in a sensor
watcher block, each time the sensor is triggered,
the repeat forever loop will stop and the entire
branch will run again. For example:
When the touch sensor
is pressed, the stack is
triggered. The repeat
forever block will
continue to execute
until the touch sensor is
pressed again. When
it’s pressed, the entire
branch will be carried
out again.
8-2
R/W-3
Students like the idea of having
something repeat forever, and therefore,
they tend to overuse it.
That is, they use it in programs where they
expect to have additional commands
following the repeat block.
However, additional commands cannot
be attached to the “end repeat” part of
the Repeat Forever block. (It is possible to
attach blocks to the Repeat Forever
block, however, they will be ignored.)
Transparency # 10
In this example, the Wait For
and Beep blocks are not part of
the repeating pattern.
Therefore, they should not be
included in the repeat block.
Wait and Beep are
Wait and Beep
executed before
included in the
out the repeat
are erroneously
the program carries
repeat pattern
block. Therefore,
Wait For and
Beep will run once.
© 2002 The LEGO Group. Used here with permission.
8-3
Be on the lookout
Repeat While & Repeat Until The Repeat While and
the Repeat Until blocks are two of the Stack Decision
Makers. Both use input (e.g., from a sensor) to direct the
flow of the program. It might be helpful to think about
these blocks as opposites—the first carries out a stack
while, or as long as, a certain condition is true; the
second carries out a stack until a condition becomes
true.
SDM-5
Students sometimes think that it’s enough to
tell a robot to do something while (or until) a
certain condition is true (e.g., turn motors on
while a bright light is shinning), and that there
is no need to tell it specifically what to do
when the condition becomes false.
For example, to program a robot to turn the
motors on while a bright light is shinning,
students might write the following program,
expecting the motors to turn off when it gets
darker.
Here motors stay on for
as long as the touch
The condition here is
sensor is pressed. As
“If light > 85”
soon as it’s released,
motors turn off.
They are then surprised to see the motors
continue running when it gets darker.
What you can do: You can explain that if
you tell a person to walk while a bright light is
SDM-6
the touch sensor is
pressed. They turn off as
soon as it’s pressed.
8-4
© 2002 The LEGO Group. Used here with permission.
Here motors stay on until
shining, and you do not give any additional
instructions, he or she will know to stop when
the lights are out. Not so for a robot. Telling a
robot to do something while a condition is true
does not tell it what to do when the condition
becomes false. For example, if you added a
Beep block to the above program, the motors
will turn on and stay on while a bright light is
shinning. When the light goes out, a beep will
sound but the motors will stay on because they
were never turned off.
Smart Tasking What happens when you disable Smart
Tasking? By default, Smart Tasking is enabled. To
unselect, you would go to the Setting option on the
Menu bar and click on Smart Tasking On and on Show
Smart Tasking. This will disable Smart Tasking and remove
the dashed lines.
Throughout our curriculum we haven’t asked for Smart
tasking to be turned off. In fact, we recommend leaving
it at the default state. However, as students explore the
working environment, some may do it anyway. If you
look at a program and are puzzled by the behavior it
produces, you should check whether or not Smart
Tasking is disabled. This is a good starting point in your
troubleshooting process.
In general, the main difference between having Smart
Tasking enabled and disabled is in the manner it return
control to the Main stack. When enabled, after a sensor
event interruption, the Main stack picks up at the top of
the logical chunk that was suspended. When Smart
Tasking is disabled, the Main stack will pick up one block
below the block that was suspended.
End program The End Program block is one of the
Advanced blocks in the Small Blocks bin. Placed at the
end of a stack, it stops all stacks from running after its
stack is executed. Note that as soon as the End Program
block is executed the entire program stops, as indicated
by the stopped person icon on the RCX display window.
Executing an End Program block is equivalent to pressing
the Run button on the RCX while a program is running.
H-13
Here is an example where End Program can be useful if
you wanted a two-motor vehicle to turn in place, move
forward for 2 seconds when the sensor is pressed, and
then stop.
© 2002 The LEGO Group. Used here with permission .
Motor A turns on, making Roverbot turn. When the sensor is
pressed, motor C turns on (joining motor A), making Roverbot
move forward for 2 seconds. Then Smart Tasking returns
control to the Main stack, making Roverbot turn again.
8-5
Adding the End Program block to the Sensor
Watcher stack stops the entire program from running.
After executing the Sensor Watcher stack, the
program does not return to the Main stack.
Be on the lookout
Yes or No Touch: The Yes or No block is one of the
Stack Decision Makers. This block controls the flow of the
program based on input from the touch sensor. The
block is always a part of a stack, and therefore, it is
carried out when its turn comes as blocks in the stack
execute. You can set the block to check for one of two
states, pressed or released. (Note that the “click” event
is not available here.)
SDM-2
Students may expect the Stack
Decision Maker to run the attached
stacks each time a sensor is pressed
(just like the sensor watcher). It is
important to help them understand
that the stack decision maker asks
the question once at the exact
moment when its turn comes as the
commands are carried out. The
timing of incoming input is key here.
What you can do: Show
block is carried out once. When
the block is reached, the program
asks, “Is the sensor pressed?
Yes or No?” If the answer is
“Yes,” the stack attached to
Yes will set off. If the answer is
“No,” the stack attached to No
will set off.
© 2002 The LEGO Group. Used here with permission.
The main point here is that this
students the Timing program in
Transparency #3. Go down the
program block by block, asking the
students what the robot would do as
each block is carried out. When you
reach the Yes or No block, say that you
are pressing the sensor and ask the
students which way the program is going
to proceed (Yes branch).
Repeat this exercise, only this time, don’t
press the touch sensor, or press it too
late. Ask them to predict which way the
program will go. Point out that input must
come exactly when the sensor block is
evaluated. If it comes too early or too
late, the condition will be considered
false, and the No branch will execute
8-6
released? Yes or No?” If the
answer is “Yes,” the stack will run.
If the answer is “No,” nothing will
happen because there are no
command blocks in the No branch.
© 2002 The LEGO Group. Used here with permission.
Here the question is “Is the sensor
How Stack Decision Maker Touch is Different from Sensor
Watcher: In some aspects, the Stack Decision Maker
block Yes or No is very similar to a Sensor Watcher. Both
watch for the state of a sensor. They are different,
however, in some import ways: While the Sensor
Watcher monitors the sensor on an ongoing basis (in
very short intervals), the Stack Decision Maker Yes or No
block looks for input once, when its turn comes.
As a result, timing the input to coincide with the
execution of the Yes or No block is critical, while timing is
less important for the Sensor Watcher. (It keeps looking.)
Another important distinction is that each Sensor
Watcher watches for a single event, responding to a
single condition (e.g., if pressed). The stack triggers only
when the condition is true. The Stack Decision Maker Yes
or No is prepared to trigger one stack if the condition is
true and another stack if the condition is false. It triggers
actions both when the answer is yes and when the
answer is no.
8-7
Here the stack will
trigger just once.
once The
program will not go back
to monitor the sensor
because it runs top-
down. Note that here
the touch sensor as you
press the Run button,
or it would be too late
for the Yes or No block
to receive that input.
One of the stacks will
trigger each time the
sensor is pressed. The
other will trigger each
time the sensor is
released.
8-8
© 2002 The LEGO Group. Used here with permission.
you must press and hold
Be on the lookout Students may
Setting the range for light Stack Decision Makers:
Setting light sensor conditions for the Stack Decision
Makers is much like that for the Sensor Watchers with
one exception. The Repeat While and Yes or No
Decision Makers can’t be set to recognize a blink.
SDM-5
Since all Stack Decision Makers look for input when their
turn to execute comes, it is critically important that the
Decision Maker block has a chance to receive and
process incoming sensor input.
The following sequence of three programs illustrates that
the timing of input is critical when working with Stack
Decision Makers.
carry over their experience with
Sensor Watcher, expecting the
Stack Decision Makers to act on
input whenever it comes.
What you can do: Use
Transparency #6A to help guide a
SDM-2
discussion about time-dependent
input. Especially help students
understand that the On AC block
in the program NoChance2 doesn’t
“buy” any time. It takes practically no
In the program
© 2002 The LEGO Group. Used here with permission.
NoChance1, the Yes or
No Stack Decision
Maker happens to be the
first block in the stack. It
will look for input as soon
as the Run button is
pressed. If a light is not
flashed at the sensor at
the same time as the Run
button is pressed, the
Decision Maker will direct
the program to the No
time to execute. Therefore, a bright
light shining at the sensor a fraction
of a second after the Run button had
been pressed is coming in too late for
the Decision Makers to consider it
as input.
branch.
The same will happen in the
program NoChance2,
because executing the On
AC block happens
instantaneously, and therefore
doesn’t buy time.
8-9
In the program Chance,
however, once the Run
3 seconds before the Yes
or No Decision Maker’s
turn comes. For the
Decision Maker to choose
the Yes branch, the sensor
has to receive a bright light
when the 3 seconds are up.
Practically, a user will start
shining a light at the sensor
before the time is up and
continue to shine it for a bit
longer while the Yes or No
block carries out.
Transparency #6A illustrates the programs NoChance1,
NoChance2, and Chance discussed above.
8-10
© 2002 The LEGO Group. Used here with permission.
button is pressed, there are
Download