Simplier is easier |
|
Robot position and direction | |
Ground modification | |
Create a subroutine | |
Main program and subroutines display | |
Subroutine call | |
The robot goes to the ground center | |
Summary | |
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. |
|
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 : |
|
* Robot direction : |
|
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. |
|
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. |
|
* 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 : |
|
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. |
|
* 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. |
|
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). |
|
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. |
|
NB : You can find the program in the tutorial files folder. | |
- Run the program and try different robot initial position or direction. | |
|