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);
}
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.
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.
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
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.