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

add servo motor support using cprezzi and DWiskow approach #811

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions grbl/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@
// NOTE: Requires USE_SPINDLE_DIR_AS_ENABLE_PIN to be enabled.
// #define SPINDLE_ENABLE_OFF_WITH_ZERO_SPEED // Default disabled. Uncomment to enable.

// If you use a servo instead of a spindle (like on EggBot or pen plotter), you need to uncomment this option.
// This will set the PWM frequency to 61Hz and limit the PWM range to 0.5 - 2.5ms, as used by most servos.
// #define SPINDLE_IS_SERVO // Default disabled. Uncomment to enable.

#define SERVO_SHORT 7 // set min pulse duration to (7 = 0.5ms, 15 = 1.03ms, 20=1.40ms)
#define SERVO_LONG 38 // set max pulse duration (38 = 2.49ms, 31 = 2.05ms)
#define SERVO_RANGE (SERVO_LONG-SERVO_SHORT)
// #define SERVO_INVERT 1 // Uncomment to invert servo direction


// With this enabled, Grbl sends back an echo of the line it has received, which has been pre-parsed (spaces
// removed, capitalized letters, no comments) and is to be immediately executed by Grbl. Echoes will not be
// sent upon a line buffer overflow, but should for all normal lines sent to Grbl. For example, if a user
Expand Down
15 changes: 10 additions & 5 deletions grbl/cpu_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,16 @@

// Prescaled, 8-bit Fast PWM mode.
#define SPINDLE_TCCRA_INIT_MASK ((1<<WGM20) | (1<<WGM21)) // Configures fast PWM mode.
// #define SPINDLE_TCCRB_INIT_MASK (1<<CS20) // Disable prescaler -> 62.5kHz
// #define SPINDLE_TCCRB_INIT_MASK (1<<CS21) // 1/8 prescaler -> 7.8kHz (Used in v0.9)
// #define SPINDLE_TCCRB_INIT_MASK ((1<<CS21) | (1<<CS20)) // 1/32 prescaler -> 1.96kHz
#define SPINDLE_TCCRB_INIT_MASK (1<<CS22) // 1/64 prescaler -> 0.98kHz (J-tech laser)


#ifdef SPINDLE_IS_SERVO
#define SPINDLE_TCCRB_INIT_MASK ((1<<CS22) | (1<<CS21) | (1<<CS20)) // 1/1024 prescaler -> 61Hz (for Servo)
#else
// #define SPINDLE_TCCRB_INIT_MASK (1<<CS20) // Disable prescaler -> 62.5kHz
// #define SPINDLE_TCCRB_INIT_MASK (1<<CS21) // 1/8 prescaler -> 7.8kHz (Used in v0.9)
// #define SPINDLE_TCCRB_INIT_MASK ((1<<CS21) | (1<<CS20)) // 1/32 prescaler -> 1.96kHz
#define SPINDLE_TCCRB_INIT_MASK (1<<CS22) // 1/64 prescaler -> 0.98kHz (J-tech laser)
#endif

// NOTE: On the 328p, these must be the same as the SPINDLE_ENABLE settings.
#define SPINDLE_PWM_DDR DDRB
#define SPINDLE_PWM_PORT PORTB
Expand Down
34 changes: 31 additions & 3 deletions grbl/spindle_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "grbl.h"


#ifdef VARIABLE_SPINDLE
static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions.
#endif
Expand Down Expand Up @@ -113,6 +112,16 @@ void spindle_stop()
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low
#endif
#endif
if (!(settings.flags & BITFLAG_LASER_MODE)) {
#ifdef SPINDLE_IS_SERVO
SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
#ifdef SERVO_INVERT
SPINDLE_OCR_REGISTER = SERVO_LONG;
#else
SPINDLE_OCR_REGISTER = SERVO_SHORT;
#endif
#endif
}
}


Expand All @@ -135,11 +144,19 @@ void spindle_stop()
}
#else
if (pwm_value == SPINDLE_PWM_OFF_VALUE) {
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
if (!(settings.flags & BITFLAG_LASER_MODE)) {
#ifndef SPINDLE_IS_SERVO
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
#else
spindle_stop();
#endif
} else {
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
}
} else {
SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
}
#endif
#endif
}


Expand Down Expand Up @@ -212,6 +229,17 @@ void spindle_stop()
sys.spindle_speed = rpm;
pwm_value = floor((rpm-settings.rpm_min)*pwm_gradient) + SPINDLE_PWM_MIN_VALUE;
}

if (!(settings.flags & BITFLAG_LASER_MODE)) {
#ifdef SPINDLE_IS_SERVO
#ifdef SERVO_INVERT
pwm_value = floor(SERVO_LONG - rpm*(SERVO_RANGE/(settings.rpm_max-settings.rpm_min)));
#else
pwm_value = floor(rpm*(SERVO_RANGE/(settings.rpm_max-settings.rpm_min))+SERVO_SHORT);
#endif
#endif
}

return(pwm_value);
}

Expand Down