0% found this document useful (0 votes)
26 views20 pages

CPE313 Week3

In Week 3 of the Robotics Technology course, students will learn to program robots using for and while loops to perform repetitive tasks with sensor input. The lesson includes practical tasks involving creating projects that utilize loops to control robot behavior, including navigating and drawing based on distance sensor readings. Students will also complete a challenge to develop an algorithm for a robot to knock over buildings in a dynamic playground layout.

Uploaded by

sccbos31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views20 pages

CPE313 Week3

In Week 3 of the Robotics Technology course, students will learn to program robots using for and while loops to perform repetitive tasks with sensor input. The lesson includes practical tasks involving creating projects that utilize loops to control robot behavior, including navigating and drawing based on distance sensor readings. Students will also complete a challenge to develop an algorithm for a robot to knock over buildings in a dynamic playground layout.

Uploaded by

sccbos31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Duration: 120 min Lecture: Week 3 Learners: UG 3rd year Course Code: CPE313

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

loop, while loop, for loop, conditions, iteration, distance sensor,


decision loop
Robot Repeating Behaviors
Introduction: Container Handler
• Robots are often asked to perform highly repetitive tasks.
• Programmers can take advantage of this fact by writing code that repeats itself as well.
• The Autonomous Container Handling robot picks up and moves large numbers of containers
from one place to another.
• The process is similar each time and the robot can account for differences using sensors.
Loops Fundamentals

• When does the while loop end?

• What is the condition to break the while loop?

This is a VEXcode Pro V5 based on C++ code

https://d36ndnmww3x0xq.cloudfront.net/trix-files/admin/pages/765/content_elements/1782/1594924904154-1588944208488-FiveS_SquareTurns_02.gif
Loops Fundamentals

What will happen with the following condition?

This is a VEXcode Pro V5 based on C++ code

• 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

For loop While loop


Week 3 Task1: Repeating behaviors for a set number of times

1. Create a New Text Project


2. Rename it: week3.task1
3. Open Art Canvas playground
4. Observe the behavior of the robot in the figure beside.
5. Let’s say that the robot is moving forward for 500 mm,
write a code to repeat this behavior for 4 times using a
for loop.
6. Repeat step 5 using a while loop.
7. Use the pen tool to track robot’s path
8. Use the monitor function to observe the variable value. https://d36ndnmww3x0xq.cloudfront.net/trix-
files/admin/pages/767/content_elements/1790/1589555875126-ezgif.com-gif-
9. Use the print function to print the variable value. maker.gif

10. Print “loop finished” when the loop ends


11. Submit your code to MS teams
Describe the robot behavior with a for loop

What is the number of Iterations? https://d36ndnmww3x0xq.cloudfront.net/trix-


files/admin/pages/767/content_elements/1791/1589555209589-gifntext-gif.gif
Understand the code flow with a for loop

This is a VEXcode Pro V5 based on C++ code

https://d36ndnmww3x0xq.cloudfront.net/trix-
files/admin/pages/767/content_elements/1798/1589551504665-Repeat_4_v06.gif
Week 3 Task2: while loop with function

1. Create a New Text Project


2. Rename it: week3.task2
3. Create a function to drive the robot in a rectangle with
a forward command that uses different distances and
speed parameters.
4. Use a While Loop with an infinite condition.
5. Call the previous function to that the robot will
endlessly drive in a rectangle unless the project
manually stopped.
6. Program a text to display on the counsel that indicate https://d36ndnmww3x0xq.cloudfront.net/trix-
files/admin/pages/771/content_elements/1831/1589556751876-gifntext-
each time the loop is being repeated. gif%20copy.gif

7. Submit your code to MS teams


Conditional Loop & Decision loop
Week 3 Task3: Conditional Loop

1. Rename the previous project to: week3.task3


2. Use the brain.timer_reset() command to reset the
brain’s timer.
3. Set the drive speed to 100%
4. Change while loop condition to run the code for less than
15 seconds instead of infinite repetition in the previous
project. You may use brain.timer_time(SECONDS)
command.
5. Use monitor_sensor function to observe the elapsed time.
6. Stop the project after while loop ends.
7. Set the drive speed to 60 % and repeat test your code. https://d36ndnmww3x0xq.cloudfront.net/trix-
files/admin/pages/774/content_elements/1833/1589559596786-gifntext-gif.gif
8. Compare the timer values at the end of the project (at 100
% speed and 60 % speed). Why they are different?
9. Submit your code to MS teams
Distance Sensor

• The Distance Sensor calculates distance by using


the time it takes for ultrasonic waves to bounce
off of an object and return to the sensor.
• Read the article Distance Sensor - Robot Features -
VEX VR for detailed information on how the
Distance Sensor works.
Distance Sensor

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

1. Create a New Text Project

2. Rename it: week3.challenge1

3. Create an algorithm to knock over all


buildings in the playground without
any project modifications.

4. Read the Helpful Hints in the next


page.

5. Submit your code to MS teams


Playground: Dynamic Castle Crasher
Week 3 Challenge 1: Dynamic Castle Crasher Robot
Helpful Hints:

● 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.

You might also like