xDefo

g 2.0 work in progress

May 3rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 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 = 5;
  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.     else if(getColorName(colorSensor)==colorRed)
  71.     {
  72.         return 2;
  73.     }
  74.     //Funzione che rileva il colore del''ogetto preso
  75. }
  76.  
  77.  
  78. void openClaw (){ //Funzione apri pinza
  79.     moveMotorTarget(claw,claw_PositionOpen,claw_Velocity);
  80.     sleep(500);
  81. }
  82.  
  83. void closeClaw(){ //Funzione chiudi pinza
  84.     moveMotorTarget(claw,claw_PositionClose,-claw_Velocity);
  85.     sleep(500);
  86. }
  87.  
  88. void liftClaw(){ //Alza braccio
  89.     moveMotorTarget(clawLifter,clawLifter_PositionUp+10,clawLifter_Velocity);
  90.     sleep(500);
  91. }
  92.  
  93. void fallClaw(){ //Abbassa braccio
  94.     moveMotorTarget(clawLifter,clawLifter_PositionDown,-clawLifter_Velocity);
  95.     sleep(500);
  96. }
  97.  
  98.  
  99. void inizialize(){// Funzione di inizializazione
  100.     liftClaw();
  101.     openClaw();
  102. }
  103.  
  104.  
  105. void liftObject(){ // Funzione prendi oggetto
  106.     movement(0,0);
  107.     fallClaw();
  108.     delay(1000);
  109.     closeClaw();
  110.     delay(1000);
  111.     //liftClaw();
  112.  
  113. }
  114.  
  115. void releaseObject(){//Rilascia oggetto
  116.     movement(0,0);
  117.     fallClaw();
  118.     openClaw();
  119.     liftClaw();
  120. }
  121.  
  122.  
  123.  
  124. void turn( char a)//Funzione gira di 90°
  125. {
  126.     resetGyro(gyroscope);
  127.     switch (a)
  128.     {
  129.     case 'L':
  130.         resetGyro(gyroscope);
  131.         while (getGyroDegrees(gyroscope)<99)
  132.         {
  133.             motor[motorA]=10;
  134.             motor[motorB]=-10;
  135.             wait1Msec(1);
  136.         }
  137.         break;
  138.     case 'R' :
  139.         resetGyro(gyroscope);
  140.         while (getGyroDegrees(gyroscope)>-99 )
  141.         {
  142.             motor[motorA]=-10;
  143.             motor[motorB]=10;
  144.             wait1Msec(1);
  145.         }
  146.         break;
  147.  
  148.     }
  149. }
  150.  
  151. void dropObject(int a)//Porta l'ogetto alla cassa
  152. {
  153.     switch (a)
  154.     {
  155.         case 0:break;
  156.         case 1:break;
  157.         case 2:break;
  158.        
  159.     }
  160. /*  movement(-10,-10);
  161.     sleep(1000);
  162.     movement(0,0);
  163.     turn('R');
  164.     movement(0,0);
  165.     delay(500);
  166.     while(!objectDetectedFront(3.5))
  167.     {
  168.         movement(10,10);
  169.     }
  170.     movement(0,0);
  171.     openClaw();*/
  172. }
  173. void routine(int n)// Funzione cerca oggetto è afferra; Warka
  174. {
  175.     while(!objectDetectedLeft(20)) //Mentre il LeftSensor non rileva oggetti va avanti
  176.     {
  177.         movement(n*moveforwardVelocity,n*moveforwardVelocity);
  178.     }
  179.     movement(0,0);//spegnimento motori
  180.     turn('L');//gira a sinistra di 90°
  181.     movement(0,0);//spegnimento motori
  182.     while(!objectDetectedFront(Object_distance))//Mentre la distanza è maggiore di Object_distance va avanti
  183.     {
  184.         movement(10,10);
  185.         if(objectDetectedFront(Object_distance))
  186.         {
  187.             movement(0,0);
  188.             liftObject();
  189.             break;
  190.         }
  191.     }
  192. }
  193. void Program(){
  194.  
  195.     inizialize();
  196.     routine(1);
  197.     movement(0,0);
  198.     dropObject(color());
  199.     delay(500);
  200.     closeClaw();
  201.     delay(500);
  202.     openClaw();
  203.     delay(500);
  204.     routine(-1);
  205. }
  206.  
  207. task main(){
  208.         //resetGyro(gyroscope);
  209.     //turn('L');
  210.      Program();
  211.     //closeClaw();
  212.  
  213. }
Add Comment
Please, Sign In to add comment