You are on page 1of 4

Getting Started: a Wall Following Robot

In this simple example we will develop a simulation of a mobile robot that follows the wall to its right.

Starting MobotSim

Open MobotSim or press the “New” button to start a new project if it is already open. MobotSim
always starts with a robot that has the default configuration. We are going to work with these values,
so don’t change the properties of the robot.

Build a suitable environment

You can copy the one from the picture or design your own. Select the objects from the Editing
Toolbar located on the left side of the main screen.

Reading sensors

Let’s start to write the code that run the simulation. Press the button that opens the BASIC Editor .
The first thing the robot needs in order to follow a wall is to get information from the environment.
We are going to make use of its ranging sensors in order to measure distance to objects. In this
simple algorithm, only one sensor is used. Keeping the default configuration (12 ranging sensors
evenly spaced at 30º) we will use the sensor number 4.

The following instruction triggers a sensor to take a reading and stores the distance value (in meters)
in the variable s:

s = MeasureRange(0,4,0)

The first parameter is the number of robot, the second parameter is the number of sensor and the
third parameter is 0 when you don’t want to implement any kind of Certainty Grid method.
Before reading the sensor, it is convenient to place the robot in a position that allows to detect a wall
to follow.

According to the environment of the picture, we can place the robot 0 at, for example, coordinates
X=13, Y=12.5 and orientation angle=0º by mean of this instruction:

SetMobotPosition(0, 13, 12.5, 0)


So, in the initialization subroutine Sub Main that the BASIC Editor automatically generates in each
new macro, add these instructions so that your code looks like this:

Sub Main

SetMobotPosition(0,13,12.5,0)
s = MeasureRange(0,4,0)

End Sub

You can run the code right now by pressing the Play Button either in the Main Screen or the Basic
Editor. However, you won’t have any feedback of what is happening while the macro is running. If we
wanted to know, for example, what is the distance value the sensor has read, we can make use of
the debug.print statement. This instruction displays an expression in the Immediate window. So, let’s
add a line of code that display the sensor reading:

Sub Main

SetMobotPosition(0,13,12.5,0)
s = MeasureRange(0,4,0)
Debug.Print s

End Sub

The Immediate window in normally visible while a macro is running, but since at this stage our macro
is executed almost instantaneously, we need to check the option “Always Split” of the menu View in
the BASIC Editor window. This way, we will be able to see the value that the sensor read after the
macro execution ended.

Programming the Wall Follower algorithm

Now that our robot can “see” the environment, we want to program its behavior. That is, how the
robot will react according to what it sees.

A very simple algorithm to follow a wall to its right, could be:

• if the robot is too close to the wall, then turn left


• if the robot is too far from the wall, then turn right
• otherwise, go straight ahead

Translated to BASIC code, this would be:

If s < 0.7 Then


SetWheelSpeed(0,0,10)
ElseIf s > 0.9 Then
SetWheelSpeed(0,10,0)
Else
SetWheelSpeed(0,10,10)
EndIf

The instruction SetWheelSpeed sets the speed for both, left and right wheels of a robot. The first
parameter is the number of robot, and the second and third parameters are the speed of the left and
right wheels in r.p.m. respectively.

You can play with the values 0.7 and 0.9 meters that determine the “too close” and “too far”
thresholds.
So far, the code looks like this:
Sub Main

SetMobotPosition(0,13,12.5,0)
s = MeasureRange(0,4,0)
Debug.Print s

If s < 0.7 Then


SetWheelSpeed(0,0,10)
ElseIf s > 0.9 Then
SetWheelSpeed(0,10,0)
Else
SetWheelSpeed(0,10,10)
EndIf

End Sub

Moving the robot

We have now the distance that the sensors reads, and the algorithm that determines the reaction of
the robot to it. However, the SetWheelSpeed instruction does not move the robot. This is only the
way we set the instant speed of the wheels. So, we need to achieve that time elapses in our
simulation. The proper function to do this is StepForward. Every time StepForward is called, the
simulation time elapses one time step. The duration of the time step is set with SetTimeStep.

Thus, after setting the speed the robot has to move at, we should call StepForward in order to move
the robot. It is also a good idea to set the duration of the time step at the beginning of the macro.

In our code:

Sub Main

SetTimeStep 0.1
SetMobotPosition(0,13,12.5,0)
s = MeasureRange(0,4,0)
Debug.Print s

If s < 0.7 Then


SetWheelSpeed(0,0,10)
ElseIf s > 0.9 Then
SetWheelSpeed(0,10,0)
Else
SetWheelSpeed(0,10,10)
EndIf

StepForward

End Sub

Closing the loop

Now that we know how to progress the simulation one time step in order to steer the robot according
to sensor data, we want to perform this task repeatedly so that the robot senses the environment
and adjusts its heading at each time step.

This can be coded by using a “for-next” loop. We leave the initializing statements out of the loop and
repeat the ‘read sensor-set speed-elapse 1 time step’ process, for example, 5000 times.
The final version of the code should look like this:
Sub Main

SetTimeStep 0.1
SetMobotPosition(0,13,12.5,0)

For t = 1 to 5000

s = MeasureRange(0,4,0)
Debug.Print s

If s < 0.7 Then


SetWheelSpeed(0,0,10)
ElseIf s > 0.9 Then
SetWheelSpeed(0,10,0)
Else
SetWheelSpeed(0,10,10)
EndIf

StepForward

Next

End Sub

The simulation of the Wall Follower Robot is now ready. Press the play button and see how it works!

You might also like