Advertisement
xDefo

g 3.0

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