Lesson 1: What is an Algorithm?
In this Lesson, you will learn what an algorithm is and how using algorithms in VEXcode VR Python enables you to create projects with more diverse behaviors. Algorithmic projects instruct the VR Robot to respond to its environment, which will be necessary to solve the Dynamic Castle Crasher Challenge.
Learning Outcomes
- Identify that algorithms are made up of sequence, selection, and iteration (loops).
- Identify that algorithms are precise sequences of instructions, implemented using programming languages, for processes to be carried out by a VR Robot.
Algorithms
Algorithms are precise sequences of instructions, implemented using programming languages, like VEXcode VR Python, for processes to be carried out by a VR Robot.
The basic elements of an algorithm are sequence, selection, and iteration.
- Sequence - the order in which behaviors and commands are combined in a project in order to produce a desired result.
- Selection - the use of conditional statements in a project. Conditional statements, such as if or if else affect the project flow of a project.
- 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.
This example project from the Unit 8 challenge 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.
def main():
for value in range(3):
for value in range(3):
# 1. Drive to disk using Down Eye Sensor
while not down_eye.near_object():
drivetrain.drive(FORWARD)
wait(5, MSEC)
drivetrain.stop()
# 2. Pick up disk
magnet.energize(BOOST)
# 3. Drive to goal using Distance Sensor
drivetrain.turn_to_heading(180, DEGREES)
while front_distance.get_distance(MM) > 200:
drivetrain.drive(FORWARD)
wait(5, MSEC)
drivetrain.stop()
# 4. Drop disk in goal
magnet.energize(DROP)
drivetrain.drive_for(REVERSE, 100, MM)
# 5. Turn to face the next disk
drivetrain.turn_to_heading(0, DEGREES)
wait(5, MSEC)
# 6. Turn and drive to the next goal
drivetrain.turn_to_heading(90, DEGREES)
drivetrain.drive_for(FORWARD, 800, MM)
# 7. Turn to disks
drivetrain.turn_to_heading(0, DEGREES)
wait(5, MSEC)
Sequence
The way commands are sequenced and combined determines the behaviors of the VR Robot. In this example, each set of behaviors needed to move a disk is executed three times, then the VR Robot moves to the next set of disks and repeats the behaviors, until all of the disks are returned to the goals.
Selection
This project also includes selection which determines the parts of the project that are run based on the sensor conditions. First, the Down Eye Sensor is used to determine when the VR Robot is near a disk, so the disk can be picked up. Then, the Distance Sensor is used to determine when the VR Robot is over the goal, so the disk can be dropped off.
In the first while loop, selection of which commands will be run is based on the sensor values reported by the Down Eye Sensor.
- While no object is detected, the VR Robot will drive forward.
- While an object is detected, then the VR Robot will stop.
In the second while loop, selection of which command will be run is based on the sensor values reported by the Distance Sensor.
- While the distance reported is greater than 200 millimeters (mm), the VR Robot will drive forward.
- While the distance reported is less than 50 millimeters (mm), the VR Robot will stop driving and move to the next part of the project to energize the Electromagnet.
Iteration
The project uses iteration, or “loops”, to repeat actions and repeatedly check reported sensor values. This project contains nested loops – one loop so that the VR Robot can collect all three disks of one color; and a second loop to repeat that process to collect all of the disks on the Playground.
This algorithm is designed to collect and return three disks, three times, to complete the Disk Mover Challenge. The same logic that is applied to collect and move disks repeatedly using sequence, selection, and iteration can also be applied to other challenges, like the Dynamic Castle Crasher Challenge.