|
First
steps
|
|
How
to write, initialize and run a program
|
|
Launch
RobotProg : you see two windows : the program window where you are
going to draw the programme flowchart and a palette containing
tools to draw the flowchart.
*
Write the flowchart. To
build the flowchart below : - for each flowchart block : take
the block in the palette and click inside program window to place
the block there. - to link the blocks together, choose link
tool, click on a block exit, move the mouse towards the following
block entry. The link will automatically be created when the mouse
pointer arrives at the block entry.
|
|
|
|
A
flowchart must contain one begin block and only one to show where
the program begins, and one or more end blocks
|
|
*
Robot
ground display :
Choose Window > Execution window menu. The ground is
diplayed in another window with buttons on top to control
execution
|
|
|
|
*
Program initialization :
click INIT button or choose Execution > Initailization
menu. The program is verified : if it contains no error, you
may set the initial robot position and direction by clicking a
ground tile or the robot ; the INIT button shows the image of the
robot to be initialized. If the program contains an error, you
may not run it, you must first correct the error.
*
Run the program :
click the Run button or choose Execution > Run menu.
|
|
Moving
the robot
|
|
There
are three commands to move the robot : MoveForward,
Turn right and Turn
left Those commands appear in rectangular blocks,
available in tools palette. * The Move
forward command makes the robot move to the next tile
ahead. But be carefull : if the robot is facing a wall when it
receives the command, it will crash on the wall, this is an
execution error and the programs stops. * The Turn
right and Turn left commands
make the robot rotate by 90 ° on its right or on its left. It
stays on the same tile.
|
|
-
Close previous program window and choose File > New program
menu. - Write a program to have the robot make a U-Turn. Run
the program. Remarks : - a U-Turn is simply turning right
(or left) twice. - in the program window, you can select the
U-Turn purpose. At the end of execution, RobotProg will check if
the purpose is reached.
|
|
Tests
and logical conditions
|
|
-
Close previous program window and choose File > New program
menu. Now, you are going to write a program to go near a
wall. - Choose Go in front of a wall purpose.
|
|
*
Programming
a
test
: - Draw and run the following flowchart :
|
|
To
change the text inside a test block, choose select tool and double
click the block.
|
|
The
logical condition inside a test block is evaluated when the block
is executed. The result is either true or false . If the result is
true, the execution continues at the block following the Y exit (Y
is for Yes or true) ; if the result is false, the execution
continues at the block following the N exit (N is for No or false
) In this example the logical condition is WallAhead,
it is a keyword returning a logical result (true or false) in
respect with the robot position at the moment the condition is
evaluated. If the robot is facing a wall, the program ends, else
the robot moves and the test is again executed.
|
|
*
Step by step execution and current state display . When
a program is executing, you can click Pause button to Pause the
execution, and make next instruction be executed by clicking Next
step button. You may also click Show variables button to display
the current state. A window is displayed, showing various keywords
with their values The complete list of keywords is available
with the Help > Robot language summary menu.
|
|
*
Logical conditions A
logical expression is an expression giving true or false as
result. It is possible to combine various keywords with logical
operators like And,
Or,
No.
Example : WallAhead
And WallOnLeft You
can find a detailed description in the documentation : see chapter
Robot language, predefined functions
|
|
In
this exercise, you are going to reach the purpose "Go to a
corner" The method to achieve that purpose can be the
following one : first the robot goes in front of a wall, then it
moves along the wall, until it reaches a wall ahead. To test if
the robot has reached a corner, you can use the logical condition
WallAhead And ( WallOnLeft Or WallOnRight )
|
|
Simplier
is easier
|
|
Why
subroutines ?
|
|
Often
a program is written to solve a problem which may be very
complicated. So it is necessary to analyze the problem and to
divide it into simplier and more elementary problems. Subroutines
allow the programmer to separate the program into smaller parts
which are easier to solve. A subroutine can be called many times
in the same program, thus avoiding to repeat the same code at
various places in the program. The purpose of the program
built in this chapter is to have the robot go to the ground center
tile, for any initial position or direction. This program will be
divided into subroutines.
|
|
Robot
position and direction
|
|
|
|
A
ground tile position is given by its coordinates x, y. x and y are
positive integer. The rear left tile coordinates are 1, 1. The
rear right tile coodinates are 9,1 in the example above.
|
|
*
Robot position : In
a program, the robot position is indicated by the two keywords
xRobot and yRobot. During the program execution, xRobot and yRobot
have the values x, y corresponding the tile occupied by the robot.
|
|
*
Robot direction : In
a program, the robot direction is indicated by the two keywords
dxRobot and dyRobot. Their values are equal to the variation of
xRobot and yRobot when the robot moves to the next tile ahead : -
if the robot faces the right of the ground : dxRobot is 1 and
dyRobot is 0 - if the robot faces the left of the ground :
dxRobot is -1 and dyRobot is 0 - if the robot faces the front
of the ground : dxRobot is 0 and dyRobot is 1 - if the robot
faces the rear of the ground : dxRobot is 0 and dyRobot is -1 The
only possible values of dxRobot and dyRobot are 0, 1 and -1. One
of the values is 0 the other is not 0. For the example shown in
the above picture dxRobot is 1 and dyRobot is 0
|
|
Ground
modification
|
|
The
ground may be edited except during a program execution. It is
possible to add or remove walls inside the ground, the walls
surrounding the ground may not be removed. Some program purposes
are more challenging when the ground contains inner walls.
|
|
Now,
you are going to built the ground which will be used in this
tutorial. It is the ground
shown above - Choose the Ground > Modify menu. The
ground modification window appears. - Choose the Ground >
New menu to create a new ground. The ground parameters window
appears. - Select a width and a depth of 9 tiles and click OK
button to validate. - Click Robots button then click + button
below : a new robot appears on the ground. - Click tile of
coordinates 3,3 to set the initial robot position (used when the
execution begins) - Click Tiles button, then click tile of
coordinates 5,5. The color of this tile becomes lighter. This tile
is the center of the ground.
|
|
With
the ground modification window, you can add or remove walls and
energy sockets. You can also save and open ground files. The
ground edited above has been saved in the file Ground9x9.bog
inside the file folder of the tutorial.
|
|
-
Click on Use this ground button to close the ground modification
window. Now, if you display the ground with the Window >
Execution window menu, you see the new ground.
|
|
Create
a subroutine
|
|
*
Here, you are going to build a subroutine to turn the robot
towards the left of the ground. * Subroutines are available at
level 2. The current level is shown in the tools palette. In
case the current level is 1, choose Configuration > Level menu
and select level 2. If the program window is not empty, close
it and choose File > New program menu * To create
a new subroutine,
choose Programming > New subroutine menu. The new subroutine
takes the place of main program and is now displayed in the
program window.
|
|
*
Subroutine
name :
each subroutine is identified by the name you assign to it. By
default, when the subroutine is created, it gets the name
Subroutine1. Replace this name by TurnTowardsGroundLeft
|
|
|
|
A
subroutine name may contain letters and digits but no space and
must begin with a letter. Its maximum length is 32 characters. It
must be different from the robot language keywords.
|
|
*
Build the TurnTowardsGroundLeft
subroutine flowchart
: A subroutine flowchart is built the same way as the main
program flowchart. It must contain one and only one begin block
and at least one end block. Here, the flowchart contains the
keyword dxRobot. When the robot is facing left, dxRobot value is
-1. If dxRobot is different from - 1, we rotate the robot by
90° then we redo the test.
|
|
|
|
You
can verify that the flowchart is correct by clicking the Verify
flowchart button in the subroutine header.
|
|
Main
program and subroutines display
|
|
*
Main program and subroutines list : click button List at the
bottom of program window to display (or hide) this list. The list
appears at the left of the program window. The first element in
this list is the main program. It has the same name as the window
and the file (in case it has been saved). Following elements
are created subroutines. Click any element of the list to display
its flowchart. * When a subroutine is displayed, you can also
click Main button to show main program.
|
|
Call
a subroutine
|
|
*
A subroutine is executed when it is called from main program or a
subroutine. This is done by inserting a subroutine
call block
into the flowchart. In order to test the execution of
TurnTowardsGroundLeft subroutine, build the main program
containing the subroutine call : - display main program -
build the following flowchart :
|
|
|
|
*
Subroutine
execution :
a subroutine call block execution causes the execution of the
subroutine begin block, then the subroutine is executed until its
end block, then the execution continues at the block following the
subroutine call block. - Initialize the program (choose
Execution > Initialization menu or click INIT button) - Run
the program. - After execution, click the robot to change its
initial direction, run again the program : in any case, the robot
faces left of the ground when the program end
|
|
Build
and test a new subroutine named TurnTowardsGroundRight
|
|
The
robot goes to the ground center
|
|
*
The ground center is at the intersection of the central column and
the central row (see the
ground). In order that the robot goes to the ground center,
it has to go to a tile in central column, then to a tile in
central row. To achieve that purpose, we need two subroutines. *
GoToCentralColumn
subroutine :
- Create a new subroutine with the name GoToCentralColumn -
Build following flowchart :
|
|
|
|
The
abscissa (x) of central row tiles has the value 5. If the robot
position xRobot is smaller than 5, we rotate it so that it becomes
oriented towards the ground right, otherwise we rotate it towards
the ground left, then we make it move ahead until it reaches the
central row.
|
|
-
in main program, insert a GoToCentralColumn subroutine call and
run the program to test the subroutine
|
|
*
GoToCentralRow subroutine :
|
|
This
subroutine method is similar to the previous one, except we have
to test the robot y position, and we move it towards front or rear
of the ground. - Build new subroutines named
TurnTowardsGroundRear and TurnTowardsGroundFront. - Build new
subroutine named GoToCentralRow - Build the main program :
|
|
|
|
NB
: You can find the program in the tutorial files folder.
|
|
-
Run the program and try different robot initial position or
direction.
|
|
The
robot counts
|
|
Variables
|
|
The
data processed by a program are stored in the computer memory. In
order to access the data, the programmer associates a variable
with the data.
|
|
*
The variable
name
is defined by the programmer. This name allows the computer to
retrieve the data in its memory. The variable name is like a data
address in memory.
|
|
A
variable name may contain letters and digits but no space and must
begin with a letter. Its maximum length is 32 characters. It must
be different from the robot language keywords.
|
|
*The
variable
value
is the content of the memory location referenced by the name. *
In RobotProg, the variables are defined for the whole program
(main program and all subroutines).Their values are integer. All
variables have the value 0 when the program execution begins. *
Example : in this chapter, we are going to count the number of
steps made by the robot. It will be represented by a variable with
the name nSteps.
|
|
Numerical
expressions
|
|
Numerical
expressions are calculus formulas They may contain variables,
numbers, parenthesis and operators. The calculus result is an
integer. Example : assume the variable named nSteps value is 5
when the expression nSteps + 3 is executed. The expression result
will be 8. A numerical expression may also contain numerical
functions predefined by means of keywords, like DistanceFromWall :
this function returns the number of tiles between the robot and
the wall ahead. Example : assume the robot is 4 tiles away from
a wall, the expression 2 * DistanceFromWall will return the result
8
|
|
Setting
a value to a variable
|
|
Setting
a value to a variable is done with an assignement instruction like
: variable Name = numericalExpression Example : nSteps =
DistanceFromWall When the assignment instruction is executed,
the expression value is computed, then this value is stored in the
variable the name of wich appears at the left of the sign = In
a flowchart, the assignment instruction is written inside an
assignment block.
|
|
|
|
The
sign = is used in many programming languages for the assignment
instruction. It has a different meaning from the sign = used in
mathematics. For instance, the instruction nSteps = nSteps + 1
is correct. Assuming nSteps value is 5 before the instruction
is executed, when the expression nSteps + 1 is computed, it gives
6 as result, then this value is stored in the variable nSteps :
this instruction has increased by one the variable value. It is
called an incrementation.
|
|
Using
variables in a program
|
|
Now,
you are going to write a program to find haw many steps were done
by the robot. The robot will move to a wall, then turn to move
to another wall. The method is easy : for each robot move, we
increment the variable nSteps. - Assignment instruction is
available at level 4 : so you have to select level 4 or higher. -
You may use any ground. - Open a new program and enter the
following flowchart :
|
|
|
|
-
Run the program and click the button "Show variables",
so that you can watch the variable nSteps value changing when the
robot moves. - Try to run the program for different initial
positions of the robot : the program will end with different
values of nSteps
|
|
Build
a program moving the robot like in previous program but with a
different way of counting the number of steps, using the keyword
DistanceFromWall For the fisrt section, before the robot moves
: distance = DistanceFromWall For the second section, just
after turn rignt block : distance = distance + DistanceFromWall
|
|
The
robot draws
|
|
Loops
are usefull
|
|
*
In this chapter, you are going to make the robot draw a square
with a side of 5 tiles. To draw a mark on a tile, the robot uses
the instruction Mark . After marking a tile, the robot has to move
and turn if necessary to mark the next tile. The number of marks
to be drawn is 16 : the robot has to move and draw a mark 16 times
and turn 4 times at each square corner. So the program will
contain at least 36 blocks. This is really too much, most of the
blocks are the same because the same action has to be repeated
many times. And in case you want to draw a larger square, you have
to change the whole program. So the solution is to use loops to
repeat a part of the program. A loop is represented by a loop
block where it is possible to insert the blocks to be repeated.
|
|
*
Before starting programming : - Open a new program and select
level 5 - The program purpose is to draw a square, so you can
select this purpose in the header of the program window -
Change the ground : select Ground > Modify menu, then Ground >
Open menu, select the ground named Ground9x9.bog in Files folder
of the tutorial, then click button Use this ground
|
|
Drawing
one side of a square
|
|
Now,
you are going to write a subroutine to draw one side of the
square. - Choose Programming > New subroutine. - Give the
name DrawSide to the subroutine - Build the following flowchart
:
|
|
|
|
*
The
loop header contains
the instruction : For tile = 1 to 4 - For and to are mandatory
keywords in this instruction. - tile is a variable, used here
as the loop variable. - 1 is the loop variable initial value
and 4 is the final value ; those values may be numbers or
numerical expressions. * Execution
progress
: - When the loop is executed for the first time, the loop
variable is set to its initial value : here, tile is set to 1 -
Then the loop
body
(flowchart inside the loop) is executed : here the robot moves
ahead and draws a mark. - Then the instruction Next
is executed : it provokes the execution to continue at the loop
header, that is for the next variable value. - When the loop
header is executed for the second time, the loop variable value is
increased by 1 ; here the tile value becomes 2 - Then the loop
boody is again executed, then the instruction Next etc ... until
the variable value becomes larger than the final value. Here, the
loop will end when tile has the value 5 - After the loop end,
the execution continues at the block following the loop block. *
In order to test this subroutine, you can insert in the main
program a begin block, a subroutine call and an end block, and run
it.
|
|
Loop
header and loop end blocks are rounded because they behave like
begin and end blocks : they show the begin and the end of the
flowchart to be repeated. The first executed block in the loop
body is the one linked to the loop header
|
|
Drawing
a square
|
|
In
order to draw a square,we have to draw a side and turn, this being
repeated 4 times. So we use again a loop to draw the square. -
Display main program and build the following flowchart :
|
|
|
|
-
run the program and verify the robot draws a square.
|
|
Draw
a rectangle : Rectangle sides are not all of equal length. It
is possible to change the subroutine DrawSide to draw a side the
length of which is stored in a variable before subroutine call.
|
|
The
robot plays ball
|
|
How
to get the ball
|
|
*
In this chapter, you are going to write a program to make the
robot find the ball and throw it. First it is necessary to know
how to make the robot go to a tile, the one where the ball is.
(Ball programming is intoduced in next paragraph).
*
So you are going to build a subroutine named GoToTileXY to make
the robot go to the tile with coodinates (x, y). x and y are two
variables set to proper values before the subroutine call. While
the robot is not on tile x, y, w'll move it towards x,y. This will
be done by means of another subroutine named GoTowardsXY and a
While loop
*
Before beginning programming : - Open a new program -
Select level 5 - Choose the ground Ground9x9.bog with the
Ground menu.
*
GoTowardsXY subroutine If the robot is oriented towards x, y it
moves one tile ahead in order to be closer from x, y, else it
rotates by 90° to be better oriented. - Create a new
subroutine with the name GoTowardsXY - Build the following
flowchart :
|
|
|
|
Logical
conditions to test the robot direction :
dyRobot=0 and
(x-xRobot)*dxRobot > 0 : The condition dyRobot = 0 is true
if the robot direction is parallel to x axis. In such a case
dxRobot value is 1 if the robot is facing the right of the ground
and - 1 if it is facing the left of the ground. The value of x
- xRobot represents the move to be done along x axis, if it is
positive the robot has to move towards right, if it is negative
the robot has to move towards left. So if
the values of x - xRobot and dxRobot are both positive or
negative, the product of those values is positive and the robot is
in the good direction.
The condition dxRobot=0 and
(y-yRobot)*dyRobot > 0 allows to test the robot direction along
y axis. It can be explained in the same way
-
Click the button "Verify flowchart" to ensure your
flowchart is correct.
*Subroutine
GoToTileXY - Create a new subroutine called GoToTileXY -
Build the following flowchart :
|
|
|
|
*
The While loop block has the same shape as the For loop block. The
loop header contains the instruction While beginning with the
keyword While followed by a logical condition (as in test
block) If the logical condition result is true, the flowchart
inside the loop body is executed , then the Wend instruction makes
the execution to continue at the block header, the logical
condtion is again evaluated, etc ... If the logical condition
result is false, the loop body is not executed, the execution
continues at the block following the loop
*
In subroutine GoToTileXY, the loop body, that is the subroutine
GoTowardsXY, is executed while the condition xRobot <> x or
yRobot <> y returns the value true ; xRobot <> x is a
logical expression comparing the values of xRobot and x,
its result is true if the values of xRobot and x are different ;
the expression yRobot <> y is true if yRobot is different
from y ; so, the condition xRobot <> x or yRobot <> y
is true if the robot isn't on tile x,y.
*
Testing subroutine GoToTileXY : - In main program, build the
following flowchart. - Run the program and verify the robot is
on tile 2, 7 when execution ends - Run again the program with a
different initial position or direction of the robot - Run
again the program for diffferent values of x and y.
|
|
|
|
How
to program the ball
|
|
*The
ball is displayed on the ground only if the program contains a
keyword related to the ball. When program execution begins, the
ball is randomly put on an empty tile.
*
The keywords xBall and yBall give the ball position on the ground.
For instance you can test if the robot is on the same tile as
the ball with the condition : xRobot = xBall and yRobot =
yBall
*
Ball commands : GetBall, DropBall, ThrowBall Those commands
are to be written inside editable command blocks :
|
|
|
|
GetBall
: if the robot is on the same tile as the ball, it takes the
ball. DropBall : the robot drops the ball on the tile it
occupies. ThrowBall : the robot throw the ball three tiles
ahead. If the ball goes out of the ground, it is automatically
thrown again at random into an empty tile.
|
|
One
robot plays ball
|
|
*
Before beginning : - Choose Configuration > Level menu and
select level 6 in order to use the ball
*
Now you are going to write a program to make the robot find the
ball and throw it. To find the ball, use two variables x and y,
set their values to xBall and yBall and execute the subroutine
GoToTileXY - replace main program with the following one :
|
|
|
|
-
run the program and enjoy...
|
|
How
to program several robots
|
|
*
At level 6, it is possible to execute several robot programs at
the same time on the same ground. Each robot is associated
with a program window. For the levels under level 6, when you
open or create a new program, the current program is closed :
there is only one open program at a time. At level 6, when you
open or create a program, the previously opened programs remain
open.
*
The open programs are parts of the current project.
You
can see a list of the project programs by opening the project
window with the Window > Project menu. With this window, you
can display or hide or remove programs. At level 6, when you close
a program window, the program remains in the project and is still
executed even if it is not displayed. If you don't want the
program to be executed you have to remove it from the project.
*
At level 6, it is possible to make the robots play games together.
Here the robots will play basket-ball game. - Choose
Configuration > Choose a game menu - Select basket-ball
game - Click button Game rules to see the rules - Click OK
to choose the selected game. You can verify that the purpose of
the program is now "Throw the ball into the basket" :
this is the game purpose, it can't be changed while a game is
selected. The ground can't either be modified during a game. You
can see the game ground by displaying the execution window. The
basket is the central tile, its is surrounded by walls.
|
|
Basket-ball
game
|
|
*
Run the previous program - We've got a problem ! In some
cases, the robot crashes against a wall in the ground center, we
have to change the GoTowardsXY subroutine which was not taking
care of walls in front of the robot. - Change GoTowardsXY
subroutine : replace the MoveForward block by a test allowing the
robot to avoid a wall ahead.
|
|
|
|
-
run the program : it's better, the robot doesn't crash against
walls, but there is still a problem : it throws the ball anywhere
in the ground !
*
To score a point, the robot has to throw the ball into the basket.
When it throws the ball, the ball goes three tiles ahead, so
before throwing the ball, the robot has to go to a position three
tiles away from the basket, for instance to the tile 2,5, then it
has to turn itself in direction of the basket. - change the
main program :
|
|
|
|
-
run the program, now the robot scores points....
|
|
*
Basket game with several robots : - To have two robots playing
together, you can save your program and then duplicate the file
and open the duplicate file. - When you run the game, you may
encounter additional problems as the robots may crash one against
the other. So you have to check if a another is ahead with the
keyword TileAheadOccupied .- Another problem is that when a robot
holds the ball, the ball coordinates change when the robot moves.
So you have to modifiy the part of the program where the robot
find the ball.
|