diff --git a/Grbl_Esp32/data/spindles.yaml b/Grbl_Esp32/data/spindles.yaml index 72456f2b..d09885c1 100644 --- a/Grbl_Esp32/data/spindles.yaml +++ b/Grbl_Esp32/data/spindles.yaml @@ -69,8 +69,9 @@ probe: pin: gpio.32:low:pu Relay: + spinup_ms: 5 output_pin: gpio.12 - tool:0 + tool: 0 # direction_pin: gpio.13 YL620: @@ -80,26 +81,28 @@ YL620: rxd_pin: gpio.15 rts_pin: gpio.13 baudrate: 9600 - tool:10 + tool: 10 Huanyang: speeds: 0=0% 0=25% 6000=25% 24000=100% modbus_id: 2 - tool:20 + tool: 20 PWM: - tool:30 spinup_ms: 200 spindown_ms: 500 speeds: 0=0% 5000=30% 10000=100% output_pin: gpio.2 enable_pin: gpio.22 + tool: 30 -PWM: - tool:40 - speeds: 0=0% 10000=100% +BESC: + min_pulse_us: 950 + max_pulse_us: 2050 + speeds: 0=0% 30000=100% output_pin: gpio.4 enable_pin: gpio.10 + tool: 40 # user_outputs: # digital0: gpio.13 diff --git a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp index 77cd857e..047ef6b1 100644 --- a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp @@ -26,50 +26,63 @@ Some ESCs can handle higher frequencies, but there is no advantage to changing it. Determine the typical min and max pulse length of your ESC - BESC_MIN_PULSE_SECS is typically 1ms (0.001 sec) or less - BESC_MAX_PULSE_SECS is typically 2ms (0.002 sec) or more + _min_pulse_secs is typically 1ms (0.001 sec) or less + _max_pulse_secs is typically 2ms (0.002 sec) or more */ #include "BESCSpindle.h" namespace Spindles { void BESC::init() { - get_pins_and_settings(); // these gets the standard PWM settings, but many need to be changed for BESC - if (_output_pin.undefined()) { info_all("Warning: BESC output pin not defined"); return; // We cannot continue without the output pin } - // override some settings to what is required for a BESC - _pwm_freq = (uint32_t)BESC_PWM_FREQ; + is_reversable = _direction_pin.defined(); + _pwm_chan_num = 0; // Channel 0 is reserved for spindle use + + // override some settings in the PWM base class to what is required for a BESC + _pwm_freq = besc_pwm_freq; _pwm_precision = 16; + _pwm_period = (1 << _pwm_precision); - auto outputPin = _output_pin.getNative(Pin::Capabilities::PWM); - + auto outputNative = _output_pin.getNative(Pin::Capabilities::PWM); ledcSetup(_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel - ledcAttachPin(outputPin, _pwm_chan_num); // attach the PWM to the pin + ledcAttachPin(outputNative, _pwm_chan_num); // attach the PWM to the pin _enable_pin.setAttr(Pin::Attr::Output); + // 1000000 is us/sec + const uint32_t besc_pulse_period_us = 1000000 / besc_pwm_freq; + + float _min_pulse_percent = 100.0 * _min_pulse_us / besc_pulse_period_us; + float _max_pulse_percent = 100.0 * _max_pulse_us / besc_pulse_period_us; + + uint32_t max_speed = 20000; // Default value if none given in speeds: + if (_speeds.size() != 0) { + log_info("Overriding PWM speed map for BESC"); + // Extract the maximum speed from the provide speed map + max_speed = maxSpeed(); + } + + // BESC PWM typically represents 0 speed as a 1ms pulse and max speed as a 2ms pulse + _speeds.clear(); + _speeds.push_back({ 0, _min_pulse_percent }); + _speeds.push_back({ max_speed, _max_pulse_percent }); + + setupSpeeds(_pwm_period); + stop(); - -#ifdef LATER - // XXX these need to be folded into the speed map - _pwm_off = BESC_MIN_PULSE_CNT; - _pwm_min = _pwm_off; - _pwm_max = BESC_MAX_PULSE_CNT; -#endif - config_message(); } // prints the startup message of the spindle config void BESC::config_message() { - info_all("BESC spindle on Pin:%s Min:%0.2fms Max:%0.2fms Freq:%dHz Res:%dbits", + info_all("BESC spindle on Pin:%s Min:%dus Max:%dus Freq:%dHz Res:%dbits", _output_pin.name().c_str(), - BESC_MIN_PULSE_SECS * 1000.0, // convert to milliseconds - BESC_MAX_PULSE_SECS * 1000.0, // convert to milliseconds + _min_pulse_us, + _max_pulse_us, _pwm_freq, _pwm_precision); } diff --git a/Grbl_Esp32/src/Spindles/BESCSpindle.h b/Grbl_Esp32/src/Spindles/BESCSpindle.h index 42594b9f..a7b8f83d 100644 --- a/Grbl_Esp32/src/Spindles/BESCSpindle.h +++ b/Grbl_Esp32/src/Spindles/BESCSpindle.h @@ -38,14 +38,11 @@ namespace Spindles { class BESC : public PWM { protected: - float BESC_MIN_PULSE_SECS = 0.0009f; // in seconds - float BESC_MAX_PULSE_SECS = 0.0022f; // in seconds + const uint32_t besc_pwm_freq = 50; // 50 Hz - uint16_t BESC_MIN_PULSE_CNT; - uint16_t BESC_MAX_PULSE_CNT; - - const double BESC_PWM_FREQ = 50.0; // Hz - const double BESC_PULSE_PERIOD = (1.0 / BESC_PWM_FREQ); + // Configurable + uint32_t _min_pulse_us = 900; // microseconds + uint32_t _max_pulse_us = 2200; // microseconds public: BESC() = default; @@ -64,14 +61,11 @@ namespace Spindles { void group(Configuration::HandlerBase& handler) override { PWM::group(handler); - handler.item("besc_min_pulse_secs", BESC_MIN_PULSE_SECS); - handler.item("besc_max_pulse_secs", BESC_MAX_PULSE_SECS); + handler.item("min_pulse_us", _min_pulse_us); + handler.item("max_pulse_us", _max_pulse_us); } - void afterParse() override { - BESC_MIN_PULSE_CNT = static_cast(BESC_MIN_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0); - BESC_MAX_PULSE_CNT = static_cast(BESC_MAX_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0); - } + void afterParse() override {} // Name of the configurable. Must match the name registered in the cpp file. const char* name() const override { return "BESC"; }