manhoosbilli1

robot arm pick and place full code

Jul 13th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo claw;
  3. Servo pivot;
  4. Servo upperPivot;
  5. Servo clawRotator;
  6. Servo base;
  7. bool flag=0;
  8. bool trigger;
  9.  
  10.  
  11. void setup() {
  12. pivot.attach(5);
  13. claw.attach(5);
  14. clawRotator.attach(3);
  15. upperPivot.attach(11);
  16. base.attach(9);
  17. demo();
  18. flag= true; //use this flag to run the function once. same flag used in switch case
  19.  
  20. }
  21.  
  22. //this is necessary for the siwtch case function to work
  23. enum state{Default, pickObject, dropObject};
  24. int currentState = Default;
  25.  
  26.  
  27. void loop() {
  28.  
  29. }
  30. //NOTE: kindly keep the order of functions the same. otherwise you might get "function not declared in this scope" Error.
  31.  
  32.  
  33. /////////////////////////MOVEMENT FUNCTIONS//////////////////////////////////
  34.  
  35.  
  36. void openClaw(){ //the for loop is to move the claw or any other part of the arm slowly
  37. for(int i=30; i<=90; i++){
  38. claw.write(i); //30
  39. delay(20);
  40. }
  41. }
  42. void closeClaw(){
  43. for(int i=90; i>=30; i--){
  44. claw.write(i);
  45. delay(20);
  46. }
  47. }
  48. void movePivotToDefault(){ //moves pivot servo to default position
  49. for(int i=40; i<=100; i++){
  50. pivot.write(i); //30
  51. delay(20);
  52. }
  53. }
  54. void movePivotToPick() { //move pivot to a position where it can pick things up.
  55. for(int i=100; i>=10; i--){
  56. pivot.write(i); //30
  57. delay(20);
  58. }
  59. }
  60. void moveUpperPivotToDefault(){
  61. for(int i=140; i>=40; i--){
  62. upperPivot.write(i); //30
  63. delay(20);
  64. }
  65. }
  66. void moveUpperPivotToPick() { //can be used for drop as well
  67. for(int i=40; i<=140; i++){
  68. upperPivot.write(i); //140
  69. delay(20);
  70. }
  71. }
  72. void moveClawRotatorToVertical(){
  73. for(int i=90; i<=180; i++){
  74. clawRotator.write(i); //140
  75. delay(15);
  76. }
  77. }
  78. void moveClawRotatorToHorizontal(){
  79. for(int i=180; i>=90; i--){
  80. clawRotator.write(i); //140
  81. delay(20);
  82. }
  83. }
  84. void moveBaseToDefault(){ //always use movebasetodrop function first before using this.
  85. base.write(180); //move right
  86. delay(550);
  87. base.write(90); //stop base
  88. }
  89. void moveBaseToDrop(){ //if base is not moving to drop. try to calibrate it manually first with its claw facing forward
  90. base.write(0); //0 is for going left. replace 0 with 180 for going right and and 180 in the next function with zero
  91. delay(550);
  92. base.write(90); //stop base
  93. }
  94.  
  95. ///////////////////////////////////////////MOVEMENT FUNCTIONS FINISHED/////////////////////////////////
  96.  
  97. void defaultFunction() {
  98. movePivotToDefault();
  99. moveClawRotatorToVertical();
  100. closeClaw();
  101. moveUpperPivotToDefault();
  102. }
  103.  
  104. void pickFunction(){
  105. moveClawRotatorToHorizontal();
  106. openClaw();
  107. moveUpperPivotToPick();
  108. movePivotToPick();
  109.  
  110. }
  111.  
  112. void fromDropToDefault(){ //
  113. closeClaw();
  114. movePivotToDefault();
  115. moveUpperPivotToDefault();
  116. }
  117.  
  118. void dropFunction(){
  119.  
  120. moveBaseToDrop();
  121. delay(1000);
  122. pickFunction();
  123. openClaw();
  124. delay(500);
  125. closeClaw();
  126. }
  127.  
  128.  
  129. ////////////////////////////
  130.  
  131.  
  132. void armFunction(){
  133. //if trigger true;
  134. switch(currentState) {
  135. case Default:
  136. if(flag == true){
  137. Serial.println("triggered");
  138. currentState = pickObject;
  139. }
  140. break;
  141.  
  142. case pickObject:
  143. Serial.println("Picking Object");
  144. pickFunction();
  145. currentState = dropObject;
  146.  
  147. break;
  148.  
  149. case dropObject:
  150. Serial.println("Dropping Object");
  151. dropFunction();
  152. Serial.println("going back to default");
  153. fromDropToDefault();
  154. flag = false;
  155. currentState = Default;
  156. break;
  157.  
  158. }
  159. }
  160. /////////////////////////////////////////MAIN FUNCTION//////////////////////////////////////////////////////////
  161.  
  162. //you can use this function in setup if you want to demo the whole working of the arm. you can use the flag technique to
  163. //run this only once when called in loop.
  164. void demo(){
  165. defaultFunction();
  166. delay(1000);
  167. pickFunction();
  168. delay(1000);
  169. fromDropToDefault();
  170. moveBaseToDrop();
  171. moveUpperPivotToPick();
  172. movePivotToPick();
  173. openClaw();
  174. fromDropToDefault();
  175. moveBaseToDefault();
  176.  
  177. }
Add Comment
Please, Sign In to add comment