Skip to content

Commit

Permalink
0.0.19 (#24)
Browse files Browse the repository at this point in the history
* attempt to fix A1 related print issues, #9 
* flow rate increase logic for M220 from x1plus community
  • Loading branch information
jneilliii authored May 12, 2024
1 parent 9081732 commit 3e77084
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
45 changes: 39 additions & 6 deletions octoprint_bambu_printer/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import collections
import datetime
import math
import os
import queue
import re
Expand Down Expand Up @@ -162,13 +163,14 @@ def new_update(self, event_type):

# self._logger.debug(device_data)

self.lastTempAt = time.monotonic()
self.temp[0] = temperatures.get("nozzle_temp", 0.0)
self.targetTemp[0] = temperatures.get("target_nozzle_temp", 0.0)
self.bedTemp = temperatures.get("bed_temp", 0.0)
self.bedTargetTemp = temperatures.get("target_bed_temp", 0.0)
self.chamberTemp = temperatures.get("chamber_temp", 0.0)

if print_job.get("gcode_state") == "RUNNING" or print_job.get("gcode_state") == "PREPARE":
if print_job.get("gcode_state") == "RUNNING":
if not self._sdPrintingSemaphore.is_set():
self._sdPrintingSemaphore.set()
if self._sdPrintingPausedSemaphore.is_set():
Expand All @@ -181,6 +183,8 @@ def new_update(self, event_type):
filename = f"{filename.lower()}.3mf"
elif self._sdFileListCache.get(f"{filename.lower()}.gcode.3mf"):
filename = f"{filename.lower()}.gcode.3mf"
else:
self._logger.debug(f"No 3mf file found for {print_job}")

self._selectSdFile(filename)
self._startSdPrint(from_printer=True)
Expand Down Expand Up @@ -215,6 +219,10 @@ def on_disconnect(self, on_disconnect):
self._logger.debug(f"on disconnect called")
return on_disconnect

def on_connect(self, on_connect):
self._logger.debug(f"on connect called")
return on_connect

async def _create_connection_async(self):
self._logger.debug(f"connecting via local mqtt: {self._settings.get_boolean(['local_mqtt'])}")
self.bambu = BambuClient(device_type=self._settings.get(["device_type"]),
Expand All @@ -228,6 +236,7 @@ async def _create_connection_async(self):
auth_token=self._settings.get(["auth_token"])
)
self.bambu.on_disconnect = self.on_disconnect(self.bambu.on_disconnect)
self.bambu.on_connect = self.on_connect(self.bambu.on_connect)
self.bambu.connect(callback=self.new_update)
self._logger.info(f"bambu connection status: {self.bambu.connected}")
self._sendOk()
Expand Down Expand Up @@ -556,8 +565,7 @@ def _gcode_M33(self, data: str) -> bool:

# noinspection PyUnusedLocal
def _gcode_M105(self, data: str) -> bool:
self._processTemperatureQuery()
return True
return self._processTemperatureQuery()

# noinspection PyUnusedLocal
def _gcode_M115(self, data: str) -> bool:
Expand Down Expand Up @@ -590,6 +598,26 @@ def _gcode_M118(self, data: str) -> bool:
self._send(text)
return True

# noinspection PyUnusedLocal
def _gcode_M220(self, data: str) -> bool:
if self.bambu.connected:
gcode_command = commands.SEND_GCODE_TEMPLATE
percent = int(data[1:])

if percent is None or percent < 1 or percent > 166:
return True

speed_fraction = 100 / percent
acceleration = math.exp((speed_fraction - 1.0191) / -0.814)
feed_rate = (2.1645 * (acceleration ** 3) - 5.3247 * (acceleration ** 2) + 4.342 * acceleration - 0.181)
speed_level = 1.539 * (acceleration ** 2) - 0.7032 * acceleration + 4.0834
speed_command = f"M204.2 K${acceleration:.2f} \nM220 K${feed_rate:.2f} \nM73.2 R${speed_fraction:.2f} \nM1002 set_gcode_claim_speed_level ${speed_level:.0f}\n"

gcode_command['print']['param'] = speed_command
if self.bambu.publish(gcode_command):
self._logger.info(f"{percent}% speed adjustment command sent successfully")
return True

# noinspection PyUnusedLocal
def _gcode_M400(self, data: str) -> bool:
return True
Expand Down Expand Up @@ -704,6 +732,7 @@ def _getSdFileData(self, filename: str) -> Optional[Dict[str, Any]]:
data = self._sdFileListCache.get(filename.lower())
if isinstance(data, str):
data = self._sdFileListCache.get(data.lower())
self._logger.debug(f"_getSdFileData: {data}")
return data

def _getSdFiles(self) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -794,10 +823,14 @@ def _generateTemperatureOutput(self) -> str:
output += " @:64\n"
return output

def _processTemperatureQuery(self):
def _processTemperatureQuery(self) -> bool:
# includeOk = not self._okBeforeCommandOutput
output = self._generateTemperatureOutput()
self._send(output)
if self.bambu.connected:
output = self._generateTemperatureOutput()
self._send(output)
return True
else:
return False

def _writeSdFile(self, filename: str) -> None:
self._send(f"Writing to file: {filename}")
Expand Down
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 = "OctoPrint-BambuPrinter"

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

# 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 3e77084

Please sign in to comment.