From a7e540e3fabe39c9d22fadc58d56cf7eedfb2199 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sat, 1 Sep 2018 15:20:50 -0700 Subject: [PATCH] Added support for 256 microstepping, Smoother operation at 250k step rate by doubling clock as needed. --- CHANGELOG.md | 2 ++ src/avr/src/config.h | 2 +- src/avr/src/motor.c | 29 +++++++++++++++++++---------- src/resources/config-template.json | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0c8e08..a0ec682 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ Buildbotics CNC Controller Firmware Changelog ## v0.3.29 - Increased display precision of position and motor config. + - Added support for 256 microstepping. + - Smoother operation at 250k step rate by doubling clock as needed. ## v0.3.28 - Show step rate on motor configuration page. diff --git a/src/avr/src/config.h b/src/avr/src/config.h index 18d7348..97570c2 100644 --- a/src/avr/src/config.h +++ b/src/avr/src/config.h @@ -169,7 +169,7 @@ enum { DRV8711_DRIVE_TDRIVEN_250 | \ DRV8711_DRIVE_OCPDEG_1 | \ DRV8711_DRIVE_OCPTH_250) -#define DRV8711_TORQUE DRV8711_TORQUE_SMPLTH_200 +#define DRV8711_TORQUE DRV8711_TORQUE_SMPLTH_50 #define DRV8711_CTRL (DRV8711_CTRL_ISGAIN_10 | \ DRV8711_CTRL_DTIME_450 | \ DRV8711_CTRL_EXSTALL_bm) diff --git a/src/avr/src/motor.c b/src/avr/src/motor.c index 832c94f..b8617c0 100644 --- a/src/avr/src/motor.c +++ b/src/avr/src/motor.c @@ -78,6 +78,7 @@ typedef struct { // Move prep bool prepped; + uint8_t clock; uint16_t timer_period; bool negative; int32_t position; @@ -280,10 +281,10 @@ void motor_load_move(int motor) { // interrupting the clock mid step or causing counter wrap around. // Set clock and period - m->timer->CTRLA = TC_CLKSEL_DIV2_gc; // Start clock + m->timer->CTRLA = m->clock; // Start clock m->timer->PERBUF = m->timer_period; // Set next frequency m->last_negative = m->negative; - m->commanded = m->position; + m->commanded = m->position; } @@ -314,20 +315,28 @@ void motor_prep_move(int motor, float target) { m->negative = steps < 0; if (m->negative) steps = -steps; - // We use clock / 2 - float seg_clocks = SEGMENT_TIME * (F_CPU * 60 / 2); - float ticks_per_step = round(seg_clocks / steps); + // Start with clock / 2 + const float seg_clocks = SEGMENT_TIME * (F_CPU * 60 / 2); + float ticks_per_step = seg_clocks / steps; - // Limit clock if step rate is too fast, disable if too slow + // Limit clock if step rate is too fast // We allow a slight fudge here (i.e. 1.9 instead 2) because the motor driver // seems to be able to handle it and otherwise we could not actually hit // an average rate of 250k usteps/sec. if (ticks_per_step < STEP_PULSE_WIDTH * 1.9) - m->timer_period = STEP_PULSE_WIDTH * 1.9; // Too fast - else if (0xffff <= ticks_per_step) m->timer_period = 0; // Too slow - else m->timer_period = ticks_per_step; // Just right + ticks_per_step = STEP_PULSE_WIDTH * 1.9; // Too fast - if (!steps) m->timer_period = 0; + // Use faster clock with faster step rates for increased resolution. + if (ticks_per_step < 0x7fff) { + ticks_per_step *= 2; + m->clock = TC_CLKSEL_DIV1_gc; + + } else m->clock = TC_CLKSEL_DIV2_gc; + + // Disable clock if too slow + if (0xffff <= ticks_per_step) ticks_per_step = 0; + + m->timer_period = steps ? round(ticks_per_step) : 0; // Power motor switch (m->power_mode) { diff --git a/src/resources/config-template.json b/src/resources/config-template.json index 77e2f0c..34f3208 100644 --- a/src/resources/config-template.json +++ b/src/resources/config-template.json @@ -58,7 +58,7 @@ }, "microsteps": { "type": "int", - "values": [1, 2, 4, 8, 16, 32, 64, 128], + "values": [1, 2, 4, 8, 16, 32, 64, 128, 256], "unit": "per full step", "default": 32, "code": "mi" -- 2.27.0