Programming Controller Buttons

So I am looking for a new, more efficient method to program the buttons on our controller. Currently, I have them all under one single if statement but if a button is pressed too soon after another gets released the functions get mixed up. I have tried splitting up the If statements which work when the arm is going forward but in reverse, the motor fights itself and violently shakes.
any help on how to code this correctly would be appreciated.
I believe that it may be a syntax problem but most likely I will recode it using tasks.

My goal is to have our ring system start running when the arm gets above a certain height and that did not work under the single if statement either

hey can you post your code please so we know what you’re talking about

also make sure it’s enclosed like this → (```)

void myfunction(){
}
3 Likes

Hi, I just switched to a different account but it’s still me. Here is my current code.

void usercontrol(void) {
  // User control code here, inside the loop
  while (1) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on fvoid start(void){
    while (true) {

      // ........................................................................
      // Insert user code here. This is where you use the joystick values to

      Claw.setRotation(0.0, degrees);
      Ringle.setRotation(0.0, degrees);
      mogo.setPosition(0.0, degrees);
      RF.setRotation(0.0, degrees);
      RB.setRotation(0.0, degrees);
      LB.setRotation(0.0, degrees);
      LF.setRotation(0.0, degrees);
      Arm.setPosition(0.0, deg);

      Claw.setVelocity(100.0, percent);
      Ringle.setVelocity(100.0, percent);

      mogo.setVelocity(100.0, percent);
      Arm.setVelocity(100.0, percent);
      Arm.setStopping(hold);
      Claw.setStopping(hold);
      Ringle.setStopping(hold);
      mogo.setStopping(hold);
      LF.setStopping(brake);
      LB.setStopping(brake);
      RF.setStopping(brake);
      RB.setStopping(brake);

      // ........................................................................
      RF.spin(directionType::fwd,
              Controller1.Axis3.position() + -Controller1.Axis1.position(),
              percentUnits::pct);
      RB.spin(directionType::fwd,
              Controller1.Axis3.position() - Controller1.Axis1.position(),
              percentUnits::pct);
      LF.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);
      LB.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);

      if (Controller1.ButtonR1.pressing()) {
        Arm.spin(forward, 100, pct);

      } else if (Controller1.ButtonR2.pressing()) {

        Arm.spin(reverse, 100, pct);
      }

      else if (Controller1.ButtonRight.pressing()) {
        Claw.spin(fwd, 100, pct);
      }

      else if (Controller1.ButtonY.pressing()) {
        Claw.spin(reverse, 100, pct);
      }

      else if (Controller1.ButtonDown.pressing()) {
        Ringle.spin(reverse, 100, pct);
      }

      else if (Controller1.ButtonB.pressing()) {
        Ringle.spin(forward, 100, pct);
      } else if (Controller1.ButtonA.pressing()) {
        Ringle.stop(hold);
      }

      else if (Controller1.ButtonLeft.pressing()) {
        Ringle.stop(hold);
      }

      else if (Controller1.ButtonL1.pressing()) {
        mogo.spin(fwd);
      }

      else if (Controller1.ButtonL2.pressing()) {
        mogo.spin(reverse, 100, pct);
      } else
        mogo.stop(hold);
      Arm.stop(hold);
      Claw.stop(hold);
    }

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

Thanks!

so an easy way to clean this up would be throwing different functions in there and just running them all in the main loop but it isn’t necessary.

What is necessary is grouping the stuff that might cause motor fighting. For instance, you would want all your lift code together, ext.


//Lift Control
      if (Controller1.ButtonR1.pressing()) {
        Arm.spin(forward, 100, pct);
      } else if (Controller1.ButtonR2.pressing()) {
        Arm.spin(reverse, 100, pct);
      } else {       
        Arm.stop(hold);
      }

//Claw Control
       if (Controller1.ButtonRight.pressing()) {
        Claw.spin(fwd, 100, pct);
      } else if (Controller1.ButtonY.pressing()) {
        Claw.spin(reverse, 100, pct);
      } else {
        Claw.stop(hold);
      }

//Ring Mech Control
       if (Controller1.ButtonDown.pressing()) {
        Ringle.spin(reverse, 100, pct);
      } else if (Controller1.ButtonB.pressing()) {
        Ringle.spin(forward, 100, pct);
      } else if (Controller1.ButtonA.pressing()) {
        Ringle.stop(hold);
      }
 
   /*   else if (Controller1.ButtonLeft.pressing()) {
        Ringle.stop(hold);
      } */ // I don't really know why this is here since it appears there's already a stop with the other button

//Mogo Control
      if (Controller1.ButtonL1.pressing()) {
        mogo.spin(fwd);
      } else if (Controller1.ButtonL2.pressing()) {
        mogo.spin(reverse, 100, pct);
      } else {
        mogo.stop(hold);
      }

so by taking all of the code out of one big if statement, the code is no longer limited to doing one thing at a time in some order of priority.

Now to increase your functionality in the future what I’d suggest is adding some sort of p-loop control where the speed of the motor is dependent on the distance it still needs to travel (error) and you can use the same principle to make it hold position better instead of break type hold.

4 Likes

I tried that way but the motors fought themselves when turning reverse, but that way worked for one of our other teams so it might just be something that is in my code that doesn’t belong.

well go ahead and post the code you’ve tried & what happened that wasn’t supposed to happen. From there we can debug.

void usercontrol(void) {
    //ser control code here, inside the loop
  while (1) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on fvoid start(void){
    while (true) {

      // ........................................................................
      // Insert user code here. This is where you use the joystick values to

      Claw.setRotation(0.0, degrees);
      Ringle.setRotation(0.0, degrees);
      mogo.setRotation(0.0, degrees);
      RF.setRotation(0.0, degrees);
      RB.setRotation(0.0, degrees);
      LB.setRotation(0.0, degrees);
      LF.setRotation(0.0, degrees);
      Arm.setPosition(0.0, deg);
      
      Claw.setVelocity(100.0, percent);
      Ringle.setVelocity(100.0, percent);
      
      mogo.setVelocity(100.0, percent);

      // ........................................................................
      RF.spin(directionType::fwd,
              Controller1.Axis3.position() + -Controller1.Axis1.position(),
              percentUnits::pct);
      RB.spin(directionType::fwd,
              Controller1.Axis3.position() - Controller1.Axis1.position(),
              percentUnits::pct);
      LF.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);
      LB.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);
     
       if (Controller1.ButtonR1.pressing()) {
        Arm.spin(forward,100,pct);

      } else if (Controller1.ButtonR2.pressing()) {

        Arm.spin(reverse,100,pct);

      }
      else{Arm.stop(hold);}
       



       
if (Controller1.ButtonRight.pressing()) {
        Claw.spin(fwd,100,pct);
      }

      else if (Controller1.ButtonY.pressing()) {
        Claw.spin(reverse,100,pct);
      } else{Claw.stop(hold);}

       if (Controller1.ButtonDown.pressing()) {
        Ringle.spin(reverse,100,pct);
      }
     // i am trying to change the ring system to automoticslly starting when the arm reaches 100 deg
           else if (Controller1.ButtonB.pressing()) {
        Ringle.spin(forward,100,pct);
      }
      else if (Controller1.ButtonA.pressing()){Ringle .stop(hold);}

      else if (Controller1.ButtonLeft.pressing()){Ringle .stop(hold);}

      
    
     
        if (Controller1.ButtonL1.pressing()) {
        mogo.spin(fwd,100,pct);
      }

      else if (Controller1.ButtonL2.pressing()) {
        mogo.spin(reverse,100,pct);
      }
      else 
        mogo.stop(hold);
      }
     


      

      wait(20, msec); // Sleep the task for a short amount of time to
                      // prevent wasted resources.
    }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

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

I believe the issue is that you are setting the velocities inside the loop.

Try moving this part out of the loop.

I have, but when i do It doesn’t even try to spin reverse and it only turns forward.

Try this

void usercontrol() {
    while (true) {
		RF.spin(directionType::fwd,
              Controller1.Axis3.position() + -Controller1.Axis1.position(),
              percentUnits::pct);
		RB.spin(directionType::fwd,
              Controller1.Axis3.position() - Controller1.Axis1.position(),
              percentUnits::pct);
		LF.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);
		LB.spin(directionType::fwd,
              Controller1.Axis3.position() + Controller1.Axis1.position(),
              percentUnits::pct);
     
		if (Controller1.ButtonR1.pressing()) {
			Arm.spin(forward,100,pct);
		} 
		else if (Controller1.ButtonR2.pressing()) {
			Arm.spin(reverse,100,pct);
		}
		else{
			Arm.stop(hold);
		}
       
		if (Controller1.ButtonRight.pressing()) {
			Claw.spin(fwd,100,pct);
		}
		else if (Controller1.ButtonY.pressing()) {
			Claw.spin(reverse,100,pct);
		} 
		else{
			Claw.stop(hold);
		}

		if (Controller1.ButtonDown.pressing()) {
			Ringle.spin(reverse,100,pct);
		}
		else if (Controller1.ButtonB.pressing()) {
			Ringle.spin(forward,100,pct);
		}
		else if (Controller1.ButtonA.pressing()){
			Ringle.stop(hold);
		}
		else if (Controller1.ButtonLeft.pressing()){
			Ringle .stop(hold);
		}
     
        if (Controller1.ButtonL1.pressing()) {
			mogo.spin(fwd,100,pct);
		}
		else if (Controller1.ButtonL2.pressing()) {
			mogo.spin(reverse,100,pct);
		}
		else 
			mogo.stop(hold);
		}
		
		wait(20, msec);
	}
}

ok i am unable to get to our robot before tuesday at the earliest so I will try it then

Write a state machine. Works flawlessly until you realize you forgot a break statement.