Guest User

Arduino Code for DIY Pick N Place

a guest
Jan 8th, 2025
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. // Pin Definitions
  4. const int CSN_PIN = 10; // Chip select for TMC5272
  5. const int Z_IN1 = 6; // L298N input 1
  6. const int Z_IN2 = 7; // L298N input 2
  7. const int Z_IN3 = 8; // L298N input 3
  8. const int Z_IN4 = 9; // L298N input 4
  9.  
  10. // TMC5272 Register Addresses
  11. #define GCONF 0x00
  12. #define GSTAT 0x01
  13. #define IHOLD_IRUN 0x10
  14. #define CHOPCONF 0x6C
  15. #define COOLCONF 0x6D
  16. #define DCCTRL 0x6E
  17. #define DRVSTATUS 0x6F
  18. #define RAMPMODE 0x20
  19. #define XACTUAL 0x21
  20. #define VACTUAL 0x22
  21. #define XTARGET 0x2D
  22.  
  23. // Motor configuration
  24. const long stepsPerRevolution = 200; // For NEMA 17
  25. const float mmPerRevolution = 40.0; // GT2 pulley circumference for X/Y
  26. const float mmPerStep = 0.02; // Lead screw pitch divided by steps for Z
  27.  
  28. // Current settings
  29. const uint8_t runCurrent = 16; // Range 0-31
  30. const uint8_t holdCurrent = 8; // Range 0-31
  31.  
  32. void setup() {
  33. Serial.begin(115200);
  34.  
  35. // Initialize SPI
  36. SPI.begin();
  37. pinMode(CSN_PIN, OUTPUT);
  38. digitalWrite(CSN_PIN, HIGH);
  39.  
  40. // Initialize L298N pins
  41. pinMode(Z_IN1, OUTPUT);
  42. pinMode(Z_IN2, OUTPUT);
  43. pinMode(Z_IN3, OUTPUT);
  44. pinMode(Z_IN4, OUTPUT);
  45.  
  46. // Configure TMC5272
  47. initTMC5272();
  48. }
  49.  
  50. void initTMC5272() {
  51. // Basic configuration
  52. writeTMC5272(GCONF, 0x00000004); // Enable internal reference voltage
  53. writeTMC5272(CHOPCONF, 0x000100C3); // Standard chopper config
  54. writeTMC5272(IHOLD_IRUN, 0x00071703); // Set current values
  55.  
  56. // Set motor parameters for X axis
  57. writeTMC5272(RAMPMODE, 0); // Position mode
  58. writeTMC5272(XACTUAL, 0); // Reset position
  59.  
  60. delay(100); // Allow time for driver to initialize
  61. }
  62.  
  63. void writeTMC5272(uint8_t addr, uint32_t data) {
  64. digitalWrite(CSN_PIN, LOW);
  65. SPI.transfer(addr | 0x80); // Set write bit
  66. SPI.transfer((data >> 24) & 0xFF);
  67. SPI.transfer((data >> 16) & 0xFF);
  68. SPI.transfer((data >> 8) & 0xFF);
  69. SPI.transfer(data & 0xFF);
  70. digitalWrite(CSN_PIN, HIGH);
  71. }
  72.  
  73. uint32_t readTMC5272(uint8_t addr) {
  74. digitalWrite(CSN_PIN, LOW);
  75. SPI.transfer(addr);
  76. uint32_t data = 0;
  77. data |= SPI.transfer(0) << 24;
  78. data |= SPI.transfer(0) << 16;
  79. data |= SPI.transfer(0) << 8;
  80. data |= SPI.transfer(0);
  81. digitalWrite(CSN_PIN, HIGH);
  82. return data;
  83. }
  84.  
  85. // Function to move X axis (connected to TMC5272)
  86. void moveX(float mm) {
  87. long steps = mm / mmPerStep;
  88. uint32_t currentPos = readTMC5272(XACTUAL);
  89. uint32_t newPos = currentPos + steps;
  90. writeTMC5272(XTARGET, newPos);
  91.  
  92. // Wait for movement to complete
  93. while(readTMC5272(XACTUAL) != newPos) {
  94. delay(1);
  95. }
  96. }
  97.  
  98. // Function to move Y axis (connected to TMC5272)
  99. void moveY(float mm) {
  100. // Similar to moveX, but using different registers for Y axis
  101. // Implementation depends on how Y axis is configured in TMC5272
  102. }
  103.  
  104. // Z axis control using L298N
  105. void moveZ(int direction, int steps) {
  106. const int stepDelay = 2; // Adjust for desired speed
  107.  
  108. for(int i = 0; i < steps; i++) {
  109. switch(direction) {
  110. case 1: // Up
  111. digitalWrite(Z_IN1, HIGH);
  112. digitalWrite(Z_IN2, LOW);
  113. digitalWrite(Z_IN3, HIGH);
  114. digitalWrite(Z_IN4, LOW);
  115. delay(stepDelay);
  116. digitalWrite(Z_IN1, HIGH);
  117. digitalWrite(Z_IN2, LOW);
  118. digitalWrite(Z_IN3, LOW);
  119. digitalWrite(Z_IN4, HIGH);
  120. delay(stepDelay);
  121. digitalWrite(Z_IN1, LOW);
  122. digitalWrite(Z_IN2, HIGH);
  123. digitalWrite(Z_IN3, LOW);
  124. digitalWrite(Z_IN4, HIGH);
  125. delay(stepDelay);
  126. digitalWrite(Z_IN1, LOW);
  127. digitalWrite(Z_IN2, HIGH);
  128. digitalWrite(Z_IN3, HIGH);
  129. digitalWrite(Z_IN4, LOW);
  130. delay(stepDelay);
  131. break;
  132.  
  133. case 0: // Down
  134. digitalWrite(Z_IN1, LOW);
  135. digitalWrite(Z_IN2, HIGH);
  136. digitalWrite(Z_IN3, HIGH);
  137. digitalWrite(Z_IN4, LOW);
  138. delay(stepDelay);
  139. digitalWrite(Z_IN1, LOW);
  140. digitalWrite(Z_IN2, HIGH);
  141. digitalWrite(Z_IN3, LOW);
  142. digitalWrite(Z_IN4, HIGH);
  143. delay(stepDelay);
  144. digitalWrite(Z_IN1, HIGH);
  145. digitalWrite(Z_IN2, LOW);
  146. digitalWrite(Z_IN3, LOW);
  147. digitalWrite(Z_IN4, HIGH);
  148. delay(stepDelay);
  149. digitalWrite(Z_IN1, HIGH);
  150. digitalWrite(Z_IN2, LOW);
  151. digitalWrite(Z_IN3, HIGH);
  152. digitalWrite(Z_IN4, LOW);
  153. delay(stepDelay);
  154. break;
  155. }
  156. }
  157. // Turn off all coils
  158. digitalWrite(Z_IN1, LOW);
  159. digitalWrite(Z_IN2, LOW);
  160. digitalWrite(Z_IN3, LOW);
  161. digitalWrite(Z_IN4, LOW);
  162. }
  163.  
  164. void loop() {
  165. // Example movement sequence
  166. if(Serial.available()) {
  167. char cmd = Serial.read();
  168. switch(cmd) {
  169. case 'x':
  170. moveX(10); // Move X axis 10mm
  171. break;
  172. case 'y':
  173. moveY(10); // Move Y axis 10mm
  174. break;
  175. case 'u':
  176. moveZ(1, 200); // Move Z up 200 steps
  177. break;
  178. case 'd':
  179. moveZ(0, 200); // Move Z down 200 steps
  180. break;
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment