mirror of
https://github.com/bdring/Grbl_Esp32.git
synced 2025-08-19 04:41:44 +02:00
Fixed Spindle Bug
- Bug when using 16 bit PWM for the spindle. Stepper.cpp copied the PWM into 8 bit variables and corrupted the value.
This commit is contained in:
@@ -126,6 +126,8 @@
|
||||
// For 5000 that is 80,000,000 / 5000 = 16000
|
||||
// round down to nearest bit count for SPINDLE_PWM_MAX_VALUE = 13bits (8192)
|
||||
|
||||
|
||||
|
||||
#define SPINDLE_PWM_BASE_FREQ 5000 // Hz
|
||||
#define SPINDLE_PWM_BIT_PRECISION 8
|
||||
#define SPINDLE_PWM_OFF_VALUE 0
|
||||
@@ -133,10 +135,11 @@
|
||||
//#define INVERT_SPINDLE_PWM
|
||||
|
||||
#ifndef SPINDLE_PWM_MIN_VALUE
|
||||
#define SPINDLE_PWM_MIN_VALUE 1 // Must be greater than zero.
|
||||
#undef SPINDLE_PWM_MIN_VALUE
|
||||
#endif
|
||||
#define SPINDLE_PWM_MIN_VALUE 1 // Must be greater than zero.
|
||||
|
||||
#define SPINDLE_ENABLE_PIN GPIO_NUM_22
|
||||
#define SPINDLE_ENABLE_PIN GPIO_NUM_22
|
||||
|
||||
// see versions for X and Z
|
||||
#define Y_LIMIT_PIN GPIO_NUM_4
|
||||
@@ -209,75 +212,64 @@
|
||||
#endif
|
||||
|
||||
#ifdef CPU_MAP_ESP32_ESC_SPINDLE
|
||||
// This is the CPU Map for the ESP32 CNC Controller R2
|
||||
// This is an example of using a Brushless DC Hobby motor as
|
||||
// a spindle motor
|
||||
// See this wiki page for more info
|
||||
// https://github.com/bdring/Grbl_Esp32/wiki/BESC-Spindle-Feature
|
||||
|
||||
// It is OK to comment out any step and direction pins. This
|
||||
// won't affect operation except that there will be no output
|
||||
// form the pins. Grbl will virtually move the axis. This could
|
||||
// be handy if you are using a servo, etc. for another axis.
|
||||
#define CPU_MAP_NAME "CPU_MAP_ESP32_ESC_SPINDLE"
|
||||
#define CPU_MAP_NAME "CPU_MAP_ESP32_ESC_SPINDLE"
|
||||
|
||||
#define X_STEP_PIN GPIO_NUM_12
|
||||
#define X_DIRECTION_PIN GPIO_NUM_26
|
||||
#define X_RMT_CHANNEL 0
|
||||
#define USE_RMT_STEPS
|
||||
|
||||
#define X_STEP_PIN GPIO_NUM_12
|
||||
#define X_DIRECTION_PIN GPIO_NUM_14
|
||||
#define X_RMT_CHANNEL 0
|
||||
|
||||
#define Y_STEP_PIN GPIO_NUM_26
|
||||
#define Y_DIRECTION_PIN GPIO_NUM_15// #define Y_STEP_PIN (see versions above)
|
||||
#define Y_RMT_CHANNEL 1
|
||||
|
||||
#define Z_STEP_PIN GPIO_NUM_27
|
||||
#define Z_DIRECTION_PIN GPIO_NUM_33
|
||||
#define Z_RMT_CHANNEL 2
|
||||
|
||||
// OK to comment out to use pin for other features
|
||||
#define STEPPERS_DISABLE_PIN GPIO_NUM_13
|
||||
|
||||
#define Y_STEP_PIN GPIO_NUM_14
|
||||
#define Y_DIRECTION_PIN GPIO_NUM_25
|
||||
#define Y_RMT_CHANNEL 1
|
||||
|
||||
#define Z_STEP_PIN GPIO_NUM_27
|
||||
#define Z_DIRECTION_PIN GPIO_NUM_33
|
||||
#define Z_RMT_CHANNEL 2
|
||||
|
||||
// OK to comment out to use pin for other features
|
||||
#define STEPPERS_DISABLE_PIN GPIO_NUM_13
|
||||
|
||||
// *** the flood coolant feature code is activated by defining this pins
|
||||
// *** Comment it out to use the pin for other features
|
||||
#define COOLANT_FLOOD_PIN GPIO_NUM_16
|
||||
//#define COOLANT_MIST_PIN GPIO_NUM_21
|
||||
|
||||
// If SPINDLE_PWM_PIN is commented out, this frees up the pin, but Grbl will still
|
||||
// use a virtual spindle. Do not comment out the other parameters for the spindle.
|
||||
#define SPINDLE_PWM_PIN GPIO_NUM_17
|
||||
#define SPINDLE_PWM_CHANNEL 0
|
||||
|
||||
// RC ESC Based Spindle
|
||||
// An ESC works like a hobby servo with 50Hz PWM 1ms to 2 ms pulse range
|
||||
#define SPINDLE_PWM_BASE_FREQ 50 // Hz for ESC
|
||||
#define SPINDLE_PWM_BIT_PRECISION 16 // 16 bit required for ESC
|
||||
#define SPINDLE_PULSE_RES_COUNT 65535
|
||||
|
||||
#define ESC_MIN_PULSE_SEC 0.001 // min pulse in seconds (OK to tune this one)
|
||||
#define ESC_MAX_PULSE_SEC 0.002 // max pulse in seconds (OK to tune this one)
|
||||
#define ESC_TIME_PER_BIT ((1.0 / (float)SPINDLE_PWM_BASE_FREQ) / ((float)SPINDLE_PULSE_RES_COUNT) ) // seconds
|
||||
#define SPINDLE_PWM_PIN GPIO_NUM_2
|
||||
#define SPINDLE_PWM_CHANNEL 0
|
||||
|
||||
// Begin RC ESC Based Spindle #defines ======================
|
||||
#define SPINDLE_PWM_BASE_FREQ 50 // Hz for ESC
|
||||
#define SPINDLE_PWM_BIT_PRECISION 16 // 16 bit required for ESC
|
||||
#define SPINDLE_PULSE_RES_COUNT 65535
|
||||
|
||||
#define ESC_MIN_PULSE_SEC 0.0007 // min pulse in seconds (OK to tune this one)
|
||||
#define ESC_MAX_PULSE_SEC 0.0022 // max pulse in seconds (OK to tune this one)
|
||||
#define ESC_TIME_PER_BIT ((1.0 / (float)SPINDLE_PWM_BASE_FREQ) / ((float)SPINDLE_PULSE_RES_COUNT) ) // seconds
|
||||
|
||||
#define SPINDLE_PWM_OFF_VALUE (uint16_t)(ESC_MIN_PULSE_SEC / ESC_TIME_PER_BIT) // in timer counts
|
||||
#define SPINDLE_PWM_MAX_VALUE (uint16_t)(ESC_MAX_PULSE_SEC / ESC_TIME_PER_BIT) // in timer counts
|
||||
|
||||
#ifndef SPINDLE_PWM_MIN_VALUE
|
||||
#define SPINDLE_PWM_MIN_VALUE SPINDLE_PWM_OFF_VALUE // Must be greater than zero.
|
||||
#endif
|
||||
|
||||
#define SPINDLE_ENABLE_PIN GPIO_NUM_22
|
||||
|
||||
// if these spindle function pins are defined, they will be activated in the code
|
||||
// comment them out to use the pins for other functions
|
||||
//#define SPINDLE_ENABLE_PIN GPIO_NUM_16
|
||||
//#define SPINDLE_DIR_PIN GPIO_NUM_16
|
||||
|
||||
#define X_LIMIT_PIN GPIO_NUM_2
|
||||
#define Y_LIMIT_PIN GPIO_NUM_4
|
||||
#define Z_LIMIT_PIN GPIO_NUM_15
|
||||
#define LIMIT_MASK B111
|
||||
|
||||
#define PROBE_PIN GPIO_NUM_32
|
||||
|
||||
#define CONTROL_SAFETY_DOOR_PIN GPIO_NUM_35 // needs external pullup
|
||||
#define CONTROL_RESET_PIN GPIO_NUM_34 // needs external pullup
|
||||
#define CONTROL_FEED_HOLD_PIN GPIO_NUM_36 // needs external pullup
|
||||
#define CONTROL_CYCLE_START_PIN GPIO_NUM_39 // needs external pullup
|
||||
|
||||
#define SPINDLE_PWM_OFF_VALUE (uint16_t)(ESC_MIN_PULSE_SEC / ESC_TIME_PER_BIT) // in timer counts
|
||||
#define SPINDLE_PWM_MAX_VALUE (uint16_t)(ESC_MAX_PULSE_SEC / ESC_TIME_PER_BIT) // in timer counts
|
||||
|
||||
#ifndef SPINDLE_PWM_MIN_VALUE
|
||||
#undef SPINDLE_PWM_MIN_VALUE
|
||||
#endif
|
||||
#define SPINDLE_PWM_MIN_VALUE SPINDLE_PWM_OFF_VALUE // Must be greater than zero.
|
||||
|
||||
#define SPINDLE_ENABLE_PIN GPIO_NUM_22
|
||||
// End RC ESC Based Spindle #defines ===========================
|
||||
|
||||
#define X_LIMIT_PIN GPIO_NUM_17
|
||||
#define Y_LIMIT_PIN GPIO_NUM_4
|
||||
#define Z_LIMIT_PIN GPIO_NUM_16
|
||||
#define LIMIT_MASK B111
|
||||
|
||||
#define PROBE_PIN GPIO_NUM_32
|
||||
|
||||
#define CONTROL_SAFETY_DOOR_PIN GPIO_NUM_35 // needs external pullup
|
||||
#define CONTROL_RESET_PIN GPIO_NUM_34 // needs external pullup
|
||||
#define CONTROL_FEED_HOLD_PIN GPIO_NUM_36 // needs external pullup
|
||||
#define CONTROL_CYCLE_START_PIN GPIO_NUM_39 // needs external pullup
|
||||
#endif
|
||||
|
||||
#ifdef CPU_MAP_PEN_LASER // The Buildlog.net pen laser controller V1 & V2
|
||||
|
@@ -57,7 +57,7 @@ typedef struct {
|
||||
uint8_t prescaler; // Without AMASS, a prescaler is required to adjust for slow timing.
|
||||
#endif
|
||||
#ifdef VARIABLE_SPINDLE
|
||||
uint8_t spindle_pwm;
|
||||
uint16_t spindle_pwm;
|
||||
#endif
|
||||
} segment_t;
|
||||
static segment_t segment_buffer[SEGMENT_BUFFER_SIZE];
|
||||
@@ -147,7 +147,7 @@ typedef struct {
|
||||
|
||||
#ifdef VARIABLE_SPINDLE
|
||||
float inv_rate; // Used by PWM laser mode to speed up segment calculations.
|
||||
uint8_t current_spindle_pwm;
|
||||
uint16_t current_spindle_pwm;
|
||||
#endif
|
||||
} st_prep_t;
|
||||
static st_prep_t prep;
|
||||
@@ -293,8 +293,8 @@ void IRAM_ATTR onStepperDriverTimer(void *para) // ISR It is time to take a ste
|
||||
#if ( (defined VARIABLE_SPINDLE) && (defined SPINDLE_PWM_PIN) )
|
||||
if (!(sys.state & STATE_JOG)) { // added to prevent ... jog after probing crash
|
||||
// Ensure pwm is set properly upon completion of rate-controlled motion.
|
||||
if (st.exec_block->is_pwm_rate_adjusted) {
|
||||
spindle_set_speed(SPINDLE_PWM_OFF_VALUE);
|
||||
if (st.exec_block->is_pwm_rate_adjusted) {
|
||||
spindle_set_speed(SPINDLE_PWM_OFF_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1090,7 +1090,7 @@ void st_prep_buffer()
|
||||
// Setup laser mode variables. PWM rate adjusted motions will always complete a motion with the
|
||||
// spindle off.
|
||||
st_prep_block->is_pwm_rate_adjusted = false;
|
||||
if (settings.flags & BITFLAG_LASER_MODE) {
|
||||
if (settings.flags & BITFLAG_LASER_MODE) {
|
||||
if (pl_block->condition & PL_COND_FLAG_SPINDLE_CCW) {
|
||||
// Pre-compute inverse programmed rate to speed up PWM updating per step segment.
|
||||
prep.inv_rate = 1.0/pl_block->programmed_rate;
|
||||
|
Reference in New Issue
Block a user