- M5 now disables PWM completely in both standard and servo modes - Inverted servo mapping: S0=pen up (16 ticks), S1000=pen down (31 ticks) - Updated SERVO_MODE.md documentation to reflect new behavior - Changed config defaults: MIN_PULSE=16, MAX_PULSE=31
180 lines
7.0 KiB
C
180 lines
7.0 KiB
C
/*
|
|
spindle_control.c - spindle control methods
|
|
Part of Grbl
|
|
|
|
Copyright (c) 2012-2015 Sungeun K. Jeon
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
|
|
|
Grbl is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Grbl is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "grbl.h"
|
|
|
|
|
|
void spindle_init()
|
|
{
|
|
// Configure variable spindle PWM and enable pin, if requried. On the Uno, PWM and enable are
|
|
// combined unless configured otherwise.
|
|
#ifdef VARIABLE_SPINDLE
|
|
SPINDLE_PWM_DDR |= (1<<SPINDLE_PWM_BIT); // Configure as PWM output pin.
|
|
#if defined(CPU_MAP_ATMEGA2560) || defined(USE_SPINDLE_DIR_AS_ENABLE_PIN)
|
|
SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin.
|
|
#endif
|
|
// Configure no variable spindle and only enable pin.
|
|
#else
|
|
SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin.
|
|
#endif
|
|
|
|
#ifndef USE_SPINDLE_DIR_AS_ENABLE_PIN
|
|
SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin.
|
|
#endif
|
|
spindle_stop();
|
|
}
|
|
|
|
|
|
void spindle_stop()
|
|
{
|
|
// Disable PWM output (both servo and standard modes)
|
|
#ifdef VARIABLE_SPINDLE
|
|
TCCRA_REGISTER &= ~(1<<COMB_BIT); // Disable PWM. Output voltage is zero.
|
|
#if defined(CPU_MAP_ATMEGA2560) || defined(USE_SPINDLE_DIR_AS_ENABLE_PIN)
|
|
#ifdef INVERT_SPINDLE_ENABLE_PIN
|
|
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high
|
|
#else
|
|
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low
|
|
#endif
|
|
#endif
|
|
#else
|
|
#ifdef INVERT_SPINDLE_ENABLE_PIN
|
|
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high
|
|
#else
|
|
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
|
|
void spindle_set_state(uint8_t state, float rpm)
|
|
{
|
|
// Halt or set spindle direction and rpm.
|
|
if (state == SPINDLE_DISABLE) {
|
|
|
|
spindle_stop();
|
|
|
|
} else {
|
|
|
|
#ifndef USE_SPINDLE_DIR_AS_ENABLE_PIN
|
|
if (state == SPINDLE_ENABLE_CW) {
|
|
SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT);
|
|
} else {
|
|
SPINDLE_DIRECTION_PORT |= (1<<SPINDLE_DIRECTION_BIT);
|
|
}
|
|
#endif
|
|
|
|
#ifdef VARIABLE_SPINDLE
|
|
#ifdef CPU_MAP_ATMEGA2560
|
|
TCCRA_REGISTER = (1<<COMB_BIT) | (1<<WAVE1_REGISTER) | (1<<WAVE0_REGISTER);
|
|
TCCRB_REGISTER = (TCCRB_REGISTER & 0b11111000) | 0x02 | (1<<WAVE2_REGISTER) | (1<<WAVE3_REGISTER); // set to 1/8 Prescaler
|
|
OCR4A = 0xFFFF; // set the top 16bit value
|
|
uint16_t current_pwm;
|
|
#else
|
|
TCCRA_REGISTER = (1<<COMB_BIT) | (1<<WAVE1_REGISTER) | (1<<WAVE0_REGISTER);
|
|
#ifdef USE_SPINDLE_SERVO_MODE
|
|
// Servo mode: Set to 1/1024 prescaler for ~50Hz frequency (actually ~61Hz on 16MHz)
|
|
// Timer formula: Freq = F_CPU / (Prescaler * 256) = 16MHz / (1024 * 256) = 61.04Hz
|
|
// This provides the ~20ms period needed for servo control (50Hz ideal, 61Hz acceptable)
|
|
TCCRB_REGISTER = (TCCRB_REGISTER & 0b11111000) | 0x07; // CS22=1, CS21=1, CS20=1 = 1/1024 prescaler
|
|
#else
|
|
// Standard spindle mode: Set to 1/8 prescaler for ~7.8kHz frequency
|
|
TCCRB_REGISTER = (TCCRB_REGISTER & 0b11111000) | 0x02; // CS21=1 = 1/8 prescaler
|
|
#endif
|
|
uint8_t current_pwm;
|
|
#endif
|
|
|
|
#ifdef USE_SPINDLE_SERVO_MODE
|
|
// Servo mode: Allow rpm=0 as valid position (0 degrees), only stop on negative
|
|
if (rpm < 0.0) { spindle_stop(); }
|
|
else {
|
|
// Servo mode: Map RPM range to servo pulse width (16-31 ticks = ~1-2ms pulses)
|
|
// Each tick at 1/1024 prescaler = 64μs, so 16 ticks ≈ 1.024ms, 31 ticks ≈ 1.984ms
|
|
// INVERTED: S1000 = pen down (max pulse), S0 = pen up (min pulse)
|
|
#define SPINDLE_SERVO_RANGE (SPINDLE_SERVO_MAX_PULSE - SPINDLE_SERVO_MIN_PULSE)
|
|
#define SPINDLE_RPM_RANGE (SPINDLE_MAX_RPM-SPINDLE_MIN_RPM)
|
|
if ( rpm < SPINDLE_MIN_RPM ) { rpm = SPINDLE_MIN_RPM; }
|
|
if ( rpm > SPINDLE_MAX_RPM ) { rpm = SPINDLE_MAX_RPM; }
|
|
// Invert RPM mapping: S1000 → max pulse (pen down), S0 → min pulse (pen up)
|
|
rpm = SPINDLE_MAX_RPM - rpm;
|
|
rpm -= SPINDLE_MIN_RPM;
|
|
// Map inverted RPM to servo pulse range
|
|
current_pwm = floor( rpm * (SPINDLE_SERVO_RANGE / SPINDLE_RPM_RANGE) + SPINDLE_SERVO_MIN_PULSE + 0.5);
|
|
OCR_REGISTER = current_pwm; // Set PWM pin output
|
|
|
|
// On the Uno, spindle enable and PWM are shared, unless otherwise specified.
|
|
#if defined(CPU_MAP_ATMEGA2560) || defined(USE_SPINDLE_DIR_AS_ENABLE_PIN)
|
|
#ifdef INVERT_SPINDLE_ENABLE_PIN
|
|
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
|
|
#else
|
|
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
|
|
#endif
|
|
#endif
|
|
}
|
|
#else
|
|
// Standard spindle mode: Stop on zero or negative RPM
|
|
if (rpm <= 0.0) { spindle_stop(); }
|
|
else {
|
|
// Standard spindle mode: Map RPM range to full PWM range (0-255)
|
|
#define SPINDLE_RPM_RANGE (SPINDLE_MAX_RPM-SPINDLE_MIN_RPM)
|
|
if ( rpm < SPINDLE_MIN_RPM ) { rpm = 0; }
|
|
else {
|
|
rpm -= SPINDLE_MIN_RPM;
|
|
if ( rpm > SPINDLE_RPM_RANGE ) { rpm = SPINDLE_RPM_RANGE; } // Prevent integer overflow
|
|
}
|
|
current_pwm = floor( rpm*(PWM_MAX_VALUE/SPINDLE_RPM_RANGE) + 0.5);
|
|
#ifdef MINIMUM_SPINDLE_PWM
|
|
if (current_pwm < MINIMUM_SPINDLE_PWM) { current_pwm = MINIMUM_SPINDLE_PWM; }
|
|
#endif
|
|
OCR_REGISTER = current_pwm; // Set PWM pin output
|
|
|
|
// On the Uno, spindle enable and PWM are shared, unless otherwise specified.
|
|
#if defined(CPU_MAP_ATMEGA2560) || defined(USE_SPINDLE_DIR_AS_ENABLE_PIN)
|
|
#ifdef INVERT_SPINDLE_ENABLE_PIN
|
|
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
|
|
#else
|
|
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
|
|
#endif
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
#else
|
|
// NOTE: Without variable spindle, the enable bit should just turn on or off, regardless
|
|
// if the spindle speed value is zero, as its ignored anyhow.
|
|
#ifdef INVERT_SPINDLE_ENABLE_PIN
|
|
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
|
|
#else
|
|
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
|
|
#endif
|
|
#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void spindle_run(uint8_t state, float rpm)
|
|
{
|
|
if (sys.state == STATE_CHECK_MODE) { return; }
|
|
protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed.
|
|
spindle_set_state(state, rpm);
|
|
}
|