Toggle-style control

So, I’m trying to really automate as much as a can in driver control, and here is a way im doing it.

I want to make it so that if L1 is pressed and released (ex, hitting the spacebar) my indexer (piston) will shoot out and back in. So basically when I press the button, my indexer fires and reloads in one swift motion, basically making my indexer semi automatic.

Im pretty sure Ive got this down here

if(Controller1.ButtonL1.pressing() && diskRecognized && indexerCount == 0){

          Indexer.set(true);
          waitUntil(!Controller1.ButtonL1.pressing());
          indexerCount = 1;
        }

        if(indexerCount == 1){
          
          Indexer.set(false);
          indexerCount = 0;
        }

definitions
diskRecognized is if a distance sensor reads that there is a disk in the disk reserve.
indexerCount is just a variable that I can change from 1-10 (or more)

Theoretically, this should do the trick for semi auto firing, but I want to go more complex

I want to make it so that if I hold this same button (L1) the indexer will do the action above, but infinitely, as long as the button is being held. This is basically an automatic firing control. To do this, I threw together this code and I wanted to see if it would work-

//full auto
        while (Controller1.ButtonL1.pressing() && FAindexerCount ==0){

          Indexer.set(true);
          wait(0.2, sec);
          Indexer.set(false);
          wait(0.2, sec);

          Indexer.set(true);
          wait(0.2, sec);
          Indexer.set(false);
          wait(0.2, sec);

          Indexer.set(true);
          wait(0.2, sec);
          Indexer.set(false);
          wait(0.2, sec);

          FAindexerCount = 1;

          if(FAindexerCount == 1){

            FAindexerCount = 0;
          }

        }

would this work? if not, can I get some pointers? thanks!

Put this into a loop, don’t copy paste it 3 times

1 Like

how would I make sure that said loop only runs while I’m holding down the button?

Using a while loop.

while(Controller1.ButtonL1.pressing()){
 //Code that you want to run
}

Put a for loop inside your current while loop

Let’s convert this to something that would run in usercontrol while simplifying the logic in the code above, while adding a rudimentary joy-stick-controlling the drivetrain. This will get at the heart of the matter:

void usercontrol() {
  while(true) {
    if(Controller1.ButtonL1.pressing()){
          Indexer.set(true);
          waitUntil(!Controller1.ButtonL1.pressing());
    }
    double xAxis = Controller.Axis1.getValue(); // Not sure the actual call here
    double yAxis = Controller.Axis2.getValue();
    driveTrain.tankDrive(xAxis, yAxis); // Again, probably wrong actual call, but illustrative

    wait(20, msec);
  }
}

What do you think the robot will do when the driver has L1 pressed while moving the joysticks around?

1 Like

I think the indexer would set to true, pushing my piston out, but it wouldn’t retract back in because there’s no statement telling it to do so.

From then (I’ve never used a drivetrain, just separate motors)

I think the bot would just move as normal.

Right, but wouldn’t the piston only shoot out and back in once? There’s nothing telling the code to keep repeating inside of that while loop.

So far so good, the bit about the piston not retracting isn’t material here.

If you can, try running this code. While L1 is pressed, the robot will continue moving (or remained stopped) as it was immediately before L1 was pressed. So if it was driving forward at 100%, even if you center the joystick to “stop” the robot, the robot will continue moving forward at 100%

The “unexpected behavior” is due to:

waitUntil(!Controller1.ButtonL1.pressing());

It may be easier to see why if we expand out what waitUtil actually does:

void usercontrol() {
  while(true) {
    if(Controller1.ButtonL1.pressing()){
          Indexer.set(true);
          while(!Controller1.ButtonL1.pressing()) {
              wait(20, msec);
          }
    }
    double xAxis = Controller.Axis1.getValue(); // Not sure the actual call here
    double yAxis = Controller.Axis2.getValue();
    driveTrain.tankDrive(xAxis, yAxis); // Again, probably wrong actual call, but illustrative

    wait(20, msec);
  }
}

Basically, you won’t get to the

    double xAxis = Controller.Axis1.getValue(); // Not sure the actual call here

Line of code until(!) the L1 button is released.

2 Likes

What’s telling the code to repeat is the while loop. The code would be this I think. I am confused with what is FAindexerCount though.

while (Controller1.ButtonL1.pressing() && FAindexerCount ==0){

          Indexer.set(true);
          wait(0.2, sec);
          Indexer.set(false);
          wait(0.2, sec);


          FAindexerCount++;

          if(FAindexerCount == 3){

            FAindexerCount = 0;
          }

F = full, A = auto, indexer = piston pushing disk into flywheel, count = just a way for me to use toggles.

It is a variable I use in that section of code to allow for toggle control.

That is a great explanation, thank you. Sadly I cannot run any code as our robot isn’t even to this point yet. I’m just thinking ahead.

So whats happening here is the button is being held, and the loop runs three times? It seems a little confusing because the first time through, it would push in then out, but the variable increases by 1. The code would then stop because when defining the while, L1 must be pressed AND the variable count must be 0… right?

So I released the code doesn’t have to be that complicated. FAindexerCount should also be a boolean from my understanding of what you’re using it for. I think this is code for FA indexer fire.

while (Controller1.ButtonL1.pressing() && FAindexerCount ==0){

          Indexer.set(true);
          wait(0.2, sec);
          Indexer.set(false);
          wait(0.2, sec)
//Don't know if below line is necessary 
If (!Controller1.ButtonL1.pressing())
break;
          }
1 Like

So with this code as far as I understand, the piston will cycle between true and false until I let go of the button, in which case the loop will break?

Yes. that’s the intention of full auto fire right?

Yep, I just wanted to make sure that we were on the same page. Thanks for all of the help! I’ll update you when I can actually run the code.

Meanwhile, I may just have some writing show up on the brain or something to test it since all I have access to is a brain and a battery lol.

There’s a high chance it will mess up with your drive code. If that happens you’ll need to either run another thread or use function callbacks.

2 Likes

I should be able to work that out pretty easily. I’m new-ish to toggle control because I’ve never really needed to use it and my teammate did it for me last year because I couldn’t be bothered to learn.