text
stringlengths
0
715
//similar to PID but minimizes overshoot
motorPower = motorPower + (gain * error);
///keep motor power variable in proper range
if (motorPower > 1) motorPower = 1;
if (motorPower < 0) motorPower = 0;
//control the slew rate (dampen voltage differences), basically don't jerk the motor too much
float slewRate = 0.05f;
if (motorPower > prevMotorPower + slewRate) {
motorPower = prevMotorPower + slewRate;
}
if (motorPower < prevMotorPower - slewRate) {
motorPower = prevMotorPower - slewRate;
}
//the other key component of the take back half algorithm, the part that takes back half
if (error >= 0 != prevError >= 0) {
//the graph crossed the target RPM line
if (!firstCross) {
//the first time you cross the target RPM, slow it down to the voltage estimate to make it stabilize faster
firstCross = true;
motorPower = motorPowerApprox;
} else {
//every time after the first time you cross the target RPM, set the voltage to be the average of what it currently is and what is was at the previous cross
motorPower = 0.5 * (motorPower + powerAtZero);
}
powerAtZero = motorPower;
}
//if the flyhweel is off, take that into account
if (!flywheelOn) {
motorPower = 0;
powerAtZero = 0;
firstCross = false;
}
if (tripleShooting) motorPower = .75;//might have to undo this, helps with consistency in some situations.
//now apply motor power, if we are using this control method
if (useTBH) Flywheel.spin(forward, 12 * motorPower, volt);
//update history of velocity and motor power
velocityHistory.push_back(speed);
powerHistory.push_back(motorPower * 100);
//graph if necessary
if (drawMode == 0) {
graphFlywheelVelocity(velocityHistory, powerHistory);
}
//update previous variables
prevMotorPower = motorPower;
prevError = error;
//don't hog CPU
wait(20, msec);
}
return 0;
}
void driveCode() {
//drives the robot around based on controller input, double arcade controls
//don't drive if the robot is currently being controlled autonomously
if (auton) return;
LeftMotors.spin(forward);
RightMotors.spin(forward);
int forward1 = Controller1.Axis3.value();
int turn = Controller1.Axis1.value();
printController(LeftMotors.velocity(rpm));
//fix drift, or not
//if (std::abs(forward) < 7) forward = 0;
//if (std::abs(turn) < 7) turn = 0;
//calculate proper motor powers
int left = forward1 * driveSpeed + turn * turnSpeed;
int right = forward1 * driveSpeed - turn * turnSpeed;
//set velocity of drive motors
LeftMotors.setVelocity(left, percent);
RightMotors.setVelocity(right, percent);
LeftMotors.spin(forward, left, percent);
}
//now all of the functions for autonomous / odometry
int turnToPoint(float x, float y) {
//given an X and Y position on the field, turn to that point using PID
//PID constants:
float kP = 0.03;
float kI = 0.03;
float kD = 0.001;
//other variables:
float error = 0;
float motorPower = 0;
float integral = 0;
float derivative = 0;
float prevError = 0;
float prevMotorPower = 0;
//first, calculate the angle we need to be at
float deltaX = x - pos[0];
float deltaY = y - pos[1];
float finalAngle = atan2(deltaY, deltaX); //in radians
//also clamp the current angle to [0, 2pi)
while (angle >= 2 * M_PI) {
angle -= 2 * M_PI;
}
while (angle < 0) {
angle += 2 * M_PI;
}
while(true) {
//calculate error / integral / derivative, of error vs time graph
error = finalAngle - angle;
integral += error;
derivative = error - prevError;