I believe the reason why you are getting the error is because spinFor asks for degrees(deg) and not a velocity(pct). Another way you could program the PID is using
BackLeftMotor.spin(fwd, lateralMotorPower, pct);
Edit: Then of course, after the PID loop is over, stop the motor from spinning
It’s hard to tell what is causing the issue without seeing more of your code. Also, do as @MatthewShute suggested and use spin instead of spinFor because spinFor is meant to go for a certain amount of rotations or time, not to set the motors to a certain speed.
You don’t seem to want to give us your code so I am going to take an educated guess at what your problem is here. Based on the previous talk, I’m going to guess you have a scoping issue. When you declare a program element such as a class, function, or variable, its name can only be “seen” and used in certain parts of your program. The context in which a name is visible is called its scope . For example, if you declare your variable lateralMotorPower within a function, lateralMotorPower is only visible within that function body. It has local scope . That means that anything that uses that variable outside that function body will get a clang error telling you that lateralMotorPower is undefined. So my guess is that you are calling BackLeftMotor.spinFor(lateralMotorPower, pct); outside of the scope of lateralMotorPower. If you put both of them inside the same scope you will not get the error.
If this is not your problem, you are going to have to give us more of your code
I didn’t post more of my code because those were the only two lines being affected by the problem, and including more code would have needlessly complicated it.
It wouldn’t have. In fact, it would have made it easier for us to help you because we could have easily identified that they weren’t in the same scope if we could see where the two statements were located.
So, altering the scope is the solution to my problem, but how, exactly, would I do that? I’m not really the best at VEXCode pro, and this is my first time working with PID.
So, to elaborate on my original question, I am trying to make my motors run during autonomous using the PID loop I’ve coded. Here’s my code.
/*---------------------------------------------------------------------------*/
/* */
/* PID Functions */
/* */
/* This is where I'm going to call all of our PID functions to keep them */
/* in one place */
/* */
/* */
/*---------------------------------------------------------------------------*/
//settings
double kP = 0.0;
double kI = 0.0;
double kD = 0.0;
double turnkP = 0.0;
double turnkI = 0.0;
double turnkD = 0.0;
//Autonomous Settings
int desiredValue = 200;
int desiredTurnValue = 0;
int error; //SensorValue - DesiredValue(Positional)
int prevError = 0; //position 20mS ago
int derivative; //error - prevError = speed
int totalError = 0;//totalError = totalError - error
int turnError; //SensorValue - DesiredValue(Positional)
int turnPrevError = 0; //position 20mS ago
int turnDerivative; //error - prevError = speed
int turnTotalError = 0;//totalError = totalError - error
bool resetDriveSensors = false;
//variables
bool enableDrivePID = true;
int DrivePID(){
while(enableDrivePID){
if(resetDriveSensors){
resetDriveSensors = false;
BackLeftMotor.setPosition(0,degrees);
BackRightMotor.setPosition(0,degrees);
FrontLeftMotor.setPosition(0,degrees);
FrontRightMotor.setPosition(0,degrees);
}
int BackLeftMotorPosition = BackLeftMotor.position(degrees);
int BackRightMotorPosition = BackRightMotor.position(degrees);
int FrontLeftMotorPosition = FrontLeftMotor.position(degrees);
int FrontRightMotorPosition = FrontRightMotor.position(degrees);
////////////////////////////////////////////////////////////////
//Lateral PID
////////////////////////////////////////////////////////////////
int averagePosition = (BackLeftMotorPosition + BackRightMotorPosition + FrontLeftMotorPosition + FrontRightMotorPosition)/4;
//potential
error = desiredValue - averagePosition;
//derivative
derivative = error - prevError;
//integral
totalError += error;
double lateralMotorPower = (error * kP + derivative * kD + totalError * kI)/12.0;
////////////////////////////////////////////////////////////////
//Turning PID
////////////////////////////////////////////////////////////////
int turnDifference = (BackLeftMotorPosition + FrontLeftMotorPosition) + (BackRightMotorPosition + FrontRightMotorPosition);
int turningAveragePosition = (BackLeftMotorPosition + BackRightMotorPosition + FrontLeftMotorPosition + FrontRightMotorPosition)/4;
//potential
turnError = desiredTurnValue - turnDifference;
//derivative
turnDerivative = turnError - turnPrevError;
//integral
turnTotalError += turnError;
double turnMotorPower = (turnError * turnkP + turnDerivative * turnkD + turnTotalError * turnkI)/12.0;
BackLeftMotor.spin(forward,lateralMotorPower,voltageUnits::volt);
BackRightMotor.spin(forward,lateralMotorPower,voltageUnits::volt);
FrontLeftMotor.spin(forward,lateralMotorPower,voltageUnits::volt);
FrontRightMotor.spin(forward,lateralMotorPower,voltageUnits::volt);
prevError = error;
turnPrevError = turnError;
vex::task::sleep(20);
}
return 1;
}
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the V5 has been powered on and */
/* not every time that the robot is disabled. */
/*---------------------------------------------------------------------------*/
void pre_auton(void) {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
void autonomous(void) {
vex::task CallDrivePID(DrivePID);
resetDriveSensors = true;
desiredValue = 300;
desiredTurnValue = 600;
vex::task::sleep(1000);
resetDriveSensors = true;
desiredValue = 300;
desiredTurnValue = 300;
//The above lines of code, 146-156, are enabling the PID loop for autonomous
BackLeftMotor.spin(forward,lateralMotorPower,voltageUnits::volt);
}
The last line is where I’d be using the variable, and it is a scope issue, I just have no clue how to fix it.
Add double lateralMotorPower; above your PID function, and inside the function change double lateralMotorPower = (error * kP + derivative * kD + totalError * kI)/12.0; to just lateralMotorPower = (error * kP + derivative * kD + totalError * kI)/12.0; by removing double. This will make lateralMotorPower a global variable that can be accessed inside of main().