1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-08-22 14:12:59 +02:00

Avoid reiterationIteration in spindle names (#530)

This commit is contained in:
Mitch Bradley
2020-08-10 10:37:32 -10:00
committed by GitHub
parent 898edc7f95
commit 77dfa95555
21 changed files with 154 additions and 154 deletions

View File

@@ -51,7 +51,7 @@ have a single class.
- Class names and namespace names are named `CamelCase`. Note that - Class names and namespace names are named `CamelCase`. Note that
class and namespace names should only start with an `_` if they are class and namespace names should only start with an `_` if they are
(A) not in the global namespace and (b) should otherwise start with a digit. (A) not in the global namespace and (b) should otherwise start with a digit.
For example `_10VSpindle`. For example `_10V`.
- Class member functions should be `snake_case` - Class member functions should be `snake_case`
- Class member variables should be `_snake_case` with a leading `_`. - Class member variables should be `_snake_case` with a leading `_`.

View File

@@ -87,7 +87,7 @@ void setup() {
if (homing_enable->get()) if (homing_enable->get())
sys.state = STATE_ALARM; sys.state = STATE_ALARM;
#endif #endif
Spindles::Spindle::spindle_select(); Spindles::Spindle::select();
#ifdef ENABLE_WIFI #ifdef ENABLE_WIFI
wifi_config.begin(); wifi_config.begin();
#endif #endif

View File

@@ -27,7 +27,7 @@
#include "10vSpindle.h" #include "10vSpindle.h"
namespace Spindles { namespace Spindles {
void _10vSpindle::init() { void _10v::init() {
get_pins_and_settings(); // these gets the standard PWM settings, but many need to be changed for BESC get_pins_and_settings(); // these gets the standard PWM settings, but many need to be changed for BESC
// a couple more pins not inherited from PWM Spindle // a couple more pins not inherited from PWM Spindle
@@ -48,8 +48,8 @@ namespace Spindles {
return; // We cannot continue without the output pin return; // We cannot continue without the output pin
} }
ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel ledcSetup(_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel
ledcAttachPin(_output_pin, _spindle_pwm_chan_num); // attach the PWM to the pin ledcAttachPin(_output_pin, _pwm_chan_num); // attach the PWM to the pin
pinMode(_enable_pin, OUTPUT); pinMode(_enable_pin, OUTPUT);
pinMode(_direction_pin, OUTPUT); pinMode(_direction_pin, OUTPUT);
@@ -65,7 +65,7 @@ namespace Spindles {
} }
// prints the startup message of the spindle config // prints the startup message of the spindle config
void _10vSpindle::config_message() { void _10v::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"0-10V spindle Out:%s Enbl:%s, Dir:%s, Fwd:%s, Rev:%s, Freq:%dHz Res:%dbits", "0-10V spindle Out:%s Enbl:%s, Dir:%s, Fwd:%s, Rev:%s, Freq:%dHz Res:%dbits",
@@ -78,7 +78,7 @@ namespace Spindles {
_pwm_precision); _pwm_precision);
} }
uint32_t _10vSpindle::set_rpm(uint32_t rpm) { uint32_t _10v::set_rpm(uint32_t rpm) {
uint32_t pwm_value; uint32_t pwm_value;
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
@@ -104,7 +104,7 @@ namespace Spindles {
return rpm; return rpm;
} }
/* /*
void _10vSpindle::set_state(uint8_t state, uint32_t rpm) { void _10v::set_state(uint8_t state, uint32_t rpm) {
if (sys.abort) if (sys.abort)
return; // Block during abort. return; // Block during abort.
@@ -112,7 +112,7 @@ namespace Spindles {
sys.spindle_speed = 0; sys.spindle_speed = 0;
stop(); stop();
} else { } else {
set_spindle_dir_pin(state == SPINDLE_ENABLE_CW); set_dir_pin(state == SPINDLE_ENABLE_CW);
set_rpm(rpm); set_rpm(rpm);
} }
@@ -123,7 +123,7 @@ namespace Spindles {
*/ */
uint8_t _10vSpindle::get_state() { uint8_t _10v::get_state() {
if (_current_pwm_duty == 0 || _output_pin == UNDEFINED_PIN) if (_current_pwm_duty == 0 || _output_pin == UNDEFINED_PIN)
return (SPINDLE_STATE_DISABLE); return (SPINDLE_STATE_DISABLE);
if (_direction_pin != UNDEFINED_PIN) if (_direction_pin != UNDEFINED_PIN)
@@ -131,14 +131,14 @@ namespace Spindles {
return (SPINDLE_STATE_CW); return (SPINDLE_STATE_CW);
} }
void _10vSpindle::stop() { void _10v::stop() {
// inverts are delt with in methods // inverts are delt with in methods
set_enable_pin(false); set_enable_pin(false);
set_output(_pwm_off_value); set_output(_pwm_off_value);
} }
void _10vSpindle::set_enable_pin(bool enable) { void _10v::set_enable_pin(bool enable) {
//grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "_10vSpindle::set_enable_pin"); //grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle::_10v::set_enable_pin");
if (_off_with_zero_speed && sys.spindle_speed == 0) if (_off_with_zero_speed && sys.spindle_speed == 0)
enable = false; enable = false;
@@ -155,8 +155,8 @@ namespace Spindles {
} }
} }
void _10vSpindle::set_spindle_dir_pin(bool Clockwise) { void _10v::set_dir_pin(bool Clockwise) {
//grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "_10vSpindle::set_spindle_dir_pin"); //grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Spindle::_10v::set_dir_pin");
digitalWrite(_direction_pin, Clockwise); digitalWrite(_direction_pin, Clockwise);
digitalWrite(_forward_pin, Clockwise); digitalWrite(_forward_pin, Clockwise);
digitalWrite(_reverse_pin, !Clockwise); digitalWrite(_reverse_pin, !Clockwise);

View File

@@ -29,14 +29,14 @@
#include "PWMSpindle.h" #include "PWMSpindle.h"
namespace Spindles { namespace Spindles {
class _10vSpindle : public PWMSpindle { class _10v : public PWM {
public: public:
_10vSpindle() = default; _10v() = default;
_10vSpindle(const _10vSpindle&) = delete; _10v(const _10v&) = delete;
_10vSpindle(_10vSpindle&&) = delete; _10v(_10v&&) = delete;
_10vSpindle& operator=(const _10vSpindle&) = delete; _10v& operator=(const _10v&) = delete;
_10vSpindle& operator=(_10vSpindle&&) = delete; _10v& operator=(_10v&&) = delete;
void init() override; void init() override;
void config_message() override; void config_message() override;
@@ -46,13 +46,13 @@ namespace Spindles {
uint8_t get_state() override; uint8_t get_state() override;
void stop() override; void stop() override;
virtual ~_10vSpindle() {} virtual ~_10v() {}
uint8_t _forward_pin; uint8_t _forward_pin;
uint8_t _reverse_pin; uint8_t _reverse_pin;
protected: protected:
void set_enable_pin(bool enable_pin) override; void set_enable_pin(bool enable_pin) override;
void set_spindle_dir_pin(bool Clockwise) override; void set_dir_pin(bool Clockwise) override;
}; };
} }

View File

@@ -52,7 +52,7 @@
#define BESC_MAX_PULSE_CNT (uint16_t)(BESC_MAX_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0) #define BESC_MAX_PULSE_CNT (uint16_t)(BESC_MAX_PULSE_SECS / BESC_PULSE_PERIOD * 65535.0)
namespace Spindles { namespace Spindles {
void BESCSpindle::init() { void BESC::init() {
get_pins_and_settings(); // these gets the standard PWM settings, but many need to be changed for BESC get_pins_and_settings(); // these gets the standard PWM settings, but many need to be changed for BESC
if (_output_pin == UNDEFINED_PIN) { if (_output_pin == UNDEFINED_PIN) {
@@ -69,8 +69,8 @@ namespace Spindles {
_pwm_min_value = _pwm_off_value; _pwm_min_value = _pwm_off_value;
_pwm_max_value = BESC_MAX_PULSE_CNT; _pwm_max_value = BESC_MAX_PULSE_CNT;
ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel ledcSetup(_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel
ledcAttachPin(_output_pin, _spindle_pwm_chan_num); // attach the PWM to the pin ledcAttachPin(_output_pin, _pwm_chan_num); // attach the PWM to the pin
pinMode(_enable_pin, OUTPUT); pinMode(_enable_pin, OUTPUT);
@@ -82,7 +82,7 @@ namespace Spindles {
} }
// prints the startup message of the spindle config // prints the startup message of the spindle config
void BESCSpindle::config_message() { void BESC::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"BESC spindle on Pin:%s Min:%0.2fms Max:%0.2fms Freq:%dHz Res:%dbits", "BESC spindle on Pin:%s Min:%0.2fms Max:%0.2fms Freq:%dHz Res:%dbits",
@@ -93,7 +93,7 @@ namespace Spindles {
_pwm_precision); _pwm_precision);
} }
uint32_t BESCSpindle::set_rpm(uint32_t rpm) { uint32_t BESC::set_rpm(uint32_t rpm) {
uint32_t pwm_value; uint32_t pwm_value;
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)

View File

@@ -36,19 +36,19 @@
#include "PWMSpindle.h" #include "PWMSpindle.h"
namespace Spindles { namespace Spindles {
class BESCSpindle : public PWMSpindle { class BESC : public PWM {
public: public:
BESCSpindle() = default; BESC() = default;
BESCSpindle(const BESCSpindle&) = delete; BESC(const BESC&) = delete;
BESCSpindle(BESCSpindle&&) = delete; BESC(BESC&&) = delete;
BESCSpindle& operator=(const BESCSpindle&) = delete; BESC& operator=(const BESC&) = delete;
BESCSpindle& operator=(BESCSpindle&&) = delete; BESC& operator=(BESC&&) = delete;
void init() override; void init() override;
void config_message() override; void config_message() override;
uint32_t set_rpm(uint32_t rpm) override; uint32_t set_rpm(uint32_t rpm) override;
virtual ~BESCSpindle() {} virtual ~BESC() {}
}; };
} }

View File

@@ -24,8 +24,8 @@
#include "DacSpindle.h" #include "DacSpindle.h"
namespace Spindles { namespace Spindles {
// ======================================== DacSpindle ====================================== // ======================================== Dac ======================================
void DacSpindle::init() { void Dac::init() {
get_pins_and_settings(); get_pins_and_settings();
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
@@ -52,7 +52,7 @@ namespace Spindles {
config_message(); config_message();
} }
void DacSpindle::config_message() { void Dac::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"DAC spindle Output:%s, Enbl:%s, Dir:%s, Res:8bits", "DAC spindle Output:%s, Enbl:%s, Dir:%s, Res:8bits",
@@ -61,7 +61,7 @@ namespace Spindles {
pinName(_direction_pin).c_str()); pinName(_direction_pin).c_str());
} }
uint32_t DacSpindle::set_rpm(uint32_t rpm) { uint32_t Dac::set_rpm(uint32_t rpm) {
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
return rpm; return rpm;
@@ -98,7 +98,7 @@ namespace Spindles {
return rpm; return rpm;
} }
void DacSpindle::set_output(uint32_t duty) { void Dac::set_output(uint32_t duty) {
if (_gpio_ok) { if (_gpio_ok) {
dacWrite(_output_pin, (uint8_t)duty); dacWrite(_output_pin, (uint8_t)duty);
} }

View File

@@ -28,20 +28,20 @@
namespace Spindles { namespace Spindles {
// This uses one of the (2) DAC pins on ESP32 to output a voltage // This uses one of the (2) DAC pins on ESP32 to output a voltage
class DacSpindle : public PWMSpindle { class Dac : public PWM {
public: public:
DacSpindle() = default; Dac() = default;
DacSpindle(const DacSpindle&) = delete; Dac(const Dac&) = delete;
DacSpindle(DacSpindle&&) = delete; Dac(Dac&&) = delete;
DacSpindle& operator=(const DacSpindle&) = delete; Dac& operator=(const Dac&) = delete;
DacSpindle& operator=(DacSpindle&&) = delete; Dac& operator=(Dac&&) = delete;
void init() override; void init() override;
void config_message() override; void config_message() override;
uint32_t set_rpm(uint32_t rpm) override; uint32_t set_rpm(uint32_t rpm) override;
virtual ~DacSpindle() {} virtual ~Dac() {}
private: private:
bool _gpio_ok; // DAC is on a valid pin bool _gpio_ok; // DAC is on a valid pin

View File

@@ -155,7 +155,7 @@ namespace Spindles {
static TaskHandle_t vfd_cmdTaskHandle = 0; static TaskHandle_t vfd_cmdTaskHandle = 0;
bool hy_spindle_ok = true; bool hy_ok = true;
// The communications task // The communications task
void vfd_cmd_task(void* pvParameters) { void vfd_cmd_task(void* pvParameters) {
@@ -190,7 +190,7 @@ namespace Spindles {
} }
} else { } else {
HuanyangSpindle::read_value(reg_item); // only this appears to work all the time. Other registers are flakey. Huanyang::read_value(reg_item); // only this appears to work all the time. Other registers are flakey.
if (reg_item < 0x03) if (reg_item < 0x03)
reg_item++; reg_item++;
else { else {
@@ -203,8 +203,8 @@ namespace Spindles {
// ================== Class methods ================================== // ================== Class methods ==================================
void HuanyangSpindle::init() { void Huanyang::init() {
hy_spindle_ok = true; // initialize hy_ok = true; // initialize
// fail if required items are not defined // fail if required items are not defined
if (!get_pins_and_settings()) { if (!get_pins_and_settings()) {
@@ -259,49 +259,49 @@ namespace Spindles {
// Checks for all the required pin definitions // Checks for all the required pin definitions
// It returns a message for each missing pin // It returns a message for each missing pin
// Returns true if all pins are defined. // Returns true if all pins are defined.
bool HuanyangSpindle::get_pins_and_settings() { bool Huanyang::get_pins_and_settings() {
#ifdef HUANYANG_TXD_PIN #ifdef HUANYANG_TXD_PIN
_txd_pin = HUANYANG_TXD_PIN; _txd_pin = HUANYANG_TXD_PIN;
#else #else
grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_TXD_PIN"); grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_TXD_PIN");
hy_spindle_ok = false; hy_ok = false;
#endif #endif
#ifdef HUANYANG_RXD_PIN #ifdef HUANYANG_RXD_PIN
_rxd_pin = HUANYANG_RXD_PIN; _rxd_pin = HUANYANG_RXD_PIN;
#else #else
grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_RXD_PIN"); grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_RXD_PIN");
hy_spindle_ok = false; hy_ok = false;
#endif #endif
#ifdef HUANYANG_RTS_PIN #ifdef HUANYANG_RTS_PIN
_rts_pin = HUANYANG_RTS_PIN; _rts_pin = HUANYANG_RTS_PIN;
#else #else
grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_RTS_PIN"); grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Undefined HUANYANG_RTS_PIN");
hy_spindle_ok = false; hy_ok = false;
#endif #endif
if (laser_mode->get()) { if (laser_mode->get()) {
grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Huanyang spindle disabled in laser mode. Set $GCode/LaserMode=Off and restart"); grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Huanyang spindle disabled in laser mode. Set $GCode/LaserMode=Off and restart");
hy_spindle_ok = false; hy_ok = false;
} }
_min_rpm = rpm_min->get(); _min_rpm = rpm_min->get();
_max_rpm = rpm_max->get(); _max_rpm = rpm_max->get();
return hy_spindle_ok; return hy_ok;
} }
void HuanyangSpindle::config_message() { void Huanyang::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"Huanyang Spindle Tx:%s Rx:%s RTS:%s", "Huanyang Tx:%s Rx:%s RTS:%s",
pinName(_txd_pin).c_str(), pinName(_txd_pin).c_str(),
pinName(_rxd_pin).c_str(), pinName(_rxd_pin).c_str(),
pinName(_rts_pin).c_str()); pinName(_rts_pin).c_str());
} }
void HuanyangSpindle::set_state(uint8_t state, uint32_t rpm) { void Huanyang::set_state(uint8_t state, uint32_t rpm) {
if (sys.abort) if (sys.abort)
return; // Block during abort. return; // Block during abort.
@@ -330,8 +330,8 @@ namespace Spindles {
return; return;
} }
bool HuanyangSpindle::set_mode(uint8_t mode, bool critical) { bool Huanyang::set_mode(uint8_t mode, bool critical) {
if (!hy_spindle_ok) if (!hy_ok)
return false; return false;
hy_command_t mode_cmd; hy_command_t mode_cmd;
@@ -365,8 +365,8 @@ namespace Spindles {
return true; return true;
} }
uint32_t HuanyangSpindle::set_rpm(uint32_t rpm) { uint32_t Huanyang::set_rpm(uint32_t rpm) {
if (!hy_spindle_ok) if (!hy_ok)
return 0; return 0;
hy_command_t rpm_cmd; hy_command_t rpm_cmd;
@@ -412,7 +412,7 @@ namespace Spindles {
} }
// This appears to read the control register and will return an RPM running or not. // This appears to read the control register and will return an RPM running or not.
void HuanyangSpindle::read_value(uint8_t reg) { void Huanyang::read_value(uint8_t reg) {
uint16_t ret_value = 0; uint16_t ret_value = 0;
hy_command_t read_cmd; hy_command_t read_cmd;
uint8_t rx_message[HUANYANG_MAX_MSG_SIZE]; uint8_t rx_message[HUANYANG_MAX_MSG_SIZE];
@@ -437,16 +437,16 @@ namespace Spindles {
grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "VFD Queue Full"); grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "VFD Queue Full");
} }
void HuanyangSpindle::stop() { set_mode(SPINDLE_DISABLE, false); } void Huanyang::stop() { set_mode(SPINDLE_DISABLE, false); }
// state is cached rather than read right now to prevent delays // state is cached rather than read right now to prevent delays
uint8_t HuanyangSpindle::get_state() { return _state; } uint8_t Huanyang::get_state() { return _state; }
// Calculate the CRC on all of the byte except the last 2 // Calculate the CRC on all of the byte except the last 2
// It then added the CRC to those last 2 bytes // It then added the CRC to those last 2 bytes
// full_msg_len This is the length of the message including the 2 crc bytes // full_msg_len This is the length of the message including the 2 crc bytes
// Source: https://ctlsys.com/support/how_to_compute_the_modbus_rtu_message_crc/ // Source: https://ctlsys.com/support/how_to_compute_the_modbus_rtu_message_crc/
void HuanyangSpindle::add_ModRTU_CRC(char* buf, int full_msg_len) { void Huanyang::add_ModRTU_CRC(char* buf, int full_msg_len) {
uint16_t crc = 0xFFFF; uint16_t crc = 0xFFFF;
for (int pos = 0; pos < full_msg_len - 2; pos++) { for (int pos = 0; pos < full_msg_len - 2; pos++) {
crc ^= (uint16_t)buf[pos]; // XOR byte into least sig. byte of crc crc ^= (uint16_t)buf[pos]; // XOR byte into least sig. byte of crc

View File

@@ -21,7 +21,7 @@
#include "Spindle.h" #include "Spindle.h"
namespace Spindles { namespace Spindles {
class HuanyangSpindle : public Spindle { class Huanyang : public Spindle {
private: private:
uint16_t ModRTU_CRC(char* buf, int len); uint16_t ModRTU_CRC(char* buf, int len);
@@ -37,12 +37,12 @@ namespace Spindles {
bool _task_running; bool _task_running;
public: public:
HuanyangSpindle() : _task_running(false) {} Huanyang() : _task_running(false) {}
HuanyangSpindle(const HuanyangSpindle&) = delete; Huanyang(const Huanyang&) = delete;
HuanyangSpindle(HuanyangSpindle&&) = delete; Huanyang(Huanyang&&) = delete;
HuanyangSpindle& operator=(const HuanyangSpindle&) = delete; Huanyang& operator=(const Huanyang&) = delete;
HuanyangSpindle& operator=(HuanyangSpindle&&) = delete; Huanyang& operator=(Huanyang&&) = delete;
void init(); void init();
void config_message(); void config_message();
@@ -53,7 +53,7 @@ namespace Spindles {
static void read_value(uint8_t reg); static void read_value(uint8_t reg);
static void add_ModRTU_CRC(char* buf, int full_msg_len); static void add_ModRTU_CRC(char* buf, int full_msg_len);
virtual ~HuanyangSpindle() {} virtual ~Huanyang() {}
protected: protected:
uint32_t _min_rpm; uint32_t _min_rpm;

View File

@@ -1,7 +1,7 @@
/* /*
Laser.cpp Laser.cpp
This is similar the the PWM Spindle except that it allows the This is similar to the PWM Spindle except that it allows the
M4 speed vs. power copensation. M4 speed vs. power copensation.
Part of Grbl_ESP32 Part of Grbl_ESP32
@@ -37,6 +37,6 @@ namespace Spindles {
_pwm_precision, _pwm_precision,
isRateAdjusted()); // the current mode isRateAdjusted()); // the current mode
use_delays = false; // this will override the value set in PWMSpindle intit() use_delays = false; // this will override the value set in Spindle::PWM::init()
} }
} }

View File

@@ -3,7 +3,7 @@
/* /*
Laser.h Laser.h
This is similar the the PWM Spindle except that it allows the This is similar to the PWM Spindle except that it allows the
M4 speed vs. power copensation. M4 speed vs. power copensation.
Part of Grbl_ESP32 Part of Grbl_ESP32
@@ -25,7 +25,7 @@
namespace Spindles { namespace Spindles {
// this is the same as a PWM spindle but the M4 compensation is supported. // this is the same as a PWM spindle but the M4 compensation is supported.
class Laser : public PWMSpindle { class Laser : public PWM {
public: public:
Laser() = default; Laser() = default;

View File

@@ -22,17 +22,17 @@
#include "NullSpindle.h" #include "NullSpindle.h"
namespace Spindles { namespace Spindles {
// ======================= NullSpindle ============================== // ======================= Null ==============================
// NullSpindle is just bunch of do nothing (ignore) methods to be used when you don't want a spindle // Null is just bunch of do nothing (ignore) methods to be used when you don't want a spindle
void NullSpindle::init() { void Null::init() {
is_reversable = false; is_reversable = false;
use_delays = false; use_delays = false;
config_message(); config_message();
} }
uint32_t NullSpindle::set_rpm(uint32_t rpm) { return rpm; } uint32_t Null::set_rpm(uint32_t rpm) { return rpm; }
void NullSpindle::set_state(uint8_t state, uint32_t rpm) {} void Null::set_state(uint8_t state, uint32_t rpm) {}
uint8_t NullSpindle::get_state() { return (SPINDLE_STATE_DISABLE); } uint8_t Null::get_state() { return (SPINDLE_STATE_DISABLE); }
void NullSpindle::stop() {} void Null::stop() {}
void NullSpindle::config_message() { grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "No spindle"); } void Null::config_message() { grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "No spindle"); }
} }

View File

@@ -26,14 +26,14 @@
namespace Spindles { namespace Spindles {
// This is a dummy spindle that has no I/O. // This is a dummy spindle that has no I/O.
// It is used to ignore spindle commands when no spinde is desired // It is used to ignore spindle commands when no spinde is desired
class NullSpindle : public Spindle { class Null : public Spindle {
public: public:
NullSpindle() = default; Null() = default;
NullSpindle(const NullSpindle&) = delete; Null(const Null&) = delete;
NullSpindle(NullSpindle&&) = delete; Null(Null&&) = delete;
NullSpindle& operator=(const NullSpindle&) = delete; Null& operator=(const Null&) = delete;
NullSpindle& operator=(NullSpindle&&) = delete; Null& operator=(Null&&) = delete;
void init() override; void init() override;
uint32_t set_rpm(uint32_t rpm) override; uint32_t set_rpm(uint32_t rpm) override;
@@ -42,6 +42,6 @@ namespace Spindles {
void stop() override; void stop() override;
void config_message() override; void config_message() override;
virtual ~NullSpindle() {} virtual ~Null() {}
}; };
} }

View File

@@ -21,7 +21,7 @@
*/ */
#include "PWMSpindle.h" #include "PWMSpindle.h"
// ======================= PWMSpindle ============================== // ======================= PWM ==============================
/* /*
This gets called at startup or whenever a spindle setting changes 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 If the spindle is running it will stop and need to be restarted with M3Snnnn
@@ -30,7 +30,7 @@
//#include "grbl.h" //#include "grbl.h"
namespace Spindles { namespace Spindles {
void PWMSpindle::init() { void PWM::init() {
get_pins_and_settings(); get_pins_and_settings();
if (_output_pin == UNDEFINED_PIN) { if (_output_pin == UNDEFINED_PIN) {
@@ -43,8 +43,8 @@ namespace Spindles {
return; return;
} }
ledcSetup(_spindle_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel ledcSetup(_pwm_chan_num, (double)_pwm_freq, _pwm_precision); // setup the channel
ledcAttachPin(_output_pin, _spindle_pwm_chan_num); // attach the PWM to the pin ledcAttachPin(_output_pin, _pwm_chan_num); // attach the PWM to the pin
pinMode(_enable_pin, OUTPUT); pinMode(_enable_pin, OUTPUT);
pinMode(_direction_pin, OUTPUT); pinMode(_direction_pin, OUTPUT);
@@ -55,7 +55,7 @@ namespace Spindles {
} }
// Get the GPIO from the machine definition // Get the GPIO from the machine definition
void PWMSpindle::get_pins_and_settings() { void PWM::get_pins_and_settings() {
// setup all the pins // setup all the pins
#ifdef SPINDLE_OUTPUT_PIN #ifdef SPINDLE_OUTPUT_PIN
@@ -112,10 +112,10 @@ namespace Spindles {
// The pwm_gradient is the pwm duty cycle units per rpm // The pwm_gradient is the pwm duty cycle units per rpm
// _pwm_gradient = (_pwm_max_value - _pwm_min_value) / (_max_rpm - _min_rpm); // _pwm_gradient = (_pwm_max_value - _pwm_min_value) / (_max_rpm - _min_rpm);
_spindle_pwm_chan_num = 0; // Channel 0 is reserved for spindle use _pwm_chan_num = 0; // Channel 0 is reserved for spindle use
} }
uint32_t PWMSpindle::set_rpm(uint32_t rpm) { uint32_t PWM::set_rpm(uint32_t rpm) {
uint32_t pwm_value; uint32_t pwm_value;
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
@@ -151,7 +151,7 @@ namespace Spindles {
return 0; return 0;
} }
void PWMSpindle::set_state(uint8_t state, uint32_t rpm) { void PWM::set_state(uint8_t state, uint32_t rpm) {
if (sys.abort) if (sys.abort)
return; // Block during abort. return; // Block during abort.
@@ -164,7 +164,7 @@ namespace Spindles {
//grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "SpinDown Done"); //grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "SpinDown Done");
} }
} else { } else {
set_spindle_dir_pin(state == SPINDLE_ENABLE_CW); set_dir_pin(state == SPINDLE_ENABLE_CW);
set_rpm(rpm); set_rpm(rpm);
set_enable_pin(state != SPINDLE_DISABLE); // must be done after setting rpm for enable features to work set_enable_pin(state != SPINDLE_DISABLE); // must be done after setting rpm for enable features to work
if (use_delays && (_current_state != state)) { if (use_delays && (_current_state != state)) {
@@ -179,7 +179,7 @@ namespace Spindles {
sys.report_ovr_counter = 0; // Set to report change immediately sys.report_ovr_counter = 0; // Set to report change immediately
} }
uint8_t PWMSpindle::get_state() { uint8_t PWM::get_state() {
if (_current_pwm_duty == 0 || _output_pin == UNDEFINED_PIN) if (_current_pwm_duty == 0 || _output_pin == UNDEFINED_PIN)
return (SPINDLE_STATE_DISABLE); return (SPINDLE_STATE_DISABLE);
if (_direction_pin != UNDEFINED_PIN) if (_direction_pin != UNDEFINED_PIN)
@@ -187,14 +187,14 @@ namespace Spindles {
return (SPINDLE_STATE_CW); return (SPINDLE_STATE_CW);
} }
void PWMSpindle::stop() { void PWM::stop() {
// inverts are delt with in methods // inverts are delt with in methods
set_enable_pin(false); set_enable_pin(false);
set_output(_pwm_off_value); set_output(_pwm_off_value);
} }
// prints the startup message of the spindle config // prints the startup message of the spindle config
void PWMSpindle::config_message() { void PWM::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"PWM spindle Output:%s, Enbl:%s, Dir:%s, Freq:%dHz, Res:%dbits", "PWM spindle Output:%s, Enbl:%s, Dir:%s, Freq:%dHz, Res:%dbits",
@@ -205,7 +205,7 @@ namespace Spindles {
_pwm_precision); _pwm_precision);
} }
void PWMSpindle::set_output(uint32_t duty) { void PWM::set_output(uint32_t duty) {
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
return; return;
@@ -220,10 +220,10 @@ namespace Spindles {
//grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "set_output(%d)", duty); //grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "set_output(%d)", duty);
ledcWrite(_spindle_pwm_chan_num, duty); ledcWrite(_pwm_chan_num, duty);
} }
void PWMSpindle::set_enable_pin(bool enable) { void PWM::set_enable_pin(bool enable) {
if (_enable_pin == UNDEFINED_PIN) if (_enable_pin == UNDEFINED_PIN)
return; return;
@@ -238,7 +238,7 @@ namespace Spindles {
digitalWrite(_enable_pin, enable); digitalWrite(_enable_pin, enable);
} }
void PWMSpindle::set_spindle_dir_pin(bool Clockwise) { digitalWrite(_direction_pin, Clockwise); } void PWM::set_dir_pin(bool Clockwise) { digitalWrite(_direction_pin, Clockwise); }
/* /*
Calculate the highest precision of a PWM based on the frequency in bits Calculate the highest precision of a PWM based on the frequency in bits
@@ -246,7 +246,7 @@ namespace Spindles {
80,000,000 / freq = period 80,000,000 / freq = period
determine the highest precision where (1 << precision) < period determine the highest precision where (1 << precision) < period
*/ */
uint8_t PWMSpindle::calc_pwm_precision(uint32_t freq) { uint8_t PWM::calc_pwm_precision(uint32_t freq) {
uint8_t precision = 0; uint8_t precision = 0;
// increase the precision (bits) until it exceeds allow by frequency the max or is 16 // increase the precision (bits) until it exceeds allow by frequency the max or is 16

View File

@@ -25,14 +25,14 @@
namespace Spindles { namespace Spindles {
// This adds support for PWM // This adds support for PWM
class PWMSpindle : public Spindle { class PWM : public Spindle {
public: public:
PWMSpindle() = default; PWM() = default;
PWMSpindle(const PWMSpindle&) = delete; PWM(const PWM&) = delete;
PWMSpindle(PWMSpindle&&) = delete; PWM(PWM&&) = delete;
PWMSpindle& operator=(const PWMSpindle&) = delete; PWM& operator=(const PWM&) = delete;
PWMSpindle& operator=(PWMSpindle&&) = delete; PWM& operator=(PWM&&) = delete;
void init() override; void init() override;
virtual uint32_t set_rpm(uint32_t rpm) override; virtual uint32_t set_rpm(uint32_t rpm) override;
@@ -41,7 +41,7 @@ namespace Spindles {
void stop() override; void stop() override;
void config_message() override; void config_message() override;
virtual ~PWMSpindle() {} virtual ~PWM() {}
protected: protected:
int32_t _current_pwm_duty; int32_t _current_pwm_duty;
@@ -53,7 +53,7 @@ namespace Spindles {
uint8_t _output_pin; uint8_t _output_pin;
uint8_t _enable_pin; uint8_t _enable_pin;
uint8_t _direction_pin; uint8_t _direction_pin;
uint8_t _spindle_pwm_chan_num; uint8_t _pwm_chan_num;
uint32_t _pwm_freq; uint32_t _pwm_freq;
uint32_t _pwm_period; // how many counts in 1 period uint32_t _pwm_period; // how many counts in 1 period
uint8_t _pwm_precision; uint8_t _pwm_precision;
@@ -62,7 +62,7 @@ namespace Spindles {
bool _invert_pwm; bool _invert_pwm;
//uint32_t _pwm_gradient; // Precalulated value to speed up rpm to PWM conversions. //uint32_t _pwm_gradient; // Precalulated value to speed up rpm to PWM conversions.
virtual void set_spindle_dir_pin(bool Clockwise); virtual void set_dir_pin(bool Clockwise);
virtual void set_output(uint32_t duty); virtual void set_output(uint32_t duty);
virtual void set_enable_pin(bool enable_pin); virtual void set_enable_pin(bool enable_pin);

View File

@@ -21,13 +21,13 @@
*/ */
#include "RelaySpindle.h" #include "RelaySpindle.h"
// ========================= RelaySpindle ================================== // ========================= Relay ==================================
namespace Spindles { namespace Spindles {
/* /*
This is a sub class of PWMSpindle but is a digital rather than PWM output This is a sub class of PWM but is a digital rather than PWM output
*/ */
void RelaySpindle::init() { void Relay::init() {
get_pins_and_settings(); get_pins_and_settings();
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
@@ -44,7 +44,7 @@ namespace Spindles {
} }
// prints the startup message of the spindle config // prints the startup message of the spindle config
void RelaySpindle ::config_message() { void Relay ::config_message() {
grbl_msg_sendf(CLIENT_SERIAL, grbl_msg_sendf(CLIENT_SERIAL,
MSG_LEVEL_INFO, MSG_LEVEL_INFO,
"Relay spindle Output:%s, Enbl:%s, Dir:%s", "Relay spindle Output:%s, Enbl:%s, Dir:%s",
@@ -53,7 +53,7 @@ namespace Spindles {
pinName(_direction_pin).c_str()); pinName(_direction_pin).c_str());
} }
uint32_t RelaySpindle::set_rpm(uint32_t rpm) { uint32_t Relay::set_rpm(uint32_t rpm) {
if (_output_pin == UNDEFINED_PIN) if (_output_pin == UNDEFINED_PIN)
return rpm; return rpm;
@@ -63,7 +63,7 @@ namespace Spindles {
return rpm; return rpm;
} }
void RelaySpindle::set_output(uint32_t duty) { void Relay::set_output(uint32_t duty) {
#ifdef INVERT_SPINDLE_PWM #ifdef INVERT_SPINDLE_PWM
duty = (duty == 0); // flip duty duty = (duty == 0); // flip duty
#endif #endif

View File

@@ -25,20 +25,20 @@
namespace Spindles { namespace Spindles {
// This is for an on/off spindle all RPMs above 0 are on // This is for an on/off spindle all RPMs above 0 are on
class RelaySpindle : public PWMSpindle { class Relay : public PWM {
public: public:
RelaySpindle() = default; Relay() = default;
RelaySpindle(const RelaySpindle&) = delete; Relay(const Relay&) = delete;
RelaySpindle(RelaySpindle&&) = delete; Relay(Relay&&) = delete;
RelaySpindle& operator=(const RelaySpindle&) = delete; Relay& operator=(const Relay&) = delete;
RelaySpindle& operator=(RelaySpindle&&) = delete; Relay& operator=(Relay&&) = delete;
void init() override; void init() override;
void config_message() override; void config_message() override;
uint32_t set_rpm(uint32_t rpm) override; uint32_t set_rpm(uint32_t rpm) override;
virtual ~RelaySpindle() {} virtual ~Relay() {}
protected: protected:
void set_output(uint32_t duty); void set_output(uint32_t duty);

View File

@@ -41,26 +41,26 @@
namespace Spindles { namespace Spindles {
// An instance of each type of spindle is created here. // An instance of each type of spindle is created here.
// This allows the spindle to be dynamicly switched // This allows the spindle to be dynamicly switched
NullSpindle null_spindle; Null null;
PWMSpindle pwm_spindle; PWM pwm;
RelaySpindle relay_spindle; Relay relay;
Laser laser; Laser laser;
DacSpindle dac_spindle; Dac dac;
HuanyangSpindle huanyang_spindle; Huanyang huanyang;
BESCSpindle besc_spindle; BESC besc;
_10vSpindle _10v_spindle; _10v _10v;
void Spindle::spindle_select() { void Spindle::select() {
switch (spindle_type->get()) { switch (spindle_type->get()) {
case SPINDLE_TYPE_PWM: spindle = &pwm_spindle; break; case SPINDLE_TYPE_PWM: spindle = &pwm; break;
case SPINDLE_TYPE_RELAY: spindle = &relay_spindle; break; case SPINDLE_TYPE_RELAY: spindle = &relay; break;
case SPINDLE_TYPE_LASER: spindle = &laser; break; case SPINDLE_TYPE_LASER: spindle = &laser; break;
case SPINDLE_TYPE_DAC: spindle = &dac_spindle; break; case SPINDLE_TYPE_DAC: spindle = &dac; break;
case SPINDLE_TYPE_HUANYANG: spindle = &huanyang_spindle; break; case SPINDLE_TYPE_HUANYANG: spindle = &huanyang; break;
case SPINDLE_TYPE_BESC: spindle = &besc_spindle; break; case SPINDLE_TYPE_BESC: spindle = &besc; break;
case SPINDLE_TYPE_10V: spindle = &_10v_spindle; break; case SPINDLE_TYPE_10V: spindle = &_10v; break;
case SPINDLE_TYPE_NONE: case SPINDLE_TYPE_NONE:
default: spindle = &null_spindle; break; default: spindle = &null; break;
} }
spindle->init(); spindle->init();
@@ -72,7 +72,7 @@ namespace Spindles {
return false; // default for basic spindle is false return false; // default for basic spindle is false
} }
void Spindle::spindle_sync(uint8_t state, uint32_t rpm) { void Spindle::sync(uint8_t state, uint32_t rpm) {
if (sys.state == STATE_CHECK_MODE) if (sys.state == STATE_CHECK_MODE)
return; return;
protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed. protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed.

View File

@@ -64,7 +64,7 @@ namespace Spindles {
virtual void stop() = 0; virtual void stop() = 0;
virtual void config_message() = 0; virtual void config_message() = 0;
virtual bool isRateAdjusted(); virtual bool isRateAdjusted();
virtual void spindle_sync(uint8_t state, uint32_t rpm); virtual void sync(uint8_t state, uint32_t rpm);
virtual ~Spindle() {} virtual ~Spindle() {}
@@ -72,7 +72,7 @@ namespace Spindles {
bool use_delays; // will SpinUp and SpinDown delays be used. bool use_delays; // will SpinUp and SpinDown delays be used.
uint8_t _current_state; uint8_t _current_state;
static void spindle_select(); static void select();
}; };
} }

View File

@@ -1122,9 +1122,9 @@ uint8_t gc_execute_line(char* line, uint8_t client) {
if (gc_state.modal.spindle != SPINDLE_DISABLE) { if (gc_state.modal.spindle != SPINDLE_DISABLE) {
if (bit_isfalse(gc_parser_flags, GC_PARSER_LASER_ISMOTION)) { if (bit_isfalse(gc_parser_flags, GC_PARSER_LASER_ISMOTION)) {
if (bit_istrue(gc_parser_flags, GC_PARSER_LASER_DISABLE)) if (bit_istrue(gc_parser_flags, GC_PARSER_LASER_DISABLE))
spindle->spindle_sync(gc_state.modal.spindle, 0); spindle->sync(gc_state.modal.spindle, 0);
else else
spindle->spindle_sync(gc_state.modal.spindle, (uint32_t)gc_block.values.s); spindle->sync(gc_state.modal.spindle, (uint32_t)gc_block.values.s);
} }
} }
gc_state.spindle_speed = gc_block.values.s; // Update spindle speed state. gc_state.spindle_speed = gc_block.values.s; // Update spindle speed state.
@@ -1146,7 +1146,7 @@ uint8_t gc_execute_line(char* line, uint8_t client) {
// Update spindle control and apply spindle speed when enabling it in this block. // Update spindle control and apply spindle speed when enabling it in this block.
// NOTE: All spindle state changes are synced, even in laser mode. Also, pl_data, // NOTE: All spindle state changes are synced, even in laser mode. Also, pl_data,
// rather than gc_state, is used to manage laser state for non-laser motions. // rather than gc_state, is used to manage laser state for non-laser motions.
spindle->spindle_sync(gc_block.modal.spindle, (uint32_t)pl_data->spindle_speed); spindle->sync(gc_block.modal.spindle, (uint32_t)pl_data->spindle_speed);
gc_state.modal.spindle = gc_block.modal.spindle; gc_state.modal.spindle = gc_block.modal.spindle;
} }
pl_data->condition |= gc_state.modal.spindle; // Set condition flag for planner use. pl_data->condition |= gc_state.modal.spindle; // Set condition flag for planner use.