Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | spindle_control.c - spindle control methods |
| 3 | Part of Grbl |
| 4 | |
| 5 | Copyright (c) 2012-2017 Sungeun K. Jeon for Gnea Research LLC |
| 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud |
| 7 | |
| 8 | Grbl is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | Grbl is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include "grbl.h" |
| 23 | |
| 24 | |
| 25 | #ifdef VARIABLE_SPINDLE |
| 26 | static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions. |
| 27 | #endif |
| 28 | |
| 29 | |
| 30 | void spindle_init() |
| 31 | { |
| 32 | #ifdef VARIABLE_SPINDLE |
| 33 | // Configure variable spindle PWM and enable pin, if requried. On the Uno, PWM and enable are |
| 34 | // combined unless configured otherwise. |
| 35 | SPINDLE_PWM_DDR |= (1<<SPINDLE_PWM_BIT); // Configure as PWM output pin. |
| 36 | SPINDLE_TCCRA_REGISTER = SPINDLE_TCCRA_INIT_MASK; // Configure PWM output compare timer |
| 37 | SPINDLE_TCCRB_REGISTER = SPINDLE_TCCRB_INIT_MASK; |
| 38 | #ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN |
| 39 | SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin. |
| 40 | #else |
| 41 | #ifndef ENABLE_DUAL_AXIS |
| 42 | SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin. |
| 43 | #endif |
| 44 | #endif |
| 45 | pwm_gradient = SPINDLE_PWM_RANGE/(settings.rpm_max-settings.rpm_min); |
| 46 | #else |
| 47 | SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin. |
| 48 | #ifndef ENABLE_DUAL_AXIS |
| 49 | SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin. |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | spindle_stop(); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | uint8_t spindle_get_state() |
| 58 | { |
| 59 | #ifdef VARIABLE_SPINDLE |
| 60 | #ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN |
| 61 | // No spindle direction output pin. |
| 62 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 63 | if (bit_isfalse(SPINDLE_ENABLE_PORT,(1<<SPINDLE_ENABLE_BIT))) { return(SPINDLE_STATE_CW); } |
| 64 | #else |
| 65 | if (bit_istrue(SPINDLE_ENABLE_PORT,(1<<SPINDLE_ENABLE_BIT))) { return(SPINDLE_STATE_CW); } |
| 66 | #endif |
| 67 | #else |
| 68 | if (SPINDLE_TCCRA_REGISTER & (1<<SPINDLE_COMB_BIT)) { // Check if PWM is enabled. |
| 69 | #ifdef ENABLE_DUAL_AXIS |
| 70 | return(SPINDLE_STATE_CW); |
| 71 | #else |
| 72 | if (SPINDLE_DIRECTION_PORT & (1<<SPINDLE_DIRECTION_BIT)) { return(SPINDLE_STATE_CCW); } |
| 73 | else { return(SPINDLE_STATE_CW); } |
| 74 | #endif |
| 75 | } |
| 76 | #endif |
| 77 | #else |
| 78 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 79 | if (bit_isfalse(SPINDLE_ENABLE_PORT,(1<<SPINDLE_ENABLE_BIT))) { |
| 80 | #else |
| 81 | if (bit_istrue(SPINDLE_ENABLE_PORT,(1<<SPINDLE_ENABLE_BIT))) { |
| 82 | #endif |
| 83 | #ifdef ENABLE_DUAL_AXIS |
| 84 | return(SPINDLE_STATE_CW); |
| 85 | #else |
| 86 | if (SPINDLE_DIRECTION_PORT & (1<<SPINDLE_DIRECTION_BIT)) { return(SPINDLE_STATE_CCW); } |
| 87 | else { return(SPINDLE_STATE_CW); } |
| 88 | #endif |
| 89 | } |
| 90 | #endif |
| 91 | return(SPINDLE_STATE_DISABLE); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | // Disables the spindle and sets PWM output to zero when PWM variable spindle speed is enabled. |
| 96 | // Called by various main program and ISR routines. Keep routine small, fast, and efficient. |
| 97 | // Called by spindle_init(), spindle_set_speed(), spindle_set_state(), and mc_reset(). |
| 98 | void spindle_stop() |
| 99 | { |
| 100 | #ifdef VARIABLE_SPINDLE |
| 101 | SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero. |
| 102 | #ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN |
| 103 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 104 | SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high |
| 105 | #else |
| 106 | SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low |
| 107 | #endif |
| 108 | #endif |
| 109 | #else |
| 110 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 111 | SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high |
| 112 | #else |
| 113 | SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); // Set pin to low |
| 114 | #endif |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | |
| 119 | #ifdef VARIABLE_SPINDLE |
| 120 | // Sets spindle speed PWM output and enable pin, if configured. Called by spindle_set_state() |
| 121 | // and stepper ISR. Keep routine small and efficient. |
| 122 | void spindle_set_speed(uint8_t pwm_value) |
| 123 | { |
| 124 | SPINDLE_OCR_REGISTER = pwm_value; // Set PWM output level. |
| 125 | #ifdef SPINDLE_ENABLE_OFF_WITH_ZERO_SPEED |
| 126 | if (pwm_value == SPINDLE_PWM_OFF_VALUE) { |
| 127 | spindle_stop(); |
| 128 | } else { |
| 129 | SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled. |
| 130 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 131 | SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); |
| 132 | #else |
| 133 | SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); |
| 134 | #endif |
| 135 | } |
| 136 | #else |
| 137 | if (pwm_value == SPINDLE_PWM_OFF_VALUE) { |
| 138 | SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero. |
| 139 | } else { |
| 140 | SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled. |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | |
| 146 | #ifdef ENABLE_PIECEWISE_LINEAR_SPINDLE |
| 147 | |
| 148 | // Called by spindle_set_state() and step segment generator. Keep routine small and efficient. |
| 149 | uint8_t spindle_compute_pwm_value(float rpm) // 328p PWM register is 8-bit. |
| 150 | { |
| 151 | uint8_t pwm_value; |
| 152 | rpm *= (0.010*sys.spindle_speed_ovr); // Scale by spindle speed override value. |
| 153 | // Calculate PWM register value based on rpm max/min settings and programmed rpm. |
| 154 | if ((settings.rpm_min >= settings.rpm_max) || (rpm >= RPM_MAX)) { |
| 155 | rpm = RPM_MAX; |
| 156 | pwm_value = SPINDLE_PWM_MAX_VALUE; |
| 157 | } else if (rpm <= RPM_MIN) { |
| 158 | if (rpm == 0.0) { // S0 disables spindle |
| 159 | pwm_value = SPINDLE_PWM_OFF_VALUE; |
| 160 | } else { |
| 161 | rpm = RPM_MIN; |
| 162 | pwm_value = SPINDLE_PWM_MIN_VALUE; |
| 163 | } |
| 164 | } else { |
| 165 | // Compute intermediate PWM value with linear spindle speed model via piecewise linear fit model. |
| 166 | #if (N_PIECES > 3) |
| 167 | if (rpm > RPM_POINT34) { |
| 168 | pwm_value = floor(RPM_LINE_A4*rpm - RPM_LINE_B4); |
| 169 | } else |
| 170 | #endif |
| 171 | #if (N_PIECES > 2) |
| 172 | if (rpm > RPM_POINT23) { |
| 173 | pwm_value = floor(RPM_LINE_A3*rpm - RPM_LINE_B3); |
| 174 | } else |
| 175 | #endif |
| 176 | #if (N_PIECES > 1) |
| 177 | if (rpm > RPM_POINT12) { |
| 178 | pwm_value = floor(RPM_LINE_A2*rpm - RPM_LINE_B2); |
| 179 | } else |
| 180 | #endif |
| 181 | { |
| 182 | pwm_value = floor(RPM_LINE_A1*rpm - RPM_LINE_B1); |
| 183 | } |
| 184 | } |
| 185 | sys.spindle_speed = rpm; |
| 186 | return(pwm_value); |
| 187 | } |
| 188 | |
| 189 | #else |
| 190 | |
| 191 | // Called by spindle_set_state() and step segment generator. Keep routine small and efficient. |
| 192 | uint8_t spindle_compute_pwm_value(float rpm) // 328p PWM register is 8-bit. |
| 193 | { |
| 194 | uint8_t pwm_value; |
| 195 | rpm *= (0.010*sys.spindle_speed_ovr); // Scale by spindle speed override value. |
| 196 | // Calculate PWM register value based on rpm max/min settings and programmed rpm. |
| 197 | if ((settings.rpm_min >= settings.rpm_max) || (rpm >= settings.rpm_max)) { |
| 198 | // No PWM range possible. Set simple on/off spindle control pin state. |
| 199 | sys.spindle_speed = settings.rpm_max; |
| 200 | pwm_value = SPINDLE_PWM_MAX_VALUE; |
| 201 | } else if (rpm <= settings.rpm_min) { |
| 202 | if (rpm == 0.0) { // S0 disables spindle |
| 203 | sys.spindle_speed = 0.0; |
| 204 | pwm_value = SPINDLE_PWM_OFF_VALUE; |
| 205 | } else { // Set minimum PWM output |
| 206 | sys.spindle_speed = settings.rpm_min; |
| 207 | pwm_value = SPINDLE_PWM_MIN_VALUE; |
| 208 | } |
| 209 | } else { |
| 210 | // Compute intermediate PWM value with linear spindle speed model. |
| 211 | // NOTE: A nonlinear model could be installed here, if required, but keep it VERY light-weight. |
| 212 | sys.spindle_speed = rpm; |
| 213 | pwm_value = floor((rpm-settings.rpm_min)*pwm_gradient) + SPINDLE_PWM_MIN_VALUE; |
| 214 | } |
| 215 | return(pwm_value); |
| 216 | } |
| 217 | |
| 218 | #endif |
| 219 | #endif |
| 220 | |
| 221 | |
| 222 | // Immediately sets spindle running state with direction and spindle rpm via PWM, if enabled. |
| 223 | // Called by g-code parser spindle_sync(), parking retract and restore, g-code program end, |
| 224 | // sleep, and spindle stop override. |
| 225 | #ifdef VARIABLE_SPINDLE |
| 226 | void spindle_set_state(uint8_t state, float rpm) |
| 227 | #else |
| 228 | void _spindle_set_state(uint8_t state) |
| 229 | #endif |
| 230 | { |
| 231 | if (sys.abort) { return; } // Block during abort. |
| 232 | |
| 233 | if (state == SPINDLE_DISABLE) { // Halt or set spindle direction and rpm. |
| 234 | |
| 235 | #ifdef VARIABLE_SPINDLE |
| 236 | sys.spindle_speed = 0.0; |
| 237 | #endif |
| 238 | spindle_stop(); |
| 239 | |
| 240 | } else { |
| 241 | |
| 242 | #if !defined(USE_SPINDLE_DIR_AS_ENABLE_PIN) && !defined(ENABLE_DUAL_AXIS) |
| 243 | if (state == SPINDLE_ENABLE_CW) { |
| 244 | SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT); |
| 245 | } else { |
| 246 | SPINDLE_DIRECTION_PORT |= (1<<SPINDLE_DIRECTION_BIT); |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | #ifdef VARIABLE_SPINDLE |
| 251 | // NOTE: Assumes all calls to this function is when Grbl is not moving or must remain off. |
| 252 | if (settings.flags & BITFLAG_LASER_MODE) { |
| 253 | if (state == SPINDLE_ENABLE_CCW) { rpm = 0.0; } // TODO: May need to be rpm_min*(100/MAX_SPINDLE_SPEED_OVERRIDE); |
| 254 | } |
| 255 | spindle_set_speed(spindle_compute_pwm_value(rpm)); |
| 256 | #endif |
| 257 | #if (defined(USE_SPINDLE_DIR_AS_ENABLE_PIN) && \ |
| 258 | !defined(SPINDLE_ENABLE_OFF_WITH_ZERO_SPEED)) || !defined(VARIABLE_SPINDLE) |
| 259 | // NOTE: Without variable spindle, the enable bit should just turn on or off, regardless |
| 260 | // if the spindle speed value is zero, as its ignored anyhow. |
| 261 | #ifdef INVERT_SPINDLE_ENABLE_PIN |
| 262 | SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT); |
| 263 | #else |
| 264 | SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); |
| 265 | #endif |
| 266 | #endif |
| 267 | |
| 268 | } |
| 269 | |
| 270 | sys.report_ovr_counter = 0; // Set to report change immediately |
| 271 | } |
| 272 | |
| 273 | |
| 274 | // G-code parser entry-point for setting spindle state. Forces a planner buffer sync and bails |
| 275 | // if an abort or check-mode is active. |
| 276 | #ifdef VARIABLE_SPINDLE |
| 277 | void spindle_sync(uint8_t state, float rpm) |
| 278 | { |
| 279 | if (sys.state == STATE_CHECK_MODE) { return; } |
| 280 | protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed. |
| 281 | spindle_set_state(state,rpm); |
| 282 | } |
| 283 | #else |
| 284 | void _spindle_sync(uint8_t state) |
| 285 | { |
| 286 | if (sys.state == STATE_CHECK_MODE) { return; } |
| 287 | protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed. |
| 288 | _spindle_set_state(state); |
| 289 | } |
| 290 | #endif |