Duration: 120 min Lecture: Week 6 Learners: UG 3rd year Course Code: CPE313
Robotics Technology
Dr. Mohannad Farag
2021-2022, Semester I
Lesson overview
In this lesson, you will learn how to improve your robot's decision-making capability using conditional statements and nested
loops. You will use sensor data from the robot's environment to control the actions of the robot. This programming technique
will allow you to create shorter and easily maintainable programs. Also, you will learn how to use the Electromagnet gripper on
the VR Robot to pick up and drop disks to solve the Disk Mover challenge and Disk maze challenge.
Learning objectives
1. Identify how to use different conditional statements in Python.
2. Identify dictionary data type.
3. Identify that algorithms are made up of sequence, selection, and iteration
4. Identify how to use nested conditional/ control statements in Python.
5. Describe why to use an Electromagnet gripper in robots.
Keywords
conditional statements, nested loops, electromagnet gripper , algorithm,
iteration, End Effector,
Conditional Statements in Python
https://www.educba.com/if-statement-in-python/
Introduction: Autonomous Orchard Tractor
• As an autonomous tractor, looking for hazards is important for both avoiding damage to the robot and for
the safety of others.
• Using sensors, the robot continually looks for unexpected obstacles in its path.
• When it sees one, it responds by altering its course if possible or stopping to wait until it is safe to continue.
Automated Tractor Obstacle Detection
How can the robot make decisions?
• Using conditional statements
and sensor feedback.
• IF there is a branch in robot’s
path, stop.
• ELSE, keep driving forward. .
Conditional Statements
• The ability to control which sections of
code are run by the robot is a handy
skill for a programmer.
• Conditional statement allows you to
control the decisions your robot can
make.
https://www.educba.com/conditional-statements-in-python/?source=leftnav
Types of Conditional Statements
If statement If Else statement
Types of Conditional Statements. Describe the flowchart
else if Statement Switch Statement
If statement
Example code Robot response
If/else statement
Else/if Statement
Mutable and Immutable Data Types in Python
• Some of the mutable data types in Python are list, dictionary, set and user-defined
classes.
• On the other hand, some of the immutable data types are int, float, decimal, bool,
string, tuple, and range.
https://towardsdatascience.com/https-towardsdatascience-com-python-basics-mutable-vs-immutable-objects-829a0cb1530a
Python Dictionaries
What are dictionaries?
• Dictionaries are used to store data
values in key: value pairs.
• Dictionaries are written with curly
brackets, and have keys and values
https://www.w3schools.com/python/python_dictionaries.asp
Properties Python Dictionaries
Dictionary Items
• Dictionary items are ordered,
changeable, and does not allow
duplicates.
• Dictionary items are presented in
key:value pairs and can be referred to
by using the key name.
https://www.w3schools.com/python/python_dictionaries.asp
Properties Python Dictionaries
Duplicates Not Allowed
• Dictionaries cannot have two items
with the same key:
Dictionary Length
• To determine how many items a
dictionary has, use the len() function:
https://www.w3schools.com/python/python_dictionaries.asp
Properties Python Dictionaries
Dictionary Items - Data Types
• The values in dictionary items can be
of any data type:
https://www.w3schools.com/python/python_dictionaries.asp
Properties Python Dictionaries
type()
From Python's perspective, dictionaries
are defined as objects with the data type
'dict’:
https://www.w3schools.com/python/python_dictionaries.asp
Create switch statement in python using dictionary
• Python language does not have a direct
switch statements. But it could be
implemented using Dictionary.
• Using the get() method, we can access the
item of the dictionary by passing the
arguments as the key and the optional
default value.
• The default value helps to implement the
‘default case’ of a switch statement.
Week6 Task1: Else/if statement with dictionary: Disc Maze playground
1. Create a New Text Project with the Disc Maze
playground
2. Rename it: week6.task1
3. Create switch statement in python using colorDict
dictionary, where the key is the brightness, and the
value is the corresponding color.
58.7: "GREEN",
11.4: "BLUE",
29.9: "RED",
100: "NONE",
Week6 Task1: Else/if statement with dictionary: Disc Maze playground
4. Use the get() method to return the value
“corresponding color” of the passed key “the
brightness”
5. Declare the required variables.
6. Monitor the variables values and the down eye
sensor brightness.
7. Create else/if statement inside an infinite while
loop to solve the disc maze challenge as in the
figure beside.
8. Print the detected color and its brightness in each
else/if branch.
9. Submit your code to MS teams in a word (docx.)
format.
Algorithm Elements & Nested conditional/ control statements
Algorithms
Algorithms are precise sequences of instructions, implemented
using programming languages, like VEXcode VR, for processes
to be carried out by a VR Robot.
The basic elements of an algorithm are sequence, selection,
and iteration.
Algorithms
• Sequence - the order in which
behaviors and commands are combined
in a project in order to produce a
desired result.
Algorithms
• Selection - is the use of conditional
statements in a project.
Conditional statements such as [If then],
or [If then else] affect the project flow of a
VEXcode VR project.
Algorithms
• Iteration - algorithms often use repetition
to execute steps a certain number of times, or
until a certain condition is met. This is also
known as "looping.“
• Iteration can change the project flow by
repeating a behavior a specified number of
times or until a condition is met.
Algorithms
• This example project is an example of an algorithm.
• It includes a combination of loops, sensor data, and
commands put together in a sequence to solve the Disk
Mover Challenge.
• The project uses iteration or “loops” to repeat actions
and check reported sensor values.
Algorithms
• The way commands are sequenced and combined determines the behaviors of the VR Robot.
• This project also includes selection which determines the parts of the project that are run
based on the sensor condition.
Nested conditional/ control statements
• A nested if is an if statement that is the target of
a previous if statement.
if <expr1>:
<statement1>
# Executes statement1 when expr1 is
True
if <expr2>:
<statement2>
# Executes statement2 when expr2 is
True
https://www.educba.com/nested-if-statement-in-python/?source=leftnav
# Inner if-block ends here
# Outer if-block ends here
Week6 Task2: Disc Maze playground with nested loops
1. Start from the: week6.task1 to create week6.task2
2. Navigate the VR Robot through the Disk Maze
Playground from start to finish using the Eye Sensor.
3. The VR Robot should return back to the starting
position after the VR Robot reaches the end (red disk)
and navigate the Disk Maze on a loop for 2 times.
4. Watch the solution video
Solution video
Electromagnetic gripper in robots
Robot End Effector
• End effectors, also known as End-
of-Arm Tooling (EOAT), are
devices that are attached to end
of a robotic arm. They are
designed and used to act as robot
wrists that interact with the
environment.
https://www.destaco.com/products/end-
effectors#:~:text=End%20effectors%2C%20also%20known%20as,that%20interact https://ars.els-cdn.com/content/image/1-s2.0-S0921889018300952-gr4.jpg
%20with%20the%20environment.
Some Types of Robotic End Effector
mechanical grippers vacuum grippers electromagnetic gripper
Electromagnet Gripper in VR robot
• An electromagnet is a type of magnet where a magnetic field
is produced by an electric current.
• The VEX VR Robot has an Electromagnet to pick up and put
down disks that contain metal cores.
• Disks with metal cores can be found on specific VR
Playgrounds and can be used with the Electromagnet on the
VR Robot.
• View the following article for more information about the
Electromagnet on the VR Robot, and to see
how it can be used in VR Playgrounds:
o Electromagnet - Robot Features - VEXcode VR
Electromagnet Gripper commands
magnet.energize(BOOST) magnet.energize(DROP)
Week 6 Challenge
Week 6 Challenge 1: Disk Mover Challenge
1. Create a new Text Project named as week6.task2.
2. Use the Electromagnet to pick up and drop nine disks: three blue, three
red, and three green disks into the matching-colored goals.
3. Solving the Disk Mover Challenge requires the VR Robot to complete
repetitive actions using conditional and control statements.
4. Once all nine disks are in the proper goals, the challenge is complete.
5. Draw the flowchart of the pseudocode first.
6. Submit your code to MS teams in a word (docx.) format.
Note:
• Do not use the hard-coded approach.
• The solution video has the VR Robot run into the wall beside the Solution video
Disk Mover playground
green goal. Try to prevent this behavior in your solution.