Auton Selector

I have been hearing about Auton Selectors and wondering how they work. If you guys could explain this that would be great!

During the pre-auton section you can set up something that indicates which auton to use. Most people create buttons on the screen for each auton, but it can be anything from using potentiometers, physical buttons, jumper pins, to lifting up your lift to a set height (yes I had that thought once).

There are examples and templates online already (I know of a PROS one of github) and I’m working on one too (plus something to help people make them for themselves) so there are plenty of resources if you look for them.

1 Like

But how do people create the buttons on the v5 lcd screen. I just not sure how to make a certain region of the screen cause it.

The theory behind it is to create a rectangle with Brain.Screen.drawRectangle(params) and then add a hitbox to it by checking if both the x and y coordinates of a button press are inside of the drawn box. Once you have that you can say ‘if button is pressed, auton = red # 2’ or something.

If you want to poke around here’s a button generator that I’m working on if you want some example code (you’ll probably be looking for the draw() and pressing() methods at the bottom of ButtonClass.h):


excuse my bad coding practices as I’m sure there are a ton here

2 Likes

Here is a basic example I of an autonomous selector in Vexcode…

int autonToRun = 0;

class Button
{
  public:
    int x, y, width, height;
    std::string text;
    vex::color buttonColor, textColor;
    
    Button(int x, int y, int width, int height, std::string text, vex::color buttonColor, vex::color textColor)
    : x(x), y(y), width(width), height(height), text(text), buttonColor(buttonColor), textColor(textColor){}

    void render()
    {
      Brain.Screen.drawRectangle(x, y, width, height, buttonColor);
      Brain.Screen.printAt(x + 10, y + 10, false, text.c_str());
    }

    bool isClicked()
    {
      if(Brain.Screen.pressing() && Brain.Screen.xPosition() >= x && Brain.Screen.xPosition() <= x + width &&
      Brain.Screen.yPosition() >= y && Brain.Screen.yPosition() <= y + width) return true;
      return false;
    }
};

Button autonButtons[] = {
  Button(10, 10, 150, 50, "Auton Red 1", vex::green, vex::black),
  Button(170, 10, 150, 50, "Auton Red 2", vex::white, vex::black),
  Button(10, 70, 150, 50, "Auton Blue 1", vex::white, vex::black),
  Button(170, 70, 150, 50, "Auton Blue 2", vex::white, vex::black)
};

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

  while(true)
  {
    Brain.Screen.clearScreen(vex::white);

    if(!Competition.isEnabled())
    {
      for(int i = 0; i < 4; i++)
      {
        autonButtons[i].render();
        if(autonButtons[i].isClicked())
        {
          autonButtons[autonToRun].buttonColor = vex::white;
          autonButtons[i].buttonColor = vex::green;
          autonToRun = i;
        }
      }
    }

    Brain.Screen.render();
    vex::task::sleep(7);
  }
}

The first thing is a button class to draw the buttons and detect clicks. It hold parameters for the dimensions, text, and colors, which are also changed to signify which autonomous has been selected. Then I created an array of four buttons to handle them in a loop. The loop goes through each button, rending it, then checking if it is being clicked. If it is it updates the colors and changes the autonToRun variable. Also the Brain.Screen.render() is needed if you are going to be rending really fast.

This a relatively basic example, but it should get the job done. If you don’t understand aspects feel free to ask. Also please take the time to understand how it work and not just copy and paste it. If you want, and probably should, make improvements which might include, only rendering when something changes, better button class, ability to run selector then autonomous from drivercontrol, etc.

When programming it helps to have API docs so here are the relevant links…

https://api.vexcode.cloud/v5/html/classvex_1_1competition.html
https://api.vexcode.cloud/v5/html/classvex_1_1brain_1_1lcd.html
https://api.vexcode.cloud/v5/html/index.html

4 Likes

This was developed for VCS - but should port to VEXcode easily.

Im confused, where does the auton code go?

If you are wondering were the autonomous code would go using my example you would do it like this…

void autonomous()
{
    if(autonToRun == 0)
    {
        //your code
    }

    if(autonToRun == 1)
    {
        //your code
    }

    if(autonToRun == 2)
    {
        //your code
    }

    //...
}

Also something else you can do is…

if(autonToRun == 1 || autonToRun == 2)
{
    int turn = 1;
    if(autonToRun == 2) turn = -1;
}

This way you don’t need to repeat the same code for both sides, just multiply turn values by the turn variable to make them negative on one side.

3 Likes