Advertisement
xDefo

g new version

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