Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature esc support #115

Open
wants to merge 5 commits into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions grbl/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@
//#define INVERT_MAX_LIMIT_PIN_MASK ((1<<X_AXIS) | (1<<Y_AXIS) | (1<<Z_AXIS))
#endif

// Enable spindle control using servo signal. Timer generate 50 Hz PWM signal, positive pulse width 1ms
// for stopped spindle and 2 ms for full speed of servo. It's suitable for ESC using.
// #define ENABLE_ESC_SPINDLE // Default disabled. Uncomment to enable.

// Inverts the spindle enable pin from low-disabled/high-enabled to low-enabled/high-disabled. Useful
// for some pre-built electronic boards.
// #define INVERT_SPINDLE_ENABLE_PIN // Default disabled. Uncomment to enable.
Expand Down
20 changes: 17 additions & 3 deletions grbl/cpu_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@

// Advanced Configuration Below You should not need to touch these variables
// Set Timer up to use TIMER4B which is attached to Digital Pin 7
#define SPINDLE_PWM_MAX_VALUE 1024.0 // Translates to about 1.9 kHz PWM frequency at 1/8 prescaler
#ifdef ENABLE_ESC_SPINDLE
#define SPINDLE_PWM_MIN_VALUE 2000 // 1 ms
#define SPINDLE_PWM_MAX_VALUE 4000 // 2 ms
#else
#define SPINDLE_PWM_MAX_VALUE 1024.0 // Translates to about 1.9 kHz PWM frequency at 1/8 prescaler
#endif
#ifndef SPINDLE_PWM_MIN_VALUE
#define SPINDLE_PWM_MIN_VALUE 1 // Must be greater than zero.
#endif
#define SPINDLE_PWM_OFF_VALUE 0

#ifdef ENABLE_ESC_SPINDLE
#define SPINDLE_PWM_OFF_VALUE 2000
#else
#define SPINDLE_PWM_OFF_VALUE 0
#endif
#define SPINDLE_PWM_RANGE (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE)
#define SPINDLE_TCCRA_REGISTER TCCR4A
#define SPINDLE_TCCRB_REGISTER TCCR4B
Expand All @@ -123,7 +133,11 @@
#define SPINDLE_TCCRA_INIT_MASK ((1<<WGM40) | (1<<WGM41))
#define SPINDLE_TCCRB_INIT_MASK ((1<<WGM42) | (1<<WGM43) | (1<<CS41))
#define SPINDLE_OCRA_REGISTER OCR4A // 16-bit Fast PWM mode requires top reset value stored here.
#define SPINDLE_OCRA_TOP_VALUE 0x0400 // PWM counter reset value. Should be the same as PWM_MAX_VALUE in hex.
#ifdef ENABLE_ESC_SPINDLE
#define SPINDLE_OCRA_TOP_VALUE 0x9C40 // PWM counter reset value for ESC. Should be the same as PWM_MAX_VALUE in hex.
#else
#define SPINDLE_OCRA_TOP_VALUE 0x0400 // PWM counter reset value. Should be the same as PWM_MAX_VALUE in hex.
#endif

// Define spindle output pins.
#define SPINDLE_PWM_DDR DDRH
Expand Down
9 changes: 8 additions & 1 deletion grbl/spindle_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ uint8_t spindle_get_state()
// Called by spindle_init(), spindle_set_speed(), spindle_set_state(), and mc_reset().
void spindle_stop()
{
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
#ifdef ENABLE_ESC_SPINDLE
SPINDLE_OCR_REGISTER = SPINDLE_PWM_OFF_VALUE; // Set off value of PWM.
SPINDLE_TCCRA_REGISTER |= (1 << SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
#else
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
#endif
#ifdef INVERT_SPINDLE_ENABLE_PIN
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high
#else
Expand All @@ -84,6 +89,8 @@ void spindle_set_speed(uint16_t pwm_value)
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
#endif
}
#elif defined(ENABLE_ESC_SPINDLE)
SPINDLE_TCCRA_REGISTER |= (1 << SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
#else
if (pwm_value == SPINDLE_PWM_OFF_VALUE) {
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
Expand Down