diff --git a/src/hal/tmc2130.cpp b/src/hal/tmc2130.cpp index 88e496c..98d3939 100644 --- a/src/hal/tmc2130.cpp +++ b/src/hal/tmc2130.cpp @@ -3,6 +3,77 @@ #include "../config/config.h" #include "../debug.h" +//! @brief Translate current to tmc2130 vsense and IHOLD or IRUN - copied from MK3 FW repo +//! @param cur current in mA +//! @return 0 .. 63 +//! @n most significant bit is CHOPCONF vsense bit (sense resistor voltage based current scaling) +//! @n rest is to be used in IRUN or IHOLD register +//! +//! | mA | trinamic register | note | +//! | --- | --- | --- | +//! | 0 | 0 | doesn't mean current off, lowest current is 1/32 current with vsense low range | +//! | 30 | 1 | | +//! | 40 | 2 | | +//! | 60 | 3 | | +//! | 90 | 4 | | +//! | 100 | 5 | | +//! | 120 | 6 | | +//! | 130 | 7 | | +//! | 150 | 8 | | +//! | 180 | 9 | | +//! | 190 | 10 | | +//! | 210 | 11 | | +//! | 230 | 12 | | +//! | 240 | 13 | | +//! | 250 | 13 | | +//! | 260 | 14 | | +//! | 280 | 15 | | +//! | 300 | 16 | | +//! | 320 | 17 | | +//! | 340 | 18 | | +//! | 350 | 19 | | +//! | 370 | 20 | | +//! | 390 | 21 | | +//! | 410 | 22 | | +//! | 430 | 23 | | +//! | 450 | 24 | | +//! | 460 | 25 | | +//! | 480 | 26 | | +//! | 500 | 27 | | +//! | 520 | 28 | | +//! | 525 | 29 | | +//! | 530 | 30 | | +//! | 540 | 33 | | +//! | 560 | 34 | | +//! | 580 | 35 | | +//! | 590 | 36 | | +//! | 610 | 37 | | +//! | 630 | 38 | | +//! | 640 | 39 | | +//! | 660 | 40 | | +//! | 670 | 41 | | +//! | 690 | 42 | | +//! | 710 | 43 | | +//! | 720 | 44 | | +//! | 730 | 45 | | +//! | 760 | 46 | | +//! | 770 | 47 | | +//! | 790 | 48 | | +//! | 810 | 49 | | +//! | 820 | 50 | | +//! | 840 | 51 | | +//! | 850 | 52 | | +//! | 870 | 53 | | +//! | 890 | 54 | | +//! | 900 | 55 | | +//! | 920 | 56 | | +//! | 940 | 57 | | +//! | 950 | 58 | | +//! | 970 | 59 | | +//! | 980 | 60 | | +//! | 1000 | 61 | | +//! | 1020 | 62 | | +//! | 1029 | 63 | | namespace hal { namespace tmc2130 { diff --git a/src/modules/idler.cpp b/src/modules/idler.cpp index 69fd872..e746455 100644 --- a/src/modules/idler.cpp +++ b/src/modules/idler.cpp @@ -54,9 +54,9 @@ bool Idler::FinishHomingAndPlanMoveToParkPos() { void Idler::FinishMove() { currentlyEngaged = plannedMove; if (Disengaged()) // reduce power into the Idler motor when disengaged (less force necessary) - SetCurrents(config::idler.iRun, config::idler.iHold); + SetCurrents(mm::motion.CurrentsForAxis(axis).iRun, mm::motion.CurrentsForAxis(axis).iHold); else if (Engaged()) { // maximum motor power when the idler is engaged - SetCurrents(config::idler.iRun, config::idler.iRun); + SetCurrents(mm::motion.CurrentsForAxis(axis).iRun, mm::motion.CurrentsForAxis(axis).iRun); } } diff --git a/src/modules/motion.cpp b/src/modules/motion.cpp index cea504f..3fa7265 100644 --- a/src/modules/motion.cpp +++ b/src/modules/motion.cpp @@ -61,7 +61,7 @@ bool Motion::InitAxis(Axis axis) { Disable(axis); // Init also applies the currently pre-set StallGuard threshold into the TMC driver - return axisData[axis].drv.Init(axisParams[axis].params, axisParams[axis].currents, axisParams[axis].mode); + return axisData[axis].drv.Init(axisParams[axis].params, axisData[axis].currents, axisParams[axis].mode); } void Motion::SetEnabled(Axis axis, bool enabled) { diff --git a/src/modules/motion.h b/src/modules/motion.h index 7e3f545..4e91f10 100644 --- a/src/modules/motion.h +++ b/src/modules/motion.h @@ -356,11 +356,19 @@ public: return axisData[axis].ctrl; } + inline const MotorCurrents &CurrentsForAxis(Axis axis) const { + return axisData[axis].currents; + } + inline void SetIRunForAxis(Axis axis, uint8_t i) { + axisData[axis].currents.iRun = i; + } + private: struct AxisData { TMC2130 drv; ///< Motor driver pulse_gen::PulseGen ctrl; ///< Motor controller bool enabled; ///< Axis enabled + MotorCurrents currents; ///< Axis related currents st_timer_t residual; ///< Axis timer residual }; @@ -372,7 +380,8 @@ private: axisParams[axis].jerk, axisParams[axis].accel, }, - .enabled = false + .enabled = false, + .currents = axisParams[axis].currents }; } diff --git a/src/modules/movable_base.cpp b/src/modules/movable_base.cpp index 8c7eb96..33eda21 100644 --- a/src/modules/movable_base.cpp +++ b/src/modules/movable_base.cpp @@ -39,6 +39,7 @@ MovableBase::OperationResult MovableBase::InitMovement() { MovableBase::OperationResult __attribute__((noinline)) MovableBase::InitMovementNoReinitAxis() { PrepareMoveToPlannedSlot(); + // @@TODO update axis currents at this spot? state = Moving; return OperationResult::Accepted; } diff --git a/src/registers.cpp b/src/registers.cpp index 68ad82a..0446539 100644 --- a/src/registers.cpp +++ b/src/registers.cpp @@ -12,6 +12,7 @@ #include "modules/fsensor.h" #include "modules/globals.h" #include "modules/idler.h" +#include "modules/motion.h" #include "modules/pulley.h" #include "modules/selector.h" #include "modules/permanent_storage.h" @@ -159,6 +160,9 @@ | 0x1bh 27 | uint16 | Set/Get_Selector_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=park pos | Read / Write | M707 A0x1b | M708 A0x1b Xn | 0x1ch 28 | uint16 | Set/Get_Idler_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=disengaged | Read / Write | M707 A0x1c | M708 A0x1c Xn | 0x1dh 29 | uint8 | Set/Get Selector cut iRun current | 0 to 63 (aka 0-1024mA)| 31 (530mA) | | Read / Write | M707 A0x1d | M708 A0x1d Xn +| 0x1eh 30 | uint16 | Set/Get Pulley iRun current| 0-63 | 14h 20 | 20->350mA: see TMC2130 current conversion| Read / Write | M707 A0x1e | M708 A0x1e Xn +| 0x1fh 31 | uint16 |Set/Get Selector iRun current| 0-63 | 1fh 31 | 31->530mA: see TMC2130 current conversion| Read / Write | M707 A0x1f | M708 A0x1f Xn +| 0x20h 32 | uint16 | Set/Get Idler iRun current | 0-63 | 1fh 31 | 31->530mA: see TMC2130 current conversion| Read / Write | M707 A0x20 | M708 A0x20 Xn */ struct RegisterFlags { @@ -374,6 +378,22 @@ static const RegisterRec registers[] /*PROGMEM*/ = { []() -> uint16_t { return mg::globals.CutIRunCurrent(); }, [](uint16_t d) { mg::globals.SetCutIRunCurrent(d); }, 1), + + // 0x1e Get/Set Pulley iRun current RW + RegisterRec( + []() -> uint16_t { return mm::motion.CurrentsForAxis(config::Pulley).iRun; }, + [](uint16_t d) { mm::motion.SetIRunForAxis(config::Pulley, d); }, + 1), + // 0x1f Set/Get Selector iRun current RW + RegisterRec( + []() -> uint16_t { return mm::motion.CurrentsForAxis(config::Selector).iRun; }, + [](uint16_t d) { mm::motion.SetIRunForAxis(config::Selector, d); }, + 1), + // 0x20 Set/Get Idler iRun current RW + RegisterRec( + []() -> uint16_t { return mm::motion.CurrentsForAxis(config::Idler).iRun; }, + [](uint16_t d) { mm::motion.SetIRunForAxis(config::Idler, d); }, + 1), }; static constexpr uint8_t registersSize = sizeof(registers) / sizeof(RegisterRec);