From ffd8924d8caec4fc017618b6c34eda37bd767cfd Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 1 Nov 2021 07:20:43 +0100 Subject: [PATCH] Fix start MMU with filament blocking the selector --- src/main.cpp | 48 +---------------------- src/modules/globals.cpp | 4 +- src/modules/selector.cpp | 11 ++++++ src/modules/selector.h | 6 +++ tests/unit/logic/helpers/helpers.ipp | 16 ++++++++ tests/unit/logic/stubs/main_loop_stub.cpp | 3 ++ tests/unit/modules/stubs/stub_eeprom.cpp | 5 +++ tests/unit/modules/stubs/stub_eeprom.h | 11 ++++++ 8 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 tests/unit/modules/stubs/stub_eeprom.h diff --git a/src/main.cpp b/src/main.cpp index ec8a632..9739e6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,49 +50,6 @@ logic::CommandBase *currentCommand = &logic::noCommand; /// will look like "X0 F" until a command (T, L, U ...) has been issued. mp::RequestMsg currentCommandRq(mp::RequestMsgCodes::Reset, 0); -// examples and test code shall be located here -void TmpPlayground() { - using namespace hal; - - // SPI example - // gpio::Init(gpio::GPIO_pin(GPIOC, 6), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::high)); - // uint8_t dat[5]; - // gpio::WritePin(gpio::GPIO_pin(GPIOC, 6), gpio::Level::low); - // spi::TxRx(SPI0, 0x01); - // spi::TxRx(SPI0, 0x00); - // spi::TxRx(SPI0, 0x00); - // spi::TxRx(SPI0, 0x00); - // spi::TxRx(SPI0, 0x00); - // gpio::WritePin(gpio::GPIO_pin(GPIOC, 6), gpio::Level::high); - // gpio::WritePin(gpio::GPIO_pin(GPIOC, 6), gpio::Level::low); - // dat[0] = spi::TxRx(SPI0, 0x00); - // dat[1] = spi::TxRx(SPI0, 0x00); - // dat[2] = spi::TxRx(SPI0, 0x00); - // dat[3] = spi::TxRx(SPI0, 0x00); - // dat[4] = spi::TxRx(SPI0, 0x00); - // gpio::WritePin(gpio::GPIO_pin(GPIOC, 6), gpio::Level::high); - // (void)dat; - - // using namespace hal::gpio; - // WritePin(GPIO_pin(GPIOB, 5), Level::low); - // TogglePin(GPIO_pin(GPIOB, 6)); - // if (hal::gpio::ReadPin(GPIO_pin(GPIOB, 7)) == hal::gpio::Level::low) - // break; - - sei(); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); - hu::usart1.puts("1234567890\n"); -} - /// One-time setup of HW and SW components /// Called before entering the loop() function /// Green LEDs signalize the progress of initialization. If anything goes wrong we shall turn on a red LED @@ -145,10 +102,7 @@ void setup() { mu::cdc.Init(); - if (mg::globals.FilamentLoaded() < mg::FilamentLoadState::InFSensor) { - // home the Selector only in case we don't have filament loaded (or at least we think we don't) - ms::selector.Home(); - } + ms::selector.Init(); // selector decides if homing is possible mi::idler.Home(); // home Idler every time _delay_ms(100); diff --git a/src/modules/globals.cpp b/src/modules/globals.cpp index ff9c960..69d7e02 100644 --- a/src/modules/globals.cpp +++ b/src/modules/globals.cpp @@ -9,7 +9,9 @@ namespace globals { Globals globals; void Globals::Init() { - mps::FilamentLoaded::get(activeSlot); //@@TODO check for errors + if (!mps::FilamentLoaded::get(activeSlot)) { + activeSlot = config::toolCount; + } if (activeSlot < config::toolCount) { // some valid slot has been recorded in EEPROM - we have some filament loaded in the selector or even in the nozzle diff --git a/src/modules/selector.cpp b/src/modules/selector.cpp index c362720..c7b2931 100644 --- a/src/modules/selector.cpp +++ b/src/modules/selector.cpp @@ -5,6 +5,7 @@ #include "motion.h" #include "permanent_storage.h" #include "../debug.h" +#include "globals.h" namespace modules { namespace selector { @@ -72,5 +73,15 @@ bool Selector::Step() { } } +void Selector::Init() { + if (mg::globals.FilamentLoaded() < mg::FilamentLoadState::InFSensor) { + // home the Selector only in case we don't have filament loaded (or at least we think we don't) + Home(); + } else { + // otherwise set selector's position according to know slot positions (and pretend it is correct) + mm::motion.SetPosition(mm::Selector, SlotPosition(mg::globals.ActiveSlot()).v); + } +} + } // namespace selector } // namespace modules diff --git a/src/modules/selector.h b/src/modules/selector.h index 186e330..113b276 100644 --- a/src/modules/selector.h +++ b/src/modules/selector.h @@ -38,6 +38,12 @@ public: /// @returns the index of idle position of the selector, usually 5 in case of 0-4 valid indices of filament slots inline static constexpr uint8_t IdleSlotIndex() { return config::toolCount; } + /// Initializes the selector after restart/cold boot + /// Reads the active slot from the EEPROM and decides if the selector is free or blocked by a piece of filament + /// - free -> home the selector + /// - blocked -> set selector's position according to the active filament slot + void Init(); + protected: virtual void PrepareMoveToPlannedSlot() override; virtual void PlanHomingMove() override; diff --git a/tests/unit/logic/helpers/helpers.ipp b/tests/unit/logic/helpers/helpers.ipp index 9b5189b..3b75a5a 100644 --- a/tests/unit/logic/helpers/helpers.ipp +++ b/tests/unit/logic/helpers/helpers.ipp @@ -3,6 +3,22 @@ bool VerifyEnvironmentState(mg::FilamentLoadState fls, uint8_t idlerSlotIndex, u CHECKED_ELSE(mg::globals.FilamentLoaded() & fls) { // beware - abusing the values as bit masks to detect multiple situations at once return false; } + if( mg::globals.FilamentLoaded() >= mg::FilamentLoadState::InSelector ){ + // check eeprom content - filament blocking the selector + uint8_t eeSlot = 0xff; + CHECKED_ELSE(mps::FilamentLoaded::get(eeSlot)){ + return false; + } + CHECKED_ELSE(eeSlot == selectorSlotIndex){ + return false; + } + } else { + // check eeprom content - filament blocking the selector + uint8_t eeSlot = 0xff; + CHECKED_ELSE(mps::FilamentLoaded::get(eeSlot) == false){ + return false; + } + } CHECKED_ELSE(mm::axes[mm::Idler].pos == mi::Idler::SlotPosition(idlerSlotIndex).v) { return false; } diff --git a/tests/unit/logic/stubs/main_loop_stub.cpp b/tests/unit/logic/stubs/main_loop_stub.cpp index 7943bb5..848ca17 100644 --- a/tests/unit/logic/stubs/main_loop_stub.cpp +++ b/tests/unit/logic/stubs/main_loop_stub.cpp @@ -1,6 +1,7 @@ #include "main_loop_stub.h" #include "../../modules/stubs/stub_adc.h" +#include "../../modules/stubs/stub_eeprom.h" #include "../../modules/stubs/stub_timebase.h" #include "../../../../src/modules/buttons.h" @@ -52,6 +53,8 @@ void ForceReinitAllAutomata() { new (&ms::selector) ms::Selector(); new (&mm::motion) mm::Motion(); + hal::eeprom::ClearEEPROM(); + // no buttons involved ;) hal::adc::ReinitADC(config::buttonsADCIndex, hal::adc::TADCData({ 1023 }), 1); diff --git a/tests/unit/modules/stubs/stub_eeprom.cpp b/tests/unit/modules/stubs/stub_eeprom.cpp index b0a2f59..bb22850 100644 --- a/tests/unit/modules/stubs/stub_eeprom.cpp +++ b/tests/unit/modules/stubs/stub_eeprom.cpp @@ -1,6 +1,7 @@ #include "catch2/catch.hpp" #include "../../../../src/hal/eeprom.h" +#include "stub_eeprom.h" #include #include @@ -14,6 +15,10 @@ EEPROM eeprom; constexpr uint16_t eepromSize = 2048U; static std::array EE; +void ClearEEPROM() { + std::fill(EE.begin(), EE.end(), 0xff); +} + /// EEPROM interface void EEPROM::WriteByte(EEPROM::addr_t offset, uint8_t value) { REQUIRE(offset < EE.size()); diff --git a/tests/unit/modules/stubs/stub_eeprom.h b/tests/unit/modules/stubs/stub_eeprom.h new file mode 100644 index 0000000..0ac2610 --- /dev/null +++ b/tests/unit/modules/stubs/stub_eeprom.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace hal { +namespace eeprom { + +void ClearEEPROM(); + +} // namespace eeprom +} // namespace hal