text
stringlengths
0
715
RightMotors.stop();
return 0;
}
void aDrive(float distance) {
driveDistance = distance;
drivePID();
}
void aTurn(float distance) {
driveDistance = distance;
turnPID();
}
void aDriveFor(int speed, int time) {
//drive the robot forward (or backward if speed is negative) for a specified amount of milliseconds
RightMotors.spin(forward);
LeftMotors.spin(forward);
RightMotors.setVelocity(speed, percent);
LeftMotors.setVelocity(speed, percent);
wait(time, msec);
LeftMotors.stop();
RightMotors.stop();
}
void fireIndexer() {
Indexer.spin(forward, 12, volt);
wait(130, msec);
Indexer.spin(forward, 6, volt);
wait(60, msec);
Indexer.spin(reverse, 6, volt);
wait(100, msec);
Indexer.stop();
}
//below code copied from jpearman on vexforum
int screen_origin_x = 150;
int screen_origin_y = 20;
int screen_width = 316;
int screen_height = 212;
// function to draw a single object
void drawObject( vision::object &obj, vex::color c ) {
int labelOffset = 0;
Brain.Screen.setPenColor( vex::color::yellow );
Brain.Screen.drawRectangle( screen_origin_x + obj.originX, screen_origin_y + obj.originY, obj.width, obj.height, c );
Brain.Screen.setFont( vex::fontType::mono12 );
if( obj.originX > 280 )
labelOffset = -40;
if( obj.originY > 10 )
Brain.Screen.printAt( screen_origin_x + obj.originX + labelOffset, screen_origin_y + obj.originY-3, "Sig %o", obj.id );
else
Brain.Screen.printAt( screen_origin_x + obj.originX + labelOffset, screen_origin_y + obj.originY+10, "Sig %o", obj.id );
}
// function to draw all objects found
void drawObjects( vision &v, vex::color c, bool clearScreen ) {
if( clearScreen ) {
Brain.Screen.setPenColor( vex::color::black );
Brain.Screen.drawRectangle( screen_origin_x, screen_origin_y, screen_width, screen_height, vex::color::black );
}
for(int i=0;i<v.objectCount;i++)
drawObject( v.objects[i], c );
}
int alignToGoal() {
//aligns the robot to the goal using the vision sensor
float finalValue;
//first of all, find the goal using the vision sensor
do {
int x = 0;
int center = 158; // the goal should be at x=158 when lined up perfectly with the robot
int maxArea = 0; //we will align to the signature with the highest area on the screen (generally the right one)
int maxX = -1;
for (int k = 0; k < 5; k++) {
if (visionTrackRed) {
VisionSensor.takeSnapshot( VisionSensor__RED_GOAL );
//if this is the new highest area, then update the variable
int area = VisionSensor.largestObject.width * VisionSensor.largestObject.height;
if (area > maxArea && area > 400) {
maxArea = area;
maxX = VisionSensor.largestObject.originX + (VisionSensor.largestObject.width / 2);
}
}
if (visionTrackBlue) {
VisionSensor.takeSnapshot( VisionSensor__BLUE_GOAL );
int area = VisionSensor.largestObject.width * VisionSensor.largestObject.height;
if (area > maxArea && area > 400) {
maxArea = area;
maxX = VisionSensor.largestObject.originX + (VisionSensor.largestObject.width / 2);
}
}
wait(20, msec);
}
if (maxX == -1) {
//dont do anything if no goal is detected
x = center;
printControllerSetup();
Controller1.Screen.print("No goal found");
} else {
x = maxX;
}
float value = x / 316.0 * 60; //angle, in degrees, that we are at. 30 degrees is dead center
finalValue = (value - 30) * -7.55; //degree-to-PID units conversion
//only turn if we are off by a good bit (over 1 degree)
if (std::abs(finalValue) > 7) {
aTurn(finalValue);
}