Documentation
Example programs
Overview
Basic instructions
Programming structures
Example programs
- Write your name
- Find the spot
- Line follower
- Maze runner
Lego NXT support
Lego EV3 support
Sparki support
FAQs
More

Example programs

In the next example programs you'll see how basic instructions and programming structures can lead to interesting behavior 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 paintWhite() and stopPainting() 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

write 'A'

#character 'A'
paintWhite()


forward(2)
right()
forward(1)
right()
forward(2)
backward(1)
right()
forward(1)


stopPainting()

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

find spot
repeat(){
    if(leftIsWhite()){
        # There's a white spot on your left
        left()
        forward(1)
        end
    }
    else{
        # There's no white spot yet
        forward(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.

repeatWhile(leftIsObstacle()){
    forward(1)
}


left()
forward(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, programming structures: loops, procedures, if-structures, end

line follower
# Go to the start of the line
right()
forward(8)


# Keep on track step by step
repeat()
{
    if(frontIsWhite()){	
        forward(1)
    }
    else if(rightIsWhite()){
        right()
    }
    else if(leftIsWhite()){
        left()
    }
    else if(frontIsObstacle()){
        end
    }
}

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 fulfill 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
right()
forward(8)


# Start tracking
follow()


# Recursive definition for tracking
procedure follow(){
    if(frontIsWhite()){	
        forward(1)
        follow()
    }
    else if(rightIsWhite()){
        right()
        follow()
    }
    else if(leftIsWhite()){
        left()
        follow()
    }
    else if(frontIsObstacle()){
        # Finish the current procedure call
        # (because there is nothing to after 
        # this return, all calls will be finished
        # and the program stops)
        return
    }
}

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

repeat(){	
    if(rightIsObstacle()){
        if(frontIsClear()){
            forward(1)
        }
        else{
            left()
        }
    }
    else{
        right()
        forward(1)
    }

    if(frontIsBeacon()){	
        pickUp()
        end
    }
}

RoboMind - Copyright © 2005 - 2016 - Research Kitchen