From 74629f01035e544f426a6a365b962f001ec036b0 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 26 Aug 2021 13:50:35 +0200 Subject: [PATCH] Fix reporting progress of running commands The problem was the error state while running a command - we never used the RUNNING error state. --- src/logic/command_base.cpp | 8 ++++---- src/logic/cut_filament.cpp | 3 ++- src/logic/eject_filament.cpp | 3 ++- src/logic/load_filament.cpp | 5 +++-- src/logic/tool_change.cpp | 1 + src/logic/unload_filament.cpp | 5 +++-- .../unit/logic/cut_filament/test_cut_filament.cpp | 14 +++++++------- .../logic/load_filament/test_load_filament.cpp | 10 +++++----- .../logic/unload_filament/test_unload_filament.cpp | 14 +++++++------- 9 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/logic/command_base.cpp b/src/logic/command_base.cpp index cd8f460..a50e2ea 100644 --- a/src/logic/command_base.cpp +++ b/src/logic/command_base.cpp @@ -11,7 +11,7 @@ inline ErrorCode &operator|=(ErrorCode &a, ErrorCode b) { } static ErrorCode TMC2130ToErrorCode(const hal::tmc2130::TMC2130 &tmc, uint8_t tmcIndex) { - ErrorCode e = ErrorCode::OK; + ErrorCode e = ErrorCode::RUNNING; if (tmc.GetErrorFlags().reset_flag) { e |= ErrorCode::TMC_RESET; @@ -29,7 +29,7 @@ static ErrorCode TMC2130ToErrorCode(const hal::tmc2130::TMC2130 &tmc, uint8_t tm e |= ErrorCode::TMC_OVER_TEMPERATURE_ERROR; } - if (e != ErrorCode::OK) { + if (e != ErrorCode::RUNNING) { switch (tmcIndex) { case config::Axis::Pulley: e |= ErrorCode::TMC_PULLEY_BIT; @@ -49,7 +49,7 @@ static ErrorCode TMC2130ToErrorCode(const hal::tmc2130::TMC2130 &tmc, uint8_t tm } bool CommandBase::Step() { - ErrorCode tmcErr = ErrorCode::OK; + ErrorCode tmcErr = ErrorCode::RUNNING; // check the global HW errors - may be we should avoid the modules layer and check for the HAL layer errors directly if (mi::idler.State() == mi::Idler::Failed) { state = ProgressCode::ERRTMCFailed; @@ -69,7 +69,7 @@ bool CommandBase::Step() { // @@TODO not sure how to prevent losing the previously accumulated error ... or do I really need to do it? // May be the TMC error word just gets updated with new flags as the motion proceeds // And how about the logical errors like FINDA_DIDNT_SWITCH_ON? - if (tmcErr != ErrorCode::OK) { + if (tmcErr != ErrorCode::RUNNING) { error |= tmcErr; return true; } diff --git a/src/logic/cut_filament.cpp b/src/logic/cut_filament.cpp index d7dd9f6..4808b3c 100644 --- a/src/logic/cut_filament.cpp +++ b/src/logic/cut_filament.cpp @@ -17,7 +17,7 @@ void CutFilament::Reset(uint8_t param) { return; } - error = ErrorCode::OK; + error = ErrorCode::RUNNING; cutSlot = param; if (mg::globals.FilamentLoaded()) { @@ -90,6 +90,7 @@ bool CutFilament::StepInner() { case ProgressCode::ReturningSelector: if (ms::selector.Slot() == 5) { // selector returned to position, feed the filament back to FINDA state = ProgressCode::OK; + error = ErrorCode::OK; ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off); feed.Reset(true); diff --git a/src/logic/eject_filament.cpp b/src/logic/eject_filament.cpp index e865022..e885408 100644 --- a/src/logic/eject_filament.cpp +++ b/src/logic/eject_filament.cpp @@ -17,7 +17,7 @@ void EjectFilament::Reset(uint8_t param) { return; } - error = ErrorCode::OK; + error = ErrorCode::RUNNING; slot = param; if (mg::globals.FilamentLoaded()) { @@ -62,6 +62,7 @@ bool EjectFilament::StepInner() { if (!mi::idler.Engaged()) { // idler disengaged mm::motion.Disable(mm::Pulley); state = ProgressCode::OK; + error = ErrorCode::OK; } break; case ProgressCode::OK: diff --git a/src/logic/load_filament.cpp b/src/logic/load_filament.cpp index d1e2e13..cafebe2 100644 --- a/src/logic/load_filament.cpp +++ b/src/logic/load_filament.cpp @@ -18,7 +18,7 @@ void LoadFilament::Reset(uint8_t param) { } state = ProgressCode::EngagingIdler; - error = ErrorCode::OK; + error = ErrorCode::RUNNING; mg::globals.SetActiveSlot(param); mi::idler.Engage(mg::globals.ActiveSlot()); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::blink0); @@ -63,6 +63,7 @@ bool LoadFilament::StepInner() { case ProgressCode::DisengagingIdler: if (!mi::idler.Engaged()) { state = ProgressCode::OK; + error = ErrorCode::OK; ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::off); mg::globals.SetFilamentLoaded(true); @@ -108,7 +109,7 @@ bool LoadFilament::StepInner() { if (mf::finda.Pressed()) { // the help was enough to press the FINDA, we are ok, continue normally state = ProgressCode::FeedingToBondtech; - error = ErrorCode::OK; + error = ErrorCode::RUNNING; } else if (mm::motion.QueueEmpty()) { // helped a bit, but FINDA didn't trigger, return to the main error state state = ProgressCode::ERRDisengagingIdler; diff --git a/src/logic/tool_change.cpp b/src/logic/tool_change.cpp index 6bfaae6..71c9ab9 100644 --- a/src/logic/tool_change.cpp +++ b/src/logic/tool_change.cpp @@ -48,6 +48,7 @@ bool ToolChange::StepInner() { // as LoadFilament should handle all the possible error states on its own // There is no way the LoadFilament to finish in an error state state = ProgressCode::OK; + error = ErrorCode::OK; } break; case ProgressCode::OK: diff --git a/src/logic/unload_filament.cpp b/src/logic/unload_filament.cpp index ccde837..306bf3a 100644 --- a/src/logic/unload_filament.cpp +++ b/src/logic/unload_filament.cpp @@ -15,7 +15,7 @@ void UnloadFilament::Reset(uint8_t /*param*/) { // unloads filament from extruder - filament is above Bondtech gears mm::motion.InitAxis(mm::Pulley); state = ProgressCode::UnloadingToFinda; - error = ErrorCode::OK; + error = ErrorCode::RUNNING; unl.Reset(maxRetries); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::blink0); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off); @@ -56,6 +56,7 @@ bool UnloadFilament::StepInner() { case ProgressCode::FinishingMoves: if (mm::motion.QueueEmpty()) { state = ProgressCode::OK; + error = ErrorCode::OK; mm::motion.Disable(mm::Pulley); mg::globals.SetFilamentLoaded(false); // filament unloaded ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::off); @@ -99,7 +100,7 @@ bool UnloadFilament::StepInner() { if (!mf::finda.Pressed()) { // the help was enough to depress the FINDA, we are ok, continue normally state = ProgressCode::DisengagingIdler; - error = ErrorCode::OK; + error = ErrorCode::RUNNING; } else if (mm::motion.QueueEmpty()) { // helped a bit, but FINDA didn't trigger, return to the main error state state = ProgressCode::ERRDisengagingIdler; diff --git a/tests/unit/logic/cut_filament/test_cut_filament.cpp b/tests/unit/logic/cut_filament/test_cut_filament.cpp index cf59de1..bd06212 100644 --- a/tests/unit/logic/cut_filament/test_cut_filament.cpp +++ b/tests/unit/logic/cut_filament/test_cut_filament.cpp @@ -33,14 +33,14 @@ void CutSlot(logic::CutFilament &cf, uint8_t cutSlot) { cf.Reset(cutSlot); // check initial conditions - REQUIRE(VerifyState(cf, false, mi::Idler::IdleSlotIndex(), cutSlot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::SelectingFilamentSlot)); + REQUIRE(VerifyState(cf, false, mi::Idler::IdleSlotIndex(), cutSlot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::SelectingFilamentSlot)); // now cycle at most some number of cycles (to be determined yet) and then verify, that the idler and selector reached their target positions // Beware - with the real positions of the selector, the number of steps needed to finish some states grows, so the ~40K steps here has a reason REQUIRE(WhileTopState(cf, ProgressCode::SelectingFilamentSlot, selectorMoveMaxSteps)); // idler and selector reached their target positions and the CF automaton will start feeding to FINDA as the next step - REQUIRE(VerifyState(cf, false, cutSlot, cutSlot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::FeedingToFinda)); + REQUIRE(VerifyState(cf, false, cutSlot, cutSlot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::FeedingToFinda)); // prepare for simulated finda trigger REQUIRE(WhileCondition( @@ -54,7 +54,7 @@ void CutSlot(logic::CutFilament &cf, uint8_t cutSlot) { // filament fed to FINDA //@@TODO filament loaded flag - decide whether the filament loaded flag means really loaded into the printer or just a piece of filament // stuck out of the pulley to prevent movement of the selector - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::UnloadingToPulley)); + REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::UnloadingToPulley)); // pull it back to the pulley + simulate FINDA depress REQUIRE(WhileCondition( @@ -65,19 +65,19 @@ void CutSlot(logic::CutFilament &cf, uint8_t cutSlot) { } return cf.TopLevelState() == ProgressCode::UnloadingToPulley; }, 5000)); - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::PreparingBlade)); + REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::PreparingBlade)); // now move the selector aside, prepare for cutting REQUIRE(WhileTopState(cf, ProgressCode::PreparingBlade, 5000)); - REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::PushingFilament)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::PushingFilament)); // pushing filament a bit for a cut REQUIRE(WhileTopState(cf, ProgressCode::PushingFilament, 5000)); - REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::PerformingCut)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::PerformingCut)); // cutting REQUIRE(WhileTopState(cf, ProgressCode::PerformingCut, selectorMoveMaxSteps)); - REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, 0, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::ReturningSelector)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, 0, false, cutSlot, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::ReturningSelector)); // moving selector to the other end of its axis REQUIRE(WhileTopState(cf, ProgressCode::ReturningSelector, selectorMoveMaxSteps)); diff --git a/tests/unit/logic/load_filament/test_load_filament.cpp b/tests/unit/logic/load_filament/test_load_filament.cpp index 977547a..7b130ca 100644 --- a/tests/unit/logic/load_filament/test_load_filament.cpp +++ b/tests/unit/logic/load_filament/test_load_filament.cpp @@ -39,11 +39,11 @@ void LoadFilamentCommonSetup(uint8_t slot, logic::LoadFilament &lf) { // no change in selector's position // FINDA off // green LED should blink, red off - REQUIRE(VerifyState(lf, false, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::EngagingIdler)); + REQUIRE(VerifyState(lf, false, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::EngagingIdler)); // Stage 1 - engaging idler REQUIRE(WhileTopState(lf, ProgressCode::EngagingIdler, idlerEngageDisengageMaxSteps)); - REQUIRE(VerifyState(lf, false, slot, slot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::FeedingToFinda)); + REQUIRE(VerifyState(lf, false, slot, slot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::FeedingToFinda)); } void LoadFilamentSuccessful(uint8_t slot, logic::LoadFilament &lf) { @@ -57,7 +57,7 @@ void LoadFilamentSuccessful(uint8_t slot, logic::LoadFilament &lf) { } return lf.TopLevelState() == ProgressCode::FeedingToFinda; }, 5000)); - REQUIRE(VerifyState(lf, false, slot, slot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::FeedingToBondtech)); + REQUIRE(VerifyState(lf, false, slot, slot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::FeedingToBondtech)); // Stage 3 - feeding to bondtech // we'll make a fsensor switch during the process @@ -69,7 +69,7 @@ void LoadFilamentSuccessful(uint8_t slot, logic::LoadFilament &lf) { } return lf.TopLevelState() == ProgressCode::FeedingToBondtech; }, 5000)); - REQUIRE(VerifyState(lf, false, slot, slot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::DisengagingIdler)); + REQUIRE(VerifyState(lf, false, slot, slot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::DisengagingIdler)); // Stage 4 - disengaging idler REQUIRE(WhileTopState(lf, ProgressCode::DisengagingIdler, idlerEngageDisengageMaxSteps)); @@ -130,7 +130,7 @@ void FailedLoadToFindaResolveHelpFindaTriggered(uint8_t slot, logic::LoadFilamen return lf.TopLevelState() == ProgressCode::ERRHelpingFilament; }, 5000)); - REQUIRE(VerifyState(lf, false, slot, slot, true, ml::off, ml::blink0, ErrorCode::OK, ProgressCode::FeedingToBondtech)); + REQUIRE(VerifyState(lf, false, slot, slot, true, ml::off, ml::blink0, ErrorCode::RUNNING, ProgressCode::FeedingToBondtech)); } void FailedLoadToFindaResolveHelpFindaDidntTrigger(uint8_t slot, logic::LoadFilament &lf) { diff --git a/tests/unit/logic/unload_filament/test_unload_filament.cpp b/tests/unit/logic/unload_filament/test_unload_filament.cpp index 7471929..c28a69d 100644 --- a/tests/unit/logic/unload_filament/test_unload_filament.cpp +++ b/tests/unit/logic/unload_filament/test_unload_filament.cpp @@ -47,7 +47,7 @@ void RegularUnloadFromSlot04(uint8_t slot, logic::UnloadFilament &uf) { // no change in selector's position // FINDA on // green LED should blink, red off - REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::UnloadingToFinda)); + REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::UnloadingToFinda)); // run the automaton // Stage 1 - unloading to FINDA @@ -65,7 +65,7 @@ void RegularUnloadFromSlot04(uint8_t slot, logic::UnloadFilament &uf) { // no change in selector's position // FINDA triggered off // green LED should blink - REQUIRE(VerifyState(uf, true, slot, slot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::DisengagingIdler)); + REQUIRE(VerifyState(uf, true, slot, slot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::DisengagingIdler)); // Stage 2 - idler was engaged, disengage it REQUIRE(WhileTopState(uf, ProgressCode::DisengagingIdler, idlerEngageDisengageMaxSteps)); @@ -75,7 +75,7 @@ void RegularUnloadFromSlot04(uint8_t slot, logic::UnloadFilament &uf) { // no change in selector's position // FINDA still triggered off // green LED should blink - REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::AvoidingGrind)); + REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::AvoidingGrind)); // Stage 3 - avoiding grind (whatever is that @@TODO) REQUIRE(WhileTopState(uf, ProgressCode::AvoidingGrind, 5000)); @@ -85,7 +85,7 @@ void RegularUnloadFromSlot04(uint8_t slot, logic::UnloadFilament &uf) { // no change in selector's position // FINDA still triggered off // green LED should blink - REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::FinishingMoves)); + REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, false, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::FinishingMoves)); // Stage 4 - finishing moves and setting global state correctly REQUIRE(WhileTopState(uf, ProgressCode::FinishingMoves, 5000)); @@ -138,7 +138,7 @@ void FindaDidntTriggerCommonSetup(uint8_t slot, logic::UnloadFilament &uf) { // FINDA triggered off // green LED should blink // no error so far - REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::UnloadingToFinda)); + REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::UnloadingToFinda)); // run the automaton // Stage 1 - unloading to FINDA - do NOT let it trigger - keep it pressed, the automaton should finish all moves with the pulley @@ -215,7 +215,7 @@ void FindaDidntTriggerResolveHelpFindaTriggered(uint8_t slot, logic::UnloadFilam // no change in selector's position // FINDA depressed // red LED should blink, green LED should be off - REQUIRE(VerifyState(uf, true, slot, slot, false, ml::off, ml::blink0, ErrorCode::OK, ProgressCode::DisengagingIdler)); + REQUIRE(VerifyState(uf, true, slot, slot, false, ml::off, ml::blink0, ErrorCode::RUNNING, ProgressCode::DisengagingIdler)); } void FindaDidntTriggerResolveHelpFindaDidntTrigger(uint8_t slot, logic::UnloadFilament &uf) { @@ -270,7 +270,7 @@ void FindaDidntTriggerResolveTryAgain(uint8_t slot, logic::UnloadFilament &uf) { // no change in selector's position // FINDA still on // red LED should blink, green LED should be off - REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::UnloadingToFinda)); + REQUIRE(VerifyState(uf, true, mi::Idler::IdleSlotIndex(), slot, true, ml::blink0, ml::off, ErrorCode::RUNNING, ProgressCode::UnloadingToFinda)); } TEST_CASE("unload_filament::finda_didnt_trigger_resolve_try_again", "[unload_filament]") {