Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
2,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <Stepper.h>
  2. int enablePin = 13;
  3.  
  4. int zUpPin = 11;
  5. int zDownPin = 10;
  6.  
  7. int feedPin = 12;
  8. int retractPin = 2;
  9.  
  10. int x1Pin = 9;
  11. int x2Pin = 8;
  12. int x3Pin = 7;
  13. int x4Pin = 6;
  14.  
  15. int y1Pin = 5;
  16. int y2Pin = 4;
  17. int y3Pin = 3;
  18. int y4Pin = 2;
  19.  
  20. int glueon = 2;
  21. int gluecycle = 145;
  22. int gluetick = 0;
  23.  
  24. #define xSTEPS 20
  25. #define ySTEPS 20
  26.  
  27. Stepper xmotor(xSTEPS, x1Pin, x2Pin, x3Pin, x4Pin);
  28. Stepper ymotor(ySTEPS, y1Pin, y2Pin, y3Pin, y4Pin);
  29.  
  30. int stepdelay = 100;
  31.  
  32. int feedtime = 2;
  33.  
  34.  
  35. /*
  36. x is x pos of head
  37. y is y pos
  38. x1 is path start x
  39. x2 is path end x
  40. y1 and y2 are for y
  41. dx is delta x, how far x is from where is should be at current step in path. dy is the same for y axis.
  42. steps is steps in path, which is x1 + y1 - x2 -y2,
  43. sx is y move over steps slopes and yx is y move over steps slope
  44. wx is the wanted x position at count step in path, wy is wanted y
  45. px is the distance from x to wx, or the pull of x. p is d upside down. py is the same for y.
  46. */
  47.  
  48. int x = 0;
  49. int y = 0;
  50. int x1 = 0;
  51. int x2 = 0;
  52. int y1 = 0;
  53. int y2 = 0;
  54. float dx = 0;
  55. float dy = 0;
  56. int steps = 0;
  57. float sx = 0;
  58. float sy = 0;
  59. float wx = 0;
  60. float wy = 0;
  61. float px = 0;
  62. float py = 0;
  63.  
  64. void setup()
  65. {
  66. pinMode(x1Pin, OUTPUT);
  67. pinMode(x2Pin, OUTPUT);
  68. pinMode(x3Pin, OUTPUT);
  69. pinMode(x4Pin, OUTPUT);
  70.  
  71. pinMode(y1Pin, OUTPUT);
  72. pinMode(y2Pin, OUTPUT);
  73. pinMode(y3Pin, OUTPUT);
  74. pinMode(y4Pin, OUTPUT);
  75.  
  76. pinMode(feedPin, OUTPUT);
  77. pinMode(retractPin, OUTPUT);
  78.  
  79. pinMode(zUpPin, OUTPUT);
  80. pinMode(zDownPin, OUTPUT);
  81. xmotor.setSpeed(100);
  82. ymotor.setSpeed(100);
  83. Serial.begin(115200);
  84. }
  85.  
  86.  
  87. void move()
  88. {
  89. Serial.print("at coords:");
  90. Serial.print(x);
  91. Serial.print(",");
  92. Serial.println(y);
  93. x1=x;
  94. y1=y;
  95. dx = x2 - x1;
  96. dy = y2 - y1;
  97. Serial.print("x2,y2:");
  98. Serial.print(x2);
  99. Serial.print(",");
  100. Serial.println(y2);
  101.  
  102. steps = abs(dx) + abs(dy);
  103. Serial.print("need to move:");
  104. Serial.print(dx);
  105. Serial.print(",");
  106. Serial.println(dy);
  107. Serial.print("steps required:");
  108. Serial.println(steps);
  109.  
  110. sx = dx / steps;
  111. sy = dy / steps;
  112.  
  113. Serial.print("slopes:");
  114. Serial.print(sx);
  115. Serial.print(",");
  116. Serial.println(sy);
  117. digitalWrite(enablePin,HIGH);
  118. Serial.println("----------------------------");
  119. for (int i=0; i < steps + 1; i++){
  120.  
  121. gluetick++;
  122. if (gluetick < glueon){
  123. digitalWrite(feedPin,HIGH);
  124. }
  125. if (gluetick > glueon){
  126. digitalWrite(feedPin,LOW);
  127. }
  128. if (gluetick > gluecycle){
  129. gluetick=0;
  130. }
  131. wx = sx * i + x1 + sx;
  132. wy = sy * i + y1 + sy;
  133. px = wx - x;
  134. py = wy - y;
  135.  
  136. /*Serial.print("we are at coords:");
  137. Serial.print(x);
  138. Serial.print(",");
  139. Serial.println(y);
  140.  
  141. Serial.print("wanted for this step:");
  142. Serial.print(wx);
  143. Serial.print(",");
  144. Serial.println(wy);
  145.  
  146. Serial.print("pull is:");
  147. Serial.print(px);
  148. Serial.print(",");
  149. Serial.println(py);
  150. */
  151. if (abs(px) >= abs(py)){
  152. if (px > 0){
  153. stepxforward();
  154. //Serial.println("Stepping x up");
  155. x = x + 1;
  156. }else{
  157. stepxback();
  158. //Serial.println("Stepping x down");
  159. x = x - 1;
  160. }
  161. }else{
  162. if (py > 0){
  163. stepyforward();
  164. //Serial.println("Stepping y up");
  165. y = y + 1;
  166. }else{
  167. stepyback();
  168. //Serial.println("Stepping y down");
  169. y = y - 1;
  170. }
  171. }
  172. Serial.println("-----------------------");
  173. delay(5);
  174. }
  175. digitalWrite(enablePin,LOW);
  176.  
  177. }
  178. void loop(){
  179.  
  180. x2=20;
  181. y2=50;
  182. move();
  183. delay(1000);
  184. x2=130;
  185. y2=50;
  186. move();
  187. delay(1000);
  188. x2=130;
  189. y2=220;
  190. move();
  191. delay(1000);
  192. x2=20;
  193. y2=220;
  194. move();
  195. delay(1000);
  196. digitalWrite(zUpPin,HIGH);
  197. delay(25);
  198. digitalWrite(zUpPin,LOW);
  199. }
  200.  
  201.  
  202. void stepxforward(){
  203. xmotor.step(1);
  204. }
  205. void stepyforward(){
  206. ymotor.step(1);
  207. }
  208. void stepxback(){
  209. xmotor.step(-1);
  210. }
  211. void stepyback(){
  212. ymotor.step(-1);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement