From e70133f36747263ed5bcae6e113be93e2d461238 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 3 Feb 2024 22:53:06 +0800 Subject: [PATCH] ztmc5130 add global scale --- .../step_motor_ctrl_module/step_motor_ctrl_module.cpp | 1 - components/tmc/ic/ztmc5130.cpp | 19 +++++++++++++++++-- components/tmc/ic/ztmc5130.hpp | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp b/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp index 684d3a6..9a0372d 100644 --- a/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp +++ b/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp @@ -539,7 +539,6 @@ int32_t StepMotorCtrlModule::motor_move_by(int32_t dx, int32_t motor_velocity, i } int32_t StepMotorCtrlModule::motor_move_to(int32_t tox, int32_t motor_velocity, int32_t acc) { zlock_guard lock(m_lock); - ZLOGI(TAG, "m%d motor_move_to %d", m_id, tox); m_thread.stop(); if (m_param.min_d != 0 && tox < m_param.min_d) { diff --git a/components/tmc/ic/ztmc5130.cpp b/components/tmc/ic/ztmc5130.cpp index 2d51720..620422d 100644 --- a/components/tmc/ic/ztmc5130.cpp +++ b/components/tmc/ic/ztmc5130.cpp @@ -146,8 +146,23 @@ void TMC5130::setAcceleration(float accelerationpps2) { writeInt(TMC5130_AMAX void TMC5130::setDeceleration(float accelerationpps2) { writeInt(TMC5130_DMAX, (int32_t)(to_motor_acc(accelerationpps2))); } // 设置最大减速度 void TMC5130::setMotorShaft(bool reverse) { PRV_FIELD_WRITE(TMC5130_GCONF, TMC5130_SHAFT_MASK, TMC5130_SHAFT_SHIFT, reverse); } void TMC5130::setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { writeInt(TMC5130_IHOLD_IRUN, (iholddelay << TMC5130_IHOLDDELAY_SHIFT) | (irun << TMC5130_IRUN_SHIFT) | (ihold << TMC5130_IHOLD_SHIFT)); } -void TMC5130::setGlobalScale(uint8_t globalscale) { - // writeInt(TMC5130_GCONF, (readInt(TMC5130_GCONF) & ~TMC5130_SCALE_MASK) | (globalscale << TMC5130_SCALE_SHIFT)); + +#define TMC5160_GLOBAL_SCALER 0x0B +#define TMC5160_GLOBAL_SCALER_MASK 0xFF +#define TMC5160_GLOBAL_SCALER_SHIFT 0 + +void TMC5130::setGlobalScale(uint8_t globalscale) { // + if (globalscale == 0) { + globalscale = 0; + } else if (globalscale <= 31 || globalscale >= 1) { + globalscale = 32; + } else { + globalscale = globalscale; + } + writeInt(TMC5160_GLOBAL_SCALER, (readInt(TMC5160_GLOBAL_SCALER) & ~TMC5160_GLOBAL_SCALER_MASK) | (globalscale << TMC5160_GLOBAL_SCALER_SHIFT)); +} +uint8_t TMC5130::getGlobalScale() { // + return PRV_FIELD_READ(TMC5160_GLOBAL_SCALER, TMC5160_GLOBAL_SCALER_MASK, TMC5160_GLOBAL_SCALER_SHIFT); } uint32_t TMC5130::readICVersion() { diff --git a/components/tmc/ic/ztmc5130.hpp b/components/tmc/ic/ztmc5130.hpp index cf82cbc..f7be04f 100644 --- a/components/tmc/ic/ztmc5130.hpp +++ b/components/tmc/ic/ztmc5130.hpp @@ -127,6 +127,7 @@ class TMC5130 : public IStepperMotor { void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay); void setGlobalScale(uint8_t globalscale); + uint8_t getGlobalScale(); // void setSubdivision(uint8_t subdivision); void setMotorShaft(bool reverse); // 设置电机旋转方向