Autonomous callback function ceases


#1

So my team has been struggling to find an efficient way to code Autonomous for our robot. We’ve been using rotateFor and setting the motors that run at the same time as false and that works well enough, but it is a lot of typing. I’ve tried to come up with a way to call functions into the autonomous() function and then set variables for the motor rotation and velocity. However, only the first function is called and then the program stops. Any help or explanation as to why this happens is very appreciated.

// Autonomous Variables
float LDriveRotation;
float RDriveRotation;
float LDriveVelocity;
float RDriveVelocity;
float LiftRotation;
float LiftVelocity;
float ClawRotation;
float ClawVelocity;
float IntakeRotation;
float LocationVal = 0;
bool DriveTF = false;
bool LiftTF = false;
bool ClawTF = false;
bool IntakeTF = false;

// Autonomous Functions
void Drive(){
    LFMotor.rotateFor(LDriveRotation, rotationUnits::rev, LDriveVelocity, velocityUnits::rpm, false);
    LBMotor.rotateFor(LDriveRotation, rotationUnits::rev, LDriveVelocity, velocityUnits::rpm, false);
    RFMotor.rotateFor(RDriveRotation, rotationUnits::rev, RDriveVelocity, velocityUnits::rpm, false);
    RBMotor.rotateFor(RDriveRotation, rotationUnits::rev, RDriveVelocity, velocityUnits::rpm, DriveTF);
}

void Lift(){
    LiftMotor.rotateFor(LiftRotation, rotationUnits::rev, LiftVelocity, velocityUnits::rpm, LiftTF);
}

void Claw(){
    ClawMotor.rotateFor(ClawRotation, rotationUnits::deg, ClawVelocity, velocityUnits::rpm, ClawTF);
}

void Punch(){
    PunchMotor.rotateFor(1, rotationUnits::rev, 100, velocityUnits::pct, true);
}

void Intake(){
    IntakeMotor.rotateFor(IntakeRotation, rotationUnits::rev, -200, velocityUnits::rpm, IntakeTF);
}

void Reset(){
    LDriveRotation = 0;
    RDriveRotation = 0;
    LDriveVelocity = 0;
    RDriveVelocity = 0;
    LiftRotation = 0;
    LiftVelocity = 0;
    ClawRotation = 0;
    ClawVelocity = 0;
    IntakeRotation = 0;
    DriveTF = false;
    LiftTF = false;
    ClawTF = false;
    IntakeTF = false;
}


void pre_auton( void ) {

}



void autonomous( void ) {
    
    // Calls the corresponding function and sets the motor variables
    DriveTF = true;
    Punch();
    LDriveRotation = 5;
    LDriveVelocity = 100;
    RDriveVelocity = 5;
    RDriveVelocity = 100;
    Drive();
    Reset();
    DriveTF = true;
    LDriveRotation = -3;
    LDriveVelocity = 100;
    RDriveVelocity = -3;
    RDriveVelocity = 100;
    Drive();
    Reset();
    DriveTF = true;
    LDriveRotation = 0;
    LDriveVelocity = 100;
    RDriveVelocity = -2;
    RDriveVelocity = 100;
    Drive();
    Reset();
    DriveTF = true;
    IntakeRotation = 5;
    LDriveRotation = 3;
    LDriveVelocity = 100;
    RDriveVelocity = 3;
    RDriveVelocity = 100;
    Drive();
    Intake();
    Reset();
}

#2

You set the velocity twice instead of the rotation

Idk if that’s why it stops after the first function but you probably want to fix that


#3

I’m not sure why your autonomous code stops after the first function, but judging by the way you’ve made your functions, I’m guessing that you haven’t heard of parameters. In my opinion, the way you’ve made your autonomous is very error prone and adding parameters to your functions will most likely help get rid of the problem. It will also make your code more readable. Here I go:

Parameters:
Parameters are similar to variables, but they are specifically for one function. Just like you declare variables, you can declare parameters in your functions. I’m going to use your drive function and modify it to be cleaner with parameters as an example. When creating a function, you can add parameters inside the parentheses, which are basically extra information that can customize the function.


void Drive( float LDriveRotation, float RDriveRotation, float LDriveVelocity, float RDriveVelocity, bool DriveTF)){
    LFMotor.rotateFor(LDriveRotation, rotationUnits::rev, LDriveVelocity, velocityUnits::rpm, false);
    LBMotor.rotateFor(LDriveRotation, rotationUnits::rev, LDriveVelocity, velocityUnits::rpm, false);
    RFMotor.rotateFor(RDriveRotation, rotationUnits::rev, RDriveVelocity, velocityUnits::rpm, false);
    RBMotor.rotateFor(RDriveRotation, rotationUnits::rev, RDriveVelocity, velocityUnits::rpm, DriveTF);
}

Now so far, it looks like I just made the function more complicated, but that will help in a minute. Let me explain what this means. Inside the parantheses, you see


( float LDriveRotation

. This creates a variable (specifically a parameter) called LDriveRotation. We did this for each of the components that you seem to use in the function:


float RDriveRotation, float LDriveVelocity, float RDriveVelocity, bool DriveTF

.

Now, when you go to actually create your autonomous, instead of setting each variable separately, you can do


Drive(5, 5, 100, 100, true)

. That will replace all of the following code that you have:


    DriveTF = true;
    LDriveRotation = 5;
    LDriveVelocity = 100;
    RDriveVelocity = 5;
    RDriveVelocity = 100;
    Drive();

By calling


Drive(5, 5, 100, 100)

, you are telling the function that 5 is LDriveRotation because that is the first number you put in and that is also the first number in the list of parameters that you put when you created the function. The second number, which is also 5, is RDriveRotation because that is the second number you put in and the second number in the original list of parameters. This goes on for the rest of your variables (parameters). Just like variables, the computer will replace the value of that parameter in every place where it sees that parameter name.

If you add parameters to your autonomous, it would really help make your autonomous easier to read, maintain, and easier for you to make. If you’re confused on what the heck I just said, let me know and I can try to explain further or link a video that explains it better than I can write it.


#4

Do you have all your motors plugged in? If you are missing, say the last motor, the code will get stuck since it’s waiting for it to finish spinning,


#5

@The Electrobotz thank you so much! I knew about parameters, but I wasn’t sure how the syntax worked for VCS. This helped a lot.