Advertisement
xDefo

G new version 0.1

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