VSCode:C++:Competition execution order

Hi, so I’ve been trying to figure out why my pre_auton() code does not finish executing before the autonomous() and usercontrol() callbacks get called. I changed the program order in main() and put a 2 second wait in pre_auton before the autonomous and usercontrol callbacks get attached to the Competition class. What this reproduces was what I’ve been trying to debug with the Gyro sample in that the autonomous or usercontrol functions get called before pre_auton completes.

My guess is that the compiler is doing something specific for callbacks? I can put checks in the autonomous and usercontrol functions to make sure the pre_auton code has finished executing before continuing, but I’m confused as to why this would be necessary.

Help appreciated, thanks.


#include "vex.h"

using namespace vex;

competition Competition;

void pre_auton(void) {
  printf("pre_auton++\n");
  wait(2, seconds);
  printf("pre_auton--\n");
}

void autonomous(void) {
  printf("autonomous\n");
}

void usercontrol(void) {
  printf("user\n");
  while (1) {
    wait(20, msec); 
  }
}

int main() {
  pre_auton();
  
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  while (true) {
    wait(100, msec);
  }
}

are you starting the robot in a disabled state ?

anyway,

Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);

even without these lines, we register “autonomous” and “usercontrol” as functiona to be called, so if the robot is started without a competition switch or field controller connected that would disable it, usercontrol will run almost immediately.

3 Likes

Hey, yeah, so I’ve tried starting several different ways. I have the VEXnet Comp Switch and have tried both plugged into the PC and standalone. I’m just seeing the same execution order regardless, which doesn’t seem to match what i would expect. Based on your response, it seems like there is some special case on these callbacks that makes them get called regardless of where they are placed in the program?

Thanks,
NIck,