Code Bricking Itself

So, I have a competition tomorrow, and a problem arose at practice yesterday I’m not quite sure how to fix.

So, we attached a clamp onto our robot, and coded it to the X and B buttons on the controller. (When X is pressed, rotate 120 degrees forward, etc.) However, we discovered that, in driver control, right after we press the B button to raise the clamp back up/let go of a goal, it’ll perform the action, then the rest of the drive program won’t execute. Meaning, once we press the B button, it kills our robot.

Here's my driver control code, the relevant part is at the bottom
 while (1) {
    vexcodeInit();
  //This next line disables our PID loop from autonomous
  enableDrivePID = false;
  //Drive Code here
  FrontLeftMotor.spin(vex::directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis3.value()*2), vex::velocityUnits::pct);
  FrontRightMotor.spin(vex::directionType::fwd, (Controller1.Axis1.value() - Controller1.Axis3.value()*2), vex::velocityUnits::pct);
  BackLeftMotor.spin(vex::directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis3.value()*2), vex::velocityUnits::pct);
  BackRightMotor.spin(vex::directionType::fwd, (Controller1.Axis1.value() - Controller1.Axis3.value()*2), vex::velocityUnits::pct);
  //Here's the if-else statement controlling the mogo lift
  if(Controller1.ButtonL2.pressing()==true){
    MainLiftMotors.spin(forward); 
  }//This makes the main lift move upwards when L2 is pressed
  else if(Controller1.ButtonR2.pressing()==true){
    MainLiftMotors.spin(reverse);
  }//This makes the main lift move downwards when R2 is pressed
  else{
    MainLiftMotors.stop();
  }//This makes the lift do nothing when neither R2 nor L2 are being pressed


  //Here's the if-else statement controlling the rear mogo lift going up and down
  if(Controller1.ButtonL1.pressing()){
    RearLiftMotor.spin(forward);
  }
  else if(Controller1.ButtonR1.pressing()){
    RearLiftMotor.spin(reverse);
  }
  else{
    RearLiftMotor.stop();
  }
 
  if(Controller1.ButtonR1.pressing()==false){
  RearLiftMotor.setStopping(hold);
  }
  else if(Controller1.ButtonL1.pressing()==false){
    RearLiftMotor.setStopping(hold);
  }


  if(Controller1.ButtonX.pressing()){
    lowerClamp();
  }
  else if(Controller1.ButtonB.pressing()){
    raiseClamp();
  }
  
 
  if(Controller1.ButtonX.pressing()==false){
  FrontClampMotor.setStopping(hold);
  }
  else if(Controller1.ButtonB.pressing()==false){
  FrontClampMotor.setStopping(hold);
  }
//Clamp Functions
void lowerClamp(void){
  dropClampVelocity();
  FrontClampMotor.spinFor(forward,120,degrees);
}


void raiseClamp(void){
  dropClampVelocity();
  FrontClampMotor.spinFor(reverse,120,degrees);
}

Any help would be awesome!

The 4th property in the SpinFor function specifies if the program should wait until the move has completed before continuing. You should set that to false.

FrontClampMotor..spinFor(forward, 90.0, degrees, false);

A little unsolicited feedback. The setStopping function only needs to be set one time. You can still operate the motor and when the motor is told to Stop it will use the same mode each time.

  if(Controller1.ButtonX.pressing()==false){
  FrontClampMotor.setStopping(hold);
  }
  else if(Controller1.ButtonB.pressing()==false){
  FrontClampMotor.setStopping(hold);
  }

Revised:

FrontClampMotor.setStopping(hold);

while(true) {
//...Previous code...
 if(Controller1.ButtonX.pressing()){
    FrontClampMotor.spinFor(forward,120,degrees, false);
  }
  else if(Controller1.ButtonB.pressing()){
    FrontClampMotor.spinFor(reverse,120,degrees, false);
  }
}
3 Likes

you can try multi threading
example from past code:

void score(void) {
wait (.01, seconds); //stops repeating from holding the button
  top.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
  wait(1, seconds);
  top.stop(brake);
  wait(.5, seconds);
  top.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
  wait(1.1, seconds);
  top.stop(vex::brakeType::brake);
}
void usercontrol(void) {
while(1){
// other code
 if (Controller1.ButtonB.pressing()) {
      thread(score).detach();
    }}}

this lets things run at the same time, user control runs while score runs. this is best for complex pre programed movements. for what you are saying, i think @Hudsonville_Robotics solution should work too

what is funny, is that this was similar to the first topic i created (Having two things run at once) , and my solution is the solution i figured out for that

1 Like

Thank you so much!
I’ll try out @Hudsonville_Robotics’s solution, and let you know if it works!

Um did it work orrrr

No, that didn’t work, but we figured it out.

And you are typing up what did fix it so we can all see it and remember it for when the next time someone asks we can point them back to this thread?

2 Likes

Yeah, definitely. So, instead of using this code:

if(Controller1.buttonX.pressing()){
FrontClampMotor.spinFor(forward,120,degrees)
FrontClampMotor.setStopping(hold);
}

Which turns the motor a specific amount.

I used this code:

while(Controller1.buttonX.pressing()){
FrontClampMotor.spin(forward);
}
if(Controller1.buttonX.pressing()==false){
FrontClampMotor.setStopping(hold);
}

Which turns the motor while the button is being pressed, and holds it at its position when it’s not being pressed.

I also used this for a while, which was a sort of hybrid between the both.

if(Controller1.buttonX.pressing()){
FrontClampMotor.spin(forward);
}
else if(Controller1.buttonX.pressing()==false){
FrontClampMotor.setStopping(hold);
}

In the end, this was just an issue of me not knowing much about the mechanical side of things, and approaching it like a programmer.

1 Like

I would say that’s not a good idea. Hook should be on hold no matter what in case someone snatches a goal, right?

One additional suggestion that I’ve made to a few teams running auton skills this weekend: look at using motor timeouts, These can be really useful if you need to wait for something to happen before another action.

A common use for this is raising an arm to a height to drop a goal on a platform. If your arm isn’t reset just right, or there’s a touch of variability in your mechanics, commanding a motor position that ends up just outside of the reachable range would leave the code unable to execute, because it hasn’t hit its commanded position.

Looking back to the beginning, it’s possible your claw motion was getting to a physical stop at 117 degrees, but couldn’t actually reach 120, so it just kept pushing the motor forever trying to hit 120.

Yeah, that’s what I did.

Well I guess since you aren’t running the motor 24/7 it is on hold.

It’s on hold when neither of the buttons that control it are being pressed.