From 57b0c068ebb1c96cc2ffc7cd4a03b991253a3393 Mon Sep 17 00:00:00 2001 From: sergiuToporjinschi Date: Tue, 1 Feb 2022 16:16:09 +0200 Subject: [PATCH] TryToSeparate controller --- octoprint_CalibrationTools/EStepsApi.py | 87 +++++++++++++++++++ octoprint_CalibrationTools/__init__.py | 2 +- .../static/js/CalibrationTools.js | 4 +- .../static/js/PIDTuneViewModel.js | 23 +++++ .../templates/CalibrationTools_tab.jinja2 | 7 +- .../templates/macros.jinja2 | 27 +++++- .../templates/tabs/tab-esteps.jinja2 | 21 ++--- .../templates/tabs/tab-pind.jinja2 | 58 ++++++++++++- 8 files changed, 207 insertions(+), 22 deletions(-) create mode 100644 octoprint_CalibrationTools/EStepsApi.py create mode 100644 octoprint_CalibrationTools/static/js/PIDTuneViewModel.js diff --git a/octoprint_CalibrationTools/EStepsApi.py b/octoprint_CalibrationTools/EStepsApi.py new file mode 100644 index 0000000..a9b559f --- /dev/null +++ b/octoprint_CalibrationTools/EStepsApi.py @@ -0,0 +1,87 @@ +from threading import Event + +CMD_ESP_TEST = "TEST" +CMD_ESP_LOAD_STEPS = "eSteps_load" +CMD_ESP_START_EXTRUSION = "eSteps_startExtrusion" +CMD_ESP_SAVE = "eSteps_save" + +class API(octoprint.plugin.SimpleApiPlugin): + + @staticmethod + def apiCommands(): + return { + CMD_ESP_SAVE: [], + CMD_ESP_LOAD_STEPS: [], + CMD_ESP_START_EXTRUSION: [] + } + + def apiGateWay(self, command, data): + self._logger.debug("api command [%s] received payload [%s]", command, data) + if command == CMD_ESP_LOAD_STEPS: + self._logger.debug("Load steps from EEPROM") + if not self._printer.is_ready(): + self._logger.warning("Printer not ready, operation canceled") + return flask.abort(503, { + "msg": "Printer not ready, operation canceled" + }) + + # Register listener waiting response for M92 command + m92Event = Event() + self.registerGCodeWaiter("M92", self.m92GCodeResponse, m92Event) + + # Issue M92 command + self._printer.commands("M92") + + m92Event.wait() + return flask.jsonify({ + "data": self.data["steps"] + }) + if command == CMD_ESP_START_EXTRUSION: + self._logger.debug("Heating the extruder [%s]", data) + if not self._printer.is_ready(): + self._logger.warning("Printer not ready, operation canceled") + return flask.abort(503, { + "msg": "Printer not ready, operation canceled" + }) + + # Register event to be trigger when temp is achieved + self.registerEventTemp("T0", int(data["extrudeTemp"]), self.startExtrusion, data["extrudeLength"], data["extrudeSpeed"]) + + # Heating the tool + self._printer.commands("M104 S%(extrudeTemp)s" % data) + return + + if command == CMD_ESP_SAVE: + eStepsSettings = self._settings.get(['eSteps']) + userControlsTemp = eStepsSettings.get("userControlsTemp") + turnOffHotend = eStepsSettings.get("turnOffHotend") + self._printer.commands(["M92 E%(newESteps)s" % data, "M500"] + ["M104 S0"] if turnOffHotend and not userControlsTemp else []) + return +############## HANDLERS ############## + @staticmethod + def startExtrusion(self, temps, extrudeLength, extrudeSpeed, *args): + self._logger.debug("Temperature achieved, extrusion started [temps:%s, extrudeLength:%s, extrudeSpeed:%s, args:%s]", + temps, extrudeLength, extrudeSpeed, args) + + # Extrude + self._printer.extrude(amount=int(extrudeLength), speed=int(extrudeSpeed)) + + @staticmethod + def m92GCodeResponse(self, line, 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: + command = isM92command.group("command") + if isM92command.group("gCode") == "M92": + self.data["steps"]["X"] = float(isM92command.group("xVal")) + self.data["steps"]["Y"] = float(isM92command.group("yVal")) + self.data["steps"]["Z"] = float(isM92command.group("zVal")) + self.data["steps"]["E"] = float(isM92command.group("eVal")) + + # Send the new data to the UI to be reloaded + self._logger.debug("gCode: %s", command) + self._logger.debug("X: %s, Y:%s, Z:%s, E:%s", self.data["steps"]["X"], self.data["steps"]["Y"], self.data["steps"]["Z"], self.data["steps"]["E"]) + self._logger.debug("Finished data collection") + self.collectCommand = False + if event: + event.set() diff --git a/octoprint_CalibrationTools/__init__.py b/octoprint_CalibrationTools/__init__.py index becb1f8..33dfd7e 100644 --- a/octoprint_CalibrationTools/__init__.py +++ b/octoprint_CalibrationTools/__init__.py @@ -7,7 +7,7 @@ from octoprint_CalibrationTools import api, hooks, models defaultSettings = { "eSteps": { - "userControlsTemp": "checked", + "userControlsTemp": "true", "turnOffHotend": "", "extrudeTemp": 210, "extrudeLength": 100, diff --git a/octoprint_CalibrationTools/static/js/CalibrationTools.js b/octoprint_CalibrationTools/static/js/CalibrationTools.js index a259c44..c14bbbd 100644 --- a/octoprint_CalibrationTools/static/js/CalibrationTools.js +++ b/octoprint_CalibrationTools/static/js/CalibrationTools.js @@ -80,7 +80,7 @@ $(function () { type: "error", hide: false }); - }) + }) } self.tempRestart = function () { @@ -130,6 +130,6 @@ $(function () { // instantiation via the parameters argument dependencies: ["loginStateViewModel", "settingsViewModel", "controlViewModel", "terminalViewModel", "accessViewModel"], // Finally, this is the list of selectors for all elements we want this view model to be bound to. - elements: ["#tab_plugin_CalibrationTools"] + elements: ["#calibration_eSteps"] }); }); \ No newline at end of file diff --git a/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js b/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js new file mode 100644 index 0000000..c94809e --- /dev/null +++ b/octoprint_CalibrationTools/static/js/PIDTuneViewModel.js @@ -0,0 +1,23 @@ +$(function () { + function CalibrationToolsPIDTuneViewModel(parameters) { + var self = this; + + self.bedCurrentTemp = ko.observable(0); + self.bedCurrentTarget = ko.observable(0); + + OctoPrint.printer.getBedState().done(function (bedState) { + self.bedCurrentTemp(bedState.bed.actual); + self.bedCurrentTarget(bedState.bed.target); + }); + } + OCTOPRINT_VIEWMODELS.push({ + // This is the constructor to call for instantiating the plugin + construct: CalibrationToolsPIDTuneViewModel, + // This is a list of dependencies to inject into the plugin, the order which you request + // here is the order in which the dependencies will be injected into your view model upon + // instantiation via the parameters argument + dependencies: ["loginStateViewModel", "settingsViewModel", "controlViewModel", "terminalViewModel", "accessViewModel"], + // Finally, this is the list of selectors for all elements we want this view model to be bound to. + elements: ["#calibration_pid"] + }); +}); \ No newline at end of file diff --git a/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 b/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 index b710ae0..fdfcacd 100644 --- a/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 +++ b/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 @@ -7,11 +7,10 @@
- - @@ -37,7 +36,7 @@ X-Y-Z-Steps
  • - PID + PID Autotune
  • diff --git a/octoprint_CalibrationTools/templates/macros.jinja2 b/octoprint_CalibrationTools/templates/macros.jinja2 index 918a27a..eece5b2 100644 --- a/octoprint_CalibrationTools/templates/macros.jinja2 +++ b/octoprint_CalibrationTools/templates/macros.jinja2 @@ -18,8 +18,10 @@ {% endmacro %} -{% macro field(label, title, type, binding, enable, unit, step) %} +{% macro field(label, title, type, binding, enable, unit, step, min, max) %} {% set step = step|default('0.01') %} +{% set min = min|default("") %} +{% set max = max|default("") %}
    - + + {{ _(unit) }} +
    +
    +
    +{% endmacro %} + + +{% macro labelField(label, title, type, binding, enable, unit, step, min, max) %} +{% set step = step|default('0.01') %} +{% set min = min|default("") %} +{% set max = max|default("") %} +
    +
    + +
    +
    +
    + {{ _(label) }} + {{ _(unit) }}
    diff --git a/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 b/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 index 8dd7cda..7f86f89 100644 --- a/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 +++ b/octoprint_CalibrationTools/templates/tabs/tab-esteps.jinja2 @@ -12,12 +12,11 @@
    M104   S - +
    -
    @@ -28,22 +27,21 @@
    G1   E - +    F -
    -{{ snipped.field("Extrusion marking length", "The length marked on filament before extrusion. ", "number", "$root.testParam.markLength", "true", "mm") }} +{{ snipped.field("Extrusion marking length", "The length marked on filament before extrusion. ", "number", "$root.testParam.markLength", "true", "mm", 0.01, 50) }}
    @@ -60,11 +58,10 @@
    - + steps/mm
    @@ -93,7 +90,7 @@
    diff --git a/octoprint_CalibrationTools/templates/tabs/tab-pind.jinja2 b/octoprint_CalibrationTools/templates/tabs/tab-pind.jinja2 index 9b490a1..f6500dc 100644 --- a/octoprint_CalibrationTools/templates/tabs/tab-pind.jinja2 +++ b/octoprint_CalibrationTools/templates/tabs/tab-pind.jinja2 @@ -1 +1,57 @@ -Something in pid tab... \ No newline at end of file +{% import "macros.jinja2" as snipped %} + +Current bed temperature: ° Bed target temperature ° +{{ snipped.subSection("Tool tuning", true) }} + + +
    +
    + +
    +
    +
    + M106   S + +
    +
    +
    +
    +
    + +
    +
    +
    + M303   E + + S + + U1 +
    +
    +
    + +
    +
    +
    + +
    +
    + +Save M501 + + +{{ snipped.subSection("Heated bed tuning", true) }} + + +{{ snipped.subSection("Note", true) }} +{{ snipped.quote("It is recommended to run the tuning with conditions as close to printing as possible. This means filament loaded and the part cooling fan set to your normal speed. It is not essential, but you may +prefer to start this process with the hot end at room temperature.", +"teachingtechyt.github.io", "text-warning") }} \ No newline at end of file