stepper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
  3. Part of Grbl
  4. Copyright (c) 2009-2011 Simen Svale Skogsrud
  5. Grbl is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Grbl is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef stepper_h
  17. #define stepper_h
  18. #include "planner.h"
  19. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  20. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  21. #if EXTRUDERS > 2
  22. #define WRITE_E_STEP(v) { if(current_block->active_extruder == 2) { WRITE(E2_STEP_PIN, v); } else { if(current_block->active_extruder == 1) { WRITE(E1_STEP_PIN, v); } else { WRITE(E0_STEP_PIN, v); }}}
  23. #define NORM_E_DIR() { if(current_block->active_extruder == 2) { WRITE(E2_DIR_PIN, !INVERT_E2_DIR); } else { if(current_block->active_extruder == 1) { WRITE(E1_DIR_PIN, !INVERT_E1_DIR); } else { WRITE(E0_DIR_PIN, !INVERT_E0_DIR); }}}
  24. #define REV_E_DIR() { if(current_block->active_extruder == 2) { WRITE(E2_DIR_PIN, INVERT_E2_DIR); } else { if(current_block->active_extruder == 1) { WRITE(E1_DIR_PIN, INVERT_E1_DIR); } else { WRITE(E0_DIR_PIN, INVERT_E0_DIR); }}}
  25. #elif EXTRUDERS > 1
  26. #define WRITE_E_STEP(v) { if(current_block->active_extruder == 1) { WRITE(E1_STEP_PIN, v); } else { WRITE(E0_STEP_PIN, v); }}
  27. #define NORM_E_DIR() { if(current_block->active_extruder == 1) { WRITE(E1_DIR_PIN, !INVERT_E1_DIR); } else { WRITE(E0_DIR_PIN, !INVERT_E0_DIR); }}
  28. #define REV_E_DIR() { if(current_block->active_extruder == 1) { WRITE(E1_DIR_PIN, INVERT_E1_DIR); } else { WRITE(E0_DIR_PIN, INVERT_E0_DIR); }}
  29. #else
  30. #define WRITE_E_STEP(v) WRITE(E0_STEP_PIN, v)
  31. #define NORM_E_DIR() WRITE(E0_DIR_PIN, !INVERT_E0_DIR)
  32. #define REV_E_DIR() WRITE(E0_DIR_PIN, INVERT_E0_DIR)
  33. #endif
  34. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  35. extern bool abort_on_endstop_hit;
  36. #endif
  37. // Initialize and start the stepper motor subsystem
  38. void st_init();
  39. // Interrupt Service Routines
  40. void isr();
  41. #ifdef LIN_ADVANCE
  42. void advance_isr();
  43. void advance_isr_scheduler();
  44. void clear_current_adv_vars(); //Used to reset the built up pretension and remaining esteps on filament change.
  45. #endif
  46. // Block until all buffered steps are executed
  47. void st_synchronize();
  48. // Set current position in steps
  49. void st_set_position(const long &x, const long &y, const long &z, const long &e);
  50. void st_set_e_position(const long &e);
  51. // Get current position in steps
  52. long st_get_position(uint8_t axis);
  53. // Get current x and y position in steps
  54. void st_get_position_xy(long &x, long &y);
  55. // Get current position in mm
  56. float st_get_position_mm(uint8_t axis);
  57. // Call this function just before re-enabling the stepper driver interrupt and the global interrupts
  58. // to avoid a stepper timer overflow.
  59. FORCE_INLINE void st_reset_timer()
  60. {
  61. // Clear a possible pending interrupt on OCR1A overflow.
  62. TIFR1 |= 1 << OCF1A;
  63. // Reset the counter.
  64. TCNT1 = 0;
  65. // Wake up after 1ms from now.
  66. OCR1A = 2000;
  67. }
  68. void checkHitEndstops(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered
  69. bool endstops_hit_on_purpose(); //avoid creation of the message, i.e. after homing and before a routine call of checkHitEndstops();
  70. bool endstop_z_hit_on_purpose();
  71. bool enable_endstops(bool check); // Enable/disable endstop checking. Return the old value.
  72. bool enable_z_endstop(bool check);
  73. void checkStepperErrors(); //Print errors detected by the stepper
  74. void finishAndDisableSteppers();
  75. extern block_t *current_block; // A pointer to the block currently being traced
  76. extern bool x_min_endstop;
  77. extern bool x_max_endstop;
  78. extern bool y_min_endstop;
  79. extern bool y_max_endstop;
  80. void quickStop();
  81. void digitalPotWrite(int address, int value);
  82. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
  83. void microstep_mode(uint8_t driver, uint8_t stepping);
  84. void digipot_init();
  85. void digipot_current(uint8_t driver, int current);
  86. void microstep_init();
  87. void microstep_readings();
  88. #ifdef BABYSTEPPING
  89. void babystep(const uint8_t axis,const bool direction); // perform a short step with a single stepper motor, outside of any convention
  90. #endif
  91. #endif