diff --git a/src/logic/feed_to_finda.cpp b/src/logic/feed_to_finda.cpp new file mode 100644 index 0000000..461e77e --- /dev/null +++ b/src/logic/feed_to_finda.cpp @@ -0,0 +1,61 @@ +#include "feed_to_finda.h" +#include "../modules/buttons.h" +#include "../modules/finda.h" +#include "../modules/idler.h" +#include "../modules/leds.h" +#include "../modules/motion.h" +#include "../modules/permanent_storage.h" + +namespace logic { + +namespace mm = modules::motion; +namespace mf = modules::finda; +namespace mi = modules::idler; +namespace ml = modules::leds; +namespace mb = modules::buttons; + +void FeedToFinda::Reset(bool feedPhaseLimited) { + state = EngagingIdler; + this->feedPhaseLimited = feedPhaseLimited; + mi::idler.Engage(0 /*active_extruder*/); // @@TODO +} + +bool FeedToFinda::Step() { + switch (state) { + case EngagingIdler: + if (mi::idler.Engaged()) { + state = PushingFilament; + ml::leds.SetMode(0, ml::Color::green, ml::blink0); //@@TODO active slot index + mm::motion.PlanMove(feedPhaseLimited ? 1500 : 65535, 0, 0, 4000, 0, 0); //@@TODO constants + } + return false; + case PushingFilament: + if (mf::finda.Pressed() || (feedPhaseLimited && mb::buttons.AnyButtonPressed())) { // @@TODO probably also a command from the printer + mm::motion.AbortPlannedMoves(); // stop pushing filament + // FINDA triggered - that means it works and detected the filament tip + state = UnloadBackToPTFE; + mm::motion.PlanMove(-600, 0, 0, 4000, 0, 0); //@@TODO constants + } else if (mm::motion.QueueEmpty()) { // all moves have been finished and FINDA didn't switch on + state = Failed; + } + return false; + case UnloadBackToPTFE: + if (mm::motion.QueueEmpty()) { // all moves have been finished + state = DisengagingIdler; + mi::idler.Disengage(); + } + return false; + case DisengagingIdler: + if (!mi::idler.Engaged()) { + state = OK; + ml::leds.SetMode(0, ml::Color::green, ml::on); //@@TODO active slot index + } + return false; + case OK: + case Failed: + default: + return true; + } +} + +} // namespace logic diff --git a/src/logic/feed_to_finda.h b/src/logic/feed_to_finda.h new file mode 100644 index 0000000..6c01cfd --- /dev/null +++ b/src/logic/feed_to_finda.h @@ -0,0 +1,44 @@ +#pragma once +#include + +/// @brief Feed filament to FINDA +/// +/// Continuously feed filament until FINDA is not switched ON +/// and than retract to align filament 600 steps away from FINDA. + +namespace logic { + +struct FeedToFinda { + enum { + EngagingIdler, + PushingFilament, + UnloadBackToPTFE, + DisengagingIdler, + OK, + Failed + }; + + inline FeedToFinda() + : state(OK) + , feedPhaseLimited(true) {} + + /// Restart the automaton + /// @param feedPhaseLimited + /// * true feed phase is limited, doesn't react on button press + /// * false feed phase is unlimited, can be interrupted by any button press after blanking time + void Reset(bool feedPhaseLimited); + + /// @returns true if the state machine finished its job, false otherwise + bool Step(); + + /// This method may be used to check the result of the automaton + /// @returns OK if everything went OK and FINDA triggered (or the user pressed a button) + /// @returns Failed if the FINDA didn't trigger + inline uint8_t State() const { return state; } + +private: + uint8_t state; + bool feedPhaseLimited; +}; + +} // namespace logic