Merge pull request #318 from EliteEng/dev

PWM Spindle Control and Invert Spindle & Coolant Pins
This commit is contained in:
Sonny Jeon 2014-01-02 16:56:02 -08:00
commit e7cd94e2bc
6 changed files with 198 additions and 48 deletions

View File

@ -39,10 +39,17 @@ void coolant_init()
void coolant_stop() void coolant_stop()
{ {
#ifdef INVERT_COOLANT
#ifdef ENABLE_M7
COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT);
#endif
COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT);
#else
#ifdef ENABLE_M7 #ifdef ENABLE_M7
COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT);
#endif #endif
COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT);
#endif
} }
@ -52,10 +59,18 @@ void coolant_run(uint8_t mode)
{ {
plan_synchronize(); // Ensure coolant turns on when specified in program. plan_synchronize(); // Ensure coolant turns on when specified in program.
if (mode == COOLANT_FLOOD_ENABLE) { if (mode == COOLANT_FLOOD_ENABLE) {
#ifdef INVERT_COOLANT
COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT);
#else
COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT);
#endif
#ifdef ENABLE_M7 #ifdef ENABLE_M7
} else if (mode == COOLANT_MIST_ENABLE) { } else if (mode == COOLANT_MIST_ENABLE) {
#ifdef INVERT_COOLANT
COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT);
#else
COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT);
#endif
#endif #endif
} else { } else {
coolant_stop(); coolant_stop();

128
cpu_map.h
View File

@ -34,6 +34,9 @@
#define SERIAL_RX USART_RX_vect #define SERIAL_RX USART_RX_vect
#define SERIAL_UDRE USART_UDRE_vect #define SERIAL_UDRE USART_UDRE_vect
// Start of PWM & Stepper Enabled Spindle
// #define VARIABLE_SPINDLE // comment this out to disable PWM & Stepper on the spindle
// NOTE: All step bit and direction pins must be on the same port. // NOTE: All step bit and direction pins must be on the same port.
#define STEPPING_DDR DDRD #define STEPPING_DDR DDRD
#define STEPPING_PORT PORTD #define STEPPING_PORT PORTD
@ -54,6 +57,20 @@
#define STEPPERS_DISABLE_BIT 0 // Uno Digital Pin 8 #define STEPPERS_DISABLE_BIT 0 // Uno Digital Pin 8
#define STEPPERS_DISABLE_MASK (1<<STEPPERS_DISABLE_BIT) #define STEPPERS_DISABLE_MASK (1<<STEPPERS_DISABLE_BIT)
#ifdef VARIABLE_SPINDLE // Z LImit has been disabled to make room for the Spindle PWM
// NOTE: All limit bit pins must be on the same port
#define LIMIT_DDR DDRB
#define LIMIT_PIN PINB
#define LIMIT_PORT PORTB
#define X_LIMIT_BIT 1 // Uno Digital Pin 9
#define Y_LIMIT_BIT 2 // Uno Digital Pin 9
#define Z_LIMIT_BIT 2 // Uno Digital Pin 11
#define LIMIT_INT PCIE0 // Pin change interrupt enable pin
#define LIMIT_INT_vect PCINT0_vect
#define LIMIT_PCMSK PCMSK0 // Pin change interrupt register
#define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)) // All limit bits
#else
// NOTE: All limit bit pins must be on the same port // NOTE: All limit bit pins must be on the same port
#define LIMIT_DDR DDRB #define LIMIT_DDR DDRB
#define LIMIT_PIN PINB #define LIMIT_PIN PINB
@ -65,6 +82,12 @@
#define LIMIT_INT_vect PCINT0_vect #define LIMIT_INT_vect PCINT0_vect
#define LIMIT_PCMSK PCMSK0 // Pin change interrupt register #define LIMIT_PCMSK PCMSK0 // Pin change interrupt register
#define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT)) // All limit bits #define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT)) // All limit bits
#endif
// WARNING if INVERT_SPINDLE is defined the pin will be LOW(Active) on reset of controller until spindle_init() is called.
// this should only be a very short period of time but will energise the spindle.
// #define INVERT_SPINDLE // This will INVERT the Spindle LOW=Active
// END WARNING
#define SPINDLE_ENABLE_DDR DDRB #define SPINDLE_ENABLE_DDR DDRB
#define SPINDLE_ENABLE_PORT PORTB #define SPINDLE_ENABLE_PORT PORTB
@ -74,6 +97,11 @@
#define SPINDLE_DIRECTION_PORT PORTB #define SPINDLE_DIRECTION_PORT PORTB
#define SPINDLE_DIRECTION_BIT 5 // Uno Digital Pin 13 (NOTE: D13 can't be pulled-high input due to LED.) #define SPINDLE_DIRECTION_BIT 5 // Uno Digital Pin 13 (NOTE: D13 can't be pulled-high input due to LED.)
// WARNING if INVERT_COOLANT is defined the pin will be LOW(Active) on reset of controller until coolant_init() is called.
// this should only be a very short period of time but will energise the coolant pump.
// #define INVERT_COOLANT // This will INVERT the Coolant LOW=Active
// END WARNING
#define COOLANT_FLOOD_DDR DDRC #define COOLANT_FLOOD_DDR DDRC
#define COOLANT_FLOOD_PORT PORTC #define COOLANT_FLOOD_PORT PORTC
#define COOLANT_FLOOD_BIT 3 // Uno Analog Pin 3 #define COOLANT_FLOOD_BIT 3 // Uno Analog Pin 3
@ -99,6 +127,32 @@
#define PINOUT_PCMSK PCMSK1 // Pin change interrupt register #define PINOUT_PCMSK PCMSK1 // Pin change interrupt register
#define PINOUT_MASK ((1<<PIN_RESET)|(1<<PIN_FEED_HOLD)|(1<<PIN_CYCLE_START)) #define PINOUT_MASK ((1<<PIN_RESET)|(1<<PIN_FEED_HOLD)|(1<<PIN_CYCLE_START))
#ifdef VARIABLE_SPINDLE
#define SPINDLE_IS_PWM // Uncomment this if your Spindle uses PWM ie. DC Motor
#ifdef SPINDLE_IS_PWM
#define SPINDLE_MAX_RPM 255 // Max RPM of your spindle - This value is equal to 100% Duty Cycle on the PWM
#endif
// Advanced Configuration Below You should not need to touch these variables
//Set Timer up to use TIMER4 OCR4B which is attached to Digital Pin 7
#define TCCRA_REGISTER TCCR2A
#define TCCRB_REGISTER TCCR2B
#define OCR_REGISTER OCR2A
#define COMB_BIT COM2A1
#define WAVE0_REGISTER WGM20
#define WAVE1_REGISTER WGM21
#define WAVE2_REGISTER WGM22
#define WAVE3_REGISTER WGM23
#define SPINDLE_PWM_DDR DDRB
#define SPINDLE_PWM_PORT PORTB
#define SPINDLE_PWM_BIT 3 // UNO Digital Pin 11
#endif // End of VARIABLE_SPINDLE
#endif #endif
@ -109,10 +163,10 @@
#define SERIAL_UDRE USART0_UDRE_vect #define SERIAL_UDRE USART0_UDRE_vect
// Increase Buffers to make use of extra SRAM // Increase Buffers to make use of extra SRAM
#define RX_BUFFER_SIZE 256 //#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 128 //#define TX_BUFFER_SIZE 128
#define BLOCK_BUFFER_SIZE 36 //#define BLOCK_BUFFER_SIZE 36
#define LINE_BUFFER_SIZE 100 //#define LINE_BUFFER_SIZE 100
// NOTE: All step pins must be on the same port. // NOTE: All step pins must be on the same port.
#define STEPPING_DDR DDRA #define STEPPING_DDR DDRA
@ -121,7 +175,7 @@
#define X_STEP_BIT 2 // MEGA2560 Digital Pin 24 #define X_STEP_BIT 2 // MEGA2560 Digital Pin 24
#define Y_STEP_BIT 3 // MEGA2560 Digital Pin 25 #define Y_STEP_BIT 3 // MEGA2560 Digital Pin 25
#define Z_STEP_BIT 4 // MEGA2560 Digital Pin 26 #define Z_STEP_BIT 4 // MEGA2560 Digital Pin 26
#define STEPPING_MASK ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT)) // All step bits #define STEP_MASK ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT)) // All step bits
// NOTE: All direction pins must be on the same port. // NOTE: All direction pins must be on the same port.
#define DIRECTION_DDR DDRA #define DIRECTION_DDR DDRA
@ -149,23 +203,33 @@
#define LIMIT_PCMSK PCMSK0 // Pin change interrupt register #define LIMIT_PCMSK PCMSK0 // Pin change interrupt register
#define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT)) // All limit bits #define LIMIT_MASK ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT)) // All limit bits
#define SPINDLE_ENABLE_DDR DDRC // WARNING if INVERT_SPINDLE is defined the pin will be LOW(Active) on reset of controller until spindle_init() is called.
#define SPINDLE_ENABLE_PORT PORTC // this should only be a very short period of time but will energise the spindle.
#define SPINDLE_ENABLE_BIT 2 // MEGA2560 Digital Pin 35 // #define INVERT_SPINDLE // This will INVERT the Spindle LOW=Active
// END WARNING
#define SPINDLE_DIRECTION_DDR DDRC #define SPINDLE_ENABLE_DDR DDRH
#define SPINDLE_DIRECTION_PORT PORTC #define SPINDLE_ENABLE_PORT PORTH
#define SPINDLE_DIRECTION_BIT 1 // MEGA2560 Digital Pin 36 #define SPINDLE_ENABLE_BIT 3 // MEGA2560 Digital Pin 6
#define COOLANT_FLOOD_DDR DDRC #define SPINDLE_DIRECTION_DDR DDRE
#define COOLANT_FLOOD_PORT PORTC #define SPINDLE_DIRECTION_PORT PORTE
#define COOLANT_FLOOD_BIT 0 // MEGA2560 Digital Pin 37 #define SPINDLE_DIRECTION_BIT 3 // MEGA2560 Digital Pin 5
// WARNING if INVERT_COOLANT is defined the pin will be LOW(Active) on reset of controller until coolant_init() is called.
// this should only be a very short period of time but will energise the coolant pump.
// #define INVERT_COOLANT // This will INVERT the Coolant LOW=Active
// END WARNING
#define COOLANT_FLOOD_DDR DDRH
#define COOLANT_FLOOD_PORT PORTH
#define COOLANT_FLOOD_BIT 5 // MEGA2560 Digital Pin 8
// #define ENABLE_M7 // Mist coolant disabled by default. Uncomment to enable. // #define ENABLE_M7 // Mist coolant disabled by default. Uncomment to enable.
#ifdef ENABLE_M7 #ifdef ENABLE_M7
#define COOLANT_MIST_DDR DDRC #define COOLANT_MIST_DDR DDRH
#define COOLANT_MIST_PORT PORTC #define COOLANT_MIST_PORT PORTH
#define COOLANT_MIST_BIT 3 // MEGA2560 Digital Pin 34 #define COOLANT_MIST_BIT 6 // MEGA2560 Digital Pin 9
#endif #endif
// NOTE: All pinouts pins must be on the same port and cannot be on same port as limit pins. // NOTE: All pinouts pins must be on the same port and cannot be on same port as limit pins.
@ -180,11 +244,41 @@
#define PINOUT_PCMSK PCMSK2 // Pin change interrupt register #define PINOUT_PCMSK PCMSK2 // Pin change interrupt register
#define PINOUT_MASK ((1<<PIN_RESET)|(1<<PIN_FEED_HOLD)|(1<<PIN_CYCLE_START)) #define PINOUT_MASK ((1<<PIN_RESET)|(1<<PIN_FEED_HOLD)|(1<<PIN_CYCLE_START))
// Start of PWM & Stepper Enabled Spindle
// #define VARIABLE_SPINDLE // comment this out to disable PWM & Stepper on the spindle
#ifdef VARIABLE_SPINDLE
//#define SPINDLE_IS_STEPPER // Uncomment this if your Spindle is a Stepper Motor
#define SPINDLE_IS_PWM // Uncomment this if your Spindle uses PWM ie. DC Motor
#ifdef SPINDLE_IS_PWM
#define SPINDLE_MAX_RPM 255 // Max RPM of your spindle
#endif
// Advanced Configuration Below You should not need to touch these variables
//Set Timer up to use TIMER4 OCR4B which is attached to Digital Pin 7
#define TCCRA_REGISTER TCCR2A
#define TCCRB_REGISTER TCCR2B
#define OCR_REGISTER OCR2B
#define COMB_BIT COM2B1
#define WAVE0_REGISTER WGM20
#define WAVE1_REGISTER WGM21
#define WAVE2_REGISTER WGM22
#define WAVE3_REGISTER WGM23
#define SPINDLE_PWM_DDR DDRH
#define SPINDLE_PWM_PORT PORTH
#define SPINDLE_PWM_BIT 6 // MEGA2560 Digital Pin 9
#endif // End of VARIABLE_SPINDLE
#endif #endif
/* /*
#ifdef CPU_MAP_CUSTOM_PROC #ifdef CPU_MAP_CUSTOM_PROC
// For a custom pin map or different processor, copy and paste one of the default cpu map // For a custom pin map or different processor, copy and paste one of the default cpu map
// settings above and modify it to your needs. Then, make sure the defined name is also // settings above and modify it to your needs. Then, make sure the defined name is also
// changed in the config.h file. // changed in the config.h file.
#endif #endif

View File

@ -238,8 +238,7 @@ uint8_t gc_execute_line(char *line)
case 'R': gc.arc_radius = to_millimeters(value); break; case 'R': gc.arc_radius = to_millimeters(value); break;
case 'S': case 'S':
if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative
// TBD: Spindle speed not supported due to PWM issues, but may come back once resolved. gc.spindle_speed = value;
// gc.spindle_speed = value;
break; break;
case 'T': case 'T':
if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative if (value < 0) { FAIL(STATUS_INVALID_STATEMENT); } // Cannot be negative
@ -266,7 +265,7 @@ uint8_t gc_execute_line(char *line)
// ([M6]: Tool change should be executed here.) // ([M6]: Tool change should be executed here.)
// [M3,M4,M5]: Update spindle state // [M3,M4,M5]: Update spindle state
spindle_run(gc.spindle_direction); spindle_run(gc.spindle_direction, gc.spindle_speed);
// [*M7,M8,M9]: Update coolant state // [*M7,M8,M9]: Update coolant state
coolant_run(gc.coolant_mode); coolant_run(gc.coolant_mode);

View File

@ -75,7 +75,7 @@ typedef struct {
float feed_rate; // Millimeters/min float feed_rate; // Millimeters/min
float position[N_AXIS]; // Where the interpreter considers the tool to be at this point in the code float position[N_AXIS]; // Where the interpreter considers the tool to be at this point in the code
uint8_t tool; uint8_t tool;
// uint16_t spindle_speed; // RPM/100 uint16_t spindle_speed; // RPM
uint8_t plane_axis_0, uint8_t plane_axis_0,
plane_axis_1, plane_axis_1,
plane_axis_2; // The axes of the selected plane plane_axis_2; // The axes of the selected plane

View File

@ -24,34 +24,74 @@
#include "planner.h" #include "planner.h"
static uint8_t current_direction; static uint8_t current_direction;
static uint16_t current_rpm;
static uint8_t current_pwm;
void spindle_init() void spindle_init()
{ {
current_direction = 0; current_direction = 0;
SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT);
SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT);
#ifdef VARIABLE_SPINDLE
SPINDLE_PWM_DDR |= (1<<SPINDLE_PWM_BIT);
#endif
spindle_stop(); spindle_stop();
} }
void spindle_stop() void spindle_stop()
{ {
#ifdef INVERT_SPINDLE
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
#else
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
#endif
#ifdef VARIABLE_SPINDLE
TCCRA_REGISTER &= ~(1<<COMB_BIT);
#endif
} }
void spindle_run(int8_t direction) //, uint16_t rpm) void spindle_run(int8_t direction, uint16_t rpm) {
{ if ((direction != current_direction) || (rpm != current_rpm)) {
if (direction != current_direction) {
plan_synchronize(); plan_synchronize();
if (direction) { if (direction) {
if (direction > 0) { if (direction > 0) {
SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT); SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT);
} else { } else {
SPINDLE_DIRECTION_PORT |= 1<<SPINDLE_DIRECTION_BIT; SPINDLE_DIRECTION_PORT |= (1<<SPINDLE_DIRECTION_BIT);
} }
SPINDLE_ENABLE_PORT |= 1<<SPINDLE_ENABLE_BIT;
#ifdef VARIABLE_SPINDLE
#ifdef SPINDLE_IS_PWM
TCCRA_REGISTER = (1<<COMB_BIT) | (1<<WAVE1_REGISTER) | (1<<WAVE0_REGISTER);
TCCRB_REGISTER = (TCCRB_REGISTER & 0b11111000) | 0x02; // set to 1/8 Prescaler
current_pwm = floor((((float) rpm / (float) SPINDLE_MAX_RPM ) * 255.0) + 0.5);
OCR_REGISTER = current_pwm;
#endif
#endif
#ifdef INVERT_SPINDLE
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
#else
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
#endif
} else { } else {
spindle_stop(); spindle_stop();
} }
current_direction = direction; current_direction = direction;
current_rpm = rpm;
} }
} }
uint8_t spindle_pwm()
{
return current_pwm;
}
void spindle_pwm_update(uint8_t pwm)
{
OCR_REGISTER = pwm;
}

View File

@ -25,7 +25,9 @@
#include <avr/io.h> #include <avr/io.h>
void spindle_init(); void spindle_init();
void spindle_run(int8_t direction); //, uint16_t rpm); void spindle_run(int8_t direction, uint16_t rpm);
void spindle_stop(); void spindle_stop();
uint8_t spindle_pwm();
void spindle_pwm_update(uint8_t pwm);
#endif #endif