text
stringlengths 0
715
|
---|
float leftMotorPower = motorPower - headingPower; |
float rightMotorPower = motorPower + headingPower; |
if (leftMotorPower > 1) leftMotorPower = 1; |
if (leftMotorPower < -1) leftMotorPower = -1; |
if (rightMotorPower > 1) rightMotorPower = 1; |
if (rightMotorPower < -1) rightMotorPower = -1; |
//apply motor voltages |
Left1.spin(forward, 11 * leftMotorPower * speedPID, volt); |
Left2.spin(forward, 11 * leftMotorPower * speedPID, volt); |
Left3.spin(forward, 11 * leftMotorPower * speedPID, volt); |
Right1.spin(forward, 11 * rightMotorPower * speedPID, volt); |
Right2.spin(forward, 11 * rightMotorPower * speedPID, volt); |
Right3.spin(forward, 11 * rightMotorPower * speedPID, volt); |
//update final variables |
prevMotorPower = motorPower; |
prevError1 = error1; |
prevError2 = error2; |
//update histories |
errorHistory.push_back(error1); |
powerHistory.push_back(std::abs(motorPower)); |
time1 += 20; |
//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() || exitPID || ((std::abs(error1) < 8) && std::abs(error1 - prevError1) < 2)) { |
break; |
} |
graphPID(errorHistory, powerHistory, driveDistance, error1, time1); |
//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, error1, time1); |
Left1.stop(); |
Left2.stop(); |
Left3.stop(); |
Right1.stop(); |
Right2.stop(); |
Right3.stop(); |
return 0; |
} |
int turnPID(bool absolute) { |
//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.018;//.0245;//.0225 and 0 for other two |
float kI1 = 0.0034;//0.0017; //0.0017 //0.0034 |
float kD1 = 0.12;//0.06;//0.010; |
//not bad; 0.024, 0.0034, .15 (1/4/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 = Inertial.rotation(degrees); |
//Inertial1.setHeading(0, degrees); |
//Inertial2.setHeading(0, degrees); |
//float degOffset |
while(true) { |
//float deg1 = Inertial1.heading(degrees); |
currentDist = Inertial.rotation(degrees) / -1 + startDist; |
//Inertial2 is much more accurate than inertial1 (10 spins, 23 degress off vs 5 degrees off) |
//printController(Inertial1.rotation(degrees)); |
//calculate error / integral / derivative, of error vs time graph |
error = driveDistance - currentDist; |
if (absolute) error = driveDistance + Inertial.rotation(degrees); |
if (std::abs(error) < 10) { |
//weigh the integral double when error < 4 |
if (std::abs(error) < 4) { |
integral += error * 2; |
} else { |
integral += error; |
} |
} else { |
integral = 0; |
} |
if (error > 0 != prevError > 0) integral = 0;//reset integral value if you just crossed the target line |
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.08; |
if (motorPower > prevMotorPower + slewRate) { |
motorPower = prevMotorPower + slewRate; |
} |
if (motorPower < prevMotorPower - slewRate) { |
motorPower = prevMotorPower - slewRate; |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.