Академический Документы
Профессиональный Документы
Культура Документы
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 dont change the properties of the robot.
Reading sensors
Lets 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 dont 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 wont 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, lets 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.
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
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!