From f45a1aec0cd85285f14d0c612e0509b82035ac60 Mon Sep 17 00:00:00 2001 From: Manuel Coenen Date: Mon, 13 Aug 2018 14:32:25 +0200 Subject: [PATCH] Limit sum of mix ratio to 1 unless disabled by M567 S0 --- src/GCodes/GCodes2.cpp | 13 +++++++++++-- src/Tools/Tool.cpp | 24 +++++++++++++++++++++++- src/Tools/Tool.h | 7 ++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp index d0e1e392d8..3a39fde1d8 100644 --- a/src/GCodes/GCodes2.cpp +++ b/src/GCodes/GCodes2.cpp @@ -3107,8 +3107,14 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) Tool* const tool = reprap.GetTool(tNumber); if (tool != nullptr) { + bool seen = false; + if (gb.Seen('S')) { + seen = true; + tool->SetCanExceedMixSumOf1(gb.GetIValue() == 0); + } if (gb.Seen(extrudeLetter)) { + seen = true; float eVals[MaxExtruders]; size_t eCount = tool->DriveCount(); gb.GetFloatArray(eVals, eCount, false); @@ -3118,10 +3124,12 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) } else { - tool->DefineMix(eVals); + if (!tool->DefineMix(eVals)) { + reply.printf("Setting mix ratios - sum of ratios > 1.0. Disable this check with M567 P%d S0", tNumber); + } } } - else + if (!seen) { reply.printf("Tool %d mix ratios:", tNumber); char sep = ' '; @@ -3130,6 +3138,7 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) reply.catf("%c%.3f", sep, (double)tool->GetMix()[drive]); sep = ':'; } + reply.printf(". Mix ratio sum of 1.0 can be exceeded: %s", tool->GetCanExceedMixSumOf1() ? "true" : "false"); } } } diff --git a/src/Tools/Tool.cpp b/src/Tools/Tool.cpp index c2622adff2..308d5c2dd9 100644 --- a/src/Tools/Tool.cpp +++ b/src/Tools/Tool.cpp @@ -116,6 +116,7 @@ Tool * Tool::freelist = nullptr; t->heaterFault = false; t->axisOffsetsProbed = 0; t->displayColdExtrudeWarning = false; + t->canExceedMixSumOf1 = false; for (size_t axis = 0; axis < MaxAxes; axis++) { @@ -356,12 +357,33 @@ bool Tool::DisplayColdExtrudeWarning() return result; } -void Tool::DefineMix(const float m[]) +bool Tool::DefineMix(const float m[]) { + if (this->CheckExceedsMixSumOf1(m)) { + return false; + } for(size_t drive = 0; drive < driveCount; drive++) { mix[drive] = m[drive]; } + return true; +} + +bool Tool::CheckExceedsMixSumOf1(const float m[]) const { + // We don't need to check if this is true + if (this->canExceedMixSumOf1) { + return false; + } + + float sum = 0.0; + // Only check for the amount of configured drives + for(size_t drive = 0; drive < driveCount; drive++) { + sum += m[drive]; + if (sum > 1.0) { + return true; + } + } + return false; } // Write the tool's settings to file returning true if successful diff --git a/src/Tools/Tool.h b/src/Tools/Tool.h index ba859e4502..b1b819ca51 100644 --- a/src/Tools/Tool.h +++ b/src/Tools/Tool.h @@ -58,7 +58,9 @@ class Tool int Heater(size_t heaterNumber) const; const char *GetName() const; int Number() const; - void DefineMix(const float m[]); + bool DefineMix(const float m[]); + void SetCanExceedMixSumOf1(const bool m) { canExceedMixSumOf1 = m; } + bool GetCanExceedMixSumOf1() const { return canExceedMixSumOf1; } const float* GetMix() const; float MaxFeedrate() const; void Print(const StringRef& reply) const; @@ -94,11 +96,14 @@ class Tool void ResetTemperatureFault(int8_t wasDudHeater); bool AllHeatersAtHighTemperature(bool forExtrusion) const; + bool CheckExceedsMixSumOf1(const float m[]) const; + Tool* next; Filament *filament; char *name; float offset[MaxAxes]; float mix[MaxExtruders]; + bool canExceedMixSumOf1; float activeTemperatures[Heaters]; float standbyTemperatures[Heaters]; size_t driveCount;