Control#
Wait#
The wait(time, units)
command is used to wait for a specific amount of time before moving to the next command.
This is a waiting command and will not allow any subsequent commands to execute until the command is finished.
Parameters |
Description |
---|---|
time |
A positive integer to be the amount of seconds or milliseconds to wait before the next command. |
units |
The units that the time will use: |
Returns: None.
def main():
# Drive forward.
drivetrain.drive(FORWARD)
# Wait 2 seconds.
wait(2, SECONDS)
# Stop the Drivetrain.
drivetrain.stop()
For#
for value in expression_list:
for value in expression_list:
is a loop that repeatedly executes commands for a number of iterations.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
value |
Any integer or previously set variable that reports a value. |
expression_list |
The |
Returns: None.
def main():
# Do the following commands 5 times.
for repeat_count in range(5):
# Drive forward for 5 inches.
drivetrain.drive_for(FORWARD, 5, INCHES)
# Wait 1 second before repeating.
wait(1, SECONDS)
If#
if condition:
# What happens if the condition is True.
The if condition
statement is used to execute a block of commands if the condition evaluates as True.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
condition |
The condition is any Boolean statement, expression, or variable that evaluates as truthy or falsy. |
Returns: None.
def main():
# Drive forward continuously.
drivetrain.drive(FORWARD)
# Continuously check the if statement every 5 Milliseconds.
while True:
wait(5, MSEC)
# If the left bumper is pressed, turn right for 90 degrees.
if left_bumper.pressed():
brain.print("Bumper pressed!")
If/Else#
if condition:
# What happens if the condition is True.
else:
# What happens if the condition is False.
if/else is a control statement that executes one block of commands if the condition evaluates as True, or another block of commands if a condition evaluates as False.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
condition |
The condition is any Boolean statement, expression, or variable that evaluates as truthy or falsy. |
Returns: None.
def main():
# Continuously check the if statement every 5 Milliseconds.
while True:
wait(5,MSEC)
# If the distance sensor has found an object, drive forward.
if distance.found_object():
drivetrain.drive(FORWARD)
# If the distance sensor hasn't found an object, turn to the right.
else:
drivetrain.turn(RIGHT)
If/Elif/Else#
if condition:
# What happens if condition 1 is True, otherwise, check condition 2.
elif condition2:
# What happens if condition 2 is True.
else
# What happens if neither conditions are True.
The elif
statement is a conditional that will check if its condition evaluates to True or False. It is possible to combine many conditions with the use of logical operators such as and
, or
, and not
.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
condition |
The condition is any Boolean statement, expression, or variable that evaluates as truthy or falsy. |
Returns: None.
You can include as many elif
statements in succession as necessary.
def main():
# Continuously check the if statement every 5 Milliseconds.
while True:
wait(5,MSEC)
# If the Down Eye Sensor detects the color green,
# turn right for 90 degrees.
if down_eye.detect(GREEN):
drivetrain.turn_for(RIGHT, 90, DEGREES)
# If the Down Eye Sensor detects the color blue,
# turn left for 90 degrees.
elif down_eye.detect(BLUE):
drivetrain.turn_for(LEFT, 90, DEGREES)
# If the Down Eye Sensor doesn't detect green or blue,
# drive forward.
else:
drivetrain.drive(FORWARD)
Wait Until#
while not condition:
while not
is a loop that repeatedly executes commands as long as the condition evaluates as True.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
condition |
The condition is any Boolean statement, expression, or variable that evaluates as truthy or falsy. |
Returns: None.
def main():
# Continuously check if the VR Robot isn't within 50 MM
# of an object in front of it every 5 Milliseconds.
while not front_distance.get_distance(MM) < 50:
wait(5, MSEC)
# While the VR Robot is farther than 50 MM of an object,
# drive forward.
drivetrain.drive(FORWARD)
# While the VR Robot is closer than 50 MM of an object, stop driving.
drivetrain.stop()
While#
while condition:
while
is a loop that repeatedly executes commands as long as the condition evaluates as True.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Parameters |
Description |
---|---|
condition |
The condition is any Boolean statement, expression, or variable that evaluates as truthy or falsy. |
Returns: None.
def main():
# Continuously check if the VR Robot is within 50 MM of an object
# in front of it every 5 Milliseconds.
while front_distance.get_distance(MM) > 50:
wait(5, MSEC)
# While the VR Robot is farther than 50 MM of an object,
# drive forward.
drivetrain.drive(FORWARD)
# While the VR Robot is closer than 50 MM of an object, stop driving.
drivetrain.stop()
Break Out of a Loop#
The break
command is used to exit a repeating loop immediately.
This is a non-waiting command and allows any subsequent commands to execute without delay.
Returns: None.
def main():
# Drive forward.
drivetrain.drive(FORWARD)
# Continuously check the if statement every 5 Milliseconds.
while True:
wait(5,MSEC)
# If the Front Eye Sensor detects a close object,
# break out of the while loop.
if front_eye.near_object():
break
# Stop driving if out of the while loop.
drivetrain.stop()
Stop Project#
The stop_project()
command is used to stop a running project.
This is a waiting command and prevents any subsequent commands from being executed.
Returns: None.
def main():
# Continuously check the if statement every 5 Milliseconds.
while True:
wait(5,MSEC)
# Stop the project if the left bumper is pressed.
if left_bumper.pressed():
stop_project()
# If the left bumper hasn't been pressed, drive forward.
else:
drivetrain.drive(FORWARD)