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:
#auton
brain.screen.print("starting auton")
drivetrain.drive(REVERSE, 10, PERCENT)
wait(0.5, SECONDS)
drivetrain.drive(REVERSE, 0, PERCENT)
left_drive_smart.spin_for(REVERSE, 1, SECONDS, 8, PERCENT)
drivetrain.drive(FORWARD, 10, PERCENT)
wait(2, SECONDS)
drivetrain.drive(REVERSE, 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
pass
def usercontrol():
while True:
wait(20, MSEC)
comp = Competition(usercontrol, autonomous)
2 Likes
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/main.py 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():
#auton
#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")
drivetrain.drive(REVERSE, 100, PERCENT)
wait(10, SECONDS)
drivetrain.drive(REVERSE, 0, PERCENT)
right_drive_smart.spin(FORWARD)
wait(1, SECONDS)
right_drive_smart.spin(FORWARD, 0, PERCENT)
drivetrain.drive(REVERSE, 100, PERCENT)
brain.screen.print("ending auton")
def usercontrol():
while True:
wait(10)
stickMovement()
flywheel()
counter = counter +1
if(counter == 5):
brain.screen.clear_screen()
brain.screen.set_cursor(1,1)
counter = 0
comp = Competition(usercontrol, autonomous)
3 Likes
Thank you so much! Marked as solution.
1 Like