Advertisement
xDefo

g alpha

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