- Re-organized source code files into a ‘grbl’ directory to lessen one step in compiling Grbl through the Arduino IDE. - Added an ‘examples’ directory with an upload .INO sketch to further simplify compiling and uploading Grbl via the Arduino IDE. - Updated the Makefile with regard to the source code no longer being in the root directory. All files generated by compiling is placed in a separate ‘build’ directory to keep things tidy. The makefile should operate in the same way as it did before.
		
			
				
	
	
		
			117 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|   spindle_control.c - spindle control methods
 | |
|   Part of Grbl v0.9
 | |
| 
 | |
|   Copyright (c) 2012-2015 Sungeun K. Jeon
 | |
| 
 | |
|   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/>.
 | |
| */
 | |
| /* 
 | |
|   This file is based on work from Grbl v0.8, distributed under the 
 | |
|   terms of the MIT-license. See COPYING for more details.  
 | |
|     Copyright (c) 2009-2011 Simen Svale Skogsrud
 | |
|     Copyright (c) 2012 Sungeun K. Jeon
 | |
| */ 
 | |
| 
 | |
| #include "grbl.h"
 | |
| 
 | |
| 
 | |
| void spindle_init()
 | |
| {    
 | |
|   // On the Uno, spindle enable and PWM are shared. Other CPUs have seperate enable pin.
 | |
|   #ifdef VARIABLE_SPINDLE
 | |
|     SPINDLE_PWM_DDR |= (1<<SPINDLE_PWM_BIT); // Configure as PWM output pin.
 | |
|     #ifndef CPU_MAP_ATMEGA328P 
 | |
|       SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin.
 | |
|     #endif     
 | |
|   #else
 | |
|     SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin.
 | |
|   #endif
 | |
|   SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin.
 | |
|   spindle_stop();
 | |
| }
 | |
| 
 | |
| 
 | |
| void spindle_stop()
 | |
| {
 | |
|   // On the Uno, spindle enable and PWM are shared. Other CPUs have seperate enable pin.
 | |
|   #ifdef VARIABLE_SPINDLE
 | |
|     TCCRA_REGISTER &= ~(1<<COMB_BIT); // Disable PWM. Output voltage is zero.
 | |
|     #ifndef CPU_MAP_ATMEGA328P 
 | |
|       SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low.
 | |
|     #endif
 | |
|   #else
 | |
|     SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low.
 | |
|   #endif  
 | |
| }
 | |
| 
 | |
| 
 | |
| void spindle_set_state(uint8_t state, float rpm)
 | |
| {
 | |
|   // Halt or set spindle direction and rpm. 
 | |
|   if (state == SPINDLE_DISABLE) {
 | |
| 
 | |
|     spindle_stop();
 | |
| 
 | |
|   } else {
 | |
| 
 | |
|     if (state == SPINDLE_ENABLE_CW) {
 | |
|       SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT);
 | |
|     } else {
 | |
|       SPINDLE_DIRECTION_PORT |= (1<<SPINDLE_DIRECTION_BIT);
 | |
|     }
 | |
| 
 | |
|     #ifdef VARIABLE_SPINDLE
 | |
|       // TODO: Install the optional capability for frequency-based output for servos.
 | |
|       #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);
 | |
|         TCCRB_REGISTER = (TCCRB_REGISTER & 0b11111000) | 0x02; // set to 1/8 Prescaler
 | |
|         uint8_t current_pwm;
 | |
|       #endif
 | |
| 
 | |
|       #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
 | |
|     
 | |
|       #ifdef CPU_MAP_ATMEGA2560 // On the Uno, spindle enable and PWM are shared.
 | |
|         SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
 | |
|       #endif
 | |
|     #else   
 | |
|       SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
 | |
|     #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);
 | |
| }
 |