text
stringlengths
0
715
Brain.Screen.drawLine(240, 0, 240, 240);
Brain.Screen.setPenColor(white);
Brain.Screen.setPenWidth(2);
int originX = 240;
int originY = 120;
float scale = 2;
for (int i = 0; i < x.size() - 1; i++) {
Brain.Screen.drawLine(x.at(i) * scale + originX, 240 - (y.at(i) * scale + originY), x.at(i + 1) * scale + originX, 240 - (y.at(i + 1) * scale + originY));
}
//draw robot as dot
Brain.Screen.setPenColor(red);
Brain.Screen.drawCircle(x.at(x.size() - 1) * scale + originX, 240 - (y.at(x.size() - 1) * scale + originY), 5);
}
/*template for a thread/task (task is a higher level abstraction of thread):
int myTaskCallback() {
while (true) {
//do something
wait(25, msec);
}
// A task's callback must return an int, even though the code will never get
// here. You must return an int here. Tasks can exit, but this one does not.
return 0;
}
in some other function like main():
task myTask = task(myTaskCallback);
*/
int updatePosition() {
//keep track of position over time
std::vector<int> x;
std::vector<int> y;
while (true) {
//odometry, yay!
//caclulate the robot's absolute position
//first calculate the change since last time
float changeLeft = LeftEncoder.position(degrees) - prevLeftEncoder;
float changeRight = RightEncoder.position(degrees) - prevRightEncoder;
float changeBack = BackEncoder.position(degrees) - prevBackEncoder;
prevLeftEncoder = LeftEncoder.position(degrees);
prevRightEncoder = RightEncoder.position(degrees);
prevBackEncoder = BackEncoder.position(degrees);
//convert the changes to inches
//degrees * (1 revolution / 360 degrees) * (pi * 2.75" (diameter of wheel) / 1 revolution) = inches
//although the actual wheel size differs by a small amount, it was 2.73 for the first half of the second week of summer camp
//after that it is off by 1.021836:1 difference (back wheel)
//for the right wheel our new ratio is 1.011
float distLeft = (float)changeLeft / 360 * M_PI * 2.75;
float distRight = (float)changeRight / 360 * M_PI * 2.75;
float distBack = (float)changeBack / 360 * M_PI * 2.75;
//calculates the change in angle according to an equation derived in the notebook
float changeInAngle = (distLeft- distRight) / (sideWheelRadius * 2);
float newAngle = angle + changeInAngle;
//now calculate the change in translational offset
//x is forward, so a changeX of +2.5 means the robot moved directly forward 2.5 inches
//y is sideways, left is positive. Think of a cartesian coordinate system.
float changeX = 0; //we need a third tracking wheel for this
float changeY = 0; //set to zero initially
//we avoid a divide by zero error by splitting the code based on whether the change in angle is zero or not
if (changeInAngle == 0) {
changeY = distBack; //there was no turning, so the lateral change in position is interepereted simply
changeX = distRight;
} else {
//now you have to account for the fact that going forward while turning changes the position differently
changeX = 2 * sin(changeInAngle / 2) * ((distRight / changeInAngle) + sideWheelRadius);
changeY = 2 * sin(changeInAngle / 2) * ((distBack / changeInAngle) + backWheelRadius);
}
//now convert local position change to global position
//the local coordinate system is offset from the global one by (angle + newAngle) / 2
//first convert to polar coordinates
float radius = sqrt(changeY * changeY + changeX * changeX);
float globalAngle = atan2(changeY, changeX) + (angle + newAngle) / 2;
//now convert back to local coordinates
float changeGlobalX = cos(globalAngle) * radius;
float changeGlobalY = sin(globalAngle) * radius;
//update global position
pos[0] += changeGlobalX;
pos[1] += changeGlobalY;
angle += changeInAngle;
//graph the robot's position if necessary
if (drawMode == 3) {
x.push_back(pos[0]);
y.push_back(pos[1]);
drawRobotPath(x, y);
}
wait(4, msec);
}
return 0;
}
int flywheelVelocityControl(){
int prevError = 0; //previous error
float motorPower = 0; //between 0 and 1, the proportion of motor power applied (.7 = 70% power
float prevMotorPower = 0; //previous motor power, used to limit the slew rate
float powerAtZero = 0; //the tbh variable, the best guess of the flywheel power to use
std::vector<int> velocityHistory; //keep track of velocity over time
std::vector<int> powerHistory; //keep track of motor power over time
//this task keeps track of the velocity of the flywheel and manages the motor power input
while (true) {
//first, get the current velocity of the flywheel. 5:1 gear ratio
int speed = Flywheel.velocity(rpm) * 5;
int error = flywheelTargetRPM - speed;
//now for the tbh (take back half) algorithm