From 35e9b4797fe6a3642e1af599cba1fd30b06bbfc2 Mon Sep 17 00:00:00 2001 From: bdring Date: Fri, 10 Apr 2020 13:58:46 -0500 Subject: [PATCH] Added some settings to test dymanic switcing of spindles --- Grbl_Esp32/Grbl_Esp32.ino | 3 +-- Grbl_Esp32/Machines/3axis_v4.h | 2 ++ Grbl_Esp32/Machines/add_esc_spindle.h | 2 +- Grbl_Esp32/defaults.h | 28 ++++++++++++++++----------- Grbl_Esp32/machine_common.h | 2 ++ Grbl_Esp32/report.cpp | 2 ++ Grbl_Esp32/settings.cpp | 18 +++++++++++------ Grbl_Esp32/settings.h | 2 ++ Grbl_Esp32/tools/Laser.cpp | 2 +- Grbl_Esp32/tools/PWMSpindle.cpp | 16 ++++++++++----- Grbl_Esp32/tools/RelaySpindle.cpp | 20 ++++++++++++++++++- Grbl_Esp32/tools/SpindleClass.cpp | 3 +-- Grbl_Esp32/tools/SpindleClass.h | 2 ++ 13 files changed, 73 insertions(+), 29 deletions(-) diff --git a/Grbl_Esp32/Grbl_Esp32.ino b/Grbl_Esp32/Grbl_Esp32.ino index 943c2c52..00d92839 100644 --- a/Grbl_Esp32/Grbl_Esp32.ino +++ b/Grbl_Esp32/Grbl_Esp32.ino @@ -117,8 +117,7 @@ void loop() { // Reset Grbl primary systems. serial_reset_read_buffer(CLIENT_ALL); // Clear serial read buffer gc_init(); // Set g-code parser to default state - // my_spindle.init(); - spindle_select(SPINDLE_TYPE_PWM); + spindle_select(settings.spindle_type); coolant_init(); limits_init(); probe_init(); diff --git a/Grbl_Esp32/Machines/3axis_v4.h b/Grbl_Esp32/Machines/3axis_v4.h index d8904f49..e16ecbfb 100644 --- a/Grbl_Esp32/Machines/3axis_v4.h +++ b/Grbl_Esp32/Machines/3axis_v4.h @@ -51,3 +51,5 @@ #define CONTROL_RESET_PIN GPIO_NUM_34 // labeled Reset, needs external pullup #define CONTROL_FEED_HOLD_PIN GPIO_NUM_36 // labeled Hold, needs external pullup #define CONTROL_CYCLE_START_PIN GPIO_NUM_39 // labeled Start, needs external pullup + +#define DEFAULT_SPINDLE_TYPE SPINDLE_TYPE_PWM diff --git a/Grbl_Esp32/Machines/add_esc_spindle.h b/Grbl_Esp32/Machines/add_esc_spindle.h index b1c078b4..64996392 100644 --- a/Grbl_Esp32/Machines/add_esc_spindle.h +++ b/Grbl_Esp32/Machines/add_esc_spindle.h @@ -35,7 +35,7 @@ #define SHOW_EXTENDED_SETTINGS -#define SPINDLE_PWM_BIT_PRECISION 16 // 16 bit recommended for ESC (don't change) +#define DEFAULT_SPINDLE_PWM_BIT_PRECISION 16 // 16 bit recommended for ESC (don't change) /* Important ESC Settings diff --git a/Grbl_Esp32/defaults.h b/Grbl_Esp32/defaults.h index 73c1c1c0..808f4669 100644 --- a/Grbl_Esp32/defaults.h +++ b/Grbl_Esp32/defaults.h @@ -114,7 +114,18 @@ #define DEFAULT_HOMING_PULLOFF 1.0 // $27 mm #endif - // ======== sPINDLE STUFF ==================== + // ======== SPINDLE STUFF ==================== + #ifndef DEFAULT_SPINDLE_RPM_MIN // $31 + #define DEFAULT_SPINDLE_RPM_MIN 0.0 // rpm + #endif + + #ifndef DEFAULT_LASER_MODE // $32 + #define DEFAULT_LASER_MODE 0 // false + #endif + + #ifndef DEFAULT_SPINDLE_RPM_MAX // $30 + #define DEFAULT_SPINDLE_RPM_MAX 1000.0 // rpm + #endif #ifndef DEFAULT_SPINDLE_FREQ #define DEFAULT_SPINDLE_FREQ 5000.0 // $33 Hz (extended set) @@ -132,20 +143,15 @@ #define DEFAULT_SPINDLE_MAX_VALUE 100.0 // $36 Percent (extended set) #endif - #ifndef DEFAULT_SPINDLE_RPM_MAX - #define DEFAULT_SPINDLE_RPM_MAX 1000.0 // rpm + #ifndef DEFAULT_SPINDLE_PWM_BIT_PRECISION // $37 + #define DEFAULT_SPINDLE_PWM_BIT_PRECISION 8 #endif - - #ifndef DEFAULT_SPINDLE_RPM_MIN - #define DEFAULT_SPINDLE_RPM_MIN 0.0 // rpm + #ifndef DEFAULT_SPINDLE_TYPE // $38 + #define DEFAULT_SPINDLE_TYPE SPINDLE_TYPE_PWM #endif - #ifndef DEFAULT_LASER_MODE - #define DEFAULT_LASER_MODE 0 // false - #endif - - // user settings + // ================ user settings ===================== #ifndef DEFAULT_USER_INT_80 #define DEFAULT_USER_INT_80 0 // $80 User integer setting #endif diff --git a/Grbl_Esp32/machine_common.h b/Grbl_Esp32/machine_common.h index 35d45cb7..4ac06ce4 100644 --- a/Grbl_Esp32/machine_common.h +++ b/Grbl_Esp32/machine_common.h @@ -1,11 +1,13 @@ #ifndef _machine_common_h #define _machine_common_h +/* #ifndef SPINDLE_PWM_BIT_PRECISION #define SPINDLE_PWM_BIT_PRECISION 8 #endif #define SPINDLE_PWM_MAX_VALUE ((1<init(); // update the spindle class break; - case 33: settings.spindle_pwm_freq = value; my_spindle->init(); break; // Re-initialize spindle pwm calibration - case 34: settings.spindle_pwm_off_value = value; my_spindle->init(); break; // Re-initialize spindle pwm calibration - case 35: settings.spindle_pwm_min_value = value; my_spindle->init(); break; // Re-initialize spindle pwm calibration - case 36: settings.spindle_pwm_max_value = value; my_spindle->init(); break; // Re-initialize spindle pwm calibration + case 33: settings.spindle_pwm_freq = value; spindle_select(settings.spindle_type); break; // Re-initialize spindle pwm calibration + case 34: settings.spindle_pwm_off_value = value; spindle_select(settings.spindle_type); break; // Re-initialize spindle pwm calibration + case 35: settings.spindle_pwm_min_value = value; spindle_select(settings.spindle_type); break; // Re-initialize spindle pwm calibration + case 36: settings.spindle_pwm_max_value = value; spindle_select(settings.spindle_type); break; // Re-initialize spindle pwm calibration + case 37: settings.spindle_pwm_precision_bits = MIN(8, value);spindle_select(settings.spindle_type); break; + case 38: settings.spindle_type = value; spindle_select(settings.spindle_type); break; case 80: case 81: case 82: diff --git a/Grbl_Esp32/settings.h b/Grbl_Esp32/settings.h index a625438a..54950682 100644 --- a/Grbl_Esp32/settings.h +++ b/Grbl_Esp32/settings.h @@ -107,6 +107,8 @@ typedef struct { float spindle_pwm_off_value; // $34 Percent (extended set) float spindle_pwm_min_value; // $35 Percent (extended set) float spindle_pwm_max_value; // $36 Percent (extended set) + uint8_t spindle_pwm_precision_bits; // $37 PWM pwm precision in bits (extended set) + uint8_t spindle_type; // $38 The spindle class to be used (extended set) float rpm_max; float rpm_min; diff --git a/Grbl_Esp32/tools/Laser.cpp b/Grbl_Esp32/tools/Laser.cpp index 68078f7d..7f5037d1 100644 --- a/Grbl_Esp32/tools/Laser.cpp +++ b/Grbl_Esp32/tools/Laser.cpp @@ -37,6 +37,6 @@ void Laser :: config_message() { "Laser spindle on GPIO:%d, Freq:%.2fHz, Res:%dbits Laser mode:$32=%d", _output_pin, _pwm_freq, - SPINDLE_PWM_BIT_PRECISION, + settings.spindle_pwm_precision_bits, isRateAdjusted()); // the current mode } \ No newline at end of file diff --git a/Grbl_Esp32/tools/PWMSpindle.cpp b/Grbl_Esp32/tools/PWMSpindle.cpp index 94168723..f3b640ce 100644 --- a/Grbl_Esp32/tools/PWMSpindle.cpp +++ b/Grbl_Esp32/tools/PWMSpindle.cpp @@ -32,13 +32,13 @@ void PWMSpindle::init() { } _pwm_freq = settings.spindle_pwm_freq; - _pwm_period = ((1 << SPINDLE_PWM_BIT_PRECISION) - 1); + _pwm_period = ((1 << settings.spindle_pwm_precision_bits) - 1); if (settings.spindle_pwm_min_value > settings.spindle_pwm_min_value) grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Warning: Spindle min pwm is greater than max. Check $35 and $36"); if ((F_TIMERS / _pwm_freq) < _pwm_period) - grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Warning spindle PWM precision (%d bits) too high for frequency (%.2f Hz)", SPINDLE_PWM_BIT_PRECISION, _pwm_freq); + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Warning spindle PWM precision (%d bits) too high for frequency (%.2f Hz)", settings.spindle_pwm_precision_bits, _pwm_freq); // pre-caculate some PWM count values _pwm_off_value = (_pwm_period * settings.spindle_pwm_off_value / 100.0); @@ -56,7 +56,7 @@ void PWMSpindle::init() { _pwm_gradient = (_pwm_max_value - _pwm_min_value) / (_max_rpm - _min_rpm); _spindle_pwm_chan_num = sys_get_next_PWM_chan_num(); - ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, SPINDLE_PWM_BIT_PRECISION); // setup the channel + ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, settings.spindle_pwm_precision_bits); // setup the channel ledcAttachPin(_output_pin, _spindle_pwm_chan_num); // attach the PWM to the pin if (_enable_pin != UNDEFINED_PIN) @@ -99,10 +99,13 @@ float PWMSpindle::set_rpm(float rpm) { if (_output_pin == UNDEFINED_PIN) return rpm; + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM:%5.2f Min:%5.2f Max:%5.2f", rpm, _min_rpm, _max_rpm); + uint32_t pwm_value; // apply overrides and limits rpm *= (0.010 * sys.spindle_speed_ovr); // Scale by spindle speed override value (percent) + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM 1:%5.2f", rpm); // Calculate PWM register value based on rpm max/min settings and programmed rpm. if ((_min_rpm >= _max_rpm) || (rpm >= _max_rpm)) { @@ -115,6 +118,7 @@ float PWMSpindle::set_rpm(float rpm) { pwm_value = _pwm_off_value; } else { // Set minimum PWM output rpm = _min_rpm; + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM 2:%5.2f", rpm); sys.spindle_speed = rpm; pwm_value = _pwm_min_value; grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM less than min RPM:%5.2f %d", rpm, pwm_value); @@ -123,6 +127,7 @@ float PWMSpindle::set_rpm(float rpm) { // Compute intermediate PWM value with linear spindle speed model. // NOTE: A nonlinear model could be installed here, if required, but keep it VERY light-weight. sys.spindle_speed = rpm; + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM 3:%5.2f", rpm); #ifdef ENABLE_PIECEWISE_LINEAR_SPINDLE pwm_value = piecewise_linear_fit(rpm); #else @@ -134,6 +139,7 @@ float PWMSpindle::set_rpm(float rpm) { set_enable_pin(rpm != 0); #endif + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle RPM Final:%5.2f", rpm); set_pwm(pwm_value); return rpm; @@ -179,7 +185,7 @@ void PWMSpindle::stop() { // prints the startup message of the spindle config void PWMSpindle :: config_message() { - grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "PWM spindle on GPIO %d, freq %.2fHz, Res %d bits", _output_pin, _pwm_freq, SPINDLE_PWM_BIT_PRECISION); + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "PWM spindle on GPIO %d, freq %.2fHz, Res %d bits", _output_pin, _pwm_freq, settings.spindle_pwm_precision_bits); } @@ -194,7 +200,7 @@ void PWMSpindle::set_pwm(uint32_t duty) { _current_pwm_duty = duty; #ifdef INVERT_SPINDLE_PWM - duty = (1 << SPINDLE_PWM_BIT_PRECISION) - duty; + duty = (1 << settings.spindle_pwm_precision_bits) - duty; #endif ledcWrite(_spindle_pwm_chan_num, duty); } diff --git a/Grbl_Esp32/tools/RelaySpindle.cpp b/Grbl_Esp32/tools/RelaySpindle.cpp index 74485358..1b7bfc3f 100644 --- a/Grbl_Esp32/tools/RelaySpindle.cpp +++ b/Grbl_Esp32/tools/RelaySpindle.cpp @@ -17,7 +17,7 @@ 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 . - + */ #include "grbl.h" #include "SpindleClass.h" @@ -47,9 +47,27 @@ void RelaySpindle :: config_message() { grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Relay spindle on GPIO %d", _output_pin); } +float RelaySpindle::set_rpm(float rpm) { + if (_output_pin == UNDEFINED_PIN) + return rpm; + + sys.spindle_speed = rpm; + + if (rpm == 0) { + sys.spindle_speed = 0.0; + set_pwm(0); + } else { + sys.spindle_speed = rpm; + set_pwm(1); + } + + return rpm; +} + void RelaySpindle::set_pwm(uint32_t duty) { #ifdef INVERT_SPINDLE_PWM duty = (duty == 0); // flip duty #endif + grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Relay output %d", duty > 0); digitalWrite(_output_pin, duty > 0); // anything greater } \ No newline at end of file diff --git a/Grbl_Esp32/tools/SpindleClass.cpp b/Grbl_Esp32/tools/SpindleClass.cpp index 0ea289d5..6436697a 100644 --- a/Grbl_Esp32/tools/SpindleClass.cpp +++ b/Grbl_Esp32/tools/SpindleClass.cpp @@ -26,8 +26,7 @@ TODO Consider breaking into one file per class. - Get rid of dependance on machine definition #defines - SPINDLE_PWM_BIT_PRECISION + Get rid of dependance on machine definition #defines SPINDLE_PWM_PIN SPINDLE_ENABLE_PIN SPINDLE_DIR_PIN diff --git a/Grbl_Esp32/tools/SpindleClass.h b/Grbl_Esp32/tools/SpindleClass.h index 5620cc19..4ff7a508 100644 --- a/Grbl_Esp32/tools/SpindleClass.h +++ b/Grbl_Esp32/tools/SpindleClass.h @@ -104,6 +104,8 @@ class RelaySpindle : public PWMSpindle { public: void init(); void config_message(); + float set_rpm(float rpm); + protected: void set_pwm(uint32_t duty); };