xDefo

g versione finale

May 25th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.65 KB | None | 0 0
  1. #pragma config(Sensor, S1,     gyroscope,      sensorEV3_Gyro)
  2. #pragma config(Sensor, S2,     colorSensor,    sensorEV3_Color, modeEV3Color_RGB_Raw)
  3. #pragma config(Sensor, S3,     frontSensor,    sensorEV3_Ultrasonic)
  4. #pragma config(Sensor, S4,     leftSensor,     sensorEV3_Ultrasonic)
  5. #pragma config(Motor,  motorA,          leftMotor,     tmotorEV3_Large, PIDControl, driveLeft, encoder)
  6. #pragma config(Motor,  motorB,          rightMotor,    tmotorEV3_Large, PIDControl, driveRight, encoder)
  7. #pragma config(Motor,  motorC,          clawLifter,    tmotorEV3_Large, PIDControl, encoder)
  8. #pragma config(Motor,  motorD,          claw,          tmotorEV3_Medium, PIDControl, encoder)
  9. //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
  10.  
  11. //Il nero lo scarta, il rosso lo mette nella chest a DX e il blu a SX.
  12.  
  13. const int moveforwardVelocity=20;
  14.  
  15. const int claw_Velocity=50;
  16. const int claw_PositionOpen=3*360;
  17. const int claw_PositionClose=-2.5*360;
  18.  
  19. const int clawLifter_Velocity=-50;
  20. const int clawLifter_PositionUp=360;
  21. const int clawLifter_PositionDown=360;
  22.  
  23. const double Front_Object_distance = 11;
  24. const int Left_Object_distance= 38;
  25. const int Blue = 5;
  26. const int Red = 3;
  27. bool compreso= false;
  28. bool mainore =false;
  29. bool maggiore =false;
  30. int tragitto=1;
  31. int oggettipresi=0;
  32. int distans=0;
  33. int contatore = 0;
  34. int temp=0;
  35. int colore;
  36. void moveForward(double v){
  37.     if(v>0){
  38.         double n= v/20.4
  39.         moveMotorTarget(motorA,n*360, 20);
  40.         moveMotorTarget(motorB,n*360, 20);
  41.         sleep(n*1000);
  42.     }
  43.     else if(v<0){
  44.         double n= v/20.4
  45.         moveMotorTarget(motorA,n*360, -20);
  46.         moveMotorTarget(motorB,n*360, -20);
  47.         sleep(n*1000);
  48.     }
  49. }
  50.  
  51. void movement (int V1, int V2 ){ // Funzione potenza ai motori;
  52.     motor[motorA]=V1;
  53.     motor[motorB]=V2;
  54.     wait1Msec(1);
  55. }
  56.  
  57. //Funzione che rileva la presenza di oggetti sulla sinistra confrontando la distanza con parametro passato alla funzione
  58. bool objectDetectedLeft(double a){
  59.     if(getUSDistance(leftSensor)<=a){
  60.         return true ;
  61.     }
  62.     else return false;
  63. }
  64.  
  65. //Funzione che rileva la presenza di oggetti  di fronte confrontando la distanza con parametro passato alla funzione
  66. bool objectDetectedFront(double a){
  67.     if(getUSDistance(frontSensor)<=a){
  68.         return true ;
  69.     }
  70.     else return false;
  71. }
  72.  
  73. void openClaw (){ //Funzione apri pinza
  74.     moveMotorTarget(claw,claw_PositionOpen,claw_Velocity);
  75.     sleep(500);
  76.  
  77. }
  78.  
  79. void closeClaw(){ //Funzione chiudi pinza
  80.     //  moveMotorTarget(claw,claw_PositionClose,-claw_Velocity);
  81.     //  sleep(500);
  82.     motor[claw]=-claw_Velocity;
  83.     delay(1500);
  84.     motor[claw]=0;
  85. }
  86.  
  87. void liftClaw(){ //Alza braccio
  88.     //moveMotorTarget(clawLifter,clawLifter_PositionUp+50,clawLifter_Velocity);
  89.     motor[clawLifter]=clawLifter_Velocity;
  90.     sleep(1000);
  91. }
  92.  
  93. void fallClaw(){ //Abbassa braccio
  94.     motor[clawLifter]=-clawLifter_Velocity;
  95.     sleep(1300);
  96.         motor[clawLifter]=0;
  97. }
  98.  
  99.  
  100. void turn(int c, int gradi){
  101.     resetGyro(gyroscope);
  102.     switch(c){
  103.     case 1:
  104.         while(getGyroDegrees(gyroscope)<gradi){
  105.             motor[motorA]=-10;
  106.             motor[motorB]=10;
  107.             wait1Msec(1);}
  108.         break;
  109.     case 2:     while(getGyroDegrees(gyroscope)>-gradi){
  110.             motor[motorA]=10;
  111.             motor[motorB]=-10;
  112.             wait1Msec(1);
  113.         }
  114.         break;}
  115. }
  116.  
  117.  
  118.  
  119. void initialize(){// Funzione di inizializazione
  120.     liftClaw();
  121.     openClaw();
  122.     moveForward(40);
  123.     turn('R',90);
  124.     moveForward(30);
  125.  
  126. }
  127.  
  128. void liftObject(){ // Funzione prendi oggetto
  129.     movement(0,0);
  130.     fallClaw();
  131.     delay(1000);
  132. }
  133.  
  134. void releaseObject(){//Rilascia oggetto
  135.     movement(0,0);
  136.     fallClaw();
  137.     openClaw();
  138.     liftClaw();
  139. }
  140.  
  141.  
  142. task main(){
  143.     //closeClaw();
  144. //inizio procedura inizializzazione
  145.     resetGyro(gyroscope);
  146.     liftClaw();
  147.     openClaw();
  148.     movement(-30,-30);
  149.     wait1Msec(1000);
  150.     movement(0,0);
  151.     wait1Msec(500);
  152.     while(true){
  153.         movement(20,20);
  154.         wait1Msec(2000);
  155.         movement(0,0);
  156.         wait1Msec(500);
  157.         turn(2,90);
  158.         movement(0,0);
  159.         sleep(500);
  160.         movement(-50,-50);
  161.         sleep(1500);
  162.         movement(0,0);
  163.         sleep(500);
  164.         movement(50,50);
  165.         sleep(1800+temp);
  166.         //fine inizializzazione
  167.         //AVANTI FIN QUANDO NON RILEVA NULLA A SINISTRA
  168.         while(!objectDetectedLeft(Left_Object_distance)){
  169.             movement(20,20);
  170.             sleep(500);
  171.             movement(0,0);
  172.             sleep(500);
  173.         }
  174.         //  UNA VOLTA RILEVATO GIRA A SINISTRA
  175.         moveForward(-6);
  176.         sleep(1000);
  177.         //gira finche non lo trovi
  178.         while(!objectDetectedFront(20)){movement(-10,10);delay(100);}
  179.         //vai avanti finche non lo becchi
  180.         while(!objectDetectedFront(Front_Object_distance)){
  181.             movement(10,10);
  182.             sleep(500);
  183.             movement(0,0);
  184.             sleep(500);
  185.         }
  186.         //PRENDE L'OGGETTO
  187.         fallClaw();
  188.         //giusto
  189.         //Inizio procedura di drop
  190.         colore=0;
  191.          colore = getColorName(colorSensor);
  192.          delay(100);
  193.         if(!(colore==3)){ //Se il colore non é ne rosso ne blu(non funziona manco per sbaglio)
  194.         // vai anvanti di un po
  195.         while (getUSDistance(frontSensor)>6.5){
  196.                 movement(10,10);
  197.                 delay(100);
  198.                 }
  199.             movement(0,0);
  200.             sleep(500);
  201.             //chiudi e alza pinza
  202.             closeClaw();
  203.             liftclaw();
  204.             sleep (1500);
  205.             // indietro per allinearsi con il muro
  206.             movement(-50,-50);
  207.             delay(3500);
  208.             //gira a destra di 90
  209.             turn(2,90);
  210.             movement(0,0);
  211.             delay(500);
  212.             //finchè non ce la cassa va avanti radente al muro
  213.             while(!objectDetectedFront(5))
  214.             {
  215.                 movement(100,100);
  216.  
  217.             }
  218.             movement(0,0);
  219.             //apre(droppa) e chiude la pinza
  220.             openClaw();
  221.             sleep(500);
  222.             closeClaw();
  223.  
  224.         }
  225.         //Fine funzione di drop
  226.         //funziona piu o meno
  227.         // va indietro fino all'inizio
  228.         movement(-200,-200);
  229.         sleep(6000);
  230.         movement(0,0);
  231.         sleep(500);
  232.         //gira a sinista
  233.         turn(1,90);
  234.         //va indietro
  235.         movement(-50,-50);
  236.         sleep(1000);
  237.         //apre la pinza
  238.         openClaw();
  239.         //non so se funziona
  240.         temp +=400;
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment