This is DirtRider. DirtRider is a cleaning robot with simple programmable functions. DirtRider knows how to stay in the cleaning zone but needs instructions so it will cover the entire area. When DirtRider moves over dirt it will clean it up. Dirt keeps coming back so we need DirtRider to keep cleaning the room over and over.
While Loops
In this lesson, you will learn about while loops. A while loop goes forever until some condition is met. Later you will also learn about for loops. A for loop runs a set number of times. In the example below the while loop moves forward and turns right forever. The for loop will move forward and turn right, repeated three times.
Getting Started
Start the simulation.
The cleaning area is a 4m2 area with pink-colored dirt in the outer part and blue-colored dirt in the middle.
When DirtRider cleans a spot, the floor will become dirty again after a short time.
The robot is not allowed to leave the cleaning area.
Our robot starts cleaning the room from the lower left corner. See below.
Robot Commands
robot.move_forward() – Moves robot 1m forward.
robot.turn_right() – Turns right 90 degrees.
robot.turn_left() – Turns left 90 degrees.
robot.move_backward() – Moves robot 1m backward.
robot.is_ok() – True if the simulation is running.
Checkpoints
Checkpoint #1 – First while loop
Open the code file ‘Node: test_node’, which is under NODES. Pay attention to punctuation and indents. Add the following code in the code file under the section: #Your code starts here while robot.is_ok(): robot.turn_right()
After adding the code, let’s run the simulation (click Reset).
Observe the motion of the robot. The robot is continuously rotating. This is because the while loop is repeating infinitely. In this respect, below and above codes works the same. robot.turn_right() robot.turn_right() robot.turn_right() robot.turn_right() … continuous indefinitely
Checkpoint #2 – While loop with multiple commands
Let’s update our code in the code file as below. Pay attention to punctuation and indents: while robot.is_ok(): robot.turn_right()
robot.turn_right() robot.turn_right()
After adding the code, let’s run the simulation again (click Reset)
Did you notice that this code behaves the same as the previous examples?
Checkpoint #3 – Clean all pink dirt.
To help you understand while loops, have a look at this diagram:
We can represent the above diagram by this code: robot.move_forward() robot.turn_right()
Now consider this diagram:
This is represented by the same code, except we add a while loop to repeat over and over. while robot.is_ok(): robot.move_forward() robot.turn_right()
2. Your goal is to send the robot on a path which cleans all the pink dirt over and over. Try to write the code which would implement the diagram below. Focus just on green and implement those commands. The other colors are just repeats of green and handled by the while loop.
Checkpoint #4 – Clean all pink and blue dirt
See if you can implement an algorithm to complete this pattern and clean all of the dirt:
Here is another pattern to try. We removed the turns from the diagram which can be implemented in different ways:
Are there other ways to do it? What would the fastest approach be?