Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
marian42 committed Feb 3, 2019
2 parents 5dfb2a8 + 59d33e0 commit 88dacc5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 22 deletions.
107 changes: 87 additions & 20 deletions octoprint_preheat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from octoprint.util.comm import strip_comment

import flask
import time
from threading import Thread

class PreheatError(Exception):
def __init__(self, message):
Expand All @@ -22,23 +24,28 @@ def get_settings_defaults(self):
return dict(enable_tool = True,
enable_bed = True,
fallback_tool = 0,
fallback_bed = 0)
fallback_bed = 0,
wait_for_bed = False)


def get_template_configs(self):
return [
dict(type="settings", custom_bindings = False)
]


def get_assets(self):
return dict(
js = ["js/preheat.js"],
css = ["css/preheat.css"]
)


def get_api_commands(self):
return dict(
preheat = []
)


def parse_line(self, line):
line = strip_comment(line)
Expand All @@ -58,30 +65,29 @@ def parse_line(self, line):
tool = "tool" + item[1:].strip()

return tool, temperature


def apply_fallback_temperature(self):
fallback_successful = False
def get_fallback_temperature(self):
enable_bed = self._settings.get_boolean(["enable_bed"])
enable_tool = self._settings.get_boolean(["enable_tool"])
fallback_tool = self._settings.get_float(["fallback_tool"])
fallback_bed = self._settings.get_float(["fallback_bed"])

printer_profile = self._printer._printerProfileManager.get_current_or_default()

result = dict()

if enable_bed and fallback_bed > 0 and printer_profile["heatedBed"]:
self._logger.info("Using fallback temperature, preheating bed to " + str(fallback_bed))
self._printer.set_temperature("bed", fallback_bed)
fallback_successful = True
result["bed"] = fallback_bed

if enable_tool and fallback_tool > 0:
extruder_count = printer_profile["extruder"]["count"]
for i in range(extruder_count):
tool = "tool" + str(i)
self._logger.info("Using fallback temperature, preheating " + tool + " to " + str(fallback_tool))
self._printer.set_temperature(tool, fallback_tool)
fallback_successful = True
result[tool] = fallback_tool

return fallback_successful
return result


def get_temperatures(self):
printer = self._printer
Expand Down Expand Up @@ -122,22 +128,82 @@ def get_temperatures(self):
self._logger.exception("Something went wrong while trying to read the preheat temperature from {}".format(path_on_disk))

if len(temperatures) == 0:
raise PreheatError("Could not find a preheat command in the gcode file.")
return temperatures
temperatures = self.get_fallback_temperature()

if len(temperatures) == 0:
raise PreheatError("Could not find any preheat commands in the gcode file. You can configure fallback temperatures for this case.")
else:
self._logger.info("Could not find any preheat commands in the gcode file, using fallback temperatures.")

offsets = self._printer.get_current_data()["offsets"]
for tool in temperatures:
if tool in offsets:
temperatures[tool] += offsets[tool]

def preheat(self):
return temperatures


def check_state(self):
if not self._printer.is_operational() or self._printer.is_printing():
raise PreheatError("Can't set the temperature because the printer is not ready.")


def start_preheat_sequence(self):
self.check_state()

preheat_temperatures = self.get_temperatures()

if "bed" not in preheat_temperatures:
self.preheat()
return

bed_temp = preheat_temperatures["bed"]
self._logger.info("Preheating bed to " + str(bed_temp) + " and waiting.")
self._printer.set_temperature("bed", bed_temp)

thread = Thread(target = self.wait_for_bed_and_preheat, args = (preheat_temperatures, ))
thread.start()


def wait_for_bed_and_preheat(self, preheat_temperatures):
current_temperatures = self._printer.get_current_temperatures()

initial_targets = {tool: current_temperatures[tool]["target"] for tool in preheat_temperatures.keys()}
initial_targets["bed"] = preheat_temperatures["bed"]

while (True):
time.sleep(0.4)

current_temperatures = self._printer.get_current_temperatures()
for tool in preheat_temperatures.keys():
if current_temperatures[tool]["target"] != initial_targets[tool]:
self._logger.info("Preheating cancelled because the temperature was changed manually.")
return

if abs(current_temperatures["bed"]["actual"] - current_temperatures["bed"]["target"]) < 4:
break

try:
temperatures = self.get_temperatures()

for key in temperatures:
self._logger.info("Preheating " + key + " to " + str(temperatures[key]))
self._printer.set_temperature(key, temperatures[key])
self.preheat_all(preheat_temperatures)
except PreheatError as error:
if not self.apply_fallback_temperature():
raise PreheatError(str(error.message) + "\n" + "You can configure fallback temperatures in the plugin settings for this case.")
self._logger.info("Preheat error: " + str(error.message))


def preheat_all(self, temperatures = None):
self.check_state()

if temperatures is None:
temperatures = self.get_temperatures()

for tool, target in temperatures.iteritems():
self._logger.info("Preheating " + tool + " to " + str(target) + ".")
self._printer.set_temperature(tool, target)

def preheat(self):
if self._settings.get_boolean(["wait_for_bed"]):
self.start_preheat_sequence()
else:
self.preheat_all()

def on_api_command(self, command, data):
import flask
Expand All @@ -149,6 +215,7 @@ def on_api_command(self, command, data):
except PreheatError as error:
self._logger.info("Preheat error: " + str(error.message))
return str(error.message), 405


def get_update_information(self, *args, **kwargs):
return dict(
Expand Down
13 changes: 12 additions & 1 deletion octoprint_preheat/templates/preheat_settings.jinja2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<form class="form-horizontal">
<div class="control-group">
<legend>Enable preheating</legend>
<legend>Enable Preheating</legend>
<label class="control-label">Enable tool preheating</label>
<div class="controls">
<input type="checkbox" class="input-block-level" data-bind="checked: settings.plugins.preheat.enable_tool">
Expand Down Expand Up @@ -31,5 +31,16 @@
<span class="add-on">&deg;</span>
</div>
</div>
<br><br>

<legend>Sequenced Preheating</legend>
<div>
Preheat bed first, then heat printheads once the bed has reached it's target temperature.
If this option is disabled, the bed and printheads are heated right away.
If you don't have a heated bed, this option makes no difference.
</div><label class="control-label">Enable sequenced preheating</label>
<div class="controls">
<input type="checkbox" class="input-block-level" data-bind="checked: settings.plugins.preheat.wait_for_bed">
</div>
</div>
</form>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "Preheat"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.2.0"
plugin_version = "0.3.0"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 88dacc5

Please sign in to comment.