sm4.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //sm4.c - simple 4-axis stepper control
  2. #include "sm4.h"
  3. #include <avr/io.h>
  4. #include <avr/pgmspace.h>
  5. #include <math.h>
  6. #include "Arduino.h"
  7. #include "boards.h"
  8. #define false 0
  9. #define true 1
  10. #include "Configuration_prusa.h"
  11. #ifdef NEW_XYZCAL
  12. // Signal pinouts
  13. // direction signal - MiniRambo
  14. //#define X_DIR_PIN 48 //PL1 (-)
  15. //#define Y_DIR_PIN 49 //PL0 (-)
  16. //#define Z_DIR_PIN 47 //PL2 (-)
  17. //#define E0_DIR_PIN 43 //PL6 (+)
  18. //direction signal - EinsyRambo
  19. //#define X_DIR_PIN 49 //PL0 (+)
  20. //#define Y_DIR_PIN 48 //PL1 (-)
  21. //#define Z_DIR_PIN 47 //PL2 (+)
  22. //#define E0_DIR_PIN 43 //PL6 (-)
  23. //step signal pinout - common for all rambo boards
  24. //#define X_STEP_PIN 37 //PC0 (+)
  25. //#define Y_STEP_PIN 36 //PC1 (+)
  26. //#define Z_STEP_PIN 35 //PC2 (+)
  27. //#define E0_STEP_PIN 34 //PC3 (+)
  28. sm4_stop_cb_t sm4_stop_cb = 0;
  29. sm4_update_pos_cb_t sm4_update_pos_cb = 0;
  30. sm4_calc_delay_cb_t sm4_calc_delay_cb = 0;
  31. uint16_t sm4_cpu_time = 0;
  32. uint8_t sm4_get_dir(uint8_t axis)
  33. {
  34. switch (axis)
  35. {
  36. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  37. case 0: return (PORTL & 2)?0:1;
  38. case 1: return (PORTL & 1)?0:1;
  39. case 2: return (PORTL & 4)?0:1;
  40. case 3: return (PORTL & 64)?1:0;
  41. #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
  42. case 0: return (PORTL & 1)?1:0;
  43. case 1: return (PORTL & 2)?0:1;
  44. case 2: return (PORTL & 4)?1:0;
  45. case 3: return (PORTL & 64)?0:1;
  46. #endif
  47. }
  48. return 0;
  49. }
  50. void sm4_set_dir(uint8_t axis, uint8_t dir)
  51. {
  52. switch (axis)
  53. {
  54. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  55. case 0: if (!dir) PORTL |= 2; else PORTL &= ~2; break;
  56. case 1: if (!dir) PORTL |= 1; else PORTL &= ~1; break;
  57. case 2: if (!dir) PORTL |= 4; else PORTL &= ~4; break;
  58. case 3: if (dir) PORTL |= 64; else PORTL &= ~64; break;
  59. #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
  60. case 0: if (dir) PORTL |= 1; else PORTL &= ~1; break;
  61. case 1: if (!dir) PORTL |= 2; else PORTL &= ~2; break;
  62. case 2: if (dir) PORTL |= 4; else PORTL &= ~4; break;
  63. case 3: if (!dir) PORTL |= 64; else PORTL &= ~64; break;
  64. #endif
  65. }
  66. asm("nop");
  67. }
  68. uint8_t sm4_get_dir_bits(void)
  69. {
  70. register uint8_t dir_bits = 0;
  71. register uint8_t portL = PORTL;
  72. //TODO -optimize in asm
  73. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  74. if (portL & 2) dir_bits |= 1;
  75. if (portL & 1) dir_bits |= 2;
  76. if (portL & 4) dir_bits |= 4;
  77. if (portL & 64) dir_bits |= 8;
  78. dir_bits ^= 0x07; //invert XYZ, do not invert E
  79. #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
  80. if (portL & 1) dir_bits |= 1;
  81. if (portL & 2) dir_bits |= 2;
  82. if (portL & 4) dir_bits |= 4;
  83. if (portL & 64) dir_bits |= 8;
  84. dir_bits ^= 0x0a; //invert YE, do not invert XZ
  85. #endif
  86. return dir_bits;
  87. }
  88. void sm4_set_dir_bits(uint8_t dir_bits)
  89. {
  90. register uint8_t portL = PORTL;
  91. portL &= 0xb8; //set direction bits to zero
  92. //TODO -optimize in asm
  93. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  94. dir_bits ^= 0x07; //invert XYZ, do not invert E
  95. if (dir_bits & 1) portL |= 2; //set X direction bit
  96. if (dir_bits & 2) portL |= 1; //set Y direction bit
  97. if (dir_bits & 4) portL |= 4; //set Z direction bit
  98. if (dir_bits & 8) portL |= 64; //set E direction bit
  99. #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
  100. dir_bits ^= 0x0a; //invert YE, do not invert XZ
  101. if (dir_bits & 1) portL |= 1; //set X direction bit
  102. if (dir_bits & 2) portL |= 2; //set Y direction bit
  103. if (dir_bits & 4) portL |= 4; //set Z direction bit
  104. if (dir_bits & 8) portL |= 64; //set E direction bit
  105. #endif
  106. PORTL = portL;
  107. asm("nop");
  108. }
  109. void sm4_do_step(uint8_t axes_mask)
  110. {
  111. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3) || (MOTHERBOARD == BOARD_EINSY_1_0a))
  112. register uint8_t portC = PORTC & 0xf0;
  113. PORTC = portC | (axes_mask & 0x0f); //set step signals by mask
  114. asm("nop");
  115. PORTC = portC; //set step signals to zero
  116. asm("nop");
  117. #endif //((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3) || (MOTHERBOARD == BOARD_EINSY_1_0a))
  118. }
  119. uint16_t sm4_line_xyze_ui(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t de)
  120. {
  121. uint16_t dd = (uint16_t)(sqrt((float)(((uint32_t)dx)*dx + ((uint32_t)dy*dy) + ((uint32_t)dz*dz) + ((uint32_t)de*de))) + 0.5);
  122. uint16_t nd = dd;
  123. uint16_t cx = dd;
  124. uint16_t cy = dd;
  125. uint16_t cz = dd;
  126. uint16_t ce = dd;
  127. uint16_t x = 0;
  128. uint16_t y = 0;
  129. uint16_t z = 0;
  130. uint16_t e = 0;
  131. while (nd)
  132. {
  133. if (sm4_stop_cb && (*sm4_stop_cb)()) break;
  134. uint8_t sm = 0; //step mask
  135. if (cx <= dx)
  136. {
  137. sm |= 1;
  138. cx += dd;
  139. x++;
  140. }
  141. if (cy <= dy)
  142. {
  143. sm |= 2;
  144. cy += dd;
  145. y++;
  146. }
  147. if (cz <= dz)
  148. {
  149. sm |= 4;
  150. cz += dd;
  151. z++;
  152. }
  153. if (ce <= de)
  154. {
  155. sm |= 8;
  156. ce += dd;
  157. e++;
  158. }
  159. cx -= dx;
  160. cy -= dy;
  161. cz -= dz;
  162. ce -= de;
  163. sm4_do_step(sm);
  164. uint16_t delay = SM4_DEFDELAY;
  165. if (sm4_calc_delay_cb) delay = (*sm4_calc_delay_cb)(nd, dd);
  166. if (delay) delayMicroseconds(delay);
  167. nd--;
  168. }
  169. if (sm4_update_pos_cb) (*sm4_update_pos_cb)(x, y, z, e);
  170. return nd;
  171. }
  172. #endif //NEW_XYZCAL