From 807eda7db3d2db3ffaa8a95d2b6b95a82ba88b02 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 21 Jun 2021 11:07:08 +0200 Subject: [PATCH] Add prototypes of remaining logic layer's unit tests - load filament - feed to bondtech - tool change --- tests/unit/logic/CMakeLists.txt | 8 +- .../logic/feed_to_bondtech/CMakeLists.txt | 30 ++++ .../test_feed_to_bondtech.cpp | 154 ++++++++++++++++++ tests/unit/logic/load_filament/CMakeLists.txt | 32 ++++ .../load_filament/test_load_filament.cpp | 56 +++++++ tests/unit/logic/tool_change/CMakeLists.txt | 35 ++++ .../logic/tool_change/test_tool_change.cpp | 56 +++++++ 7 files changed, 366 insertions(+), 5 deletions(-) create mode 100644 tests/unit/logic/feed_to_bondtech/CMakeLists.txt create mode 100644 tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp create mode 100644 tests/unit/logic/load_filament/CMakeLists.txt create mode 100644 tests/unit/logic/load_filament/test_load_filament.cpp create mode 100644 tests/unit/logic/tool_change/CMakeLists.txt create mode 100644 tests/unit/logic/tool_change/test_tool_change.cpp diff --git a/tests/unit/logic/CMakeLists.txt b/tests/unit/logic/CMakeLists.txt index e6d8f8b..48def39 100644 --- a/tests/unit/logic/CMakeLists.txt +++ b/tests/unit/logic/CMakeLists.txt @@ -2,16 +2,14 @@ add_subdirectory(cut_filament) add_subdirectory(feed_to_finda) -# add_subdirectory(feed_to_bondtech) +add_subdirectory(feed_to_bondtech) add_subdirectory(unload_to_finda) add_subdirectory(eject_filament) -# add_subdirectory(load_filament) +add_subdirectory(load_filament) -# add_subdirectory(tool_change) - -# add_subdirectory(cut_filament) +add_subdirectory(tool_change) add_subdirectory(unload_filament) diff --git a/tests/unit/logic/feed_to_bondtech/CMakeLists.txt b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt new file mode 100644 index 0000000..dccbf2c --- /dev/null +++ b/tests/unit/logic/feed_to_bondtech/CMakeLists.txt @@ -0,0 +1,30 @@ +# define the test executable +add_executable( + feed_to_bondtech_tests + ../../../../src/logic/feed_to_bondtech.cpp + ../../../../src/modules/buttons.cpp + ../../../../src/modules/debouncer.cpp + ../../../../src/modules/finda.cpp + ../../../../src/modules/fsensor.cpp + ../../../../src/modules/globals.cpp + ../../../../src/modules/idler.cpp + ../../../../src/modules/leds.cpp + ../../../../src/modules/permanent_storage.cpp + ../../../../src/modules/selector.cpp + ../../modules/stubs/stub_adc.cpp + ../../modules/stubs/stub_eeprom.cpp + ../../modules/stubs/stub_shr16.cpp + ../../modules/stubs/stub_timebase.cpp + ../stubs/main_loop_stub.cpp + ../stubs/stub_motion.cpp + test_feed_to_bondtech.cpp + ) + +# define required search paths +target_include_directories( + feed_to_bondtech_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/modules ${CMAKE_SOURCE_DIR}/src/hal + ${CMAKE_SOURCE_DIR}/src/logic + ) + +# tell build system about the test case +add_catch_test(feed_to_bondtech_tests) 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 new file mode 100644 index 0000000..edd02eb --- /dev/null +++ b/tests/unit/logic/feed_to_bondtech/test_feed_to_bondtech.cpp @@ -0,0 +1,154 @@ +#include "catch2/catch.hpp" + +#include "../../../../src/modules/buttons.h" +#include "../../../../src/modules/finda.h" +#include "../../../../src/modules/fsensor.h" +#include "../../../../src/modules/globals.h" +#include "../../../../src/modules/idler.h" +#include "../../../../src/modules/leds.h" +#include "../../../../src/modules/motion.h" +#include "../../../../src/modules/permanent_storage.h" +#include "../../../../src/modules/selector.h" + +#include "../../../../src/logic/feed_to_bondtech.h" + +#include "../../modules/stubs/stub_adc.h" + +#include "../stubs/main_loop_stub.h" +#include "../stubs/stub_motion.h" + +using Catch::Matchers::Equals; + +namespace mm = modules::motion; +namespace mf = modules::finda; +namespace mi = modules::idler; +namespace ml = modules::leds; +namespace mb = modules::buttons; +namespace mg = modules::globals; +namespace ms = modules::selector; + +namespace ha = hal::adc; + +template +bool WhileCondition(logic::FeedToBondtech &ff, COND cond, uint32_t maxLoops = 5000) { + while (cond() && --maxLoops) { + main_loop(); + ff.Step(); + } + return maxLoops > 0; +} + +TEST_CASE("feed_to_finda::feed_phase_unlimited", "[feed_to_finda]") { + using namespace logic; + + ForceReinitAllAutomata(); + + FeedToBondtech fb; + main_loop(); + + // restart the automaton + fb.Reset(false); + + // REQUIRE(ff.State() == FeedToBondtech::EngagingIdler); + + // // it should have instructed the selector and idler to move to slot 1 + // // check if the idler and selector have the right command + // CHECK(mm::axes[mm::Idler].targetPos == mi::Idler::SlotPosition(0)); + // CHECK(mm::axes[mm::Selector].targetPos == ms::Selector::SlotPosition(0)); + // CHECK(mm::axes[mm::Idler].enabled == true); + + // // engaging idler + // REQUIRE(WhileCondition( + // ff, + // [&]() { return !mi::idler.Engaged(); }, + // 5000)); + + // CHECK(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(0)); + // CHECK(mm::axes[mm::Selector].pos == ms::Selector::SlotPosition(0)); + + // // idler engaged, selector in position, we'll start pushing filament + // REQUIRE(ff.State() == FeedToBondtech::PushingFilament); + // // at least at the beginning the LED should shine green (it should be blinking, but this mode has been already verified in the LED's unit test) + // REQUIRE(ml::leds.LedOn(mg::globals.ActiveSlot(), ml::Color::green)); + + // // now let the filament be pushed into the FINDA - do 500 steps without triggering the condition + // // and then let the simulated ADC channel 1 create a FINDA switch + // ha::ReinitADC(1, ha::TADCData({ 600, 700, 800, 900 }), 1); + + // REQUIRE(WhileCondition( + // ff, + // [&]() { return ff.State() == FeedToBondtech::PushingFilament; }, + // 1500)); + // // From now on the FINDA is reported as ON + + // // unloading back to PTFE + // REQUIRE(ff.State() == FeedToBondtech::UnloadBackToPTFE); + // REQUIRE(WhileCondition( + // ff, + // [&]() { return ff.State() == FeedToBondtech::UnloadBackToPTFE; }, + // 5000)); + + // // disengaging idler + // REQUIRE(ff.State() == FeedToBondtech::DisengagingIdler); + // REQUIRE(WhileCondition( + // ff, + // [&]() { return mi::idler.Engaged(); }, + // 5000)); + + // CHECK(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(5)); // @@TODO constants + // CHECK(mm::axes[mm::Selector].pos == ms::Selector::SlotPosition(0)); + + // // state machine finished ok, the green LED should be on + // REQUIRE(ff.State() == FeedToBondtech::OK); + // REQUIRE(ml::leds.LedOn(mg::globals.ActiveSlot(), ml::Color::green)); + + // REQUIRE(ff.Step() == true); // the automaton finished its work, any consecutive calls to Step must return true + //} + + //TEST_CASE("feed_to_finda::FINDA_failed", "[feed_to_finda]") { + // using namespace logic; + + // ForceReinitAllAutomata(); + + // FeedToBondtech ff; + // main_loop(); + + // // restart the automaton - we want the limited version of the feed + // ff.Reset(true); + + // REQUIRE(ff.State() == FeedToBondtech::EngagingIdler); + + // // it should have instructed the selector and idler to move to slot 1 + // // check if the idler and selector have the right command + // CHECK(mm::axes[mm::Idler].targetPos == mi::Idler::SlotPosition(0)); + // CHECK(mm::axes[mm::Selector].targetPos == ms::Selector::SlotPosition(0)); + + // // engaging idler + // REQUIRE(WhileCondition( + // ff, + // [&]() { return !mi::idler.Engaged(); }, + // 5000)); + + // CHECK(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(0)); + // CHECK(mm::axes[mm::Selector].pos == ms::Selector::SlotPosition(0)); + + // // idler engaged, we'll start pushing filament + // REQUIRE(ff.State() == FeedToBondtech::PushingFilament); + // // at least at the beginning the LED should shine green (it should be blinking, but this mode has been already verified in the LED's unit test) + // REQUIRE(ml::leds.Mode(mg::globals.ActiveSlot(), ml::Color::green) == ml::blink0); + + // // now let the filament be pushed into the FINDA - but we make sure the FINDA doesn't trigger at all + // ha::ReinitADC(1, ha::TADCData({ 0 }), 100); + + // REQUIRE(WhileCondition( + // ff, // boo, this formatting is UGLY! + // [&]() { return ff.State() == FeedToBondtech::PushingFilament; }, + // 5000)); + + // // the FINDA didn't trigger, we should be in the Failed state + // REQUIRE(ff.State() == FeedToBondtech::Failed); + // REQUIRE(ml::leds.Mode(mg::globals.ActiveSlot(), ml::Color::green) == ml::off); + // REQUIRE(ml::leds.Mode(mg::globals.ActiveSlot(), ml::Color::red) == ml::blink0); + + // REQUIRE(ff.Step() == true); // the automaton finished its work, any consecutive calls to Step must return true +} diff --git a/tests/unit/logic/load_filament/CMakeLists.txt b/tests/unit/logic/load_filament/CMakeLists.txt new file mode 100644 index 0000000..a402c2f --- /dev/null +++ b/tests/unit/logic/load_filament/CMakeLists.txt @@ -0,0 +1,32 @@ +# define the test executable +add_executable( + load_filament_tests + ../../../../src/logic/feed_to_bondtech.cpp + ../../../../src/logic/feed_to_finda.cpp + ../../../../src/logic/load_filament.cpp + ../../../../src/modules/buttons.cpp + ../../../../src/modules/debouncer.cpp + ../../../../src/modules/finda.cpp + ../../../../src/modules/fsensor.cpp + ../../../../src/modules/globals.cpp + ../../../../src/modules/idler.cpp + ../../../../src/modules/leds.cpp + ../../../../src/modules/permanent_storage.cpp + ../../../../src/modules/selector.cpp + ../../modules/stubs/stub_adc.cpp + ../../modules/stubs/stub_eeprom.cpp + ../../modules/stubs/stub_shr16.cpp + ../../modules/stubs/stub_timebase.cpp + ../stubs/main_loop_stub.cpp + ../stubs/stub_motion.cpp + test_load_filament.cpp + ) + +# define required search paths +target_include_directories( + load_filament_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/modules ${CMAKE_SOURCE_DIR}/src/hal + ${CMAKE_SOURCE_DIR}/src/logic + ) + +# tell build system about the test case +add_catch_test(load_filament_tests) diff --git a/tests/unit/logic/load_filament/test_load_filament.cpp b/tests/unit/logic/load_filament/test_load_filament.cpp new file mode 100644 index 0000000..4e65f52 --- /dev/null +++ b/tests/unit/logic/load_filament/test_load_filament.cpp @@ -0,0 +1,56 @@ +#include "catch2/catch.hpp" + +#include "../../../../src/modules/buttons.h" +#include "../../../../src/modules/finda.h" +#include "../../../../src/modules/fsensor.h" +#include "../../../../src/modules/globals.h" +#include "../../../../src/modules/idler.h" +#include "../../../../src/modules/leds.h" +#include "../../../../src/modules/motion.h" +#include "../../../../src/modules/permanent_storage.h" +#include "../../../../src/modules/selector.h" + +#include "../../../../src/logic/load_filament.h" + +#include "../../modules/stubs/stub_adc.h" + +#include "../stubs/main_loop_stub.h" +#include "../stubs/stub_motion.h" + +using Catch::Matchers::Equals; + +namespace mm = modules::motion; +namespace mf = modules::finda; +namespace mi = modules::idler; +namespace ml = modules::leds; +namespace mb = modules::buttons; +namespace mg = modules::globals; +namespace ms = modules::selector; + +TEST_CASE("unload_filament::unload0", "[unload_filament]") { + using namespace logic; + + ForceReinitAllAutomata(); + + LoadFilament lf; + // restart the automaton + currentCommand = &lf; + lf.Reset(0); + + main_loop(); + + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::UnloadingToFinda; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::DisengagingIdler); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::DisengagingIdler; }, 5000)); + + // CHECK(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(5)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::AvoidingGrind); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::AvoidingGrind; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::FinishingMoves); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::FinishingMoves; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::OK); +} diff --git a/tests/unit/logic/tool_change/CMakeLists.txt b/tests/unit/logic/tool_change/CMakeLists.txt new file mode 100644 index 0000000..d207344 --- /dev/null +++ b/tests/unit/logic/tool_change/CMakeLists.txt @@ -0,0 +1,35 @@ +# define the test executable +add_executable( + tool_change_tests + ../../../../src/logic/feed_to_bondtech.cpp + ../../../../src/logic/feed_to_finda.cpp + ../../../../src/logic/load_filament.cpp + ../../../../src/logic/tool_change.cpp + ../../../../src/logic/unload_filament.cpp + ../../../../src/logic/unload_to_finda.cpp + ../../../../src/modules/buttons.cpp + ../../../../src/modules/debouncer.cpp + ../../../../src/modules/finda.cpp + ../../../../src/modules/fsensor.cpp + ../../../../src/modules/globals.cpp + ../../../../src/modules/idler.cpp + ../../../../src/modules/leds.cpp + ../../../../src/modules/permanent_storage.cpp + ../../../../src/modules/selector.cpp + ../../modules/stubs/stub_adc.cpp + ../../modules/stubs/stub_eeprom.cpp + ../../modules/stubs/stub_shr16.cpp + ../../modules/stubs/stub_timebase.cpp + ../stubs/main_loop_stub.cpp + ../stubs/stub_motion.cpp + test_tool_change.cpp + ) + +# define required search paths +target_include_directories( + tool_change_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/modules ${CMAKE_SOURCE_DIR}/src/hal + ${CMAKE_SOURCE_DIR}/src/logic + ) + +# tell build system about the test case +add_catch_test(tool_change_tests) diff --git a/tests/unit/logic/tool_change/test_tool_change.cpp b/tests/unit/logic/tool_change/test_tool_change.cpp new file mode 100644 index 0000000..cdc8e8d --- /dev/null +++ b/tests/unit/logic/tool_change/test_tool_change.cpp @@ -0,0 +1,56 @@ +#include "catch2/catch.hpp" + +#include "../../../../src/modules/buttons.h" +#include "../../../../src/modules/finda.h" +#include "../../../../src/modules/fsensor.h" +#include "../../../../src/modules/globals.h" +#include "../../../../src/modules/idler.h" +#include "../../../../src/modules/leds.h" +#include "../../../../src/modules/motion.h" +#include "../../../../src/modules/permanent_storage.h" +#include "../../../../src/modules/selector.h" + +#include "../../../../src/logic/tool_change.h" + +#include "../../modules/stubs/stub_adc.h" + +#include "../stubs/main_loop_stub.h" +#include "../stubs/stub_motion.h" + +using Catch::Matchers::Equals; + +namespace mm = modules::motion; +namespace mf = modules::finda; +namespace mi = modules::idler; +namespace ml = modules::leds; +namespace mb = modules::buttons; +namespace mg = modules::globals; +namespace ms = modules::selector; + +TEST_CASE("tool_change::test0", "[tool_change]") { + using namespace logic; + + ForceReinitAllAutomata(); + + ToolChange tc; + // restart the automaton + currentCommand = &tc; + tc.Reset(0); + + main_loop(); + + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::UnloadingToFinda; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::DisengagingIdler); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::DisengagingIdler; }, 5000)); + + // CHECK(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(5)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::AvoidingGrind); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::AvoidingGrind; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::FinishingMoves); + // REQUIRE(WhileCondition([&]() { return uf.TopLevelState() == ProgressCode::FinishingMoves; }, 5000)); + + // REQUIRE(uf.TopLevelState() == ProgressCode::OK); +}