Add global state hive

+ solve many TODO's in the code (active slot/extruder and filament loaded flag)
pull/25/head
D.R.racer 2021-06-14 08:25:52 +02:00 committed by DRracer
parent 4d1a26aeea
commit d6c9e58e66
10 changed files with 105 additions and 44 deletions

View File

@ -166,15 +166,13 @@ if(CMAKE_CROSSCOMPILING)
# produce ASM listing
add_custom_command(
TARGET firmware POST_BUILD
COMMAND ${CMAKE_OBJDUMP} -CSd firmware > firmware.asm
)
TARGET firmware POST_BUILD COMMAND ${CMAKE_OBJDUMP} -CSd firmware > firmware.asm
)
# inform about the firmware's size in terminal
add_custom_command(
TARGET firmware POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega32u4 firmware
)
TARGET firmware POST_BUILD COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega32u4 firmware
)
report_size(firmware)
# generate linker map file
@ -199,6 +197,7 @@ target_sources(
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/motion.cpp

View File

@ -1,6 +1,7 @@
#include "cut_filament.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -14,15 +15,14 @@ CutFilament cutFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mg = modules::globals;
void CutFilament::Reset(uint8_t param) {
error = ErrorCode::OK;
bool isFilamentLoaded = true; //@@TODO
if (isFilamentLoaded) {
if (mg::globals.FilamentLoaded()) {
state = ProgressCode::UnloadingFilament;
unl.Reset(param); //@@TODO probably only act on active_extruder
unl.Reset(mg::globals.ActiveSlot());
} else {
SelectFilamentSlot();
}
@ -30,8 +30,8 @@ void CutFilament::Reset(uint8_t param) {
void CutFilament::SelectFilamentSlot() {
state = ProgressCode::SelectingFilamentSlot;
uint8_t newFilamentSlot = 0; //@@TODO
mi::idler.Engage(newFilamentSlot);
uint8_t newFilamentSlot = mg::globals.ActiveSlot() + 1; // move 1 slot aside
mi::idler.Engage(newFilamentSlot); //@@TODO does this make sense?
ms::selector.MoveToSlot(newFilamentSlot);
}
@ -64,16 +64,14 @@ bool CutFilament::Step() {
} else {
// move selector aside - prepare the blade into active position
state = ProgressCode::PreparingBlade;
uint8_t newFilamentSlot = 1; //@@TODO
ms::selector.MoveToSlot(newFilamentSlot + 1);
ms::selector.MoveToSlot(mg::globals.ActiveSlot());
}
}
break;
case ProgressCode::PreparingBlade:
if (mm::motion.QueueEmpty()) {
state = ProgressCode::EngagingIdler;
uint8_t newFilamentSlot = 0; //@@TODO
mi::idler.Engage(newFilamentSlot);
mi::idler.Engage(mg::globals.ActiveSlot());
}
break;
case ProgressCode::EngagingIdler:
@ -91,8 +89,7 @@ bool CutFilament::Step() {
case ProgressCode::PerformingCut:
if (mm::motion.QueueEmpty()) { // this may not be necessary if we want the selector and pulley move at once
state = ProgressCode::ReturningSelector;
uint8_t newFilamentSlot = 0; //@@TODO
ms::selector.MoveToSlot(newFilamentSlot); // return selector back
ms::selector.MoveToSlot(mg::globals.ActiveSlot()); // return selector back
}
break;
case ProgressCode::ReturningSelector:

View File

@ -1,6 +1,7 @@
#include "eject_filament.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -14,14 +15,13 @@ EjectFilament ejectFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mg = modules::globals;
void EjectFilament::Reset(uint8_t param) {
error = ErrorCode::OK;
slot = param;
bool isFilamentLoaded = true; //@@TODO
if (isFilamentLoaded) {
if (mg::globals.FilamentLoaded()) {
state = ProgressCode::UnloadingFilament;
unl.Reset(param); //@@TODO probably act on active extruder only
} else {

View File

@ -1,6 +1,7 @@
#include "feed_to_bondtech.h"
#include "../modules/buttons.h"
#include "../modules/fsensor.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -13,11 +14,12 @@ namespace mfs = modules::fsensor;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mp = modules::permanent_storage;
namespace mg = modules::globals;
void FeedToBondtech::Reset(uint8_t maxRetries) {
state = EngagingIdler;
this->maxRetries = maxRetries;
mi::idler.Engage(0 /*active_extruder*/); // @@TODO
mi::idler.Engage(mg::globals.ActiveSlot());
}
bool FeedToBondtech::Step() {
@ -27,7 +29,7 @@ bool FeedToBondtech::Step() {
case EngagingIdler:
if (mi::idler.Engaged()) {
state = PushingFilament;
ml::leds.SetMode(0, ml::Color::green, ml::blink0); //@@TODO active slot index
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::green, ml::blink0);
mm::motion.PlanMove(steps, 0, 0, 4500, 0, 0); //@@TODO constants - there was some strange acceleration sequence in the original FW,
// we can probably hand over some array of constants for hand-tuned acceleration + leverage some smoothing in the stepper as well
}
@ -46,7 +48,7 @@ bool FeedToBondtech::Step() {
case DisengagingIdler:
if (!mi::idler.Engaged()) {
state = OK;
ml::leds.SetMode(0, ml::Color::green, ml::on); //@@TODO active slot index
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::green, ml::on);
}
return false;
case OK:

View File

@ -1,6 +1,7 @@
#include "feed_to_finda.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -13,11 +14,12 @@ namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
void FeedToFinda::Reset(bool feedPhaseLimited) {
state = EngagingIdler;
this->feedPhaseLimited = feedPhaseLimited;
mi::idler.Engage(0 /*active_extruder*/); // @@TODO
mi::idler.Engage(mg::globals.ActiveSlot());
}
bool FeedToFinda::Step() {
@ -25,7 +27,7 @@ bool FeedToFinda::Step() {
case EngagingIdler:
if (mi::idler.Engaged()) {
state = PushingFilament;
ml::leds.SetMode(0, ml::Color::green, ml::blink0); //@@TODO active slot index
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::green, ml::blink0);
mm::motion.PlanMove(feedPhaseLimited ? 1500 : 65535, 0, 0, 4000, 0, 0); //@@TODO constants
}
return false;
@ -48,7 +50,7 @@ bool FeedToFinda::Step() {
case DisengagingIdler:
if (!mi::idler.Engaged()) {
state = OK;
ml::leds.SetMode(0, ml::Color::green, ml::on); //@@TODO active slot index
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::green, ml::on);
}
return false;
case OK:

View File

@ -1,6 +1,7 @@
#include "load_filament.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -16,11 +17,13 @@ namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mf = modules::finda;
namespace ml = modules::leds;
namespace mg = modules::globals;
void LoadFilament::Reset(uint8_t param) {
state = ProgressCode::EngagingIdler;
error = ErrorCode::OK;
mi::idler.Engage(param); // @@TODO
mg::globals.SetActiveSlot(param);
mi::idler.Engage(mg::globals.ActiveSlot());
}
bool LoadFilament::Step() {
@ -38,7 +41,7 @@ bool LoadFilament::Step() {
// @@TODO - try to repeat 6x - push/pull sequence - probably something to put into feed_to_finda as an option
state = ProgressCode::ERR1DisengagingIdler;
mi::idler.Disengage();
ml::leds.SetMode(0, ml::Color::red, ml::Mode::blink0); // signal loading error //@@TODO slot index
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::red, ml::Mode::blink0); // signal loading error
} else {
state = ProgressCode::FeedingToBondtech;
james.Reset(2);
@ -46,7 +49,7 @@ bool LoadFilament::Step() {
}
break;
case ProgressCode::FeedingToBondtech:
if (james.Step()) {
if (james.Step()) { // No, Mr. Bond, I expect you to FEED
switch (james.State()) {
case FeedToBondtech::Failed:
@ -62,6 +65,7 @@ bool LoadFilament::Step() {
}
break;
case ProgressCode::OK:
mg::globals.SetFilamentLoaded(true);
return true;
case ProgressCode::ERR1DisengagingIdler: // couldn't unload to FINDA
error = ErrorCode::FINDA_DIDNT_TRIGGER;
@ -82,8 +86,8 @@ bool LoadFilament::Step() {
Reset(0); // @@TODO param
} else if (userResolved) {
// problem resolved - the user pulled the fillament by hand
// modules::leds::leds.SetMode(active_extruder, modules::leds::red, modules::leds::off);
// modules::leds::leds.SetMode(active_extruder, modules::leds::green, modules::leds::on);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::red, modules::leds::off);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::green, modules::leds::on);
// mm::motion.PlanMove(mm::Pulley, 450, 5000); // @@TODO constants
state = ProgressCode::AvoidingGrind;
}

View File

@ -1,6 +1,7 @@
#include "tool_change.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
@ -14,18 +15,17 @@ ToolChange toolChange;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mg = modules::globals;
void ToolChange::Reset(uint8_t param) {
// if( param == active_extruder ) // @@TODO
// return true;
if (param == mg::globals.ActiveSlot())
return;
plannedSlot = param;
bool isFilamentLoaded = true; //@@TODO
if (isFilamentLoaded) {
if (mg::globals.FilamentLoaded()) {
state = ProgressCode::UnloadingFilament;
unl.Reset(0); //@@TODO act on active extruder only
unl.Reset(mg::globals.ActiveSlot());
} else {
state = ProgressCode::LoadingFilament;
load.Reset(plannedSlot);

View File

@ -1,9 +1,10 @@
#include "unload_filament.h"
#include "../modules/buttons.h"
#include "../modules/finda.h"
#include "../modules/globals.h"
#include "../modules/idler.h"
#include "../modules/leds.h"
#include "../modules/motion.h"
#include "../modules/idler.h"
#include "../modules/permanent_storage.h"
namespace logic {
@ -12,13 +13,14 @@ UnloadFilament unloadFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace mg = modules::globals;
void UnloadFilament::Reset(uint8_t param) {
// unloads filament from extruder - filament is above Bondtech gears
mm::motion.InitAxis(mm::Pulley);
state = ProgressCode::EngagingIdler;
error = ErrorCode::OK;
modules::idler::idler.Engage(0); //@@TODO
modules::idler::idler.Engage(mg::globals.ActiveSlot());
}
bool UnloadFilament::Step() {
@ -34,7 +36,7 @@ bool UnloadFilament::Step() {
if (unl.state == UnloadToFinda::Failed) {
// couldn't unload to FINDA, report error and wait for user to resolve it
state = ProgressCode::ERR1DisengagingIdler;
// modules::leds::leds.SetMode(active_extruder, modules::leds::red, modules::leds::blink0);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::red, modules::leds::blink0);
} else {
state = ProgressCode::DisengagingIdler;
}
@ -80,15 +82,15 @@ bool UnloadFilament::Step() {
Reset(0); // @@TODO param
} else if (userResolved) {
// problem resolved - the user pulled the fillament by hand
// modules::leds::leds.SetMode(active_extruder, modules::leds::red, modules::leds::off);
// modules::leds::leds.SetMode(active_extruder, modules::leds::green, modules::leds::on);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::red, modules::leds::off);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::green, modules::leds::on);
// mm::motion.PlanMove(mm::Pulley, 450, 5000); // @@TODO constants
state = ProgressCode::AvoidingGrind;
}
return false;
}
case ProgressCode::OK:
// isFilamentLoaded = false; // filament unloaded
mg::globals.SetFilamentLoaded(false); // filament unloaded
return true; // successfully finished
}
return false;

30
src/modules/globals.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "globals.h"
#include "permanent_storage.h"
namespace modules {
namespace globals {
void Globals::Init() {
modules::permanent_storage::FilamentLoaded::get(activeSlot); //@@TODO check for errors
// @@TODO where to obtain information whether a slot is loaded with a filament?
}
uint8_t Globals::ActiveSlot() const {
return activeSlot;
}
void Globals::SetActiveSlot(uint8_t newActiveSlot) {
activeSlot = newActiveSlot;
modules::permanent_storage::FilamentLoaded::set(activeSlot);
}
bool Globals::FilamentLoaded() const {
return filamentLoaded;
}
void Globals::SetFilamentLoaded(bool newFilamentLoaded) {
filamentLoaded = newFilamentLoaded;
}
} // namespace globals
} // namespace modules

25
src/modules/globals.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <stdint.h>
namespace modules {
namespace globals {
class Globals {
public:
void Init();
uint8_t ActiveSlot() const;
void SetActiveSlot(uint8_t newActiveSlot);
bool FilamentLoaded() const;
void SetFilamentLoaded(bool newFilamentLoaded);
private:
uint8_t activeSlot;
bool filamentLoaded;
};
extern Globals globals;
} // namespace globals
} // namespace modules