From 8b6cc8c44431a336d14b9e3f0ef2e6cd594ec42e Mon Sep 17 00:00:00 2001 From: sergiuToporjinschi Date: Fri, 4 Feb 2022 10:14:46 +0200 Subject: [PATCH] - Settings split on heater index -General impl of hookRegistered functions -registerRegexMsg recevies a compiled regex -single regex for all pid types of respond --- octoprint_CalibrationTools/EStepsApi.py | 2 +- octoprint_CalibrationTools/PIDAutoTune.py | 48 ++++++----- octoprint_CalibrationTools/__init__.py | 15 +++- octoprint_CalibrationTools/hooks.py | 4 +- .../static/js/CalibrationTools.js | 2 +- .../static/js/PIDTuneViewModel.js | 58 ++++++++++--- .../CalibrationTools_settings.jinja2 | 16 ++-- .../templates/tabs/tab-esteps.jinja2 | 2 +- .../templates/tabs/tab-pid.jinja2 | 82 +++++++++++++++---- 9 files changed, 168 insertions(+), 61 deletions(-) diff --git a/octoprint_CalibrationTools/EStepsApi.py b/octoprint_CalibrationTools/EStepsApi.py index a8e0fa4..c221e30 100644 --- a/octoprint_CalibrationTools/EStepsApi.py +++ b/octoprint_CalibrationTools/EStepsApi.py @@ -74,7 +74,7 @@ class API(octoprint.plugin.SimpleApiPlugin): self._printer.extrude(amount=int(extrudeLength), speed=int(extrudeSpeed)) @staticmethod - def m92GCodeResponse(self, line, event): + def m92GCodeResponse(self, line, regex, event): reg = re.compile("echo:\s*(?P(?PM\d{1,3}) X(?P\d{1,3}.\d{1,3}) Y(?P\d{1,3}.\d{1,3}) Z(?P\d{1,3}.\d{1,3}) E(?P\d{1,3}.\d{1,3}))") isM92command = reg.match(line) if isM92command: diff --git a/octoprint_CalibrationTools/PIDAutoTune.py b/octoprint_CalibrationTools/PIDAutoTune.py index ca9726a..74446fa 100644 --- a/octoprint_CalibrationTools/PIDAutoTune.py +++ b/octoprint_CalibrationTools/PIDAutoTune.py @@ -12,17 +12,20 @@ CMD_PID_START = "pid_start" CMD_PID_LOAD_CURRENT_VALUES = "pid_getCurrentValues" CMD_PID_GET_VALUES = "pid_getValues" -regexPID = "Kp:\s*(?P

\d{1,3}.\d{1,3})\s*Ki:\s*(?P\d{1,3}.\d{1,3})\s*Kd:\s*(?P\d{1,3}.\d{1,3})" -regexGetPid = "\s*Kp:\s*(?P

\d{1,3}.\d{1,3})\s*Ki:\s*(?P\d{1,3}.\d{1,3})\s*Kd:\s*(?P\d{1,3}.\d{1,3})" -PIDStarted = "PID Autotune start" -PIDStoped = "PID Autotune finished!" -PIDStopedP = "#define DEFAULT_Kp\s*(?P

\d{1,3}.\d{1,3})" -PIDStopedI = "#define DEFAULT_Ki\s*(?P\d{1,3}.\d{1,3})" -PIDStopedD = "#define DEFAULT_Kd\s*(?P\d{1,3}.\d{1,3})" -regexFinished = "PID Autotune finished!.*\n#define DEFAULT_Kp\s*(?P

\d{1,3}.\d{1,3}).*\n#define DEFAULT_Ki\s*(?P\d{1,3}.\d{1,3})\n#define DEFAULT_Kd\s*(?P\d{1,3}.\d{1,3})" +#this regex matches: +# !!DEBUG:send echo: Kp: 30.56 Ki: 3.03 Kd: 77.16 +# !!DEBUG:send Kp: 30.56 Ki: 3.03 Kd: 77.16 +# !!DEBUG:send echo: p:18.84 i:1.18 d:201.41 +# !!DEBUG:send p:18.84 i:1.18 d:201.41 +# !!DEBUG:send echo: M304 P131.06 I11.79 D971.23 +# !!DEBUG:send M304 P131.06 I11.79 D971.23 +allPIDsFormats = r".*p:{0,1}\s{0,1}?(?P

\d{1,3}.\d{1,3})\sk*i:{0,1}\s{0,1}?(?P\d{1,3}.\d{1,3})\sk*d:{0,1}\s{0,1}?(?P\d{1,3}.\d{1,3})" + class API(octoprint.plugin.SimpleApiPlugin): pidHotEndCycles = [] pidCurrentValues = {} + #catch for "echo: p:28.27 i:2.82 d:70.81" or "M301 P27.08 I2.51 D73.09" + getPid = re.compile(allPIDsFormats, flags=re.IGNORECASE) @staticmethod def apiCommands(): return { @@ -35,14 +38,10 @@ class API(octoprint.plugin.SimpleApiPlugin): def apiGateWay(self, command, data): self._logger.debug("DIPGateway") if command == CMD_PID_LOAD_CURRENT_VALUES: - #catch for "echo: p:28.27 i:2.82 d:70.81" or "M301 P27.08 I2.51 D73.09" - - getPid = re.compile(r".*p:??(?P

\d{1,3}.\d{1,3})\s*i:?(?P\d{1,3}.\d{1,3})\s*d:?(?P\d{1,3}.\d{1,3})", flags=re.IGNORECASE) - hasResult301 = Event() hasResult304 = Event() - self.registerRegexMsg(getPid, self.m301_m304CodeResponse, hasResult301, "hotEnd") - self.registerRegexMsg(getPid, self.m301_m304CodeResponse, hasResult304, "bed") + self.registerRegexMsg(self.getPid, self.m301_m304CodeResponse, hasResult301, "hotEnd") + self.registerRegexMsg(self.getPid, self.m301_m304CodeResponse, hasResult304, "bed") self._printer.commands(["M301", "M304"]) hasResult301.wait(5) @@ -53,14 +52,16 @@ class API(octoprint.plugin.SimpleApiPlugin): }) if command == CMD_PID_START: - self.pidHotEndCycles = [] - + self.pidHotEndCycles = { + "hotEnd": [], + "bed":[] + } #Two cycles are for tuning for i in range(0, data['noCycles'] - 2): - self.registerRegexMsg(regexGetPid, self.m106CodeResponse) + #response type !!DEBUG:send Kp: 30.56 Ki: 3.03 Kd: 77.16 + self.registerRegexMsg(self.getPid, self.m106CodeResponse, data["heater"]) self._printer.commands(["M106 S%(fanSpeed)s" % data, "M303 C%(noCycles)s E%(hotEndIndex)s S%(targetTemp)s U1" % data]) - self._logger.debug("cycles %s", self.pidHotEndCycles) if command == CMD_PID_SAVE: self._logger.debug("DIPSave-") @@ -87,6 +88,13 @@ class API(octoprint.plugin.SimpleApiPlugin): event.set() @staticmethod - def m106CodeResponse(self, line): + def m106CodeResponse(self, line, regex, storingKey): self._logger.debug("m106CodeResponse: %s", line) - self.pidHotEndCycles.append(line) + match = regex.match(line) + if match: + self.pidHotEndCycles[storingKey].append({ + "P": match.group("p"), + "I": match.group("i"), + "D": match.group("d") + }) + self._logger.debug("cycles %s", self.pidHotEndCycles) diff --git a/octoprint_CalibrationTools/__init__.py b/octoprint_CalibrationTools/__init__.py index 4fd1cc7..8475987 100644 --- a/octoprint_CalibrationTools/__init__.py +++ b/octoprint_CalibrationTools/__init__.py @@ -15,10 +15,17 @@ defaultSettings = { "markLength": 120 }, "pid": { - "fanSpeed": 255, - "noCycles": 5, - "hotEndIndex": 0, - "targetTemp": 200, + "hotEnd":{ + "fanSpeed": 255, + "noCycles": 8, + "hotEndIndex": 0, + "targetTemp": 200, + },"bed":{ + "fanSpeed": 255, + "noCycles": 8, + "hotEndIndex": -1, + "targetTemp": 70, + } } } diff --git a/octoprint_CalibrationTools/hooks.py b/octoprint_CalibrationTools/hooks.py index a993900..fc999cd 100644 --- a/octoprint_CalibrationTools/hooks.py +++ b/octoprint_CalibrationTools/hooks.py @@ -88,7 +88,7 @@ class Hooks(): if command is None or not reg.match(command.upper()): self._logger.warn("registerGCodeAnswer: Attempt to register gCodeAnswer without a function or valid gCode command") return - self.registerRegexMsg(".*\s*(?P[M,G]\d{1,4})", func, *arguments) + self.registerRegexMsg(reg, func, *arguments) def registerRegexMsg(self, regex, func, *arguments): if regex is None or func is None or not isinstance(func, collections.Callable): @@ -96,7 +96,7 @@ class Hooks(): return self.gCodeWaiters.append({ - "regex": re.compile(regex), + "regex": regex, "func": func, "args": arguments, "callCount": 0 diff --git a/octoprint_CalibrationTools/static/js/CalibrationTools.js b/octoprint_CalibrationTools/static/js/CalibrationTools.js index b674e58..6ce969e 100644 --- a/octoprint_CalibrationTools/static/js/CalibrationTools.js +++ b/octoprint_CalibrationTools/static/js/CalibrationTools.js @@ -48,7 +48,7 @@ $(function () { self.steps["E"](response.data.E); } - self.loadEStepsActive = ko.observable(true) + self.loadEStepsActive = ko.observable(true); self.loadESteps = function () { self.loadEStepsActive(false); OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_load").done(function (response) { diff --git a/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js b/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js index 8245c16..39270cb 100644 --- a/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js +++ b/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js @@ -20,10 +20,18 @@ $(function () { self.isAdmin = ko.observable(false); self.pid = { - fanSpeed: ko.observable(255), - noCycles: ko.observable(5), - hotEndIndex: ko.observable(0), - targetTemp: ko.observable(200) + "hotEnd": { + fanSpeed: ko.observable(255), + noCycles: ko.observable(8), + hotEndIndex: ko.observable(0), + targetTemp: ko.observable(200) + }, + "bed": { + fanSpeed: ko.observable(255), + noCycles: ko.observable(8), + index: ko.observable(-1), + targetTemp: ko.observable(200) + } }; /** @@ -48,19 +56,45 @@ $(function () { }; self.onBeforeBinding = self.onUserLoggedIn = self.onUserLoggedOut = function () { - self.pid.fanSpeed(self.settingsViewModel.settings.plugins.CalibrationTools.pid.fanSpeed()); - self.pid.noCycles(self.settingsViewModel.settings.plugins.CalibrationTools.pid.noCycles()); - self.pid.hotEndIndex(self.settingsViewModel.settings.plugins.CalibrationTools.pid.hotEndIndex()); - self.pid.targetTemp(self.settingsViewModel.settings.plugins.CalibrationTools.pid.targetTemp()); + self.pid.hotEnd.fanSpeed(self.settingsViewModel.settings.plugins.CalibrationTools.pid.hotEnd.fanSpeed()); + self.pid.hotEnd.hotEndIndex(self.settingsViewModel.settings.plugins.CalibrationTools.pid.hotEnd.hotEndIndex()); + self.pid.hotEnd.noCycles(self.settingsViewModel.settings.plugins.CalibrationTools.pid.hotEnd.noCycles()); + self.pid.hotEnd.targetTemp(self.settingsViewModel.settings.plugins.CalibrationTools.pid.hotEnd.targetTemp()); + self.pid.bed.index(-1); + self.pid.bed.noCycles(self.settingsViewModel.settings.plugins.CalibrationTools.pid.bed.noCycles()); + self.pid.bed.targetTemp(self.settingsViewModel.settings.plugins.CalibrationTools.pid.bed.targetTemp()); self.isAdmin(self.loginStateViewModel.isAdmin()); } self.startPidHotEnd = function () { OctoPrint.simpleApiCommand("CalibrationTools", "pid_start", { - "fanSpeed": self.pid.fanSpeed(), - "noCycles": self.pid.noCycles(), - "hotEndIndex": self.pid.hotEndIndex(), - "targetTemp": self.pid.targetTemp() + "fanSpeed": Number(self.pid.hotEnd.fanSpeed()), + "noCycles": Number(self.pid.hotEnd.noCycles()), + "hotEndIndex": Number(self.pid.hotEnd.hotEndIndex()), + "targetTemp": Number(self.pid.hotEnd.targetTemp()) + }).done(function (response) { + new PNotify({ + title: "PID HotEnd tuning has started", + text: "In progress", + type: "info" + }); + console.log(response); + }).fail(function (response) { + new PNotify({ + title: "Error on starting PID autotune ", + text: response.responseJSON.error, + type: "error", + hide: false + }); + }); + } + self.startPidBed = function () { + OctoPrint.simpleApiCommand("CalibrationTools", "pid_start", { + "heater": "bed", + "fanSpeed": self.pid.bed.fanSpeed(), + "noCycles": self.pid.bed.noCycles(), + "hotEndIndex": self.pid.bed.hotEndIndex(), + "targetTemp": self.pid.bed.targetTemp() }).done(function (response) { new PNotify({ title: "PID HotEnd tuning has started", diff --git a/octoprint_CalibrationTools/templates/CalibrationTools_settings.jinja2 b/octoprint_CalibrationTools/templates/CalibrationTools_settings.jinja2 index e7bd1ef..b0947af 100644 --- a/octoprint_CalibrationTools/templates/CalibrationTools_settings.jinja2 +++ b/octoprint_CalibrationTools/templates/CalibrationTools_settings.jinja2 @@ -5,7 +5,7 @@

-
+
@@ -25,8 +25,12 @@ {{ snipped.subSection("X-Y-Z-Steps", true) }} -{{ snipped.subSection("PID", true) }} -{{ snipped.field("Fan speed", "Default value for fan speed while tuning", "number", "settings.plugins.CalibrationTools.pid.fanSpeed", "true", "", 1, 0, 255) }} -{{ snipped.field("Number of cycles", "Default number of cycles to sample while tuning", "number", "settings.plugins.CalibrationTools.pid.noCycles", "true", "", 1, 3, 200) }} -{{ snipped.field("HotEnd index", "Default number of cycles to sample while tuning", "number", "settings.plugins.CalibrationTools.pid.hotEndIndex", "true", "", 1, 0) }} -{{ snipped.field("Target temperature", "Default target temperature for tuning", "number", "settings.plugins.CalibrationTools.pid.targetTemp", "true", "ºC", 1, 3, 200) }} \ No newline at end of file +{{ snipped.subSection("Hot-end PID", true) }} +{{ snipped.field("Fan speed", "Default value for fan speed while tuning", "number", "settings.plugins.CalibrationTools.pid.hotEnd.fanSpeed", "true", "", 1, 0, 255) }} +{{ snipped.field("Number of cycles", "Default number of cycles to sample while tuning", "number", "settings.plugins.CalibrationTools.pid.hotEnd.noCycles", "true", "", 1, 3, 200) }} +{{ snipped.field("HotEnd index", "Default number of cycles to sample while tuning", "number", "settings.plugins.CalibrationTools.pid.hotEnd.hotEndIndex", "true", "", 1, 0) }} +{{ snipped.field("Target temperature", "Default target temperature for tuning", "number", "settings.plugins.CalibrationTools.pid.hotEnd.targetTemp", "true", "ºC", 1, 3, 200) }} +{{ snipped.subSection("Bed PID", true) }} +{{ snipped.field("Fan speed", "Default value for fan speed while tuning", "number", "settings.plugins.CalibrationTools.pid.bed.fanSpeed", "true", "", 1, 0, 255) }} +{{ snipped.field("Number of cycles", "Default number of cycles to sample while tuning", "number", "settings.plugins.CalibrationTools.pid.bed.noCycles", "true", "", 1, 3, 200) }} +{{ snipped.field("Target temperature", "Default target temperature for tuning", "number", "settings.plugins.CalibrationTools.pid.bed.targetTemp", "true", "ºC", 1, 3, 200) }} \ No newline at end of file diff --git a/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 b/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 index 68bc1db..976a94f 100644 --- a/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 +++ b/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 @@ -61,7 +61,7 @@
- + steps/mm
@@ -38,7 +35,67 @@ +
+
+
+
+
+ +
+
+
+{{ snipped.subSection("Hot-end tuning", true) }} +
+
+ +
+
+
+ {{ snipped.linkToMarlin("M106", "Marlin website") }} + M106   S + +
+
+
+
+
+ +
+
+
+ {{ snipped.linkToMarlin("M303", "Marlin website") }} + M303   C + +    E + +    S + +    U1 + {{ snipped.m500Icon() }} +
+
+
+ +
+
+
+ +
+
+ +{{ snipped.subSection("Heated bed tuning", true) }}