From c76cc25853be69c9c33c3953ecf82850ad926f14 Mon Sep 17 00:00:00 2001 From: bdring Date: Sun, 19 Apr 2020 13:19:35 -0500 Subject: [PATCH] Updates - BESCSpindle changes - Spindle PWM channel is fixed at 0 now. - New channels start at 2 --- Grbl_Esp32/Machines/spindle_test.h | 45 ++++++++++++++++++ Grbl_Esp32/machine.h | 2 +- Grbl_Esp32/system.cpp | 11 ++++- Grbl_Esp32/tools/BESCSpindle.cpp | 76 ++++++++++++++++++++---------- Grbl_Esp32/tools/PWMSpindle.cpp | 11 ++++- Grbl_Esp32/tools/RelaySpindle.cpp | 2 +- 6 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 Grbl_Esp32/Machines/spindle_test.h diff --git a/Grbl_Esp32/Machines/spindle_test.h b/Grbl_Esp32/Machines/spindle_test.h new file mode 100644 index 00000000..7c528232 --- /dev/null +++ b/Grbl_Esp32/Machines/spindle_test.h @@ -0,0 +1,45 @@ +/* + test_drive.h + Part of Grbl_ESP32 + + Pin assignments (or lack thereof) for testing Grbl_ESP32. + + It creates a basic 3 axis machine without actually driving + I/O pins. Grbl will report that axes are moving, but no physical + motor motion will occur. + + This can be uploaded to an unattached ESP32 or attached to + unknown hardware with no risk of pins trying to output signals + into a short, etc that could dmamge the ESP32 + + It can also be used to get the basic program running so OTA + (over the air) firmware loading can be done. + + 2018 - Bart Dring + 2020 - Mitch Bradley + + Grbl_ESP32 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_ESP32. If not, see . +*/ + +#define MACHINE_NAME "Spindle Class Testing" + +#define SPINDLE_TYPE SPINDLE_TYPE_BESC +#define SPINDLE_PWM_PIN GPIO_NUM_27 + + +#define LIMIT_MASK 0 // no limit pins + +#ifdef USE_RMT_STEPS +#undef USE_RMT_STEPS // Suppress unused variable warning +#endif diff --git a/Grbl_Esp32/machine.h b/Grbl_Esp32/machine.h index 1d49672e..28a57e3b 100644 --- a/Grbl_Esp32/machine.h +++ b/Grbl_Esp32/machine.h @@ -8,7 +8,7 @@ // !!! For initial testing, start with test_drive.h which disables // all I/O pins -#include "Machines/3axis_v4.h" +#include "Machines/spindle_test.h" // !!! For actual use, change the line above to select a board // from Machines/, for example: diff --git a/Grbl_Esp32/system.cpp b/Grbl_Esp32/system.cpp index 635e6bee..cbfaaf2b 100644 --- a/Grbl_Esp32/system.cpp +++ b/Grbl_Esp32/system.cpp @@ -588,9 +588,16 @@ int8_t sys_get_next_RMT_chan_num() { return -1; } } - +/* + This returns an unused pwm channel. + The 8 channels share 4 timers, so pairs 0,1 & 2,3 , etc + have to be the same frequency. The spindle always uses channel 0 + so we start counting from 2. + There are still possible issues if requested channels use different frequencies + TODO: Make this more robust. +*/ int8_t sys_get_next_PWM_chan_num() { - static uint8_t next_PWM_chan_num = 0; // channels 0-7 are valid + static uint8_t next_PWM_chan_num = 2; // start at 2 to avoid spindle if (next_PWM_chan_num < 8) // 7 is the max PWM channel number return next_PWM_chan_num++; else { diff --git a/Grbl_Esp32/tools/BESCSpindle.cpp b/Grbl_Esp32/tools/BESCSpindle.cpp index b85eacf9..c0531695 100644 --- a/Grbl_Esp32/tools/BESCSpindle.cpp +++ b/Grbl_Esp32/tools/BESCSpindle.cpp @@ -1,31 +1,56 @@ /* - Important ESC Settings - $33=50 // Hz this is the typical good frequency for an ESC - #define DEFAULT_SPINDLE_FREQ 5000.0 // $33 Hz (extended set) + BESCSpindle.cpp - Determine the typical min and max pulse length of your ESC - min_pulse is typically 1ms (0.001 sec) or less - max_pulse is typically 2ms (0.002 sec) or more + This a special type of PWM spindle for RC type Brushless DC Speed + controllers. - determine PWM_period. It is (1/freq) if freq = 50...period = 0.02 + Part of Grbl_ESP32 + 2020 - Bart Dring - determine pulse length for min_pulse and max_pulse in percent. + 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 . + + + + Important ESC Settings + $33=50 // Hz this is the typical good frequency for an ESC + #define DEFAULT_SPINDLE_FREQ 5000.0 // $33 Hz (extended set) + + Determine the typical min and max pulse length of your ESC + min_pulse is typically 1ms (0.001 sec) or less + max_pulse is typically 2ms (0.002 sec) or more + + determine PWM_period. It is (1/freq) if freq = 50...period = 0.02 + + determine pulse length for min_pulse and max_pulse in percent. + + (pulse / PWM_period) + min_pulse = (0.001 / 0.02) = 0.05 = 5% + max_pulse = (0.002 / .02) = 0.1 = 10% - (pulse / PWM_period) - min_pulse = (0.001 / 0.02) = 0.05 = 5% so ... $34 and $35 = 5.0 - max_pulse = (0.002 / .02) = 0.1 = 10% so ... $36=10 */ #include "grbl.h" #include "SpindleClass.h" - // don't change these -#define BESC_PWM_FREQ 50.0 // Hz +#define BESC_PWM_FREQ 50.0f // Hz #define BESC_PWM_BIT_PRECISION 16 // bits -#define BESC_DEFAULT_MIN_PULSE 5.0 // percent -#define BESC_DEFAULT_MAX_PULSE 10.0 // percent - +#define BESC_PULSE_PERIOD (1.0 / BESC_PWM_FREQ) +// ok to tweak +#define BESC_MIN_PULSE_SECS 0.001f +#define BESC_MAX_PULSE_SECS 0.002f +//don't change +#define BESC_MIN_PULSE_CNT (uint16_t)(BESC_MIN_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0) +#define BESC_MAX_PULSE_CNT (uint16_t)(BESC_MAX_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0) void BESCSpindle :: init() { @@ -40,10 +65,11 @@ void BESCSpindle :: init() { _pwm_freq = BESC_PWM_FREQ; _pwm_precision = 16; - // to do make these tweakable using existing setting as percentages on these defaults - _pwm_off_value = BESC_DEFAULT_MIN_PULSE; - _pwm_min_value = BESC_DEFAULT_MIN_PULSE; - _pwm_max_value = BESC_DEFAULT_MAX_PULSE; + // override these settings + // to do make these tweakable + _pwm_off_value = BESC_MIN_PULSE_CNT; + _pwm_min_value = BESC_MIN_PULSE_CNT; + _pwm_max_value = BESC_MAX_PULSE_CNT; ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel ledcAttachPin(_output_pin, _spindle_pwm_chan_num); // attach the PWM to the pin @@ -51,6 +77,8 @@ void BESCSpindle :: init() { if (_enable_pin != UNDEFINED_PIN) pinMode(_enable_pin, OUTPUT); + set_rpm(0); + config_message(); } @@ -80,13 +108,9 @@ float BESCSpindle::set_rpm(float rpm) { // determine the pwm value if (rpm == 0.0) { pwm_value = _pwm_off_value; - } else if (rpm == _min_rpm) { - pwm_value = _pwm_min_value; - } else if (rpm == _max_rpm) { - pwm_value = _pwm_max_value; } else { - pwm_value = floor((rpm - _min_rpm) * _pwm_gradient) + _pwm_min_value; - } + pwm_value = (uint16_t)map_float(rpm, _min_rpm, _max_rpm, _pwm_min_value, _pwm_max_value); + } #ifdef SPINDLE_ENABLE_OFF_WITH_ZERO_SPEED set_enable_pin(rpm != 0); diff --git a/Grbl_Esp32/tools/PWMSpindle.cpp b/Grbl_Esp32/tools/PWMSpindle.cpp index e17f6979..05cbf292 100644 --- a/Grbl_Esp32/tools/PWMSpindle.cpp +++ b/Grbl_Esp32/tools/PWMSpindle.cpp @@ -23,6 +23,10 @@ #include "SpindleClass.h" // ======================= PWMSpindle ============================== +/* + This gets called at startup or whenever a spindle setting changes + If the spindle is running it will stop and need to be restarted with M3Snnnn +*/ void PWMSpindle::init() { get_pin_numbers(); @@ -94,7 +98,7 @@ void PWMSpindle :: get_pin_numbers() { // The pwm_gradient is the pwm duty cycle units per rpm _pwm_gradient = (_pwm_max_value - _pwm_min_value) / (_max_rpm - _min_rpm); - _spindle_pwm_chan_num = sys_get_next_PWM_chan_num(); + _spindle_pwm_chan_num = 0; // Channel 0 is reserved for spindle use } @@ -180,7 +184,7 @@ uint8_t PWMSpindle::get_state() { void PWMSpindle::stop() { // inverts are delt with in methods set_enable_pin(false); - set_pwm(0); + set_pwm(_pwm_off_value); } // prints the startup message of the spindle config @@ -190,6 +194,9 @@ void PWMSpindle :: config_message() { void PWMSpindle::set_pwm(uint32_t duty) { + +grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle set duty:%d", duty); + if (_output_pin == UNDEFINED_PIN) return; diff --git a/Grbl_Esp32/tools/RelaySpindle.cpp b/Grbl_Esp32/tools/RelaySpindle.cpp index f517e817..00ef60b4 100644 --- a/Grbl_Esp32/tools/RelaySpindle.cpp +++ b/Grbl_Esp32/tools/RelaySpindle.cpp @@ -34,7 +34,7 @@ void RelaySpindle::init() { pinMode(_output_pin, OUTPUT); if (_enable_pin != UNDEFINED_PIN) - pinMode(SPINDLE_ENABLE_PIN, OUTPUT); + pinMode(_enable_pin, OUTPUT); if (_direction_pin != UNDEFINED_PIN) pinMode(_direction_pin, OUTPUT);