Приклади скриптів
In the next example programs you'll see how basic instructions and programming structures can lead to interesting behaviour of the robot. Try to understand the script and why it works. Even better is to try them yourself by copying the code and pasting it into the RoboMind script panel. Make sure the right map is used before running them.
Example 1 : Write your name
Because the robot is able to paint, you're able to create simple drawing programs. Using фарбуватиБілим()and зупинитиФарбування() you can command the robot to put its brush on the ground or not. When you let the robot move, it will leave a line on the ground. In this manner you can write characters like 'A'.
In the map openArea.map you'll have enough space to create a nice piece of art. Try using the remote control (Run > Remote control) to let the robot paint.
See also: basic instructions: paint, move |
|
#character 'A'
фарбуватиБілим()
вперед(2)
праворуч()
вперед(1)
праворуч()
вперед(2)
назад(1)
праворуч()
вперед(1)
зупинитиФарбування()
Example 2 : Find the white spot
In the map findSpot1.map which you can find in Rhomboid's default map folder, you'll find somewhere on the left a corner containing a white spot. Assume you don't know in advance at what distance this corner is located. How is the robot able to find it if it starts somewhere along the wall? Of course you can count the number of steps the robot has to move yourself, but there are better ways.
Let the robot make one step forward at the time, and let him check if it already sees the spot on its left. If it sees the spot, it has to stand on it and then it is finished. Else it should make another step forward and check again if it already is near the spot. This can be repeated in a loop.
See also: basic instructions:move, see, programming structures: loops, if-structures, end |
|
повторити(){
якщо(ліворучБіле()){
# There's a white spot on your left
ліворуч()
вперед(1)
кінець
}
інакше{
# There's no white spot yet
вперед(1)
}
}
Another approach that shows the same results is the following script. Here the robot repeats moving forward until it doesn't see the wall anymore (so it must be the corner we are looking for). Then the robot turns and moves forward to land on the spot.
повторитиПоки(ліворучЗайнято()){
вперед(1)
}
ліворуч()
вперед(1)
Example 3 : Line follower
In default.map you find a white line in the east. This line forms a track through the environment. How can you make the robot follow this track?
The solution is somewhat similar to the previous example. After the robot is brought to the beginning of the track, it determines what to do step by step.
See also: basic instructions:move, see, programmeerstructuren: loops, procedures, if-structures, end |
|
# Go to the start of the line
праворуч()
вперед(8)
# Keep on track step by step
повторити()
{
якщо(попередуБіле()){
вперед(1)
}
інакше якщо(праворучБіле()){
праворуч()
}
інакше якщо(ліворучБіле()){
ліворуч()
}
інакше якщо(попередуЗайнято()){
кінець
}
}
Another approach is using so-called recursive procedures. In the next script you see that a procedure follow() is defined. In this procedure, after the correct step is performed, you see that follow() is used in its own definition. What you get is a sequence of follow(...follow(...follow(...)...)...)that perform the correct action each time step to fulfil the task. This cyclic definition is called recursion, and it is likely that you'll find it somewhat strange the first time you see it. No worries, play with it and becomes more clear.
# Go to the start of the line
праворуч()
вперед(8)
# Start tracking
follow()
# Recursive definition for tracking
процедура follow(){
якщо(попередуБіле()){
вперед(1)
follow()
}
інакше якщо(праворучБіле()){
праворуч()
follow()
}
інакше якщо(ліворучБіле()){
ліворуч()
follow()
}
інакше якщо(попередуЗайнято()){
# Finish the current procedure call
# (because there is nothing to after
# this return, all calls will be finished
# and the program stops)
повернутися
}
}
Example 4 : Maze runner
How to escape a maze in general? It appears that this hard question has a simple solution. By always following the wall on the right side (or always following the wall on the left side) you will find the exit for sure.
The next script makes the robot find the beacon in for example maze1.map.
See also: basic instructions:move, see,
programming structures: loops, if-structures, end |
|
повторити(){
якщо(праворучЗайнято()){
якщо(попередуПусто()){
вперед(1)
}
інакше{
ліворуч()
}
}
інакше{
праворуч()
вперед(1)
}
якщо(попередуМаяк()){
взяти()
кінець
}
}
|