From c7aeca0b77062a3e572a6e0432f075b5a5fb4a6d Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Thu, 1 Nov 2018 10:12:45 -0700 Subject: [PATCH] Allow PWM output up to 320kHz and no slower than 8Hz, Major improvements for LASER raster GCodes --- CHANGELOG.md | 7 ++++-- src/avr/src/exec.c | 1 - src/avr/src/line.c | 36 ++++++++++++++++++++++------- src/avr/src/pwm_spindle.c | 37 +++++++++++++++++++++--------- src/avr/src/vars.def | 2 +- src/resources/config-template.json | 4 ++-- 6 files changed, 62 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b7bb2f..9ba1168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ Buildbotics CNC Controller Firmware Changelog - Much improved camera support. - Camera hotpluging. - Move camera video to header. - - Click to switch through three video sizes. + - Click to switch video size. - Automount/unmount USB drives. - Automatically install ``buildbotics.gc`` when no other GCode exists. - Preplan GCode and check for errors. @@ -21,8 +21,11 @@ Buildbotics CNC Controller Firmware Changelog - Show machine working envelope in path plan viewer. - Don't reload browser view on reconnect unless controller has reloaded. - Increased max switch backoff search distance. - - Improvements for LASER raster GCodes. + - Major improvements for LASER raster GCodes. - Fixed major bug in command queuing. + - Ignore Program Number O-Codes. + - Improved planning of colinear line segments. + - Allow PWM output up to 320kHz and no slower than 8Hz. ## v0.3.28 - Show step rate on motor configuration page. diff --git a/src/avr/src/exec.c b/src/avr/src/exec.c index 120985a..3736632 100644 --- a/src/avr/src/exec.c +++ b/src/avr/src/exec.c @@ -134,7 +134,6 @@ stat_t _segment_exec() { ex.seg.max_accel, ex.seg.max_jerk); v = ex.velocity + SEGMENT_TIME * a; t *= ex.seg.vel / v; - if (v <= 0) t = v = 0; if (v < MIN_VELOCITY) { t = v = 0; diff --git a/src/avr/src/line.c b/src/avr/src/line.c index be5b708..8ced036 100644 --- a/src/avr/src/line.c +++ b/src/avr/src/line.c @@ -121,6 +121,26 @@ static bool _section_next() { } +static void _set_sync_speeds(float d) { + float speed = FLT_MAX; + + while (true) { + // Load new sync speed if needed and available + if (l.speed.offset < 0 && command_peek() == COMMAND_sync_speed) + l.speed = *(speed_t *)(command_next() + 1); + + // Exit if we don't have a speed or it's not ready to be set + if (l.speed.offset < 0 || d < l.speed.offset) break; + + // Set speed + speed = l.speed.speed; + l.speed.offset = -1; // Mark done + } + + if (speed != FLT_MAX) spindle_set_speed(speed); +} + + static stat_t _line_exec() { // Compute times float section_time = l.line.times[l.section]; @@ -142,13 +162,7 @@ static stat_t _line_exec() { if (l.line.length < d) d = l.line.length; // Handle syncronous speeds - if (l.speed.offset < 0 && command_peek() == COMMAND_sync_speed) - l.speed = *(speed_t *)(command_next() + 1); - - if (0 <= l.speed.offset && l.speed.offset <= d) { - spindle_set_speed(l.speed.speed); - l.speed.offset = -1; - } + _set_sync_speeds(d); // Check if section complete if (t == section_time) { @@ -258,6 +272,7 @@ void command_line_exec(void *data) { l.line = *(line_t *)data; l.speed.offset = -1; + _set_sync_speeds(0); // Setup first section l.seg = 0; @@ -307,4 +322,9 @@ stat_t command_sync_speed(char *cmd) { unsigned command_sync_speed_size() {return sizeof(speed_t);} -void command_sync_speed_exec(void *data) {} // Should not get here + + +void command_sync_speed_exec(void *data) { + speed_t s = *(speed_t *)data; + spindle_set_speed(s.speed); +} diff --git a/src/avr/src/pwm_spindle.c b/src/avr/src/pwm_spindle.c index 512b348..027eb71 100644 --- a/src/avr/src/pwm_spindle.c +++ b/src/avr/src/pwm_spindle.c @@ -35,7 +35,7 @@ typedef struct { - uint16_t freq; // base frequency for PWM driver, in Hz + float freq; // base frequency for PWM driver, in Hz float min_duty; float max_duty; float duty; @@ -75,8 +75,20 @@ static void _update_pwm() { return; } + // Compute duty cycle + spindle.duty = + speed * (spindle.max_duty - spindle.min_duty) + spindle.min_duty; + + // Configure clock + TIMER_PWM.CTRLB = TC1_CCAEN_bm | TC_WGMODE_SINGLESLOPE_gc; + TIMER_PWM.CCA = TIMER_PWM.PER * spindle.duty; +} + + +static void _update_freq() { // Set clock period and optimal prescaler value - float prescale = (float)(F_CPU >> 16) / spindle.freq; + float prescale = (F_CPU >> 16) / spindle.freq; + if (prescale <= 1) { TIMER_PWM.PER = F_CPU / spindle.freq; TIMER_PWM.CTRLA = TC_CLKSEL_DIV1_gc; @@ -99,13 +111,7 @@ static void _update_pwm() { } else TIMER_PWM.CTRLA = 0; - // Compute duty cycle - spindle.duty = - speed * (spindle.max_duty - spindle.min_duty) + spindle.min_duty; - - // Configure clock - TIMER_PWM.CTRLB = TC1_CCAEN_bm | TC_WGMODE_SINGLESLOPE_gc; - TIMER_PWM.CCA = TIMER_PWM.PER * spindle.duty; + _update_pwm(); } @@ -113,6 +119,7 @@ void pwm_spindle_init() { // Configure IO _set_dir(true); _set_enable(false); + _update_freq(); // PWM output OUTCLR_PIN(SPIN_PWM_PIN); @@ -163,8 +170,16 @@ void set_pwm_max_duty(float value) { float get_pwm_duty() {return spindle.duty;} -uint16_t get_pwm_freq() {return spindle.freq;} -void set_pwm_freq(uint16_t value) {spindle.freq = value; _update_pwm();} +float get_pwm_freq() {return spindle.freq;} + + +void set_pwm_freq(float value) { + if (value < 8) value = 8; + if (320000 < value) value = 320000; + spindle.freq = value; _update_freq(); +} + + bool get_pwm_invert() {return PINCTRL_PIN(SPIN_PWM_PIN) & PORT_INVEN_bm;} diff --git a/src/avr/src/vars.def b/src/avr/src/vars.def index 8b4e99b..bd0ab9d 100644 --- a/src/avr/src/vars.def +++ b/src/avr/src/vars.def @@ -94,7 +94,7 @@ VAR(pwm_invert, pi, b8, 0, 1, 1) // Inverted spindle PWM VAR(pwm_min_duty, nd, f32, 0, 1, 1) // Minimum PWM duty cycle VAR(pwm_max_duty, md, f32, 0, 1, 1) // Maximum PWM duty cycle VAR(pwm_duty, pd, f32, 0, 0, 1) // Current PWM duty cycle -VAR(pwm_freq, sf, u16, 0, 1, 1) // Spindle PWM frequency in Hz +VAR(pwm_freq, sf, f32, 0, 1, 1) // Spindle PWM frequency in Hz // Modbus spindle VAR(modbus_debug, hb, b8, 0, 1, 1) // Modbus debugging diff --git a/src/resources/config-template.json b/src/resources/config-template.json index f10621c..7aa38be 100644 --- a/src/resources/config-template.json +++ b/src/resources/config-template.json @@ -315,8 +315,8 @@ "pwm-freq": { "type": "int", "unit": "Hz", - "min": 0, - "max": 65535, + "min": 8, + "max": 320000, "default": 1000, "code": "sf" } -- 2.27.0