text
stringlengths 0
715
|
---|
//core of the PID loop here, calculate the necessary motor power
|
motorPower = kP * error + kI * integral + kD * derivative;
|
///keep motor power variable in proper range, 0-1
|
if (motorPower > 1) motorPower = 1;
|
if (motorPower < 0) motorPower = 0;
|
//control the slew rate (dampen voltage differences), limits harsh acceleration
|
float slewRate = 0.05f;
|
if (motorPower > prevMotorPower + slewRate) {
|
motorPower = prevMotorPower + slewRate;
|
}
|
if (motorPower < prevMotorPower - slewRate) {
|
motorPower = prevMotorPower - slewRate;
|
}
|
//apply motor voltages
|
LeftMotors.spin(forward, 12 * motorPower, volt);
|
RightMotors.spin(reverse, 12 * motorPower, volt);
|
//update final variables
|
prevMotorPower = motorPower;
|
prevError = error;
|
//don't hog CPU
|
wait(20, msec);
|
}
|
return 0;
|
}
|
int drivePID() {
|
//drive straightforward with driveDistance as the distance, in degrees (for now)
|
//forward PID constants:
|
float kP1 = 0.0048;//.003 and 0 for other two
|
float kI1 = 0.00003;
|
float kD1 = 0.013;
|
//turning PID constants:
|
float kP2 = 0.001;
|
float kI2 = 0.00;
|
float kD2 = 0.00;
|
//other variables for forward PID
|
float error1 = 0;
|
float integral1 = 0;
|
float derivative1 = 0;
|
float prevError1 = 0;
|
//other variables for turn PID
|
float error2 = 0;
|
float integral2 = 0;
|
float derivative2 = 0;
|
float prevError2 = 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
|
RightMotors.setPosition(0, degrees);
|
LeftMotors.setPosition(0, degrees);
|
while(true) {
|
currentDist = (RightMotors.position(degrees) + LeftMotors.position(degrees)) / 2;
|
//calculate error / integral / derivative, of error vs time graph
|
error1 = driveDistance - currentDist;
|
if (std::abs(error1) < 200) {
|
//weigh the integral double when error < 50
|
if (std::abs(error1) < 50) {
|
integral1 += error1 * 2;
|
} else {
|
integral1 += error1;
|
}
|
}
|
derivative1 = error1 - prevError1;
|
error2 = RightMotors.position(degrees) - RightMotors.position(degrees);
|
integral2 += error2;
|
derivative2 = error2 - prevError2;
|
//core of the PID loop here, calculate the necessary motor power, combine both PID loops
|
motorPower = (kP1 * error1 + kI1 * integral1 + kD1 * derivative1);
|
///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;
|
}
|
//apply motor voltages
|
LeftMotors.spin(forward, 11 * motorPower * speedPID, volt);
|
RightMotors.spin(forward, 11 * motorPower * speedPID, volt);
|
//update final variables
|
prevMotorPower = motorPower;
|
prevError1 = error1;
|
prevError2 = error2;
|
//update histories
|
errorHistory.push_back(error1);
|
powerHistory.push_back(std::abs(motorPower));
|
time += 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() || ((std::abs(error1) < 5) && std::abs(error1 - prevError1) < 2)) {
|
break;
|
}
|
//don't hog CPU
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.