From 3ffe1881432b3087e626a49a73f9e3f7b12b4896 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Sat, 21 May 2022 17:28:58 +0200 Subject: [PATCH] Fixes after rebase onto PR#174 --- src/logic/feed_to_bondtech.cpp | 49 +++++-------------- src/logic/feed_to_bondtech.h | 2 +- src/modules/axisunit.h | 10 ++++ src/modules/permanent_storage.cpp | 2 +- .../test_feed_to_bondtech.cpp | 9 +++- tests/unit/logic/stubs/main_loop_stub.cpp | 7 +++ tests/unit/logic/stubs/main_loop_stub.h | 1 + .../logic/tool_change/test_tool_change.cpp | 5 ++ 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/logic/feed_to_bondtech.cpp b/src/logic/feed_to_bondtech.cpp index d5bef91..2959ff8 100644 --- a/src/logic/feed_to_bondtech.cpp +++ b/src/logic/feed_to_bondtech.cpp @@ -9,6 +9,7 @@ #include "../modules/permanent_storage.h" #include "../modules/pulley.h" #include "../debug.h" +#include "../config/axis.h" namespace logic { @@ -40,18 +41,18 @@ void FeedToBondtech::UpdateBowdenLength(int32_t feedEnd_mm) { } } -bool FeedToBondtech::PushingFilament() { - if (mfs::fsensor.Pressed()) { - mm::motion.AbortPlannedMoves(); // stop pushing filament - GoToPushToNozzle(); - } else if (mm::motion.StallGuard(mm::Pulley)) { - // stall guard occurred during movement - the filament got stuck - state = Failed; // @@TODO may be even report why it failed - } else if (mm::motion.QueueEmpty()) { - return false; - } - return true; -} +//bool FeedToBondtech::PushingFilament() { +// if (mfs::fsensor.Pressed()) { +// mm::motion.AbortPlannedMoves(); // stop pushing filament +// GoToPushToNozzle(); +// } else if (mm::motion.StallGuard(mm::Pulley)) { +// // stall guard occurred during movement - the filament got stuck +// state = Failed; // @@TODO may be even report why it failed +// } else if (mm::motion.QueueEmpty()) { +// return false; +// } +// return true; +//} bool FeedToBondtech::Step() { switch (state) { @@ -92,30 +93,6 @@ bool FeedToBondtech::Step() { // // stall guard occurred during movement - the filament got stuck // state = PulleyStalled; } else if (mm::motion.QueueEmpty()) { // all moves have been finished and the fsensor didn't switch on -/*======= - dbg_logic_fP(PSTR("Pulley start steps %u"), mm::motion.CurPosition(mm::Pulley)); - state = PushingFilamentToFSensorFast; - mm::motion.InitAxis(mm::Pulley); - feedStart_mm = mm::stepsToUnit(mm::P_pos_t({ mm::motion.CurPosition(mm::Pulley) })); - // fast feed in millimeters - if the EEPROM value is incorrect, we'll get the default length - mm::motion.PlanMove( - { (long double)mps::BowdenLength::Get(mg::globals.ActiveSlot()) }, - config::pulleyFeedrate, config::pulleySlowFeedrate); - } - return false; - case PushingFilamentToFSensorFast: - if (!PushingFilament()) { // ran out of stored bowden length, continue slowly - state = PushingFilamentToFSensorSlow; - // do the remaining move up to maximum bowden length slowly - mm::motion.PlanMove( - { (long double)abs(config::maximumBowdenLength.v - mps::BowdenLength::Get(mg::globals.ActiveSlot())) }, // fast feed in millimeters - if the EEPROM value is incorrect, we'll - config::pulleySlowFeedrate, config::pulleySlowFeedrate); - } - return false; - case PushingFilamentToFSensorSlow: - if (!PushingFilament()) { // all moves have been finished and the fsensor didn't switch on ->>>>>>> Add bowden length runtime detection and tuning -*/ state = Failed; } return false; diff --git a/src/logic/feed_to_bondtech.h b/src/logic/feed_to_bondtech.h index 74b907e..834bd3c 100644 --- a/src/logic/feed_to_bondtech.h +++ b/src/logic/feed_to_bondtech.h @@ -55,7 +55,7 @@ private: void UpdateBowdenLength(int32_t feedEnd_mm); /// Common processing of pushing filament into fsensor (reused by multiple states) - bool PushingFilament(); + // bool PushingFilament(); uint8_t state; uint8_t maxRetries; diff --git a/src/modules/axisunit.h b/src/modules/axisunit.h index 36f3846..92d8482 100644 --- a/src/modules/axisunit.h +++ b/src/modules/axisunit.h @@ -138,6 +138,16 @@ static constexpr T truncatedUnit(U v, long double mul = 1.) { return (T)(v.v * mul); } +/// Convert an AxisUnit to unit::Unit. +/// The scaling factor is stored with the pair config::AxisConfig::uSteps and +/// config::AxisConfig::stepsPerUnit (one per-axis). +template +static constexpr typename U::type_t axisUnitToUnit(AU v) { + static_assert(AU::unit == U::unit, "incorrect unit type conversion"); + //static_assert(U::base == axisScale[AU::axis].base, "incorrect unit base conversion"); + return { (typename U::type_t)(v.v / axisScale[AU::axis].stepsPerUnit) }; +} + /// Convert a unit::Unit to a steps type (pos_t or steps_t). /// Extract the raw step count from an AxisUnit with type checking. template diff --git a/src/modules/permanent_storage.cpp b/src/modules/permanent_storage.cpp index cdf3479..cae52cc 100644 --- a/src/modules/permanent_storage.cpp +++ b/src/modules/permanent_storage.cpp @@ -96,7 +96,7 @@ static bool validBowdenLen(const uint16_t BowdenLength) { uint16_t BowdenLength::Get(uint8_t slot) { if (validFilament(slot)) { // @@TODO these reinterpret_cast expressions look horrible but I'm keeping them almost intact to respect the original code from MM_control_01 - uint16_t bowdenLength = ee::EEPROM::ReadByte(reinterpret_cast(&(eepromBase->eepromBowdenLen[slot]))); + uint16_t bowdenLength = ee::EEPROM::ReadWord(reinterpret_cast(&(eepromBase->eepromBowdenLen[slot]))); if (eepromEmpty == bowdenLength) { const uint8_t LengthCorrectionLegacy = ee::EEPROM::ReadByte(reinterpret_cast(&(eepromBase->eepromLengthCorrection))); diff --git a/tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp b/tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp index 481b998..eda204d 100644 --- a/tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp +++ b/tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp @@ -29,6 +29,13 @@ TEST_CASE("feed_to_bondtech::feed_phase_unlimited", "[feed_to_bondtech]") { ForceReinitAllAutomata(); REQUIRE(EnsureActiveSlotIndex(slot, mg::FilamentLoadState::AtPulley)); + // reset bowden lenghts in EEPROM + InitBowdenLengths(); + // check bowden lengths + for (uint8_t slot = 0; slot < config::toolCount; ++slot) { + REQUIRE(mps::BowdenLength::Get(mg::globals.ActiveSlot()) == config::minimumBowdenLength.v); + } + FeedToBondtech fb; main_loop(); @@ -71,7 +78,7 @@ TEST_CASE("feed_to_bondtech::feed_phase_unlimited", "[feed_to_bondtech]") { if( step == 100 ){ mfs::fsensor.ProcessMessage(true); } - return fb.State() == FeedToBondtech::PushingFilamentToFSensorFast; }, + return fb.State() == FeedToBondtech::PushingFilamentToFSensor; }, 1500)); REQUIRE(mfs::fsensor.Pressed()); diff --git a/tests/unit/logic/stubs/main_loop_stub.cpp b/tests/unit/logic/stubs/main_loop_stub.cpp index 34ee0c9..30b0b33 100644 --- a/tests/unit/logic/stubs/main_loop_stub.cpp +++ b/tests/unit/logic/stubs/main_loop_stub.cpp @@ -158,6 +158,13 @@ void ClearButtons(logic::CommandBase &cb) { } } +void InitBowdenLengths() { + // reset bowdenLenght in EEPROM + for (uint8_t slot = 0; slot < config::toolCount; ++slot) { + mps::BowdenLength::Set(slot, config::minimumBowdenLength.v); + } +} + void SetFSensorStateAndDebounce(bool press) { mfs::fsensor.ProcessMessage(press); for (uint8_t fs = 0; fs < config::fsensorDebounceMs + 1; ++fs) { diff --git a/tests/unit/logic/stubs/main_loop_stub.h b/tests/unit/logic/stubs/main_loop_stub.h index bcd2847..6a769bb 100644 --- a/tests/unit/logic/stubs/main_loop_stub.h +++ b/tests/unit/logic/stubs/main_loop_stub.h @@ -32,6 +32,7 @@ bool SimulateRetractFromFINDA(uint32_t step, uint32_t findaOff); void PressButtonAndDebounce(logic::CommandBase &cb, uint8_t btnIndex, bool fromPrinter); void ClearButtons(logic::CommandBase &cb); +void InitBowdenLengths(); void SetFSensorStateAndDebounce(bool press); // these are recommended max steps for simulated movement of the idler and selector diff --git a/tests/unit/logic/tool_change/test_tool_change.cpp b/tests/unit/logic/tool_change/test_tool_change.cpp index fda5b60..e8e077f 100644 --- a/tests/unit/logic/tool_change/test_tool_change.cpp +++ b/tests/unit/logic/tool_change/test_tool_change.cpp @@ -65,6 +65,7 @@ void CheckFinishedCorrectly(logic::ToolChange &tc, uint8_t toSlot) { void ToolChange(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { ForceReinitAllAutomata(); + InitBowdenLengths(); REQUIRE(EnsureActiveSlotIndex(fromSlot, mg::FilamentLoadState::InNozzle)); SetFINDAStateAndDebounce(true); @@ -96,6 +97,7 @@ void ToolChange(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { void NoToolChange(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { ForceReinitAllAutomata(); + InitBowdenLengths(); REQUIRE(EnsureActiveSlotIndex(fromSlot, mg::FilamentLoadState::InNozzle)); // the filament is LOADED @@ -114,6 +116,7 @@ void NoToolChange(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { void JustLoadFilament(logic::ToolChange &tc, uint8_t slot) { ForceReinitAllAutomata(); + InitBowdenLengths(); REQUIRE(EnsureActiveSlotIndex(slot, mg::FilamentLoadState::AtPulley)); @@ -175,6 +178,7 @@ TEST_CASE("tool_change::same_slot_just_unloaded_filament", "[tool_change]") { void ToolChangeFailLoadToFinda(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { ForceReinitAllAutomata(); + InitBowdenLengths(); REQUIRE(EnsureActiveSlotIndex(fromSlot, mg::FilamentLoadState::InNozzle)); SetFINDAStateAndDebounce(true); @@ -340,6 +344,7 @@ TEST_CASE("tool_change::load_fail_FINDA_resolve_btnR_FINDA", "[tool_change]") { void ToolChangeFailFSensor(logic::ToolChange &tc, uint8_t fromSlot, uint8_t toSlot) { using namespace std::placeholders; ForceReinitAllAutomata(); + InitBowdenLengths(); REQUIRE(EnsureActiveSlotIndex(fromSlot, mg::FilamentLoadState::InNozzle)); SetFINDAStateAndDebounce(true);