Guest User

drviers.cpp

a guest
Oct 15th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include "Repetier.h"
  2.  
  3. #if defined(NUM_MOTOR_DRIVERS) && NUM_MOTOR_DRIVERS > 0
  4. MOTOR_DRIVER_1(motorDriver1);
  5. #if NUM_MOTOR_DRIVERS > 1
  6. MOTOR_DRIVER_2(motorDriver2);
  7. #endif
  8. #if NUM_MOTOR_DRIVERS > 2
  9. MOTOR_DRIVER_3(motorDriver3);
  10. #endif
  11. #if NUM_MOTOR_DRIVERS > 3
  12. MOTOR_DRIVER_4(motorDriver4);
  13. #endif
  14. #if NUM_MOTOR_DRIVERS > 4
  15. MOTOR_DRIVER_5(motorDriver5);
  16. #endif
  17. #if NUM_MOTOR_DRIVERS > 5
  18. MOTOR_DRIVER_6(motorDriver6);
  19. #endif
  20.  
  21. MotorDriverInterface *motorDrivers[NUM_MOTOR_DRIVERS] =
  22. {
  23. &motorDriver1
  24. #if NUM_MOTOR_DRIVERS > 1
  25. , &motorDriver2
  26. #endif
  27. #if NUM_MOTOR_DRIVERS > 2
  28. , &motorDriver3
  29. #endif
  30. #if NUM_MOTOR_DRIVERS > 3
  31. , &motorDriver4
  32. #endif
  33. #if NUM_MOTOR_DRIVERS > 4
  34. , &motorDriver5
  35. #endif
  36. #if NUM_MOTOR_DRIVERS > 5
  37. , &motorDriver6
  38. #endif
  39. };
  40.  
  41. MotorDriverInterface *getMotorDriver(int idx)
  42. {
  43. return motorDrivers[idx];
  44. }
  45.  
  46. /**
  47. Run motor P until it is at position X
  48. */
  49. void commandG201(GCode &code)
  50. {
  51. int id = 0;
  52. if(code.hasP())
  53. id = code.P;
  54. if(id < 0) id = 0;
  55. if(id >= NUM_MOTOR_DRIVERS) id = 0;
  56. if(!code.hasX()) return;
  57. motorDrivers[id]->gotoPosition(code.X);
  58. }
  59.  
  60. //G202 P<motorId> X<setpos> - Mark current position as X
  61. void commandG202(GCode &code)
  62. {
  63. int id = 0;
  64. if(code.hasP())
  65. id = code.P;
  66. if(id < 0) id = 0;
  67. if(id >= NUM_MOTOR_DRIVERS) id = 0;
  68. if(!code.hasX()) return;
  69. motorDrivers[id]->setCurrentAs(code.X);
  70. }
  71. //G203 P<motorId> - Report current motor position
  72. void commandG203(GCode &code)
  73. {
  74. int id = 0;
  75. if(code.hasP())
  76. id = code.P;
  77. if(id < 0) id = 0;
  78. if(id >= NUM_MOTOR_DRIVERS) id = 0;
  79. Com::printF(PSTR("Motor"),id);
  80. Com::printFLN(PSTR("Pos:"),motorDrivers[id]->getPosition());
  81. }
  82. //G204 P<motorId> S<0/1> - Enable/disable motor
  83. void commandG204(GCode &code)
  84. {
  85. int id = 0;
  86. if(code.hasP())
  87. id = code.P;
  88. if(id < 0) id = 0;
  89. if(id >= NUM_MOTOR_DRIVERS) id = 0;
  90. if(!code.hasS()) return;
  91. if(code.S)
  92. motorDrivers[id]->enable();
  93. else
  94. motorDrivers[id]->disable();
  95. }
  96.  
  97. void disableAllMotorDrivers()
  98. {
  99. for(int i = 0; i < NUM_MOTOR_DRIVERS; i++)
  100. motorDrivers[i]->disable();
  101. }
  102. void initializeAllMotorDrivers()
  103. {
  104. for(int i = 0; i < NUM_MOTOR_DRIVERS; i++)
  105. motorDrivers[i]->initialize();
  106. }
  107.  
  108. #endif // NUM_MOTOR_DRIVERS
  109.  
  110. #if defined(SUPPORT_LASER) && SUPPORT_LASER
  111. uint8_t LaserDriver::intensity = 255; // Intensity to use for next move queued if we want lasers. This is NOT the current value!
  112. bool LaserDriver::laserOn = false;
  113. void LaserDriver::initialize()
  114. {
  115. if(EVENT_INITALIZE_LASER)
  116. {
  117. #if LASER_PIN > -1
  118. SET_OUTPUT(LASER_PIN);
  119. #endif
  120. }
  121. changeIntensity(0);
  122. }
  123. void LaserDriver::changeIntensity(uint8_t newIntensity)
  124. {
  125. if(EVENT_SET_LASER(newIntensity))
  126. {
  127. // Default implementation
  128. #if LASER_PIN > -1
  129. WRITE(LASER_PIN,(LASER_ON_HIGH ? newIntensity > 199 : newIntensity < 200));
  130. #endif
  131. }
  132. }
  133. #endif // SUPPORT_LASER
  134.  
  135. #if defined(SUPPORT_CNC) && SUPPORT_CNC
  136. /**
  137. The CNC driver differs a bit from laser driver. Here only M3,M4,M5 have an influence on the spindle.
  138. The motor also keeps running for G0 moves. M3 and M4 wait for old moves to be finished and then enables
  139. the motor. It then waits CNC_WAIT_ON_ENABLE milliseconds for the spindle to reach target speed.
  140. */
  141.  
  142. int8_t CNCDriver::direction = 0;
  143. /** Initialize cnc pins. EVENT_INITALIZE_CNC should return false to prevent default initalization.*/
  144. void CNCDriver::initialize()
  145. {
  146. if(EVENT_INITALIZE_CNC)
  147. {
  148. #if CNC_ENABLE_PIN > -1
  149. SET_OUTPUT(CNC_ENABLE_PIN);
  150. WRITE(CNC_ENABLE_PIN,!CNC_ENABLE_WITH);
  151. #endif
  152. #if CNC_DIRECTION_PIN > -1
  153. SET_OUTPUT(CNC_DIRECTION_PIN);
  154. #endif
  155. }
  156. }
  157. /** Turns off spindle. For event override implement
  158. EVENT_SPINDLE_OFF
  159. returning false.
  160. */
  161. void CNCDriver::spindleOff()
  162. {
  163. if(direction == 0) return; // already off
  164. if(EVENT_SPINDLE_OFF)
  165. {
  166. #if CNC_ENABLE_PIN > -1
  167. WRITE(CNC_ENABLE_PIN,!CNC_ENABLE_WITH);
  168. #endif
  169. }
  170. HAL::delayMilliseconds(CNC_WAIT_ON_DISABLE);
  171. direction = 0;
  172. }
  173. /** Turns spindle on. Default implementation uses a enable pin CNC_ENABLE_PIN. If
  174. CNC_DIRECTION_PIN is not -1 it sets direction to CNC_DIRECTION_CW. rpm is ignored.
  175. To override with event system, return false for the event
  176. EVENT_SPINDLE_CW(rpm)
  177. */
  178. void CNCDriver::spindleOnCW(int32_t rpm)
  179. {
  180. if(direction == 1)
  181. return;
  182. spindleOff();
  183. direction = 1;
  184. if(EVENT_SPINDLE_CW(rpm)) {
  185. #if CNC_DIRECTION_PIN > -1
  186. WRITE(CNC_DIRECTION_PIN, CNC_DIRECTION_CW);
  187. #endif
  188. #if CNC_ENABLE_PIN > -1
  189. WRITE(CNC_ENABLE_PIN, CNC_ENABLE_WITH);
  190. #endif
  191. }
  192. HAL::delayMilliseconds(CNC_WAIT_ON_ENABLE);
  193. }
  194. /** Turns spindle on. Default implementation uses a enable pin CNC_ENABLE_PIN. If
  195. CNC_DIRECTION_PIN is not -1 it sets direction to !CNC_DIRECTION_CW. rpm is ignored.
  196. To override with event system, return false for the event
  197. EVENT_SPINDLE_CCW(rpm)
  198. */
  199. void CNCDriver::spindleOnCCW(int32_t rpm)
  200. {
  201. if(direction == -1)
  202. return;
  203. spindleOff();
  204. direction = -1;
  205. if(EVENT_SPINDLE_CW(rpm)) {
  206. #if CNC_DIRECTION_PIN > -1
  207. WRITE(CNC_DIRECTION_PIN, !CNC_DIRECTION_CW);
  208. #endif
  209. #if CNC_ENABLE_PIN > -1
  210. WRITE(CNC_ENABLE_PIN, CNC_ENABLE_WITH);
  211. #endif
  212. }
  213. HAL::delayMilliseconds(CNC_WAIT_ON_ENABLE);
  214. }
  215. #endif
Add Comment
Please, Sign In to add comment