Match Auton does not run - Python

I am new to VEX this year, and as my team started late in the season, I had to use python instead of C++. I am using the official VEX extension for vscode.

Anyway, I have coded the autonomous period for our robot. When running it normally, it works fine. However, when connected to field control at comp, it did not run. After some testing, I realised that the auton code started running as soon as the driver pressed run, not when the field control switch was flicked. This meant that our auton did not run at all. Here’s the code:


brain.screen.print("starting auton"), 10, PERCENT)
wait(0.5, SECONDS), 0, PERCENT)
left_drive_smart.spin_for(REVERSE, 1, SECONDS, 8, PERCENT), 10, PERCENT)
wait(2, SECONDS), 0, PERCENT)
left_drive_smart.spin_for(FORWARD, 0.5, SECONDS, 20, PERCENT)
brain.screen.print("ending auton")
wait(12, SECONDS)
flywheelSpeed = 85

This is all outside the while loop. How do I make this run when the actual switch is flicked? Turning the program on when the switch is flicked is not an option here.

Any help is appreciated!

1 Like

You need the auton code inside a function that is triggered by the python competition class object. something along these lines.

def autonomous():
    # some auton code

def usercontrol():
    while True:
        wait(20, MSEC)

comp = Competition(usercontrol, autonomous)

Thank you! once I’ve declared the Competition object, what do I do with it? Do I need to feed it into some other variable? For reference, here is my entire code: 13765Y-code/match_code/src/ at main · AHumanIBelieve/13765Y-code · GitHub
Thank you so much for the help!

1 Like

leaving all the functions as they are, the end of your code is going to end up looking like this.

def autonomous():
    #drivetrain.drive_for(REVERSE, 36, INCHES)
    #drivetrain.turn_for(LEFT, 90, DEGREES)
    #flywheel_smart.spin(REVERSE, -20, PERCENT)
    #drivetrain.drive_for(FORWARD, 20, INCHES)

    brain.screen.print("starting auton")
    brain.screen.print("velocity set"), 100, PERCENT)
    wait(10, SECONDS), 0, PERCENT)
    wait(1, SECONDS)
    right_drive_smart.spin(FORWARD, 0, PERCENT), 100, PERCENT)
    brain.screen.print("ending auton")

def usercontrol():
    while True:
        counter = counter +1
        if(counter == 5):
            counter = 0

comp = Competition(usercontrol, autonomous)


Thank you so much! Marked as solution.

1 Like