Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. #pragma config(Sensor, in2, ArmLF, sensorLineFollower)
  2. #pragma config(Sensor, in3, CDLF, sensorLineFollower)
  3. #pragma config(Sensor, dgtl1, distance, sensorSONAR_cm)
  4. #pragma config(Sensor, dgtl3, eStop, sensorTouch)
  5. #pragma config(Sensor, dgtl4, en, sensorQuadEncoder)
  6. #pragma config(Motor, port2, mGrip, tmotorVex393_MC29, openLoop)
  7. #pragma config(Motor, port3, mSlider, tmotorVex393_MC29, openLoop)
  8. #pragma config(Motor, port4, mRotate, tmotorServoContinuousRotation, openLoop)
  9. #pragma config(Motor, port5, mm, tmotorVex393_MC29, openLoop)
  10. #pragma config(Motor, port6, lidStamper, tmotorVex393_MC29, openLoop)
  11. #pragma config(Motor, port7, pmCyl, tmotorVex393_MC29, openLoop)
  12. #pragma config(Motor, port8, chestClose, tmotorVex393_MC29, openLoop)
  13. #pragma config(Motor, port9, coinStamper, tmotorVex393_MC29, openLoop)
  14. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  15.  
  16. //mm is the cd racks motor
  17. //cm is the counters motor
  18. //km is the lid closer motor
  19.  
  20. int counter = 0;
  21. int coinsPassed = 0;
  22. int cdRotations = 0;
  23.  
  24. task e_stop()
  25. {
  26. while(true)
  27. {
  28. if(SensorValue(eStop) == 1)
  29. {
  30. stopAllTasks(); // ends the program and all tasks including task main.
  31. }
  32. wait1Msec(10); // prevents the current task from using majority of available CPU capacity
  33. }
  34. }
  35. task cdCounter()
  36. {
  37. while(true) {
  38. if(SensorValue(CDLF) < 1800 && SensorValue(CDLF) > 1400) {
  39. coinsPassed = coinsPassed + 1;
  40. waitUntil(SensorValue(CDLF) > 1800); //Wait until coin passes
  41. }
  42.  
  43. if(coinsPassed == 4) {
  44. coinsPassed = 0;
  45. }
  46.  
  47. wait1Msec(5);
  48. }
  49. }
  50. bool goTo(tMotor m, float point, int speed)
  51. {
  52. while(SensorValue(en) != point)
  53. {
  54. if(SensorValue(en) > point)
  55. {
  56. startMotor(m, -1 * speed);
  57. }
  58. else
  59. {
  60. startMotor(m, speed);
  61. }
  62. }
  63. stopMotor(m);
  64. return true;
  65. }
  66.  
  67. /* Will attempt to go to an exact point 6 times
  68. If after 6 attempts it can't get the exact value requested it will
  69. accept a value that is +- 1 of the requested value */
  70. //Update now will accept anyting within 3 degrees of requested value
  71.  
  72. bool goToExactValue(tMotor m, float point)
  73. {
  74. int completed = false;
  75. //int fixAttempts = 0; //Here so robot doesn't get stuck and try to correct itself forever
  76.  
  77. while(!completed) {
  78. waitUntil(goTo(m, point, 19));
  79. wait(.25); //Allow the motor to fully stop before correction
  80. completed = SensorValue(en) < (point + 3) && SensorValue(en) > (point - 3);
  81. //if(SensorValue(en) != point && fixAttempts <= 0) {
  82. //waitUntil(goTo(m, point, 19));
  83. //fixAttempts = fixAttempts + 1;
  84. //} else {
  85. //completed = SensorValue(en) < (point + 3) && SensorValue(en) > (point + 3);
  86. //completed = (SensorValue(en) == point || SensorValue(en) == point + 1 || SensorValue(en) == point - 1);
  87. //}
  88. }
  89.  
  90. return true;
  91. }
  92.  
  93. bool manipulateGrip(bool open) {
  94. if(open) {
  95. startMotor(mGrip, 40);
  96. } else {
  97. startMotor(mGrip, -40);
  98. }
  99.  
  100. wait(.5);
  101. stopMotor(mGrip);
  102. return true;
  103. }
  104.  
  105. bool moveSlider(float point)
  106. {
  107. if(SensorValue(distance) > point) {
  108. startMotor(mSlider, -25);
  109. waitUntil(SensorValue(distance) <= point && SensorValue(distance) != -1);
  110. startMotor(mSlider, 25);
  111. waitUntil(SensorValue(distance) <= point && SensorValue(distance) != -1);
  112. } else {
  113. startMotor(mSlider, 25);
  114. waitUntil(SensorValue(distance) >= point && SensorValue(distance) != -1);
  115. startMotor(mSlider, -25);
  116. waitUntil(SensorValue(distance) >= point && SensorValue(distance) != -1);
  117. }
  118. stopMotor(mSlider);
  119.  
  120. return true;
  121. }
  122.  
  123. bool rotateArm(bool home) {
  124. if (home) {
  125. setServo(mRotate, -15);
  126. } else {
  127. setServo(mRotate, -120);
  128. }
  129.  
  130. wait(1);
  131.  
  132. return true;
  133. }
  134.  
  135. task cdRack()
  136. {
  137. SensorValue(en) = 0;
  138. //startMotor(cm, 20);
  139. startTask(cdCounter);
  140. while(true) {
  141. int rotationEffect = cdRotations * 360;
  142.  
  143. waitUntil(goToExactValue(mm, rotationEffect + 65));//Pick up a new container
  144. waitUntil(goToExactValue(mm, rotationEffect + 50)); //Move back to ensure the container is grabbed
  145. waitUntil(goToExactValue(mm, rotationEffect + 180)); //Move to coin dropper
  146. waitUntil(coinsPassed == 4); //Count coins
  147. wait(5); //Wait for all coins to slide down
  148. //Wiggle to ensure coins are fully in the container
  149. waitUntil(goToExactValue(mm, rotationEffect + 170)); //Wiggle
  150. waitUntil(goToExactValue(mm, rotationEffect + 190)); //Wiggle
  151. waitUntil(goToExactValue(mm, rotationEffect + 180)); //Wiggle
  152.  
  153. wait(goTo(mm, rotationEffect + 280, 20)); //Go to lid stop
  154. //Close the lid
  155. startMotor(lidStamper, 30);
  156. wait(.5);
  157. startMotor(lidStamper, -30);
  158. wait(.5);
  159. stopMotor(lidStamper);
  160. wait(goToExactValue(mm, rotationEffect + 360)); //Go to start
  161. wait(goToExactValue(mm, rotationEffect + 365)); //Go to start
  162. wait(goToExactValue(mm, rotationEffect + 355)); //Go to start
  163.  
  164. cdRotations = cdRotations + 1;
  165. }
  166.  
  167.  
  168. //while(true) {
  169. //if(coinsPassed == 4) {
  170. //wait(15);
  171. //Wiggle before moving
  172. //waitUntil(goTo(mm, (90 * cdRotations) + 10, 100));
  173. //wait(.5);
  174. //waitUntil(goTo(mm, (90 * cdRotations) - 10, 100));
  175. //wait(.5);
  176. //cdRotations = cdRotations + 1;
  177. //goToExactValue(mm, 60);
  178. //waitUntil(goToExactValue(mm, 90 * cdRotations));// No longer using the gear makes corrections not needed
  179. //waitUntil(goTo(mm, 90 * cdRotations, 30));
  180. //}
  181. //}
  182. }
  183.  
  184. task main()
  185. {
  186. startTask(e_stop);
  187. startTask(cdRack);
  188.  
  189. waitUntil(manipulateGrip(true)); //open gripper
  190. waitUntil(rotateArm(true)); //Move the arm to the home position
  191.  
  192. while(true) {
  193. if(SensorValue(ArmLF) < 2800 && counter <= 3) {
  194. waitUntil(manipulateGrip(false)); //close gripper
  195. int position = (counter * 3) + 6;
  196. waitUntil(rotateArm(false));//Rotate the arm
  197. waitUntil(moveSlider(position)); //Move slider to position
  198. counter = counter + 1;
  199.  
  200. waitUntil(manipulateGrip(true)); //open the gripper
  201. wait(2);
  202. waitUntil(moveSlider(6)); //Move slider back home
  203. waitUntil(rotateArm(true)); //Move arm back to home
  204.  
  205. if(counter == 3) {
  206. startMotor(chestClose, 25);
  207. wait(.5);
  208. startMotor(chestClose, -25);
  209. wait(.5);
  210. stopMotor(chestClose);
  211. counter = 0;
  212. }
  213. }
  214. waitInMilliseconds(10);
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement