Prusa-Firmware-MMU/tests/unit/logic/cut_filament/test_cut_filament.cpp

89 lines
3.4 KiB
C++

#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/cut_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("cut_filament::cut0", "[cut_filament]") {
using namespace logic;
ForceReinitAllAutomata();
CutFilament cf;
// restart the automaton
cf.Reset(0);
main_loop();
// it should have instructed the selector and idler to move to slot 1
// check if the idler and selector have the right command
CHECK(modules::motion::axes[modules::motion::Idler].targetPos == mi::Idler::SlotPosition(0));
CHECK(modules::motion::axes[modules::motion::Selector].targetPos == ms::Selector::SlotPosition(0));
// 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));
CHECK(modules::motion::axes[modules::motion::Idler].pos == mi::Idler::SlotPosition(0));
CHECK(modules::motion::axes[modules::motion::Selector].pos == ms::Selector::SlotPosition(0));
// idler and selector reached their target positions and the CF automaton will start feeding to FINDA as the next step
REQUIRE(cf.TopLevelState() == 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)
hal::adc::SetADC(1, 900);
}
return cf.TopLevelState() == ProgressCode::FeedingToFinda; }, 5000));
// filament fed into FINDA, cutting...
REQUIRE(cf.TopLevelState() == ProgressCode::PreparingBlade);
REQUIRE(WhileTopState(cf, ProgressCode::PreparingBlade, 5000));
REQUIRE(cf.TopLevelState() == ProgressCode::EngagingIdler);
REQUIRE(WhileTopState(cf, ProgressCode::EngagingIdler, 5000));
// the idler should be at the active slot @@TODO
REQUIRE(cf.TopLevelState() == ProgressCode::PushingFilament);
REQUIRE(WhileTopState(cf, ProgressCode::PushingFilament, 5000));
// filament pushed - performing cut
REQUIRE(cf.TopLevelState() == ProgressCode::PerformingCut);
REQUIRE(WhileTopState(cf, ProgressCode::PerformingCut, 5000));
// returning selector
REQUIRE(cf.TopLevelState() == ProgressCode::ReturningSelector);
REQUIRE(WhileTopState(cf, ProgressCode::ReturningSelector, 5000));
// the next states are still @@TODO
}
// comments:
// The tricky part of the whole state machine are the edge cases - filament not loaded, stall guards etc.
// ... all the external influence we can get on the real HW
// But the good news is we can simulate them all in the unit test and thus ensure proper handling