9. Practical Guide\9.5 Using Ultrasonic Sensor

9.5  Using Ultrasonic Sensor

Program a robot to move forward at 60% of the full speed in WORLD_1. The robot turns right if detects the obstacles in front is 20 cm away.

    

Flowchart:

Problem analysis:

The robot has two actions – Moving forward and Turning right. Therefore, two statements need to be added:

v  1st statement: The robot will make a right turn if the front ultrasonic sensor reading is less than 20 cm (it has sensed the front obstacle is less than 20 cm away)

v  2nd statement: The robot moves forward if the 1st statement is not executed.

Procedure:

We will write the program code based on the example 9.2.

1.    In the “Practice” panel, click on “AI” to launch the AI programming panel.

2.    Open the “Moving Forward” project.

3.    Choose the “Moving Forward.smp”.

4.    The Moving Forward project is loaded.

5.    Add a new statement - “Right Turn”.

The robot needs to check whether it is approaching to the front obstacles using the front ultrasonic sensor. It only moves forward if there is no obstacle detected within 20cm in front. Hence, the “Right Turn” statement has the higher priority than the “Moving Forward”. We should put the “Right Turn” as the first statement.

6.    The “Right Turn” is now added. It is before the “Moving Forward”. If you accidentally add the “Right Turn” below the “Moving Forward” statement, move it up (Refer to 8.2.2).

7.    Assign conditions and actions to the “Right Turn” statement.

When US_Front < 20, robot turns right.  (WheelRight = +3, WheelLeft = -2. Duration = 60 ms, LED_1 = 0 )

8.    Save project.

9.    You should change the project name as “Navigation”.

10. Build the project. Load the “Navigation.dll” to the RED robot and monitor the performance.

 

Top of Page