text
stringlengths
0
715
if (turn < 10 && turn > -10) {
turn = 0; //fix drift
} else if (turn < 60 && turn > -60) {
turn = (turn / 2) + 10; //fine control for small movements
} else {
turn = (turn * 3 / 2) - 50; //full power at higher throttles
}*/
//adjust the turn variable based on the straight one
if (forward1 > 50) {
turn *= (((forward1 - 50) / 50) + 1);
} else if (forward1 < -50) {
turn *= (((-50 - forward1) / 50) + 1);
}
//Inverse Inertia accumulator--slow down fast turns after 200 ms
if (turn < 50 && turn > -50) {
curveTime = 0;
}
if (curveTime > 200) {
turn *= 0.6;
}
//slow down turn:
turn *= 0.7;
curveTime += 20;
}
left = forward1 * driveSpeed + turn * turnSpeed;
right = forward1 * driveSpeed - turn * turnSpeed;
//set velocity of drive motors
Left1.setVelocity(left, percent);
Left2.setVelocity(left, percent);
Left3.setVelocity(left, percent);
Right1.setVelocity(right, percent);
Right2.setVelocity(right, percent);
Right3.setVelocity(right, percent);
}
int drivePID() {
//drive straightforward with driveDistance as the distance, in degrees (for now)
//forward PID constants:
float kP1 = 0.0016;//.003 and 0 for other two
float kI1 = 0.0001;
float kD1 = 0.012;
//0.0027, 0.0001, and 0.015 with 0.08 slew rate was Brighton values
//turning PID constants:
float kP2 = 0.02;
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;
float headingPower = 0;
//lists
std::vector<int> errorHistory; //keep track of error over time
std::vector<float> powerHistory; //keep track of motor power over time
int time1 = 0;
float currentDist = 0; //the distance the robot is from its starting point
float startDist = Inertial.rotation(degrees);
Right1.setPosition(0, degrees);
Right2.setPosition(0, degrees);
Right3.setPosition(0, degrees);
Left1.setPosition(0, degrees);
Left2.setPosition(0, degrees);
Left3.setPosition(0, degrees);
while(true) {
currentDist = (Right1.position(degrees) + Left1.position(degrees) + Right2.position(degrees) + Left2.position(degrees) + Right3.position(degrees) + Left3.position(degrees)) / 6;
//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;
//heading correction
error2 = Inertial.rotation(degrees) - startDist;
if (std::abs(error1) < 600) error2 = 0;
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);
headingPower = kP2 * error2;
///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.05f;
if (motorPower > prevMotorPower + slewRate) motorPower = prevMotorPower + slewRate;
if (motorPower < prevMotorPower - slewRate) motorPower = prevMotorPower - slewRate;
//account for heading correction P loop