Advertisement
xDefo

g work in progress

May 2nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 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.  
  11. const int moveforwardVelocity=20;
  12.  
  13. const int claw_Velocity=100;
  14. const int claw_PositionOpen=3*360;
  15. const int claw_PositionClose=-3*360;
  16.  
  17. const int clawLifter_Velocity=-50;
  18. const int clawLifter_PositionUp=360;
  19. const int clawLifter_PositionDown=360;
  20.  
  21. const int Object_distance = 5;
  22.  
  23. const int Blue = 5;
  24. const int Red = 3;
  25.  
  26.  
  27.  
  28. void moveForward(int v){
  29.     motor[motorA]=v;
  30.     motor[motorB]=v;
  31.     wait1Msec(1);
  32. }
  33.  
  34. void movement (int V1, int V2 ){
  35.     //setMotorSpeed(motorA, V1);
  36.     //setMotorSpeed(motorB, V2);
  37.     motor[motorA]=V1;
  38.     motor[motorB]=V2;
  39.     wait1Msec(1);
  40. }
  41. bool objectDetectedLeft(int a)
  42. {
  43.     if(getUSDistance(leftSensor)<=a)
  44.     {
  45.         return true;
  46.     }
  47.     else return false;
  48. }
  49. bool objectDetectedFront(int a){
  50.  
  51.  
  52.     if(getUSDistance(frontSensor)<=a){
  53.         return true ;
  54.     }
  55.     else return false;
  56. }
  57.  
  58.  
  59.  
  60.  
  61. void openClaw (){
  62.     moveMotorTarget(claw,claw_PositionOpen,claw_Velocity);
  63.     sleep(500);
  64. }
  65.  
  66. void closeClaw(){
  67.     moveMotorTarget(claw,claw_PositionClose,-claw_Velocity);
  68.     sleep(500);
  69. }
  70.  
  71. void liftClaw(){
  72.     moveMotorTarget(clawLifter,clawLifter_PositionUp+10,clawLifter_Velocity);
  73.     sleep(500);
  74. }
  75.  
  76. void fallClaw(){
  77.     moveMotorTarget(clawLifter,clawLifter_PositionDown,-clawLifter_Velocity);
  78.     sleep(500);
  79. }
  80.  
  81.  
  82. void inizialize(){
  83.     liftClaw();
  84.     openClaw();
  85. }
  86.  
  87.  
  88. void liftObject(){
  89.     movement(0,0);
  90.     fallClaw();
  91.     delay(1000);
  92.     closeClaw();
  93.     delay(1000);
  94.     liftClaw();
  95.  
  96. }
  97.  
  98. void releaseObject(){
  99.     movement(0,0);
  100.     fallClaw();
  101.     openClaw();
  102.     liftClaw();
  103. }
  104.  
  105.  
  106.  
  107. void turn( char a)
  108. {
  109.     resetGyro(gyroscope);
  110.     switch (a)
  111.     {
  112.     case 'L':
  113.         resetGyro(gyroscope);
  114.         while (getGyroDegrees(gyroscope)<99)
  115.         {
  116.             motor[motorA]=10;
  117.             motor[motorB]=-10;
  118.             wait1Msec(1);
  119.         }
  120.         break;
  121.     case 'R' :
  122.         resetGyro(gyroscope);
  123.         while (getGyroDegrees(gyroscope)>-99 )
  124.         {
  125.             motor[motorA]=-10;
  126.             motor[motorB]=10;
  127.             wait1Msec(1);
  128.         }
  129.         break;
  130.  
  131.     }
  132. }
  133.  
  134. void dropObject()
  135. {
  136.     movement(-10,-10);
  137.     sleep(1000);
  138.     movement(0,0);
  139.     turn('R');
  140.     movement(0,0);
  141.     while(!objectDetectedFront(3.5))
  142.     {
  143.         movement(10,10);
  144.     }
  145.     movement(0,0);
  146.     openClaw();
  147. }
  148. void routine(int n)// Funzione cerca oggetto è afferra; Warka
  149. {
  150.     while(!objectDetectedLeft(20)) //Mentre il LeftSensor non rileva oggetti va avanti
  151.     {
  152.         movement(n*moveforwardVelocity,n*moveforwardVelocity);
  153.     }
  154.     movement(0,0);//spegnimento motori
  155.     turn('L');//gira a sinistra di 90°
  156.     movement(0,0);//spegnimento motori
  157.     while(!objectDetectedFront(Object_distance))//Mentre la distanza è maggiore di Object_distance va avanti
  158.     {
  159.         movement(10,10);
  160.         if(objectDetectedFront(Object_distance))
  161.         {
  162.             movement(0,0);
  163.             liftObject();
  164.             break;
  165.         }
  166.     }
  167. }
  168. void Program(){
  169.  
  170.     inizialize();
  171.     routine(1);
  172.     movement(0,0);
  173.     dropObject();
  174.     delay(500);
  175.     closeClaw();
  176.     openClaw();
  177.     delay(500);
  178.     routine(-1)
  179. }
  180.  
  181. task main(){
  182.  Program();
  183. //closeClaw();
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement