Skip to content

Commit b1ecb47

Browse files
committed
#4: PWM $ settings
1 parent 5a862a5 commit b1ecb47

File tree

8 files changed

+52
-25
lines changed

8 files changed

+52
-25
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
![GitHub Logo](https://github.com/gnea/gnea-Media/blob/master/Grbl%20Logo/Grbl%20Logo%20250px.png?raw=true)
22

33
***
4-
_Click the `Release` tab to download pre-compiled `.bin` files or just [click here](https://github.com/gnea/grbl-LPC/releases)_
4+
Old releases are in the `Release` tab. See [cprezzi's branch](https://github.com/cprezzi/grbl-LPC) for more recent releases.
5+
Note: cprezzi's branch disables current control and has defaults more suitable for other boards.
56
***
67
This is GRBL 1.1 ported to the LPC1769. It can run on Smoothieboard.
78

@@ -10,23 +11,22 @@ Usage notes:
1011
If it doesn't, try installing VCOM_lib/usbser.inf.
1112
* This doesn't pass the sdcard to the host. Once installed you need to use a micro sdcard adaptor to replace or change it.
1213
* Only tested with lasers with PWM. Non-PWM spindle control not ported.
13-
* These are fixed PWM config values. To change these you'll have to change ```SPINDLE_PWM_*``` in config.h and rebuild.
14-
* Pin 2.4
15-
* 40 kHz
16-
* PWM off value: 0%
17-
* Mimimum PWM value: 0%
18-
* Maximum PWM value: 100%
1914
* These are defaults for easy-to-change config values.
15+
* WPos enabled for LaserWeb compatability ($10=0)
2016
* Laser mode: ON ($32)
2117
* Minimum S value: 0.0 ($31)
2218
* Maximum S value: 1.0 ($30)
2319
* Hard limits not yet ported
2420
* Control inputs not yet ported (e.g. Cycle Start and Safety Door switches)
2521

2622
New configuration settings
23+
* $33 is PWM frequency in Hz
24+
* $34 is PWM off value in %
25+
* $35 is PWM min value in %
26+
* $36 is PWM max value in %
2727
* $140, $141, $142 are X, Y, Z current (amps)
28-
* Default to 0.0 A to avoid burning out your motors
29-
* Your motors will likely stall if you don't set these!
28+
* Default to 0.0 A to avoid burning out your motors
29+
* Your motors will likely stall if you don't set these!
3030

3131
Build notes:
3232
* Include ```make``` and the ```arm-none-eabi-*``` tools in your path.

grbl/config.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@
359359
// pwm = scaled value. settings.rpm_min scales to SPINDLE_PWM_MIN_VALUE. settings.rpm_max
360360
// scales to SPINDLE_PWM_MAX_VALUE.
361361

362-
#define SPINDLE_PWM_PERIOD (SystemCoreClock / 40000) // SystemCoreClock / frequency
363-
#define SPINDLE_PWM_OFF_VALUE (SPINDLE_PWM_PERIOD * 0.0) // SPINDLE_PWM_PERIOD * fraction
364-
#define SPINDLE_PWM_MIN_VALUE (SPINDLE_PWM_PERIOD * 0.0) // SPINDLE_PWM_PERIOD * fraction
365-
#define SPINDLE_PWM_MAX_VALUE (SPINDLE_PWM_PERIOD * 1.0) // SPINDLE_PWM_PERIOD * fraction
366-
367362
// Used by variable spindle output only. This forces the PWM output to a minimum duty cycle when enabled.
368363
// The PWM pin will still read 0V when the spindle is disabled. Most users will not need this option, but
369364
// it may be useful in certain scenarios. This minimum PWM settings coincides with the spindle rpm minimum
@@ -755,13 +750,17 @@
755750
#define DEFAULT_X_MAX_TRAVEL 200.0 // mm
756751
#define DEFAULT_Y_MAX_TRAVEL 200.0 // mm
757752
#define DEFAULT_Z_MAX_TRAVEL 200.0 // mm
753+
#define DEFAULT_SPINDLE_PWM_FREQ 5000 // Hz
754+
#define DEFAULT_SPINDLE_PWM_OFF_VALUE 0.0 // Percent
755+
#define DEFAULT_SPINDLE_PWM_MIN_VALUE 0.0 // Percent
756+
#define DEFAULT_SPINDLE_PWM_MAX_VALUE 100.0 // Percent
758757
#define DEFAULT_SPINDLE_RPM_MAX 1.0 // rpm
759758
#define DEFAULT_SPINDLE_RPM_MIN 0.0 // rpm
760759
#define DEFAULT_STEP_PULSE_MICROSECONDS 1
761760
#define DEFAULT_STEPPING_INVERT_MASK 0
762761
#define DEFAULT_DIRECTION_INVERT_MASK 0
763762
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
764-
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
763+
#define DEFAULT_STATUS_REPORT_MASK 0 // WPos enabled
765764
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
766765
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
767766
#define DEFAULT_REPORT_INCHES 0 // false

grbl/report.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ void report_grbl_settings() {
208208
#else
209209
report_util_uint8_setting(32,0);
210210
#endif
211+
report_util_float_setting(33,settings.spindle_pwm_freq,N_DECIMAL_SETTINGVALUE);
212+
report_util_float_setting(34,settings.spindle_pwm_off_value,N_DECIMAL_SETTINGVALUE);
213+
report_util_float_setting(35,settings.spindle_pwm_min_value,N_DECIMAL_SETTINGVALUE);
214+
report_util_float_setting(36,settings.spindle_pwm_max_value,N_DECIMAL_SETTINGVALUE);
211215
// Print axis settings
212216
uint8_t idx, set_idx;
213217
uint8_t val = AXIS_SETTINGS_START_VAL;

grbl/settings.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ void settings_restore(uint8_t restore_flag) {
8686
settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION;
8787
settings.arc_tolerance = DEFAULT_ARC_TOLERANCE;
8888

89+
settings.spindle_pwm_freq = DEFAULT_SPINDLE_PWM_FREQ;
90+
settings.spindle_pwm_off_value = DEFAULT_SPINDLE_PWM_OFF_VALUE;
91+
settings.spindle_pwm_min_value = DEFAULT_SPINDLE_PWM_MIN_VALUE;
92+
settings.spindle_pwm_max_value = DEFAULT_SPINDLE_PWM_MAX_VALUE;
8993
settings.rpm_max = DEFAULT_SPINDLE_RPM_MAX;
9094
settings.rpm_min = DEFAULT_SPINDLE_RPM_MIN;
9195

@@ -317,6 +321,10 @@ uint8_t settings_store_global_setting(uint8_t parameter, float value) {
317321
return(STATUS_SETTING_DISABLED);
318322
#endif
319323
break;
324+
case 33: settings.spindle_pwm_freq = value; spindle_init(); break; // Re-initialize spindle pwm calibration
325+
case 34: settings.spindle_pwm_off_value = value; spindle_init(); break; // Re-initialize spindle pwm calibration
326+
case 35: settings.spindle_pwm_min_value = value; spindle_init(); break; // Re-initialize spindle pwm calibration
327+
case 36: settings.spindle_pwm_max_value = value; spindle_init(); break; // Re-initialize spindle pwm calibration
320328
default:
321329
return(STATUS_INVALID_STATEMENT);
322330
}

grbl/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ typedef struct {
9292
float junction_deviation;
9393
float arc_tolerance;
9494

95+
float spindle_pwm_freq; // Hz
96+
float spindle_pwm_off_value; // Percent
97+
float spindle_pwm_min_value; // Percent
98+
float spindle_pwm_max_value; // Percent
9599
float rpm_max;
96100
float rpm_min;
97101

grbl/spindle_control.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@
2525

2626
#ifdef VARIABLE_SPINDLE
2727
static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions.
28+
float spindle_pwm_period;
29+
float spindle_pwm_off_value;
30+
float spindle_pwm_min_value;
31+
float spindle_pwm_max_value;
2832
#endif
2933

3034

3135
void spindle_init()
3236
{
3337
#ifdef VARIABLE_SPINDLE
34-
pwm_init(&SPINDLE_PWM_CHANNEL, SPINDLE_PWM_USE_PRIMARY_PIN, SPINDLE_PWM_USE_SECONDARY_PIN, SPINDLE_PWM_PERIOD, 0);
38+
spindle_pwm_period = (SystemCoreClock / settings.spindle_pwm_freq);
39+
spindle_pwm_off_value = (spindle_pwm_period * settings.spindle_pwm_off_value / 100);
40+
spindle_pwm_min_value = (spindle_pwm_period * settings.spindle_pwm_min_value / 100);
41+
spindle_pwm_max_value = (spindle_pwm_period * settings.spindle_pwm_max_value / 100);
42+
pwm_init(&SPINDLE_PWM_CHANNEL, SPINDLE_PWM_USE_PRIMARY_PIN, SPINDLE_PWM_USE_SECONDARY_PIN, spindle_pwm_period, 0);
3543
pwm_enable(&SPINDLE_PWM_CHANNEL);
3644

3745
/* not ported
@@ -44,7 +52,7 @@ void spindle_init()
4452
#endif
4553
*/
4654

47-
pwm_gradient = (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE)/(settings.rpm_max-settings.rpm_min);
55+
pwm_gradient = (spindle_pwm_max_value-spindle_pwm_min_value)/(settings.rpm_max-settings.rpm_min);
4856

4957
#else
5058
/* not ported
@@ -134,21 +142,21 @@ void spindle_stop()
134142
rpm *= (0.010*sys.spindle_speed_ovr); // Scale by spindle speed override value.
135143
if (rpm <= 0) {
136144
sys.spindle_speed = 0;
137-
pwm_value = SPINDLE_PWM_OFF_VALUE;
145+
pwm_value = spindle_pwm_off_value;
138146
}
139147
else if (rpm <= settings.rpm_min) {
140148
sys.spindle_speed = settings.rpm_min;
141-
pwm_value = SPINDLE_PWM_MIN_VALUE;
149+
pwm_value = spindle_pwm_min_value;
142150
}
143151
else if (rpm >= settings.rpm_max) {
144152
sys.spindle_speed = settings.rpm_max;
145-
pwm_value = SPINDLE_PWM_MAX_VALUE - 1;
153+
pwm_value = spindle_pwm_max_value - 1;
146154
}
147155
else {
148156
sys.spindle_speed = rpm;
149-
pwm_value = floor((rpm - settings.rpm_min) * pwm_gradient) + SPINDLE_PWM_MIN_VALUE;
150-
if(pwm_value >= SPINDLE_PWM_MAX_VALUE)
151-
pwm_value = SPINDLE_PWM_MAX_VALUE - 1;
157+
pwm_value = floor((rpm - settings.rpm_min) * pwm_gradient) + spindle_pwm_min_value;
158+
if(pwm_value >= spindle_pwm_max_value)
159+
pwm_value = spindle_pwm_max_value - 1;
152160
}
153161
return(pwm_value);
154162
}

grbl/spindle_control.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ uint8_t spindle_get_state();
4040
// Immediately sets spindle running state with direction and spindle rpm via PWM, if enabled.
4141
// Called by spindle_sync() after sync and parking motion/spindle stop override during restore.
4242
#ifdef VARIABLE_SPINDLE
43+
extern float spindle_pwm_period;
44+
extern float spindle_pwm_off_value;
45+
extern float spindle_pwm_min_value;
46+
extern float spindle_pwm_max_value;
4347

4448
// Called by g-code parser when setting spindle state and requires a buffer sync.
4549
void spindle_sync(uint8_t state, float rpm);

grbl/stepper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ extern "C" void TIMER1_IRQHandler()
385385
st_go_idle();
386386
#ifdef VARIABLE_SPINDLE
387387
// Ensure pwm is set properly upon completion of rate-controlled motion.
388-
if (st.exec_block->is_pwm_rate_adjusted) { spindle_set_speed(SPINDLE_PWM_OFF_VALUE); }
388+
if (st.exec_block->is_pwm_rate_adjusted) { spindle_set_speed(spindle_pwm_off_value); }
389389
#endif
390390
system_set_exec_state_flag(EXEC_CYCLE_STOP); // Flag main program for cycle end
391391
return; // Nothing to do but exit.
@@ -907,7 +907,7 @@ void st_prep_buffer()
907907
prep.current_spindle_pwm = spindle_compute_pwm_value(rpm);
908908
} else {
909909
sys.spindle_speed = 0.0;
910-
prep.current_spindle_pwm = SPINDLE_PWM_OFF_VALUE;
910+
prep.current_spindle_pwm = spindle_pwm_off_value;
911911
}
912912
bit_false(sys.step_control,STEP_CONTROL_UPDATE_SPINDLE_PWM);
913913
}

0 commit comments

Comments
 (0)