Programming an autonomous chooser on the V5 controller

I’m new to this forum so sorry in advance for any formatting errors I make in this topic.
I’m trying to make an auto chooser using the controller screen in the Vex Extention on VS Code using C++, and through testing I think all of my logic is right for the most part but I’m unsure whether it’ll work once the controller is plugged into a field. The team that I’m on doesn’t have a competition switch or anything other than the field so the only way I can confirm it works is by testing it through just driver control, but I want to have it programmed so my driver can select an auto while its on a field before the autonomous period starts. I know I could have several different code files on the controller but for me this is just easier because I like to have a lot of autos for many situations and its a hassle to go through several different codes to change one variable, so I thought this would make it simpler and ran into some issues. And I know it could probably be simplified using arrays or something but I didn’t have a lot of time and I had trouble using arrays so I just decided to make it less technical but something I at least know how to do. I’ve gone through other posts about this but didn’t find anything super helpful to me.

Before I send my code could someone tell me how to properly format my code before sending it? I saw a post where someone forgot to format it correctly and one of the moderators told the one who posted it to correct it before he’d look at it.

To format code:
[code] before and [/code] after
Alternatively, three backticks (`) in a row both before and after
Or just click </> when creating a post or topic.

while (selecting && !Competition.isEnabled()) {
       autoChooser();
  }

  inertialDude.calibrate();
  
  if (controllerScreenID == 0) {
    Competition.autonomous(donothing);

  } else if (controllerScreenID == 1) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 2) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 3) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 4) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 5) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 6) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 7) {
    Competition.autonomous(redLeftWP);

  } else if (controllerScreenID == 8) {
    Competition.autonomous(redLeftWP);

  } 

  Competition.drivercontrol(usercontrol);

  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }

The function autoChooser changes the variable controllerScreenID and the bool selecting is only turned false after the auton has been confirmed within the chosing function and ignore that all of the autons being ran are the same, they will be changed.

All of the logic in the autoChooser should be correct, the only question I would have is whether or not I have the right idea with the Competition.isEnabled(), I wanted it to move on if he didn’t select an auton in time for the match to start. I also am not sure if the function will even run in the main, because my team doesn’t have a competition switch, all we have is the physical field.

So my main questions are:
Will this run before the auto starts like intended? And will the Competition.isEnabled() work the way I think it will and move on to the auto/driver control after the match starts?

use the brain screen, pretty sure you can only display and not create buttons on the controller screen. much easier on the brain because it has actual drawing tools and touch screen

I suspect that won’t work, you are registering the autonomous callback after the robot is enabled.

What does autoChooser do ?

The autoChooser function changes the value of the controllerScreenID using the Dpad buttons on the controller, and as you can see in the previous code it takes the ID of whatever screen it is(I made an if statement in the function to change the controller screen based upon the ID to show what auto was about to be selected) on to determine what auton it ran

This is the code in the function

if (frame != controllerScreenID) {
      updateController = true;

    }

    if (updateController) {
      truit.Screen.clearScreen();
      truit.Screen.setCursor(1,19);
      truit.Screen.print(vexBatteryCapacityGet());
      if (controllerScreenID == 0){
        truit.Screen.setCursor(1,0);
        truit.Screen.print("[None]");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 0;
        updateController = false;

      } else if (controllerScreenID == 1) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print("[BL] BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 1;
        updateController = false;

      } else if (controllerScreenID == 2) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL [BR] BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 2;
        updateController = false;

      } else if (controllerScreenID == 3) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR [BLWP] BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 3;
        updateController = false;

      } else if (controllerScreenID == 4) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP [BRWP]");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 4;
        updateController = false;

      } else if (controllerScreenID == 5) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print("[RL] RR  RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 5;
        updateController = false;

      } else if (controllerScreenID == 6) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL [RR] RLWP  RRWP");
        truit.Screen.setCursor(0,0);
        frame = 6;
        updateController = false;

      } else if (controllerScreenID == 7) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR [RLWP] RRWP");
        truit.Screen.setCursor(0,0);
        frame = 7;
        updateController = false;

      } else if (controllerScreenID == 8) {
        truit.Screen.setCursor(1,0);
        truit.Screen.print(" None");
        truit.Screen.setCursor(2,0);
        truit.Screen.print(" BL  BR  BLWP  BRWP");
        truit.Screen.setCursor(3,0);
        truit.Screen.print(" RL  RR  RLWP [RRWP]");
        truit.Screen.setCursor(0,0);
        frame = 8;
        updateController = false;

      } else if (controllerScreenID == 9) {
        truit.Screen.setCursor(1,1);
        truit.Screen.print("Selected Autonomous:");
        truit.Screen.setCursor(3,1);
        truit.Screen.print("Y to Confirm");
        truit.Screen.setCursor(2,1);
        
        if (selectedAutoID == 0) {
          truit.Screen.print("No Auto");

        } else if(selectedAutoID == 1) {
          truit.Screen.print("Blue Left");

        } else if(selectedAutoID == 2) {
          truit.Screen.print("Blue Right");

        } else if(selectedAutoID == 3) {
          truit.Screen.print("Blue Left WP");

        } else if(selectedAutoID == 4) {
          truit.Screen.print("Blue Right WP");

        } else if(selectedAutoID == 5) {
          truit.Screen.print("Red Left");

        } else if(selectedAutoID == 6) {
          truit.Screen.print("Red Right");

        } else if(selectedAutoID == 7) {
          truit.Screen.print("Red Left WP");

        } else if(selectedAutoID == 8) {
          truit.Screen.print("Red Right WP");

        } 

        truit.Screen.setCursor(0,0);
        frame = 9;
        updateController = false;

      }

    }

    if (truit.ButtonRight.pressing() || truit.ButtonLeft.pressing() 
        || truit.ButtonUp.pressing() || truit.ButtonDown.pressing()) {
      isPressedLR = true;

    }

    if (truit.ButtonA.pressing() || truit.ButtonB.pressing()) {
      isPressedAB = true;

    }

    if (isPressedLR && !confirmingAuto) {
      if (truit.ButtonLeft.pressing() && controllerScreenID > 0) {
        controllerScreenID += -1;
        waitUntil(!truit.ButtonLeft.pressing());
        isPressedLR = false;

      } else if (truit.ButtonRight.pressing() && controllerScreenID < 8) {
        controllerScreenID += 1;
        waitUntil(!truit.ButtonRight.pressing());
        isPressedLR = false;

      } else if (truit.ButtonUp.pressing()) {
        if (controllerScreenID >= 4) {
          controllerScreenID += -4;
          waitUntil(!truit.ButtonRight.pressing());
          isPressedLR = false;

        } else {
          controllerScreenID = 0;
          waitUntil(!truit.ButtonRight.pressing());
          isPressedLR = false;

        }

      } else if (truit.ButtonDown.pressing() && controllerScreenID <= 4) {
        controllerScreenID += 4;
        waitUntil(!truit.ButtonRight.pressing());
        isPressedLR = false;

      }

    }

    if (isPressedAB) {
      if (truit.ButtonA.pressing()) {
        confirmingAuto = true;
        selectedAutoID = controllerScreenID;
        controllerScreenID = 9;
        waitUntil(!truit.ButtonA.pressing());
        isPressedAB = false;

      } else if (truit.ButtonB.pressing()) {
        controllerScreenID = selectedAutoID;
        confirmingAuto = false;
        updateController = true;
        waitUntil(!truit.ButtonB.pressing());
        isPressedAB = false;

      }

    }

    printf("%d\n",controllerScreenID;

    if (truit.ButtonY.pressing() && confirmingAuto) {
      autoConfirmed = true;

    }

    if (autoConfirmed || Competition.isAutonomous()) {
      truit.Screen.clearScreen();
      truit.Screen.setCursor(1,1);
      truit.Screen.print(vexBatteryCapacityGet());
      truit.Screen.setCursor(2,1);
      truit.Screen.print("Auto Confirmed");
      selecting = false;

    }

I know it’s probabaly a nightmare to look at but from what I could test with this in driver control the selection process worked just fine, but that was before I put it in main. I tried it while it was in main and it conflicted with driver control because I use some of the same buttons in both the choosing function and teleop, but I havent gotten to test it on a field though with the robot being disabled.

You can’t use the controller when the robot is disabled before a match if plugged into field control.

Is that a rule or will the code just not respond to it? And if thats the case would it be possible to do it on the brain?

Controller will return default values when attempting to read from it while disabled by field control. If you start a program before plugging into field control, you’ll be placed into driver so you could just select before plugging in (which would then disable you). Reading touch input from the brain display is always available, so you could do that.

And that would then go in main correct(I switched to the brain)? I know it doesn’t make sense to go in driver control or auto because then it wouldnt run. But I’m currently testing and it’s in main and its running once and then ignoring any input I try to give it, is that an error on my part or is that because it’s running the driver control? Would it be different if it was plugged into a field?