From 4bb33455d8216c65d2504f6bcc53670a76513215 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Wed, 28 Dec 2022 10:25:00 +0100 Subject: [PATCH] Compute vSense dynamically from iRun/iHold --- src/config/axis.h | 1 - src/config/config.h | 3 --- src/hal/tmc2130.h | 5 +++++ src/modules/motion.h | 6 +++--- src/modules/movable_base.cpp | 11 +---------- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/config/axis.h b/src/config/axis.h index 7df69b0..c9d9d69 100644 --- a/src/config/axis.h +++ b/src/config/axis.h @@ -24,7 +24,6 @@ enum MRes : uint8_t { struct AxisConfig { bool dirOn; ///< direction ON state (for inversion) MRes mRes; ///< microstepping [0-8, where 0 is x256 and 8 is fullstepping] - bool vSense; ///< vSense scaling uint8_t iRun; ///< running current uint8_t iHold; ///< holding current bool stealth; ///< Default to Stealth mode diff --git a/src/config/config.h b/src/config/config.h index e0c5141..4a1cb9f 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -100,7 +100,6 @@ static constexpr U_mm fsensorUnloadCheckDistance = 40.0_mm; static constexpr AxisConfig pulley = { .dirOn = false, .mRes = MRes_8, - .vSense = true, .iRun = 20, /// 348mA .iHold = 0, /// 17mA in SpreadCycle, freewheel in StealthChop .stealth = false, @@ -126,7 +125,6 @@ static constexpr U_mm_s pulleySlowFeedrate = 20._mm_s; static constexpr AxisConfig selector = { .dirOn = true, .mRes = MRes_8, - .vSense = true, .iRun = 31, /// 530mA .iHold = 0, /// 17mA in SpreadCycle, freewheel in StealthChop .stealth = false, @@ -177,7 +175,6 @@ static constexpr U_mm_s selectorHomingFeedrate = 30._mm_s; static constexpr AxisConfig idler = { .dirOn = true, .mRes = MRes_16, - .vSense = true, .iRun = 31, /// 530mA .iHold = 5, /// 99mA - parked current .stealth = false, diff --git a/src/hal/tmc2130.h b/src/hal/tmc2130.h index b56aaf5..811fc88 100644 --- a/src/hal/tmc2130.h +++ b/src/hal/tmc2130.h @@ -33,6 +33,11 @@ struct MotorCurrents { bool vSense; ///< VSense current scaling uint8_t iRun; ///< Running current uint8_t iHold; ///< Holding current + + constexpr MotorCurrents(uint8_t ir, uint8_t ih) + : vSense((ir < 32) ? 1 : 0) + , iRun((ir < 32) ? ir : (ir >> 1)) + , iHold((ir < 32) ? ih : (ih >> 1)) {} }; struct __attribute__((packed)) ErrorFlags { diff --git a/src/modules/motion.h b/src/modules/motion.h index 7a0df15..5e3056c 100644 --- a/src/modules/motion.h +++ b/src/modules/motion.h @@ -43,7 +43,7 @@ static AxisParams axisParams[NUM_AXIS] = { { .name = 'P', .params = { .spi = hal::spi::TmcSpiBus, .idx = Pulley, .dirOn = config::pulley.dirOn, .csPin = PULLEY_CS_PIN, .stepPin = PULLEY_STEP_PIN, .sgPin = PULLEY_SG_PIN, .mRes = config::pulley.mRes, .sg_thrs = config::pulley.sg_thrs, Axis::Pulley }, - .currents = { .vSense = config::pulley.vSense, .iRun = config::pulley.iRun, .iHold = config::pulley.iHold }, + .currents = MotorCurrents(config::pulley.iRun, config::pulley.iHold), //{ .vSense = config::pulley.vSense, .iRun = config::pulley.iRun, .iHold = config::pulley.iHold }, .mode = DefaultMotorMode(config::pulley), .jerk = unitToSteps(config::pulleyLimits.jerk), .accel = unitToSteps(config::pulleyLimits.accel), @@ -52,7 +52,7 @@ static AxisParams axisParams[NUM_AXIS] = { { .name = 'S', .params = { .spi = hal::spi::TmcSpiBus, .idx = Selector, .dirOn = config::selector.dirOn, .csPin = SELECTOR_CS_PIN, .stepPin = SELECTOR_STEP_PIN, .sgPin = SELECTOR_SG_PIN, .mRes = config::selector.mRes, .sg_thrs = config::selector.sg_thrs, Axis::Selector }, - .currents = { .vSense = config::selector.vSense, .iRun = config::selector.iRun, .iHold = config::selector.iHold }, + .currents = MotorCurrents(config::selector.iRun, config::selector.iHold), //{ .vSense = config::selector.vSense, .iRun = config::selector.iRun, .iHold = config::selector.iHold }, .mode = DefaultMotorMode(config::selector), .jerk = unitToSteps(config::selectorLimits.jerk), .accel = unitToSteps(config::selectorLimits.accel), @@ -61,7 +61,7 @@ static AxisParams axisParams[NUM_AXIS] = { { .name = 'I', .params = { .spi = hal::spi::TmcSpiBus, .idx = Idler, .dirOn = config::idler.dirOn, .csPin = IDLER_CS_PIN, .stepPin = IDLER_STEP_PIN, .sgPin = IDLER_SG_PIN, .mRes = config::idler.mRes, .sg_thrs = config::idler.sg_thrs, Axis::Idler }, - .currents = { .vSense = config::idler.vSense, .iRun = config::idler.iRun, .iHold = config::idler.iHold }, + .currents = MotorCurrents(config::idler.iRun, config::idler.iHold), //{ .vSense = config::idler.vSense, .iRun = config::idler.iRun, .iHold = config::idler.iHold }, .mode = DefaultMotorMode(config::idler), .jerk = unitToSteps(config::idlerLimits.jerk), .accel = unitToSteps(config::idlerLimits.accel), diff --git a/src/modules/movable_base.cpp b/src/modules/movable_base.cpp index 5d3d19f..8c7eb96 100644 --- a/src/modules/movable_base.cpp +++ b/src/modules/movable_base.cpp @@ -24,16 +24,7 @@ void MovableBase::PlanHome() { } void MovableBase::SetCurrents(uint8_t iRun, uint8_t iHold) { - hal::tmc2130::MotorCurrents tempCurrent = mm::axisParams[axis].currents; - if (iRun < 32) { - tempCurrent.vSense = 1; - tempCurrent.iRun = iRun; - tempCurrent.iHold = iHold; - } else { - tempCurrent.vSense = 0; - tempCurrent.iRun = iRun >> 1; - tempCurrent.iHold = iHold >> 1; - } + hal::tmc2130::MotorCurrents tempCurrent(iRun, iHold); mm::motion.DriverForAxis(axis).SetCurrents(mm::axisParams[axis].params, tempCurrent); }