Skip to main content

Controllers and Loops - Python

Controllers and Loops

In competitions, teams must manipulate their robots wirelessly with controllers. The controller is programmed to update the robot based on input from the user. Loops are used in the project so that the robot repeatedly checks for updated input information. Loops allow the project to rapidly check which buttons have been pressed, or how far joysticks have been pushed. Once checked, this information is quickly relayed to the robot so that it responds quickly to the controller's instructions.

The following shows the Tank Control example project from VEXcode V5. The while True infinite loop in this project checks the positions of Axes 2 and 3 forever in order to set the velocity of the motors.

# Library imports
from vex import *

# Begin project code
# Main Controller loop to set motors to controller axis positions
while True:
    left_motor.set_velocity(controller_1.axis3.position(), PERCENT)
    right_motor.set_velocity(controller_1.axis2.position(), PERCENT)
    left_motor.spin(FORWARD)
    right_motor.spin(FORWARD)
    wait(5, MSEC)

Loops are important even for autonomous programming without a controller. A loop helps to simplify and organize repeated commands within a project.