CPE313 Week3
CPE313 Week3
Robotics Technology
Dr. Mohannad Farag
2021-2022, Semester I
Lesson overview
In this week, you will program your robot to use repeated behaviors to perform multiple different tasks using while and for
loops. You will use sensors and values to set loop conditions. Also, you will learn how to use sensor input to navigate the VR
Robot regardless of its surroundings, instead of "hard-coding” movement with only Drivetrain commands
Learning objectives
1. Identify how to use the [for] loop to repeat the blocks inside of it for a set
number of times.
2. Identify that the for/ while loop can accept integers as parameters.
3. Identify that conditions are used to end a for/ while loop.
4. Describe why a for/ while loop would be used in a project.
5. Describe the flow of a project that contains a for/ while loop.
Keywords
https://d36ndnmww3x0xq.cloudfront.net/trix-files/admin/pages/765/content_elements/1782/1594924904154-1588944208488-FiveS_SquareTurns_02.gif
Loops Fundamentals
• What happens? Nothing! The Robot does not move. This is because the (condition) is false.
• The robot will only run the code within a While Loop if the (condition) is true.
• 1 does not equal 2, therefore, the robot will not run the code inside of loop.
Loops Fundamentals
• Loops can especially come in handy when repeating sequences of code similar to the
actions performed by the Container Handling robot.
• A loop allows you to continually execute the same code multiple times.
• Loops take a program’s flow and sends it back to the beginning of the loop.
• The code within the loop will run and then program flow jumps back to the top of the
loop to be executed again.
• You can program your robot to use repeated behaviors to perform multiple different
tasks.
While vs. For Loop
https://d36ndnmww3x0xq.cloudfront.net/trix-
files/admin/pages/767/content_elements/1798/1589551504665-Repeat_4_v06.gif
Week 3 Task2: while loop with function
https://youtu.be/5clQUFOAYsk
Week 3 Task4: while loop with sensors
1. Create a New Text Project with the Grid map playground.
2. Rename it: week3.task4
3. Set robot speed to 100%.
4. Program the robot to begin driving forward with the pen down.
5. Check the distance sensor to draw a green line while 1.5 meter or further away from the wall using
while loop and distance.get_distance(MM).
6. In the while loop set the pen color to GREEN.
7. When the loop ends, print the distance sensor value in the output counsel.
8. Repeat steps 5-7 three times as follows:
1. Distance > 1000: pen color is BLUE
2. Distance > 500: pen color is BLACK
3. Distance > 100: pen color is RED
9. Stop the robot and project once the robot is less than 100mm away from the wall
10. Change the robot speed to 40% and test your code. Then compare distance sensor values.
11. Submit your code to MS teams
Week 3 Challenge
Week 3 Challenge 1: Dynamic Castle Crasher Robot
● Each time the Playground is reset, the layout of the buildings randomly changes. There are ten
total Castle Crasher Playground layouts.
● Keep in mind that the algorithm should be written so that the VR Robot can knock over all five
buildings regardless of the Playground layout.
● Castle building pieces will be detected as objects by the Distance sensor.
● Be careful not to fall off the side of the Playground because there are no walls!
● Use the color sensing block from the Sensing category to detect the hazard area (red stripe) around
the Playground. The hazard area is detected as color "red" to the DownEye sensor.