Advertisement
xDefo

g 4.0

May 12th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.34 KB | None | 0 0
  1. #pragma config(Sensor, S1,     gyroscope,      sensorEV3_Gyro, modeEV3Gyro_RateAndAngle)
  2. #pragma config(Sensor, S2,     colorSensor,    sensorEV3_Color, modeEV3Color_Color)
  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. //Il nero lo scarta, il rosso lo mette nella chest a DX e il blu a SX.
  11.  
  12. const int moveforwardVelocity=20;
  13.  
  14. const int claw_Velocity=100;
  15. const int claw_PositionOpen=3*360;
  16. const int claw_PositionClose=-3*360;
  17.  
  18. const int clawLifter_Velocity=-50;
  19. const int clawLifter_PositionUp=360;
  20. const int clawLifter_PositionDown=360;
  21.  
  22. const int Object_distance = 7;
  23.  
  24. const int Blue = 5;
  25. const int Red = 3;
  26.  
  27. int tragitto=1;
  28. int oggettipresi=0;
  29.  
  30. /*void moveForward(int v){
  31. motor[motorA]=v;
  32. motor[motorB]=v;
  33. wait1Msec(1);
  34. }*/
  35.  
  36. void movement (int V1, int V2 ){ // Funzione potenza hai motori;
  37.     motor[motorA]=V1;
  38.     motor[motorB]=V2;
  39.     wait1Msec(1);
  40. }
  41.  
  42. //Funzione che rileva la presenza di oggetti sulla sinistra confrontando la distanza con parametro passato alla funzione
  43. bool objectDetectedLeft(int a){
  44.     if(getUSDistance(leftSensor)<=a)
  45.     {
  46.         return true;
  47.     }
  48.     else return false;
  49. }
  50.  
  51. //Funzione che rileva la presenza di oggetti  di fronte confrontando la distanza con parametro passato alla funzione
  52. bool objectDetectedFront(int a){
  53.  
  54.  
  55.  
  56.     if(getUSDistance(frontSensor)<=a){
  57.         return true ;
  58.     }
  59.     else return false;
  60. }
  61.  
  62. int color()
  63. {
  64.     if(getColorName(colorSensor)==colorBlack)
  65.     {
  66.         return 0;
  67.     }
  68.     else if(getColorName(colorSensor)==colorBlue)
  69.     {
  70.         return 1;
  71.     }
  72.     return 2;
  73.     //Funzione che rileva il colore dell' oggetto preso
  74. }
  75.  
  76.  
  77. void openClaw (){ //Funzione apri pinza
  78.     moveMotorTarget(claw,claw_PositionOpen,claw_Velocity);
  79.     sleep(500);
  80. }
  81.  
  82. void closeClaw(){ //Funzione chiudi pinza
  83.     moveMotorTarget(claw,claw_PositionClose,-claw_Velocity);
  84.     sleep(500);
  85. }
  86.  
  87. void liftClaw(){ //Alza braccio
  88.     moveMotorTarget(clawLifter,clawLifter_PositionUp+10,clawLifter_Velocity);
  89.     sleep(500);
  90. }
  91.  
  92. void fallClaw(){ //Abbassa braccio
  93.     moveMotorTarget(clawLifter,clawLifter_PositionDown,-clawLifter_Velocity);
  94.     sleep(500);
  95. }
  96.  
  97.  
  98. void initialize(){// Funzione di inizializazione
  99.     liftClaw();
  100.     openClaw();
  101. }
  102.  
  103.  
  104. void liftObject(){ // Funzione prendi oggetto
  105.     movement(0,0);
  106.     fallClaw();
  107.     delay(1000);
  108.     closeClaw();
  109.     delay(1000);
  110.     liftClaw();
  111.  
  112. }
  113.  
  114. void releaseObject(){//Rilascia oggetto
  115.     movement(0,0);
  116.     fallClaw();
  117.     openClaw();
  118.     liftClaw();
  119. }
  120.  
  121.  
  122.  
  123. void turn(char a, int gradi)//Funzione gira di 90°
  124. {
  125.     resetGyro(gyroscope);
  126.     switch (a)
  127.     {
  128.     case 'L':
  129.         resetGyro(gyroscope);
  130.         while (getGyroDegrees(gyroscope)<gradi)
  131.         {
  132.             motor[motorA]=10;
  133.             motor[motorB]=-10;
  134.             wait1Msec(1);
  135.         }
  136.         break;
  137.     case 'R' :
  138.         resetGyro(gyroscope);
  139.         while (getGyroDegrees(gyroscope)>-gradi )
  140.         {
  141.             motor[motorA]=-10;
  142.             motor[motorB]=10;
  143.             wait1Msec(1);
  144.         }
  145.         break;
  146.  
  147.     }
  148. }
  149.  
  150. void dropObject_right(int a)//Porta l'ogetto alla cassa
  151. {
  152.     switch (a)
  153.     {
  154.         case 0:
  155.            movement(-100,-100);
  156.            sleep(1000);
  157.            movement(0,0);
  158.            turn('L',90);
  159.            while(!objectDetectedFront(3))
  160.                 {
  161.                    movement(50,50);
  162.  
  163.                }
  164.            movement(0,0);
  165.            openClaw();
  166.            closeClaw();
  167.            turn('R', 180);
  168.            oggettipresi++;
  169.             break;
  170.  
  171.         case 1:
  172.              movement(-100,-100);
  173.            sleep(1000);
  174.            movement(0,0);
  175.            turn('R',90);
  176.            while(!objectDetectedFront(3))
  177.                 {
  178.                    movement(50,50);
  179.  
  180.                }
  181.            movement(0,0);
  182.            openClaw();
  183.            closeClaw();
  184.            tragitto=-1;
  185.            oggettipresi++;
  186.           break;
  187.  
  188.     }
  189.  
  190. }
  191.  
  192. void dropObject_left(){
  193. }
  194.  
  195. void routine(int n)// Funzione cerca oggetto è afferra; Warka
  196. {
  197.     while(!objectDetectedLeft(30)) //Mentre il LeftSensor non rileva oggetti va avanti
  198.     {
  199.         movement(n*moveforwardVelocity,n*moveforwardVelocity);
  200.     }
  201.     movement(0,0);//spegnimento motori
  202.    turn('L',90);//gira a sinistra di 90°
  203.     movement(0,0);//spegnimento motori
  204.     while(!objectDetectedFront(Object_distance))//Mentre la distanza è maggiore di Object_distance va avanti
  205.     {
  206.         movement(10,10);
  207.         if(objectDetectedFront(Object_distance))
  208.         {
  209.             movement(0,0);
  210.             liftObject();
  211.             break;
  212.         }
  213.     }
  214. }
  215.  
  216. void Program(){
  217.  
  218.  
  219.     routine(tragitto);
  220.     movement(0,0);
  221.     dropObject_right(color());
  222. }
  223.  
  224. task main(){
  225.     initialize();
  226.   while(oggettipresi < 6){Program();}
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement