text
stringlengths 0
715
|
---|
wait(20, msec); |
} |
wait(20, msec); //allow time for status update to draw itself |
//graph the PID at the end |
drawMode = 1; |
graphPID(errorHistory, powerHistory, driveDistance, error1, time); |
LeftMotors.stop(); |
RightMotors.stop(); |
return 0; |
} |
int turnPID() { |
//drive straightforward with driveDistance as the distance, in degrees (for now) |
//forward PID constants: |
//zieger-nicholas on 11/14: ku = .07 period = .5 sec |
float kP1 = 0.034;//.0245;//.0225 and 0 for other two |
float kI1 = 0.0037;//0.0017; //0.0017 |
float kD1 = 0.19;//0.06;//0.010; |
//not bad; 0.017, 0.0042, 0 |
//not bad v2; .04, 0.0037, 0.23 |
//other variables for forward PID |
float error = 0; |
float integral = 0; |
float derivative = 0; |
float prevError = 0; |
//motor power variables |
float motorPower = 0; |
float prevMotorPower = 0; |
//lists |
std::vector<int> errorHistory; //keep track of error over time |
std::vector<float> powerHistory; //keep track of motor power over time |
int time = 0; |
float currentDist = 0; //the distance the robot is from its starting point, rotationally |
float startDist = (Inertial1.rotation(degrees) + Inertial2.rotation(degrees)) / 2; |
//Inertial1.setHeading(0, degrees); |
//Inertial2.setHeading(0, degrees); |
//float degOffset |
while(true) { |
//float deg1 = Inertial1.heading(degrees); |
currentDist = (Inertial1.rotation(degrees) + Inertial2.rotation(degrees)) / -2 + startDist; |
//printController(Inertial1.rotation(degrees)); |
//calculate error / integral / derivative, of error vs time graph |
error = driveDistance - currentDist; |
if (std::abs(error) < 8) { |
//weigh the integral double when error < 4 |
if (std::abs(error) < 3) { |
integral += error * 2; |
} else { |
integral += error; |
} |
} else { |
integral = 0; |
} |
derivative = error - prevError; |
//core of the PID loop here, calculate the necessary motor power, combine both PID loops |
motorPower = (kP1 * error + kI1 * integral + kD1 * derivative); |
///keep motor power variable in proper range, -1 to 1 |
if (motorPower > 1) motorPower = 1; |
if (motorPower < -1) motorPower = -1; |
//control the slew rate (dampen voltage differences), limits harsh acceleration |
float slewRate = 0.08f; |
if (motorPower > prevMotorPower + slewRate) { |
motorPower = prevMotorPower + slewRate; |
} |
if (motorPower < prevMotorPower - slewRate) { |
motorPower = prevMotorPower - slewRate; |
} |
//minimum voltage of 11V * .2 |
/*if (std::abs(slewRate) != .12) { |
if (std::abs(motorPower) < 0.2) { |
if (motorPower > 0) { |
motorPower = 0.2; |
} else { |
motorPower = -0.2; |
} |
} |
}*/ |
//apply motor voltages |
LeftMotors.spin(forward, 11 * motorPower * -1, volt); |
RightMotors.spin(forward, 11 * motorPower, volt); |
//update histories |
errorHistory.push_back(error); |
powerHistory.push_back(motorPower); |
time += 20; |
//update final variables |
//printController(error); |
//break out of the loop if we have reached the target or B is pressed |
//we have reached the target if the error is less than 5 and the previous error is similar |
if (Controller1.ButtonB.pressing() || ((std::abs(error) < 0.5) && std::abs(error - prevError) < 0.3)) { |
break; |
} |
prevMotorPower = motorPower; |
prevError = error; |
//don't hog CPU |
wait(20, msec); |
} |
wait(20, msec); //allow time for status update to draw itself |
//graph the PID at the end |
drawMode = 1; |
graphPID(errorHistory, powerHistory, driveDistance, error, time); |
LeftMotors.stop(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.