//MForward pid
//--------------------------------------------------------------------------------------------------------------------------------------------//
//Lateral constants
double MForwardkP = 0.02;
//Desired value
int MLatValue = 0;
//defining errors
int MLerror; //SensorValue - DesiredValue : Positional value
int MLprevError = 0; //Position 20ms ago
int MLderivative; // error - prevError : speed
int MLtotalError = 0; //totalError = totalError + error
//variables for settings
bool resetMForwardSensors = false;
void MresetDriveSensors(){
Rot.setPosition(1, degrees);
Rinertial.setRotation(1, degrees);
}
//Drive pid
int MLateralPID(){
if (resetMForwardSensors) {
resetMForwardSensors = false;
Rot.setPosition(1, degrees);
Rinertial.setHeading(1, degrees);
}
while(enableDrivePID){
//Getting drive position
//---------------------------------------------------------------------------//
//---------------------------------------------------------------------------//
//Potential
MLerror = MLatValue - Rot.position(degrees);
//Derivative
MLderivative = MLerror - MLprevError;
//Lateral PID equation
double SLforwardSpeed = (MLerror * MForwardkP);
FLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
BLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
TLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
FRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
BRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
TRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
MLprevError = MLerror;
vex::task::sleep(20);
}
return 1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------//
//SForward pid
//--------------------------------------------------------------------------------------------------------------------------------------------//
//Lateral constants
double SForwardkP = 0.035;
//Desired value
int SLatValue = 0;
//defining errors
int SLerror; //SensorValue - DesiredValue : Positional value
int SLprevError = 0; //Position 20ms ago
int SLderivative; // error - prevError : speed
int SLtotalError = 0; //totalError = totalError + error
//variables for settings
bool resetSForwardSensors = false;
void SresetDriveSensors(){
Rot.setPosition(1, degrees);
Rinertial.setRotation(1, degrees);
}
//Drive pid
int SLateralPID(){
if (resetSForwardSensors) {
resetSForwardSensors = false;
Rot.setPosition(1, degrees);
Rinertial.setHeading(1, degrees);
}
while(enableDrivePID){
//Getting drive position
//---------------------------------------------------------------------------//
//---------------------------------------------------------------------------//
//Potential
SLerror = SLatValue - Rot.position(degrees);
//Derivative
SLderivative = SLerror - SLprevError;
//Lateral PID equation
double SLforwardSpeed = (SLerror * SForwardkP);
FLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
BLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
TLDrive.spin(forward, SLforwardSpeed - Rinertial.rotation(degrees), voltageUnits::volt);
FRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
BRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
TRDrive.spin(forward, SLforwardSpeed + Rinertial.rotation(degrees), voltageUnits::volt);
SLprevError = SLerror;
vex::task::sleep(20);
}
return 1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------//
//Turn 90 pid
//--------------------------------------------------------------------------------------------------------------------------------------------//
//turn right constants
double turn90kP = 0.2;
//Desired value
double Turn90Value = 0;
//defining errors
int turn90Error; //SensorValue - DesiredValue : Positional value
int turn90PrevError = 0; //Position 20ms ago
int turn90Derivative; // error - prevError : speed
int turn90TotalError = 0; //totalError = totalError + error
//variables for settings
bool enableTurn90PID = true;
bool resetTurn90Sensors = false;
//Drive pid
int Turn90PID(){
while(enableDrivePID){
//Averaging position
int TL90CurrentPosition = Rinertial.rotation(degrees);
//Potential
turn90Error = Turn90Value - TL90CurrentPosition;
//Derivative
turn90Derivative = turn90Error - turn90PrevError;
//Lateral PID equation
double turnLeft90Speed = (turn90Error * turn90kP);
FLDrive.spin(forward, turnLeft90Speed, voltageUnits::volt);
BLDrive.spin(forward, turnLeft90Speed, voltageUnits::volt);
TLDrive.spin(forward, turnLeft90Speed, voltageUnits::volt);
FRDrive.spin(reverse, turnLeft90Speed, voltageUnits::volt);
BRDrive.spin(reverse, turnLeft90Speed, voltageUnits::volt);
TRDrive.spin(reverse, turnLeft90Speed, voltageUnits::volt);
turn90PrevError = turn90Error;
vex::task::sleep(20);
}
return 1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------//
//Turn 45 pid
//--------------------------------------------------------------------------------------------------------------------------------------------//
//turn right constants
double turn45kP = 0.3;
//Desired value
double Turn45Value = 0;
//defining errors
int turn45Error; //SensorValue - DesiredValue : Positional value
int turn45PrevError = 0; //Position 20ms ago
int turn45Derivative; // error - prevError : speed
int turn45TotalError = 0; //totalError = totalError + error
//variables for settings
bool enableTurn45PID = true;
bool resetTurn45Sensors = false;
//Drive pid
int Turn45PID(){
while(enableDrivePID){
//Averaging position
int TL45CurrentPosition = Rinertial.rotation(degrees);
//Potential
turn45Error = Turn45Value - TL45CurrentPosition;
//Derivative
turn45Derivative = turn45Error - turn45PrevError;
//Lateral PID equation
double turn45Speed = (turn45Error * turn45kP);
FLDrive.spin(forward, turn45Speed, voltageUnits::volt);
BLDrive.spin(forward, turn45Speed, voltageUnits::volt);
TLDrive.spin(forward, turn45Speed, voltageUnits::volt);
FRDrive.spin(reverse, turn45Speed, voltageUnits::volt);
BRDrive.spin(reverse, turn45Speed, voltageUnits::volt);
TRDrive.spin(reverse, turn45Speed, voltageUnits::volt);
turn45PrevError = turn45Error;
vex::task::sleep(20);
}
return 1;
}
//--------------------------------------------------------------------------------------------------------------------------------------------//
say I want to drive forward for 1000 degrees, then I want to turn left 90 degrees, then drive forward 1000 degrees again.
my robot will go forward 1000 degrees, turn left 89 degrees, then it will not drive forward 1000 degrees because it didnt turn for 90 degrees. If I physically push the robot to where it does turn for 90 degrees, then it will drive forward for the final 1000 degrees.