1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-01 18:32:37 +02:00

Brought BESC Spindle into the new mapSpeeds framework.

This commit is contained in:
Mitch Bradley
2021-06-21 13:03:58 -10:00
parent 457758d92d
commit 1846923fbd
3 changed files with 50 additions and 40 deletions

View File

@@ -69,6 +69,7 @@ probe:
pin: gpio.32:low:pu pin: gpio.32:low:pu
Relay: Relay:
spinup_ms: 5
output_pin: gpio.12 output_pin: gpio.12
tool: 0 tool: 0
# direction_pin: gpio.13 # direction_pin: gpio.13
@@ -88,18 +89,20 @@ Huanyang:
tool: 20 tool: 20
PWM: PWM:
tool:30
spinup_ms: 200 spinup_ms: 200
spindown_ms: 500 spindown_ms: 500
speeds: 0=0% 5000=30% 10000=100% speeds: 0=0% 5000=30% 10000=100%
output_pin: gpio.2 output_pin: gpio.2
enable_pin: gpio.22 enable_pin: gpio.22
tool: 30
PWM: BESC:
tool:40 min_pulse_us: 950
speeds: 0=0% 10000=100% max_pulse_us: 2050
speeds: 0=0% 30000=100%
output_pin: gpio.4 output_pin: gpio.4
enable_pin: gpio.10 enable_pin: gpio.10
tool: 40
# user_outputs: # user_outputs:
# digital0: gpio.13 # digital0: gpio.13

View File

@@ -26,50 +26,63 @@
Some ESCs can handle higher frequencies, but there is no advantage to changing it. 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 Determine the typical min and max pulse length of your ESC
BESC_MIN_PULSE_SECS is typically 1ms (0.001 sec) or less _min_pulse_secs is typically 1ms (0.001 sec) or less
BESC_MAX_PULSE_SECS is typically 2ms (0.002 sec) or more _max_pulse_secs is typically 2ms (0.002 sec) or more
*/ */
#include "BESCSpindle.h" #include "BESCSpindle.h"
namespace Spindles { namespace Spindles {
void BESC::init() { 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()) { if (_output_pin.undefined()) {
info_all("Warning: BESC output pin not defined"); info_all("Warning: BESC output pin not defined");
return; // We cannot continue without the output pin return; // We cannot continue without the output pin
} }
// override some settings to what is required for a BESC is_reversable = _direction_pin.defined();
_pwm_freq = (uint32_t)BESC_PWM_FREQ; _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_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 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); _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(); 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(); config_message();
} }
// prints the startup message of the spindle config // prints the startup message of the spindle config
void BESC::config_message() { 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(), _output_pin.name().c_str(),
BESC_MIN_PULSE_SECS * 1000.0, // convert to milliseconds _min_pulse_us,
BESC_MAX_PULSE_SECS * 1000.0, // convert to milliseconds _max_pulse_us,
_pwm_freq, _pwm_freq,
_pwm_precision); _pwm_precision);
} }

View File

@@ -38,14 +38,11 @@
namespace Spindles { namespace Spindles {
class BESC : public PWM { class BESC : public PWM {
protected: protected:
float BESC_MIN_PULSE_SECS = 0.0009f; // in seconds const uint32_t besc_pwm_freq = 50; // 50 Hz
float BESC_MAX_PULSE_SECS = 0.0022f; // in seconds
uint16_t BESC_MIN_PULSE_CNT; // Configurable
uint16_t BESC_MAX_PULSE_CNT; uint32_t _min_pulse_us = 900; // microseconds
uint32_t _max_pulse_us = 2200; // microseconds
const double BESC_PWM_FREQ = 50.0; // Hz
const double BESC_PULSE_PERIOD = (1.0 / BESC_PWM_FREQ);
public: public:
BESC() = default; BESC() = default;
@@ -64,14 +61,11 @@ namespace Spindles {
void group(Configuration::HandlerBase& handler) override { void group(Configuration::HandlerBase& handler) override {
PWM::group(handler); PWM::group(handler);
handler.item("besc_min_pulse_secs", BESC_MIN_PULSE_SECS); handler.item("min_pulse_us", _min_pulse_us);
handler.item("besc_max_pulse_secs", BESC_MAX_PULSE_SECS); handler.item("max_pulse_us", _max_pulse_us);
} }
void afterParse() override { void afterParse() override {}
BESC_MIN_PULSE_CNT = static_cast<uint16_t>(BESC_MIN_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0);
BESC_MAX_PULSE_CNT = static_cast<uint16_t>(BESC_MAX_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0);
}
// Name of the configurable. Must match the name registered in the cpp file. // Name of the configurable. Must match the name registered in the cpp file.
const char* name() const override { return "BESC"; } const char* name() const override { return "BESC"; }