Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Autonomous() {
- /*
- * Start arm inside robot
- * Track line while moving arm to EXT_HIG_VOLTAGE
- * Once hit the "T" at the end, stop and reverse for 1 second
- * Move arm to HIG_INNER/OUTER_VOLTAGE
- * Release the claw
- * Drop the arm
- * Back up for some time
- * Turn around (?)
- */
- double ArmMult = 0;
- bool quitAll = fase;
- bool L = false;
- bool M = false;
- bool R = false;
- bool turning = false;
- bool yHit = false;
- bool stageComplete = false;
- bool ArmAtTargetVoltage = false;
- int ArmDir = 0;
- double leftMotorSpeed = 0;
- double rightMotorSpeed = 0;
- double SpeedScale = 0;
- double targVolt = 0;
- double curVolt = 0;
- Timer *reverseTimer;
- reverseTimer = new Timer; //timer for backing up
- Timer *findLineTimer;
- findLineTimer = new Timer; //timer for finding the line
- if(tapeSelect1->Get() || tapeSelect2->Get()) { //Do we have autonomous?
- if(tapeSelect1->Get() && !tapeSelect2->Get()) { //Are we in T mode?
- /*=====================================
- // STAGE PURPOSES:
- // -Track straight line
- // -Raise the arm to the maximum height
- //=====================================*/
- output->Clear();
- printUserMessage(output, 1, 1, "STAGE 1 - T MODE");
- output->UpdateLCD();
- stageComplete = false;
- SpeedScale = 0.5;
- targVolt = EXT_HIG_VOLTAGE;
- while(!stageComplete && IsAutonomous())
- {
- L = trackerL->Get();
- M = trackerM->Get();
- R = trackerR->Get();
- //Move arm to the correct voltage
- curVolt = pot.GetVoltage();
- if(targVolt-curVolt < -ARM_VOLT_RANGE) {
- ArmDir = -1;
- } else if(targVolt-curVolt > ARM_VOLT_RANGE) {
- ArmDir = 1;
- } else {
- ArmDir = 0;
- ArmAtTargetVoltage = true;
- }
- if(!armLimitUp.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, -1, 0) * ArmMult);
- } else if(!armLimitDown.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, 0, 1) * ArmMult);
- } else {
- motorArm.Set(0.65 * ArmDir * ArmMult);
- }
- if (!L && M && !R) { //OXO
- //Forward
- leftMotorSpeed = 1;
- rightMotorSpeed = 1;
- }
- if (!L && !M && R) { //OOX
- //Right
- leftMotorSpeed = 1;
- rightMotorSpeed = 0.5;
- }
- if (L && !M && !R) { //XOO
- //Left
- leftMotorSpeed = 0.5;
- rightMotorSpeed = 1;
- }
- if ( (L && M && !R) || (!L && M && R) ) { //XXO or OXX
- //Slow Forward
- leftMotorSpeed = 0.75;
- rightMotorSpeed = 0.75;
- }
- if (L && M && R) //XXX, ALL ON
- {
- leftMotorSpeed = 0;
- rightMotorSpeed = 0;
- if (ArmAtTargetVoltage) {
- stageComplete = true;
- }
- }
- if (!L && !M && !R) { //OOO, ALL OFF
- if(findLineTimer->Get() <= 0) {
- findLineTimer->Start();
- } else if(findLineTimer->Get() < 2) {
- //Move slowly to find the line
- leftMotorSpeed = 0.5;
- rightMotorSpeed = 0.5;
- } else {
- leftMotorSpeed = 0;
- rightMotorSpeed = 0;
- quitAll = true;
- break;
- }
- }
- setMotorSpeeds(-1*leftMotorSpeed * SpeedScale, rightMotorSpeed * SpeedScale, motorFLeft, motorBLeft, motorFRight, motorBRight);
- }
- }
- else if (tapeSelect1->Get() && tapeSelect2->Get())
- {
- output->Clear();
- printUserMessage(output, 1, 1, "STAGE 1 - Y MODE");
- output->UpdateLCD();
- /*=====================================
- // STAGE PURPOSES:
- // -Track Y Tape
- // -Raise the arm to the maximum height
- //=====================================*/
- stageComplete = false;
- yHit = false; //already turned at the junction
- turning = false; //in the process of turning at the junction or turning before the T
- ArmAtTargetVoltage = false;
- SpeedScale = 0.5;
- targVolt = EXT_HIG_VOLTAGE;
- while(!stageComplete && IsAutonomous() && !quitAll)
- {
- L = trackerL->Get();
- M = trackerM->Get();
- R = trackerR->Get();
- //Move arm to the correct voltage
- curVolt = pot.GetVoltage();
- if(targVolt-curVolt < -ARM_VOLT_RANGE) {
- ArmDir = -1;
- } else if(targVolt-curVolt> ARM_VOLT_RANGE) {
- ArmDir = 1;
- } else {
- ArmDir = 0;
- ArmAtTargetVoltage = true;
- }
- if(!armLimitUp.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, -1, 0) * ArmMult);
- } else if(!armLimitDown.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, 0, 1) * ArmMult);
- } else {
- motorArm.Set(0.65 * ArmDir * ArmMult);
- }
- if(!turning) {
- if (!L && M && !R) { //OXO
- //Forward
- leftMotorSpeed = 1;
- rightMotorSpeed = 1;
- }
- if (!L && !M && R) { //OOX
- //Right
- leftMotorSpeed = 1;
- rightMotorSpeed = 0.5;
- }
- if (L && !M && !R) { //XOO
- //Left
- leftMotorSpeed = 0.5;
- rightMotorSpeed = 1;
- }
- if ( (L && M && !R) || (!L && M && R) ) { //XXO or OXX
- //Slow Forward
- leftMotorSpeed = 0.75;
- rightMotorSpeed = 0.75;
- }
- if (L && M && R) //XXX, ALL ON
- {
- leftMotorSpeed = 0;
- rightMotorSpeed = 0;
- if (ArmAtTargetVoltage) {
- stageComplete = true;
- }
- }
- if (!L && !M && !R) { //OOO, ALL OFF
- if(!yHit) {
- //start turning next iteration
- turning = true;
- }
- }
- } else { //turning == true
- //Turning around
- if(!L && !M && !R) { //None are on
- if(!yHit) { //Turn in specified direction
- if(dirSwitch->Get()) {
- //right
- leftMotorSpeed = 0.5;
- rightMotorSpeed = -0.5;
- } else {
- //left
- leftMotorSpeed = -0.5;
- rightMotorSpeed = 0.5;
- }
- } else { //turn opposite to how we turned before
- if(dirSwitch->Get()) {
- //left
- leftMotorSpeed = -0.5;
- rightMotorSpeed = 0.5;
- } else {
- //right
- leftMotorSpeed = 0.5;
- rightMotorSpeed = -0.5;
- }
- }
- } else { //hit the line
- if(!yHit) {
- yHit = true;
- } else {
- //hit the line after the second time we turn, break
- stageComplete = true;
- }
- turning = false;
- //go back to driving along the line
- }
- }
- setMotorSpeeds(-1*leftMotorSpeed * SpeedScale, rightMotorSpeed * SpeedScale, motorFLeft, motorBLeft, motorFRight, motorBRight);
- }
- }
- output->Clear();
- printUserMessage(output, 2, 1, "STAGE 1 COMPLETED");
- printUserMessage(output, 1, 1, "STAGE 2 STARTING");
- output->UpdateLCD();
- /*=====================================
- // STAGE PURPOSES:
- // -move backwards for 1 second
- // -lower the arm to AUTO_PLACEMENT_VOLTAGE
- //=====================================*/
- stageComplete = false;
- targVolt = AUTO_PLACEMENT_VOLTAGE;
- reverseTimer->Start();
- while(!stageComplete && IsAutonomous() && !quitAll)
- {
- if(reverseTimer->Get() < 1) //if it hasn't been one second yet
- {
- setMotorSpeeds(-0.25 * -1, -0.25, motorFLeft, motorBLeft, motorFRight, motorBRight);
- } else { //it's been 1 second
- setMotorSpeeds(0, 0, motorFLeft, motorBLeft, motorFRight, motorBRight);
- curVolt = pot.GetVoltage();
- if(targVolt-curVolt < -ARM_VOLT_RANGE) {
- ArmDir = -1;
- } else if(targVolt-curVolt> ARM_VOLT_RANGE) { //once its backed up, lower the arm
- ArmDir = 1;
- } else {
- ArmDir = 0;
- stageComplete = true;
- }
- if(!armLimitUp.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, -1, 0) * ArmMult);
- } else if(!armLimitDown.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, 0, 1) * ArmMult);
- } else {
- motorArm.Set(0.65 * ArmDir * ArmMult);
- }
- }
- }
- output->Clear();
- printUserMessage(output, 2, 1, "STAGE 2 COMPLETED");
- printUserMessage(output, 2, 1, "STAGE 3 STARTING");
- output->UpdateLCD();
- /*=====================================
- // STAGE PURPOSES:
- // -Release the claw
- // -lower the arm to AUTO_AFTER_PLACEMENT_VOLTAGE
- // -Back up for 2.5 seconds
- //=====================================*/
- stageComplete = false;
- curVolt = 0;
- targVolt = AUTO_AFTER_PLACEMENT_VOLTAGE;
- reverseTimer = new Timer; //timer for backing up
- clawPiston->Set(true); //open claw
- while(!stageComplete && IsAutonomous() && !quitAll)
- {
- curVolt = pot.GetVoltage();
- if(targVolt-curVolt < -ARM_VOLT_RANGE) {
- ArmDir = -1;
- } else if(targVolt-curVolt> ARM_VOLT_RANGE) { //lower the arm
- ArmDir = 1;
- } else {
- ArmDir = 0;
- if(reverseTimer->Get() <= 0)
- {
- reverseTimer->Start();
- } else if(reverseTimer->Get() < 2.5) { //if it hasn't been two and a half seconds yet
- setMotorSpeeds(-1*-0.25, -0.25, motorFLeft, motorBLeft, motorFRight, motorBRight); //back up
- } else {
- setMotorSpeeds(0, 0, motorFLeft, motorBLeft, motorFRight, motorBRight);
- stageComplete=true;
- }
- }
- if(!armLimitUp.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, -1, 0) * ArmMult);
- } else if(!armLimitDown.Get()) {
- motorArm.Set(clamp(0.65 * ArmDir, 0, 1) * ArmMult);
- } else {
- motorArm.Set(0.65 * ArmDir * ArmMult);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement