In the previous article, we introduced you to the Scribbler 2 robot and its graphical programming environment. We will be continuing this introduction in this new article, which looks at specific points such as multi-tasks and the use of variables.
In this article, we are going to illustrate the previous concepts by creating a complete obstacle avoidance behaviour.
Obstacle avoidance algorithm implemented by the Scribbler 2 Robot
The algorithm we have decided to implement is probably not the best algorithm ever created, but it offers the advantage of allowing us to explore the different features of the Scribbler 2 Robot programming software that we wish to tackle. Basically, we are going to separate the frontal area of the robot into sectors and try to detect where the obstacles are in these different sectors.
The algorithm is as follows:
- The Scribbler 2 Robot moves forward in a straight line until it meets an obstacle;
- When it does, it makes a quarter turn (the choice of direction is random). It makes a second measurement, and if no obstacle is encountered it moves forward in a straight line. If it encounters an obstacle, it makes a half turn in the opposite direction. If it encounters an obstacle here too, it reverses and makes slightly more than a half turn (random choice of direction). If on the other hand it does not encounter any obstacle, it moves forward in a straight line;
- The robot moves forward in a straight line as long as it does not meet an obstacle; and
- Finally, if the robot’s wheels spin, i.e. they go round but the robot is jammed against an obstacle that the algorithm was unable to avoid, the robot must move in reverse for a few seconds and turn either right or left at random.
For this algorithm, we have chosen behaviour that is identical in different situations to help us illustrate certain concepts, in particular the subroutine concept.
The figure below shows the obstacle avoidance program as designed by us:
The program source can be obtained by following this link: Obstacle avoidance program.
We are going to describe each step of this program.
The main routine
The program is organised into several parts. Each part is called a routine. By default, the program starts with the green subroutine (the gear wheel on the start tile is green, which makes it a program start tile and not just a subroutine start tile).
In the main program, we start with a motion tile where both motors are configured to turn in the same direction at full power. The robot therefore moves forward in a straight line for an unlimited period. We have thus met one of the prerequisites of our algorithm. Remember that a Motion tile, which runs for an unlimited period, will only be stopped by another Motion tile telling the robot to make a different movement or by the end of the program.
We then carry out a series of tests (yellow tiles) to find out if the robot detects an obstacle on its left or on its right, or if the robot detects that its wheels are spinning (i.e. both its motors are active but it is not moving forward).
For each test we decide to display a colour code with the three LEDs and to play a little tune. This is in no way compulsory, but it is a good way of knowing if the code we are producing actually has the expected effects when we activate it.
Finally, depending on the case, we decide to call a subroutine. To call a subroutine, you need to use the following button:
The following window opens:
By clicking on the gear wheel represented in the left-hand button, the colour of the gear wheel changes, thus indicating which subroutine you wish to create. This is created by the program next to your main program, and is displayed in semi-transparent colour as shown in the picture below.
Its colour will remain semi-transparent as long as this subroutine is not called from anywhere in your program. This is very practical, as it allows you to easily identify the “dead” parts in your program.
To call a subroutine, you need to use the following button:
When you click on this button and you place the subroutine call tile in your program, it displays a settings window that is identical to the settings window that opens when you create a subroutine, shown above. When you click on the gear wheel button in the settings window, its colour changes, thus modifying the subroutine called.
The main routine then contains a tile that resets the three LEDs, a 1-second pause tile to temporise the program a little and better understand how it works (this tile causes the robot to behave jerkily; you can delete it to stop this jerky movement).
All of the program seen up to this point is inserted into an infinite loop in order to repeat the overall behaviour (an infinite loop is very common in robotics).
After the infinite loop are two tiles that are never executed if the loop is really infinite. These tiles are only used if you configure a finite number of times for the loop to repeat, which can be useful when debugging the program. These two tiles therefore indicate the end of the program and stop the robot.
The S2 Robot environment test subroutine
This is the orange subroutine in our program. It starts by an if-then-else tile, which uses the “coin toss” test.
This is a random test that activates the program below or next to it with a certain probability (1/2 in our case). This satisfies the need to get the robot to produce behaviour that is not always the same when it encounters an obstacle.
The code underneath causes the robot to rotate by a quarter turn to the left and populates variables (flags) if it then finds obstacles in front of it, or to exit the subroutine if it finds nothing. The fact of exiting the orange subroutine will return the program to the main subroutine at the position where it was, and will therefore cause the robot to move forward in a straight line.
The code on the right performs in the same way, but causes the robot to rotate through a quarter of a turn to the right and not to the left.
Using flags with the S2 programming software
Scribbler Program Maker proposes very simple variables management of the yes/no type only (these are termed Boolean variables). These variables are represented by coloured flags that are raised or lowered. There are a maximum of seven variables.
To raise or lower a flag, use the button below:
The settings window that opens when you drop the Flag tile on the worksheet is shown below:
The button on the left showing the flag allows you to indicate which flag you wish to raise or lower. Once a flag is raised or lowered, you can test its status further on in your program using an if-then-else tile.
In our program, the orange subroutine performs the following operations:
- One of the following two propositions is executed randomly with a probability of a half:
- The robot rotates through a quarter of a turn to the left and tests to see if it finds obstacles. If it does, a green flag is raised if the obstacle is located on its left and a yellow flag is raised if the obstacle is located on its right (we could have used one single flag here to simplify the program, but we have left two flags for those wishing to create an even more precise program). If no obstacle is found, it does not need to go any further in the subroutine and a tile to interrupt the subroutine is executed.
- The robot rotates through a quarter of a turn to the right and tests to see if it finds obstacles. If it does, a pink flag is raised if the obstacle is located on its left and a purple flag is raised if the obstacle is located on its right. If no obstacle is found, it does not need to go any further in the subroutine and a tile to interrupt the subroutine is executed; or
- The colour of the flag is tested. Depending on the colour found, the robot rotates through half a turn to the right or left to explore a new area. If obstacles are found, the robot calls one of the avoidance subroutines. If no obstacle is detected, the program exits the subroutine and returns to the main routine.
In these subroutines, the program implements an obstacle avoidance behaviour. The purple and light blue subroutines are almost identical, the only difference being that the robot does not turn in the same direction, so we will only describe the purple subroutine.
Purple subroutine: Obstacle avoidance on the left
In this subroutine, we use an infinite loop. Each time the loop completes, the robot tests to see if it still detects an obstacle on its right. If it does, it rotates to the left and starts again. If not, it exits the loop by a loop interruption tile.
This tile, shown above, is the tile entitled “Insert a loop interruption”.
Pink get-out-of-dead-end subroutine
This subroutine is called if the robot detects obstacles all around it. In this case, it reverses for 2.5 seconds and then turns randomly to the right or left using the “coin toss” test we saw earlier.
In this article, we created a random exploration and obstacle avoidance behaviour. The main aim of the program proposed was to use as many new behaviour tiles as possible. In particular we saw:
- The subroutine creation tile
- The subroutine call tile
- The pause tile
- The random test tile
- Flag management
- The subroutine interruption tile
- The loop interruption tile
This program has allowed us to take you a little further in your discovery of Scribbler Program Maker. This software is easy to use and particularly suitable for lower secondary school pupils. It doesn’t require any particular programming skills and is purposefully limited in certain aspects such as variables. This software serves as a useful introduction to robotics for younger users and beginners.
Generation Robots (http://www.generationrobots.co.uk)
all use and reproduction subject to explicit prior authorization.