2018-11-16 03:32:01 -06:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
|
|
|
|
* Derived from Grbl
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2011 Simen Svale Skogsrud
|
|
|
|
*
|
|
|
|
* Grbl is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Grbl is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEPPER_H
|
|
|
|
#define STEPPER_H
|
|
|
|
|
|
|
|
#include "planner.h"
|
|
|
|
#include "speed_lookuptable.h"
|
|
|
|
#include "stepper_indirection.h"
|
|
|
|
#include "language.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
class Stepper;
|
|
|
|
extern Stepper stepper;
|
|
|
|
|
|
|
|
#define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
|
|
|
|
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
|
|
|
|
|
2018-11-16 03:32:01 -06:00
|
|
|
// intRes = intIn1 * intIn2 >> 16
|
|
|
|
// uses:
|
|
|
|
// r26 to store 0
|
|
|
|
// r27 to store the byte 1 of the 24 bit result
|
2019-01-31 05:25:28 -06:00
|
|
|
#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" \
|
|
|
|
)
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
class Stepper {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static block_t* current_block; // A pointer to the block currently being traced
|
|
|
|
|
|
|
|
#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
|
|
|
|
static bool abort_on_endstop_hit;
|
|
|
|
#endif
|
|
|
|
|
2018-11-16 03:32:01 -06:00
|
|
|
#if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
static bool performing_homing;
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_MOTOR_CURRENT_PWM
|
|
|
|
#ifndef PWM_MOTOR_CURRENT
|
|
|
|
#define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
|
|
|
|
#endif
|
|
|
|
static uint32_t motor_current_setting[3];
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static int16_t cleaning_buffer_counter;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
private:
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static uint8_t last_direction_bits; // The next stepping-bits to be output
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
#if ENABLED(X_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
static bool locked_x_motor, locked_x2_motor;
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
#if ENABLED(Y_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
static bool locked_y_motor, locked_y2_motor;
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
#if ENABLED(Z_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
static bool locked_z_motor, locked_z2_motor;
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
// Counter variables for the Bresenham line tracer
|
|
|
|
static long counter_X, counter_Y, counter_Z, counter_E;
|
|
|
|
static volatile uint32_t step_events_completed; // The number of step events executed in the current block
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#if ENABLED(LIN_ADVANCE)
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static uint16_t nextMainISR, nextAdvanceISR, eISR_Rate;
|
|
|
|
#define _NEXT_ISR(T) nextMainISR = T
|
|
|
|
static volatile int e_steps[E_STEPPERS];
|
|
|
|
static int final_estep_rate;
|
|
|
|
static int current_estep_rate[E_STEPPERS]; // Actual extruder speed [steps/s]
|
|
|
|
static int current_adv_steps[E_STEPPERS]; // The amount of current added esteps due to advance.
|
|
|
|
// i.e., the current amount of pressure applied
|
|
|
|
// to the spring (=filament).
|
|
|
|
#else // !LIN_ADVANCE
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#define _NEXT_ISR(T) OCR1A = T
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#endif // !LIN_ADVANCE
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static long acceleration_time, deceleration_time;
|
|
|
|
static uint8_t step_loops, step_loops_nominal;
|
|
|
|
|
|
|
|
static uint16_t OCR1A_nominal,
|
|
|
|
acc_step_rate; // needed for deceleration start point
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static volatile long endstops_trigsteps[XYZ];
|
|
|
|
static volatile long endstops_stepsTotal, endstops_stepsDone;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Positions of stepper motors, in step units
|
|
|
|
//
|
2019-01-31 05:25:28 -06:00
|
|
|
static volatile long count_position[NUM_AXIS];
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Current direction of stepper motors (+1 or -1)
|
|
|
|
//
|
2019-01-31 05:25:28 -06:00
|
|
|
static volatile signed char count_direction[NUM_AXIS];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Mixing extruder mix counters
|
|
|
|
//
|
|
|
|
#if ENABLED(MIXING_EXTRUDER)
|
|
|
|
static long counter_m[MIXING_STEPPERS];
|
|
|
|
#define MIXING_STEPPERS_LOOP(VAR) \
|
|
|
|
for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++) \
|
|
|
|
if (current_block->mix_event_count[VAR])
|
|
|
|
#endif
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
//
|
|
|
|
// Constructor / initializer
|
|
|
|
//
|
|
|
|
Stepper() { };
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// Initialize stepper hardware
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
static void init();
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// Interrupt Service Routines
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
static void isr();
|
|
|
|
|
|
|
|
#if ENABLED(LIN_ADVANCE)
|
2019-01-31 05:25:28 -06:00
|
|
|
static void advance_isr();
|
|
|
|
static void advance_isr_scheduler();
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Block until all buffered steps are executed
|
|
|
|
//
|
|
|
|
static void synchronize();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set the current position in steps
|
|
|
|
//
|
|
|
|
static void set_position(const long &a, const long &b, const long &c, const long &e);
|
|
|
|
static void set_position(const AxisEnum &a, const long &v);
|
|
|
|
static void set_e_position(const long &e);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Set direction bits for all steppers
|
|
|
|
//
|
|
|
|
static void set_directions();
|
|
|
|
|
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// Get the position of a stepper, in steps
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
static long position(const AxisEnum axis);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// Report the positions of the steppers, in steps
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
static void report_positions();
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Get the position (mm) of an axis based on stepper position(s)
|
|
|
|
//
|
|
|
|
static float get_axis_position_mm(const AxisEnum axis);
|
|
|
|
|
|
|
|
//
|
|
|
|
// SCARA AB axes are in degrees, not mm
|
|
|
|
//
|
|
|
|
#if IS_SCARA
|
|
|
|
FORCE_INLINE static float get_axis_position_degrees(const AxisEnum axis) { return get_axis_position_mm(axis); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
|
|
|
|
// to notify the subsystem that it is time to go to work.
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
static void wake_up();
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Wait for moves to finish and disable all steppers
|
|
|
|
//
|
|
|
|
static void finish_and_disable();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Quickly stop all steppers and clear the blocks queue
|
|
|
|
//
|
|
|
|
static void quick_stop();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
// The direction of a single motor
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
2018-11-16 03:32:01 -06:00
|
|
|
FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
|
|
|
|
|
|
|
|
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
|
|
|
|
static void digitalPotWrite(const int16_t address, const int16_t value);
|
|
|
|
static void digipot_current(const uint8_t driver, const int16_t current);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_MICROSTEPS
|
|
|
|
static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
|
|
|
|
static void microstep_mode(const uint8_t driver, const uint8_t stepping);
|
|
|
|
static void microstep_readings();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(X_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
FORCE_INLINE static void set_homing_flag_x(const bool state) { performing_homing = state; }
|
|
|
|
FORCE_INLINE static void set_x_lock(const bool state) { locked_x_motor = state; }
|
|
|
|
FORCE_INLINE static void set_x2_lock(const bool state) { locked_x2_motor = state; }
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
#if ENABLED(Y_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
FORCE_INLINE static void set_homing_flag_y(const bool state) { performing_homing = state; }
|
|
|
|
FORCE_INLINE static void set_y_lock(const bool state) { locked_y_motor = state; }
|
|
|
|
FORCE_INLINE static void set_y2_lock(const bool state) { locked_y2_motor = state; }
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
#if ENABLED(Z_DUAL_ENDSTOPS)
|
2019-01-31 05:25:28 -06:00
|
|
|
FORCE_INLINE static void set_homing_flag_z(const bool state) { performing_homing = state; }
|
|
|
|
FORCE_INLINE static void set_z_lock(const bool state) { locked_z_motor = state; }
|
|
|
|
FORCE_INLINE static void set_z2_lock(const bool state) { locked_z2_motor = state; }
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(BABYSTEPPING)
|
|
|
|
static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
static inline void kill_current_block() {
|
|
|
|
step_events_completed = current_block->step_event_count;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Handle a triggered endstop
|
|
|
|
//
|
|
|
|
static void endstop_triggered(const AxisEnum axis);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
//
|
|
|
|
// Triggered position of an axis in mm (not core-savvy)
|
|
|
|
//
|
|
|
|
FORCE_INLINE static float triggered_position_mm(const AxisEnum axis) {
|
|
|
|
return endstops_trigsteps[axis] * planner.steps_to_mm[axis];
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#if HAS_MOTOR_CURRENT_PWM
|
|
|
|
static void refresh_motor_power();
|
|
|
|
#endif
|
|
|
|
|
2018-11-16 03:32:01 -06:00
|
|
|
private:
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
FORCE_INLINE static unsigned short calc_timer_interval(unsigned short step_rate) {
|
|
|
|
unsigned short timer;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
NOMORE(step_rate, MAX_STEP_FREQUENCY);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
|
|
|
|
step_rate >>= 2;
|
|
|
|
step_loops = 4;
|
|
|
|
}
|
|
|
|
else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
|
|
|
|
step_rate >>= 1;
|
|
|
|
step_loops = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
step_loops = 1;
|
|
|
|
}
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
NOLESS(step_rate, F_CPU / 500000);
|
|
|
|
step_rate -= F_CPU / 500000; // Correct for minimal speed
|
2018-11-16 03:32:01 -06:00
|
|
|
if (step_rate >= (8 * 256)) { // higher step rate
|
2019-01-31 05:25:28 -06:00
|
|
|
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;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
else { // lower step rates
|
2019-01-31 05:25:28 -06:00
|
|
|
unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
|
2018-11-16 03:32:01 -06:00
|
|
|
table_address += ((step_rate) >> 1) & 0xFFFC;
|
2019-01-31 05:25:28 -06:00
|
|
|
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) { // (20kHz - this should never happen)
|
|
|
|
timer = 100;
|
|
|
|
MYSERIAL.print(MSG_STEPPER_TOO_HIGH);
|
|
|
|
MYSERIAL.println(step_rate);
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
// Initialize the trapezoid generator from the current block.
|
|
|
|
// Called whenever a new block begins.
|
|
|
|
FORCE_INLINE static 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
deceleration_time = 0;
|
|
|
|
// step_rate to timer interval
|
|
|
|
OCR1A_nominal = calc_timer_interval(current_block->nominal_rate);
|
|
|
|
// make a note of the number of step loops required at nominal speed
|
|
|
|
step_loops_nominal = step_loops;
|
|
|
|
acc_step_rate = current_block->initial_rate;
|
|
|
|
acceleration_time = calc_timer_interval(acc_step_rate);
|
|
|
|
_NEXT_ISR(acceleration_time);
|
|
|
|
|
|
|
|
#if ENABLED(LIN_ADVANCE)
|
|
|
|
if (current_block->use_advance_lead) {
|
|
|
|
current_estep_rate[current_block->active_extruder] = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
|
|
|
|
final_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// SERIAL_ECHO_START();
|
|
|
|
// SERIAL_ECHOPGM("advance :");
|
|
|
|
// SERIAL_ECHO(current_block->advance/256.0);
|
|
|
|
// SERIAL_ECHOPGM("advance rate :");
|
|
|
|
// SERIAL_ECHO(current_block->advance_rate/256.0);
|
|
|
|
// SERIAL_ECHOPGM("initial advance :");
|
|
|
|
// SERIAL_ECHO(current_block->initial_advance/256.0);
|
|
|
|
// SERIAL_ECHOPGM("final advance :");
|
|
|
|
// SERIAL_ECHOLN(current_block->final_advance/256.0);
|
|
|
|
}
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
|
|
|
|
static void digipot_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_MICROSTEPS
|
|
|
|
static void microstep_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // STEPPER_H
|