|
22 | 22 | #include "grbl.h" |
23 | 23 |
|
24 | 24 |
|
25 | | -#ifdef SPINDLE_MINIMUM_PWM |
26 | | - #define SPINDLE_PWM_MIN_VALUE SPINDLE_MINIMUM_PWM |
27 | | -#else |
28 | | - #define SPINDLE_PWM_MIN_VALUE 0.0 |
29 | | -#endif |
30 | | -#define SPINDLE_PWM_RANGE (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE) |
31 | | - |
32 | 25 | #ifdef VARIABLE_SPINDLE |
33 | 26 | static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions. |
34 | 27 | #endif |
@@ -142,24 +135,28 @@ void spindle_stop() |
142 | 135 | // Called by spindle_set_state() and step segment generator. Keep routine small and efficient. |
143 | 136 | uint8_t spindle_compute_pwm_value(float rpm) // 328p PWM register is 8-bit. |
144 | 137 | { |
| 138 | + uint8_t pwm_value; |
145 | 139 | rpm *= (0.01*sys.spindle_speed_ovr); // Scale by spindle speed override value. |
146 | 140 | // Calculate PWM register value based on rpm max/min settings and programmed rpm. |
147 | 141 | if ((settings.rpm_min >= settings.rpm_max) || (rpm >= settings.rpm_max)) { |
148 | 142 | // No PWM range possible. Set simple on/off spindle control pin state. |
149 | 143 | sys.spindle_speed = settings.rpm_max; |
150 | | - return(SPINDLE_PWM_MAX_VALUE); |
151 | | - } else if (rpm < settings.rpm_min) { |
152 | | - if (rpm == 0.0) { |
| 144 | + pwm_value = SPINDLE_PWM_MAX_VALUE; |
| 145 | + } else if (rpm <= settings.rpm_min) { |
| 146 | + if (rpm == 0.0) { // S0 disables spindle |
153 | 147 | sys.spindle_speed = 0.0; |
154 | | - return(SPINDLE_PWM_OFF_VALUE); } |
155 | | - else { |
| 148 | + pwm_value = SPINDLE_PWM_OFF_VALUE; |
| 149 | + } else { // Set minimum PWM output |
156 | 150 | sys.spindle_speed = settings.rpm_min; |
157 | | - return(SPINDLE_PWM_MIN_VALUE); |
| 151 | + pwm_value = SPINDLE_PWM_MIN_VALUE; |
158 | 152 | } |
159 | | - } else { |
| 153 | + } else { |
| 154 | + // Compute intermediate PWM value with linear spindle speed model. |
| 155 | + // NOTE: A nonlinear model could be installed here, if required, but keep it light-weight. |
160 | 156 | sys.spindle_speed = rpm; |
161 | | - return(floor( (rpm-settings.rpm_min)*pwm_gradient + (SPINDLE_PWM_MIN_VALUE+0.5))); |
| 157 | + pwm_value = floor( (rpm-settings.rpm_min)*pwm_gradient + (SPINDLE_PWM_MIN_VALUE+0.5)); |
162 | 158 | } |
| 159 | + return(pwm_value); |
163 | 160 | } |
164 | 161 | #endif |
165 | 162 |
|
|
0 commit comments