- Settings split on heater index

-General impl of hookRegistered functions
-registerRegexMsg recevies a compiled regex
-single regex for all pid types of respond
This commit is contained in:
sergiuToporjinschi
2022-02-04 10:14:46 +02:00
parent c3f86acc7d
commit 8b6cc8c444
9 changed files with 168 additions and 61 deletions
+28 -20
View File
@@ -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<P>\d{1,3}.\d{1,3})\s*Ki:\s*(?P<I>\d{1,3}.\d{1,3})\s*Kd:\s*(?P<D>\d{1,3}.\d{1,3})"
regexGetPid = "\s*Kp:\s*(?P<p>\d{1,3}.\d{1,3})\s*Ki:\s*(?P<i>\d{1,3}.\d{1,3})\s*Kd:\s*(?P<d>\d{1,3}.\d{1,3})"
PIDStarted = "PID Autotune start"
PIDStoped = "PID Autotune finished!"
PIDStopedP = "#define DEFAULT_Kp\s*(?P<P>\d{1,3}.\d{1,3})"
PIDStopedI = "#define DEFAULT_Ki\s*(?P<I>\d{1,3}.\d{1,3})"
PIDStopedD = "#define DEFAULT_Kd\s*(?P<D>\d{1,3}.\d{1,3})"
regexFinished = "PID Autotune finished!.*\n#define DEFAULT_Kp\s*(?P<P>\d{1,3}.\d{1,3}).*\n#define DEFAULT_Ki\s*(?P<I>\d{1,3}.\d{1,3})\n#define DEFAULT_Kd\s*(?P<D>\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<p>\d{1,3}.\d{1,3})\sk*i:{0,1}\s{0,1}?(?P<i>\d{1,3}.\d{1,3})\sk*d:{0,1}\s{0,1}?(?P<d>\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<p>\d{1,3}.\d{1,3})\s*i:?(?P<i>\d{1,3}.\d{1,3})\s*d:?(?P<d>\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)