Step 7: The Code (wired Connection)
#include Servo firgelli; Servo bodyRotationServo; Servo armRotationServo; Servo liftDuleR; Servo liftDuleL; Servo armLift; int led = 13; int firgelliPin = A4; int bodyRotationServoPin = A0; int armRotationServoPin = 6; int liftDuleRPin = 7; int liftDuleLPin = 8; int armLiftPin = 9; int distanceSenPinBottom = A1; int distanceSenPinTop = A2; int pressureSenPin = A3; int distanceBottom = 0; int distanceTop = 0; int pressure = 0; boolean bottomFound=false; boolean topFound=false; boolean sushiPlateFound=false; boolean sushiFound=false; boolean grabFroceOkay=false; boolean grabbibgTheSushi=false; int CurrentFirgelliPos; int CurrentbodyRotationPos; int CurrentArmLiftPos; int CurrentliftDulePos; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); delay(20); initPosition(); analogWrite(led, 100); delay(500); analogWrite(led, 0); delay(500); analogWrite(led, 100); delay(500); analogWrite(led, 0); } void loop() { sens(); // Serving Station: waiting state / looking for human hand or plate // Sushi Station: Move body to Sushi Station and find Sushi //Serial.print(bottomFound); if ( bottomFound ){ //bottomActive=true; //pre parallelMove move up liftDuleMove(CurrentliftDulePos, 100, 10); // arm down, move back CurrentliftDulePos=100; armLiftMove(CurrentArmLiftPos, 30, 10);//lower arm about to check sushi location CurrentArmLiftPos=30; //premove the body bodyRotationServoMove(CurrentbodyRotationPos, 60, 20); // go away from general sushi station CurrentbodyRotationPos=60; liftDuleMove(CurrentliftDulePos, 90, 10); // arm down, move back CurrentliftDulePos=90; //lower to scan //armLiftMove(25, 45, 10);// higher the number lower the arm // Start scanning using body move //Find Sushi serving Plate: source of Sushi // stop if foud findSourcePlate(); //liftDuleMove(100, 90, 10); // arm down Serial.println(sushiPlateFound); Serial.println("findSourcePlate Done"); if(sushiPlateFound) { Serial.println("Start Grabbing Sushi"); armLiftMove(CurrentArmLiftPos, 71, 20); // higher the number lower the arm CurrentArmLiftPos=71; sens(); while (!topFound){ sens(); } // delay(1000); //sushiFound=true; // start grabbing sushi with pressure sensor int loopa=0; int loopb=93; // larger = tighter int delayTime=25; for(int fpos = loopa; fpos <= loopb ; fpos += 1){ float t1 = (float) (fpos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - fpos) / (float) (loopb - loopa); firgelli.write(180-fpos); //delay(delayTime); sens(); //grab sushi with sensor if (grabFroceOkay){ CurrentFirgelliPos=fpos; fpos =loopb; grabbibgTheSushi=true; analogWrite(led, 100);} delay( delayTime+pow (max(t1, t2), 6) * 30); }//grab sushi end delay(200); // have grabbed the sushi, we have the sushi //if (grabbibgTheSushi){ // lift sushi armLiftMove(CurrentArmLiftPos, 50, 20); // higher the number lower the arm CurrentArmLiftPos=50; //move to soy sauce station armRotationServoMove(90, 30, 15 ); // turn right delay(500); bodyRotationServoMove(CurrentbodyRotationPos, min(CurrentbodyRotationPos+30, 180), 35); // go to general sushi station CurrentbodyRotationPos=CurrentbodyRotationPos+30; delay(1000); //bodyRotationServoMove(loopb, 90, 20); //double dipping //down armLiftMove(CurrentArmLiftPos, 70, 5); // higher the number lower the arm CurrentArmLiftPos=70; //up armLiftMove(CurrentArmLiftPos, 65, 5); // higher the number lower the arm CurrentArmLiftPos=65; //down armLiftMove(CurrentArmLiftPos, 70, 5); // higher the number lower the arm CurrentArmLiftPos=70; //up armLiftMove(CurrentArmLiftPos, 50, 5); // higher the number lower the arm CurrentArmLiftPos=50; //move sushi back to source positon armRotationServoMove(30, 90, 20 ); // turn right //move sushi back to human bodyRotationServoMove(CurrentbodyRotationPos, 45, 25); // go to human drop sushi CurrentbodyRotationPos=45; //down armLiftMove(CurrentArmLiftPos, 80, 20); // higher the number lower the arm CurrentArmLiftPos=80; //drop sushi firgelliMove(CurrentFirgelliPos, 0, 5); delay(500); //retrack /* liftDuleMove(CurrentliftDulePos, 110, 20); // arm down, move back CurrentliftDulePos=110; */ armLiftMove(CurrentArmLiftPos, 60, 20); // higher the number lower the arm CurrentArmLiftPos=60; //} }// end of sushi plate found delay(5000); reset(); }// end of bottom found // parallelMove (70,100,50); //liftDuleMove(45, 90, 30); // arm down //armLiftMove(90, 80, 50);//lower arm to check sushi location } ////////////////////////////////////////////// ////////////////////////////////////////////// /// Functions void sens(){ distanceBottom = analogRead(distanceSenPinBottom); distanceTop = analogRead(distanceSenPinTop); pressure = analogRead(pressureSenPin); if (distanceTop<400) { topFound=true;} else{topFound=false;} if (distanceBottom<400){ bottomFound=true;} else{bottomFound=false;} if (pressure>8 ){ grabFroceOkay=true;}else{grabFroceOkay=false;} /* Serial.print(distanceBottom); Serial.print(" "); Serial.print(distanceTop); Serial.print(" "); Serial.println(pressure); */ delay(20); } void grabber(){ if (500distanceTop){ firgelli.write(80); delay(5000); } } void bodyRotationServoMove(int loopa, int loopb, int delayTime){ sens(); if (loopa > loopb){ for(int pos = loopa; pos>=loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); bodyRotationServo.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); bodyRotationServo.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } void armRotationServoMove(int loopa, int loopb, int delayTime){ if (loopa > loopb){ for(int pos = loopa; pos>=loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); armRotationServo.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); armRotationServo.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } void liftDuleMove(int loopa, int loopb, int delayTime){ if (loopa > loopb){ for(int pos = loopa; pos>=loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); liftDuleR.write(180-pos); liftDuleL.write(pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); liftDuleR.write(180-pos); liftDuleL.write(pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } void armLiftMove(int loopa, int loopb, int delayTime){ if (loopa > loopb){ for(int pos = loopa; pos >= loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); armLift.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); armLift.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } void firgelliMove(int loopa, int loopb, int delayTime){ if (loopa > loopb){ for(int pos = loopa; pos>=loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); firgelli.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); firgelli.write(180-pos); //delay(delayTime); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } // // // // // // // // one angle drive void bodyRotationServoGo(int go){ bodyRotationServo.write(180-go); } void armRotationServoGo(int go){ armRotationServo.write(180-go); } void liftDuleGo(int go){ liftDuleR.write(180-go); liftDuleL.write(go); } void armLiftGo(int go){ armLift.write(180-go); } void firgelliGo(int go){ firgelli.write(180-go); } // tests void t1(){ // arm lift + lift dule + arm rotation int loopa=70; int loopb=100; for(int pos = loopa; pos <= loopb; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree armLiftGo(180-pos*1.25); liftDuleGo(pos); armRotationServoGo(pos*1.50); delay(15); } delay(1000); for(int pos = loopb; pos>=loopa; pos-=1) // goes from 180 degrees to 0 degrees { armLiftGo(180-pos*1.25); liftDuleGo(pos); armRotationServoGo(pos*1.50); delay(15); } } void parallelMove(int loopa, int loopb, int delayTime){ if (loopa > loopb){ for(int pos = loopa; pos>=loopb; pos-=1) { float t1 = (float) (pos - loopb) / (float) (loopa - loopb); float t2 = (float) (loopa - pos) / (float) (loopa - loopb); armLiftGo(180-pos); liftDuleGo(180-pos); delay( delayTime+pow (max(t1, t2), 6) * 30); } } else{ for(int pos = loopa; pos <= loopb; pos += 1){ float t1 = (float) (pos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - pos) / (float) (loopb - loopa); armLiftGo(180-pos); liftDuleGo(180-pos); delay( delayTime+pow (max(t1, t2), 6) * 30); } } } void grabbingSushiHardcoded(){ //init delay(5000); firgelliGo(200); delay(1500); armLiftMove(60, 80, 20); delay(500); bodyRotationServoGo(90); delay(1000); // grab firgelliGo(120); delay(3000); //lift armLiftMove(80, 40, 20); delay(1000); // move body bodyRotationServoMove(90, 130, 50); delay(2000); // move mini arm armLiftMove(40, 80, 20); delay(100); //drop firgelliGo(180); delay(500); //Go back armLiftMove(80, 40, 20); bodyRotationServoMove(130, 90, 50); delay(5000000); } void initPosition(){ //servo tester /* bodyRotationServoMove(90, 80, 50); bodyRotationServoMove(80, 100, 50); bodyRotationServoMove(100, 90, 50); delay(500);*/ /* armRotationServoMove(90, 70, 50); armRotationServoMove(70, 120, 50); //armRotationServoMove(120, 10, 50); delay(500); liftDuleMove(90, 70, 50); // (outward / inward) liftDuleMove(70, 100, 50); // (outward / inward) liftDuleMove(100, 70, 50); // (outward / inward) delay(500); armLiftMove(90, 40, 50);// (outward / inward) armLiftMove(40, 80, 50);// (outward / inward) delay(500); */ bodyRotationServo.attach(bodyRotationServoPin); bodyRotationServoMove(90, 45, 30); CurrentbodyRotationPos=45;bodyRotationServoGo(CurrentbodyRotationPos);delay(600); firgelli.attach(firgelliPin); liftDuleR.attach(liftDuleRPin); liftDuleL.attach(liftDuleLPin); armRotationServo.attach(armRotationServoPin); armLift.attach(armLiftPin); armLiftMove(90, 40, 20); CurrentArmLiftPos=40; armLiftGo(CurrentArmLiftPos);delay(600); // higher the number the lower the arm CurrentFirgelliPos=0; firgelliGo(CurrentFirgelliPos);delay(1000); // larger the wilder CurrentliftDulePos=90; liftDuleGo(CurrentliftDulePos);delay(1000); // larger the wilder armRotationServoGo(90);delay(600); Serial.println("Init DONE"); } boolean findSourcePlate(){ armLiftMove(CurrentArmLiftPos, 60, 10); // higher the number lower the arm CurrentArmLiftPos=60; int loopa=60; int loopb=150; int delayTime=80; for(int bodypos = loopa; bodypos <= loopb ; bodypos += 1){ float t1 = (float) (bodypos - loopa) / (float) (loopb - loopa); float t2 = (float) (loopb - bodypos) / (float) (loopb - loopa); bodyRotationServo.write(180-bodypos); //delay(delayTime); sens(); if (bottomFound){ CurrentbodyRotationPos=bodypos; bodypos =loopb; sushiPlateFound=true; bodyRotationServoMove(CurrentbodyRotationPos, CurrentbodyRotationPos+3, 20); CurrentbodyRotationPos=CurrentbodyRotationPos+3; // move a little bit over } Serial.println("found the sushi plate"); delay( delayTime+pow (max(t1, t2), 2) * 30); } return sushiPlateFound; } void reset(){ bodyRotationServoMove(CurrentbodyRotationPos, 45, 20);CurrentbodyRotationPos=45; armLiftMove(CurrentArmLiftPos, 40, 20);CurrentArmLiftPos=40; delay(600); // higher the number the lower the arm CurrentFirgelliPos=0; firgelliGo(CurrentFirgelliPos);delay(1000); // larger the wilder liftDuleMove(CurrentliftDulePos, 90, 20); // arm down, move back CurrentliftDulePos=90; bottomFound=false; topFound=false; sushiPlateFound=false; sushiFound=false; grabFroceOkay=false; grabbibgTheSushi=false; }