From 9efb127acbe61b258b289a93bc728bdf3afd7fc0 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Thu, 1 Jul 2021 08:07:52 +0200 Subject: [PATCH] Explain and fix the LED states while cutting filament now the test is correct including LEDs --- src/logic/cut_filament.cpp | 5 +++ .../logic/cut_filament/test_cut_filament.cpp | 17 ++++---- tests/unit/logic/helpers/helpers.ipp | 42 ++++++++++++++++++- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/logic/cut_filament.cpp b/src/logic/cut_filament.cpp index 601060f..151cad4 100644 --- a/src/logic/cut_filament.cpp +++ b/src/logic/cut_filament.cpp @@ -14,6 +14,7 @@ CutFilament cutFilament; namespace mm = modules::motion; namespace mi = modules::idler; +namespace ml = modules::leds; namespace ms = modules::selector; namespace mg = modules::globals; @@ -33,6 +34,8 @@ void CutFilament::SelectFilamentSlot() { state = ProgressCode::SelectingFilamentSlot; mi::idler.Engage(cutSlot); ms::selector.MoveToSlot(cutSlot); + ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::blink0); + ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off); } bool CutFilament::Step() { @@ -89,6 +92,8 @@ bool CutFilament::Step() { case ProgressCode::ReturningSelector: if (ms::selector.Slot() == 5) { // selector returned to position, feed the filament back to FINDA state = ProgressCode::OK; + ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); + ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off); feed.Reset(true); } break; diff --git a/tests/unit/logic/cut_filament/test_cut_filament.cpp b/tests/unit/logic/cut_filament/test_cut_filament.cpp index 31c74bc..66b6c3e 100644 --- a/tests/unit/logic/cut_filament/test_cut_filament.cpp +++ b/tests/unit/logic/cut_filament/test_cut_filament.cpp @@ -34,6 +34,7 @@ void CutSlot(uint8_t cutSlot) { ForceReinitAllAutomata(); logic::CutFilament cf; + REQUIRE(VerifyState(cf, false, 5, 0, false, ml::off, ml::off, ErrorCode::OK, ProgressCode::OK)); EnsureActiveSlotIndex(cutSlot); @@ -41,19 +42,19 @@ void CutSlot(uint8_t cutSlot) { cf.Reset(cutSlot); // check initial conditions - REQUIRE(VerifyState(cf, false, 5, cutSlot, false, ml::off, ml::off, ErrorCode::OK, ProgressCode::SelectingFilamentSlot)); + REQUIRE(VerifyState(cf, false, 5, cutSlot, false, ml::blink0, ml::off, ErrorCode::OK, 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 REQUIRE(WhileTopState(cf, ProgressCode::SelectingFilamentSlot, 5000)); // 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::off, ml::off, ErrorCode::OK, ProgressCode::FeedingToFinda)); + REQUIRE(VerifyState(cf, false, cutSlot, cutSlot, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::FeedingToFinda)); // prepare for simulated finda trigger REQUIRE(WhileCondition( cf, [&](int step) -> bool { - if( step == 1000 ){ // simulate FINDA trigger - will get pressed in 100 steps (due to debouncing) + if( step == 100 ){ // simulate FINDA trigger - will get pressed in 100 steps (due to debouncing) hal::adc::SetADC(1, 900); } return cf.TopLevelState() == ProgressCode::FeedingToFinda; }, 5000)); @@ -76,19 +77,19 @@ void CutSlot(uint8_t cutSlot) { // now move the selector aside, prepare for cutting REQUIRE(WhileTopState(cf, ProgressCode::PreparingBlade, 5000)); - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot + 1, false, ml::off, ml::off, ErrorCode::OK, ProgressCode::PushingFilament)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::PushingFilament)); // pushing filament a bit for a cut REQUIRE(WhileTopState(cf, ProgressCode::PushingFilament, 5000)); - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, cutSlot + 1, false, ml::off, ml::off, ErrorCode::OK, ProgressCode::PerformingCut)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, cutSlot + 1, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::PerformingCut)); // cutting - REQUIRE(WhileTopState(cf, ProgressCode::PerformingCut, 5000)); - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, 0, false, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::ReturningSelector)); + REQUIRE(WhileTopState(cf, ProgressCode::PerformingCut, 10000)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, 0, false, cutSlot, ml::blink0, ml::off, ErrorCode::OK, ProgressCode::ReturningSelector)); // moving selector to the other end of its axis REQUIRE(WhileTopState(cf, ProgressCode::ReturningSelector, 5000)); - REQUIRE(VerifyState(cf, /*true*/ false, cutSlot, 5, false, ml::off, ml::off, ErrorCode::OK, ProgressCode::OK)); + REQUIRE(VerifyState2(cf, /*true*/ false, cutSlot, 5, false, cutSlot, ml::on, ml::off, ErrorCode::OK, ProgressCode::OK)); } TEST_CASE("cut_filament::cut0", "[cut_filament]") { diff --git a/tests/unit/logic/helpers/helpers.ipp b/tests/unit/logic/helpers/helpers.ipp index 025de54..9eea269 100644 --- a/tests/unit/logic/helpers/helpers.ipp +++ b/tests/unit/logic/helpers/helpers.ipp @@ -1,3 +1,4 @@ +// LED checked at selector's index template bool VerifyState(SM &uf, bool filamentLoaded, uint8_t idlerSlotIndex, uint8_t selectorSlotIndex, bool findaPressed, ml::Mode greenLEDMode, ml::Mode redLEDMode, ErrorCode err, ProgressCode topLevelProgress) { @@ -7,8 +8,45 @@ bool VerifyState(SM &uf, bool filamentLoaded, uint8_t idlerSlotIndex, uint8_t se CHECKED_ELSE(mm::axes[mm::Selector].pos == ms::Selector::SlotPosition(selectorSlotIndex)) { return false; } CHECKED_ELSE(ms::selector.Slot() == selectorSlotIndex) { return false; } CHECKED_ELSE(mf::finda.Pressed() == findaPressed) { return false; } - CHECKED_ELSE(ml::leds.Mode(selectorSlotIndex, ml::red) == redLEDMode) { return false; } - CHECKED_ELSE(ml::leds.Mode(selectorSlotIndex, ml::green) == greenLEDMode) { return false; } + + for(uint8_t ledIndex = 0; ledIndex < 5; ++ledIndex){ + if( ledIndex != selectorSlotIndex ){ + // the other LEDs should be off + CHECKED_ELSE(ml::leds.Mode(ledIndex, ml::red) == ml::off) { return false; } + CHECKED_ELSE(ml::leds.Mode(ledIndex, ml::green) == ml::off) { return false; } + } else { + CHECKED_ELSE(ml::leds.Mode(selectorSlotIndex, ml::red) == redLEDMode) { return false; } + CHECKED_ELSE(ml::leds.Mode(selectorSlotIndex, ml::green) == greenLEDMode) { return false; } + } + } + + CHECKED_ELSE(uf.Error() == err) { return false; } + CHECKED_ELSE(uf.TopLevelState() == topLevelProgress) { return false; } + return true; +} + +// LED checked at their own ledCheckIndex index +template +bool VerifyState2(SM &uf, bool filamentLoaded, uint8_t idlerSlotIndex, uint8_t selectorSlotIndex, + bool findaPressed, uint8_t ledCheckIndex, ml::Mode greenLEDMode, ml::Mode redLEDMode, ErrorCode err, ProgressCode topLevelProgress) { + CHECKED_ELSE(mg::globals.FilamentLoaded() == filamentLoaded) { return false; } + CHECKED_ELSE(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(idlerSlotIndex)) { return false; } + CHECKED_ELSE(mi::idler.Engaged() == (idlerSlotIndex < 5)) { return false; } + CHECKED_ELSE(mm::axes[mm::Selector].pos == ms::Selector::SlotPosition(selectorSlotIndex)) { return false; } + CHECKED_ELSE(ms::selector.Slot() == selectorSlotIndex) { return false; } + CHECKED_ELSE(mf::finda.Pressed() == findaPressed) { return false; } + + for(uint8_t ledIndex = 0; ledIndex < 5; ++ledIndex){ + if( ledIndex != ledCheckIndex ){ + // the other LEDs should be off + CHECKED_ELSE(ml::leds.Mode(ledIndex, ml::red) == ml::off) { return false; } + CHECKED_ELSE(ml::leds.Mode(ledIndex, ml::green) == ml::off) { return false; } + } else { + CHECKED_ELSE(ml::leds.Mode(ledCheckIndex, ml::red) == redLEDMode) { return false; } + CHECKED_ELSE(ml::leds.Mode(ledCheckIndex, ml::green) == greenLEDMode) { return false; } + } + } + CHECKED_ELSE(uf.Error() == err) { return false; } CHECKED_ELSE(uf.TopLevelState() == topLevelProgress) { return false; } return true;