123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
-
-
-
-
- #ifndef STEPPER_H
- #define STEPPER_H
-
- #include "planner.h"
- #include "speed_lookuptable.h"
- #include "stepper_indirection.h"
- #include "language.h"
-
- class Stepper;
- extern Stepper stepper;
-
-
-
-
-
- #define MultiU16X8toH16(intRes, charIn1, intIn2) \
- asm volatile ( \
- "clr r26 \n\t" \
- "mul %A1, %B2 \n\t" \
- "movw %A0, r0 \n\t" \
- "mul %A1, %A2 \n\t" \
- "add %A0, r1 \n\t" \
- "adc %B0, r26 \n\t" \
- "lsr r0 \n\t" \
- "adc %A0, r26 \n\t" \
- "adc %B0, r26 \n\t" \
- "clr r1 \n\t" \
- : \
- "=&r" (intRes) \
- : \
- "d" (charIn1), \
- "d" (intIn2) \
- : \
- "r26" \
- )
-
- class Stepper {
-
- public:
-
- block_t* current_block = NULL;
-
- #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
- bool abort_on_endstop_hit = false;
- #endif
-
- #if ENABLED(Z_DUAL_ENDSTOPS)
- bool performing_homing = false;
- #endif
-
- #if ENABLED(ADVANCE)
- long e_steps[4];
- #endif
-
- private:
-
- unsigned char last_direction_bits = 0;
- unsigned int cleaning_buffer_counter = 0;
-
- #if ENABLED(Z_DUAL_ENDSTOPS)
- bool locked_z_motor = false,
- locked_z2_motor = false;
- #endif
-
-
- long counter_X = 0, counter_Y = 0, counter_Z = 0, counter_E = 0;
- volatile unsigned long step_events_completed = 0;
-
- #if ENABLED(ADVANCE)
- unsigned char old_OCR0A;
- long advance_rate, advance, final_advance = 0;
- long old_advance = 0;
- #endif
-
- long acceleration_time, deceleration_time;
-
- unsigned short acc_step_rate;
- uint8_t step_loops;
- uint8_t step_loops_nominal;
- unsigned short OCR1A_nominal;
-
- volatile long endstops_trigsteps[3];
- volatile long endstops_stepsTotal, endstops_stepsDone;
-
- #if HAS_MOTOR_CURRENT_PWM
- #ifndef PWM_MOTOR_CURRENT
- #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
- #endif
- const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
- #endif
-
-
-
-
- volatile long count_position[NUM_AXIS] = { 0 };
-
-
-
-
- volatile signed char count_direction[NUM_AXIS] = { 1 };
-
- public:
-
-
-
-
- Stepper() {};
-
-
-
-
- void init();
-
-
-
-
-
- void isr();
-
- #if ENABLED(ADVANCE)
- void advance_isr();
- #endif
-
-
-
-
- void synchronize();
-
-
-
-
- void set_position(const long& x, const long& y, const long& z, const long& e);
- void set_e_position(const long& e);
-
-
-
-
- void set_directions();
-
-
-
-
- long position(AxisEnum axis);
-
-
-
-
- void report_positions();
-
-
-
-
- float get_axis_position_mm(AxisEnum axis);
-
-
-
-
-
- void wake_up();
-
-
-
-
- void finish_and_disable();
-
-
-
-
- void quick_stop();
-
-
-
-
- FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
-
- #if HAS_DIGIPOTSS
- void digitalPotWrite(int address, int value);
- #endif
- void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
- void digipot_current(uint8_t driver, int current);
- void microstep_readings();
-
- #if ENABLED(Z_DUAL_ENDSTOPS)
- void set_homing_flag(bool state);
- void set_z_lock(bool state);
- void set_z2_lock(bool state);
- #endif
-
- #if ENABLED(BABYSTEPPING)
- void babystep(const uint8_t axis, const bool direction);
- #endif
-
- inline void kill_current_block() {
- step_events_completed = current_block->step_event_count;
- }
-
-
-
-
- void endstop_triggered(AxisEnum axis);
-
-
-
-
- FORCE_INLINE float triggered_position_mm(AxisEnum axis) {
- return endstops_trigsteps[axis] / planner.axis_steps_per_unit[axis];
- }
-
- FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
- unsigned short timer;
-
- NOMORE(step_rate, MAX_STEP_FREQUENCY);
-
- if (step_rate > 20000) {
- step_rate = (step_rate >> 2) & 0x3fff;
- step_loops = 4;
- }
- else if (step_rate > 10000) {
- step_rate = (step_rate >> 1) & 0x7fff;
- step_loops = 2;
- }
- else {
- step_loops = 1;
- }
-
- NOLESS(step_rate, F_CPU / 500000);
- step_rate -= F_CPU / 500000;
- if (step_rate >= (8 * 256)) {
- unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
- unsigned char tmp_step_rate = (step_rate & 0x00ff);
- unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
- MultiU16X8toH16(timer, tmp_step_rate, gain);
- timer = (unsigned short)pgm_read_word_near(table_address) - timer;
- }
- else {
- unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
- table_address += ((step_rate) >> 1) & 0xfffc;
- timer = (unsigned short)pgm_read_word_near(table_address);
- timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
- }
- if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }
- return timer;
- }
-
-
-
- FORCE_INLINE void trapezoid_generator_reset() {
-
- static int8_t last_extruder = -1;
-
- if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
- last_direction_bits = current_block->direction_bits;
- last_extruder = current_block->active_extruder;
- set_directions();
- }
-
- #if ENABLED(ADVANCE)
- advance = current_block->initial_advance;
- final_advance = current_block->final_advance;
-
- e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
- old_advance = advance >>8;
- #endif
- deceleration_time = 0;
-
- OCR1A_nominal = calc_timer(current_block->nominal_rate);
-
- step_loops_nominal = step_loops;
- acc_step_rate = current_block->initial_rate;
- acceleration_time = calc_timer(acc_step_rate);
- OCR1A = acceleration_time;
-
-
-
-
-
-
-
-
-
-
- }
-
- private:
- void microstep_mode(uint8_t driver, uint8_t stepping);
- void digipot_init();
- void microstep_init();
-
- };
-
- #endif
|