LimitXYAccelJerk.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # Limit XY Accel: Authored by: Greg Foresi (GregValiant)
  2. # July 2023
  3. # Sometimes bed-slinger printers need different Accel and Jerk values for the Y but Cura always makes them the same.
  4. # This script changes the Accel and/or Jerk from the beginning of the 'Start Layer' to the end of the 'End Layer'.
  5. # The existing M201 Max Accel will be changed to limit the Y (and/or X) accel at the printer. If you have Accel enabled in Cura and the XY Accel is set to 3000 then setting the Y limit to 1000 will result in the printer limiting the Y to 1000. This can keep tall skinny prints from breaking loose of the bed and failing. The script was not tested with Junction Deviation.
  6. # If enabled - the Jerk setting is changed line-by-line within the gcode as there is no "limit" on Jerk.
  7. # if 'Gradual ACCEL change' is enabled then the Accel is changed gradually from the Start to the End layer and that will be the final Accel setting in the file. If 'Gradual' is enabled then the Jerk settings will continue to be changed to the end of the file (rather than ending at the End layer).
  8. # This post is intended for printers with moving beds (bed slingers) so UltiMaker printers are excluded.
  9. # When setting an accel limit on multi-extruder printers ALL extruders are effected.
  10. # This post does not distinguish between Print Accel and Travel Accel. The limit is the limit for all regardless. Example: Skin Accel = 1000 and Outer Wall accel = 500. If the limit is set to 300 then both Skin and Outer Wall will be Accel = 300.
  11. # 9/15/2023 added support for RepRap M566 command for Jerk in mm/min
  12. # 2/4/2024 Added a block so the script doesn't run unless Accel Control is enabled in Cura. This should keep users from increasing the Accel Limits.
  13. from ..Script import Script
  14. from cura.CuraApplication import CuraApplication
  15. import re
  16. from UM.Message import Message
  17. class LimitXYAccelJerk(Script):
  18. def initialize(self) -> None:
  19. super().initialize()
  20. # Get the Accel and Jerk and set the values in the setting boxes--
  21. mycura = CuraApplication.getInstance().getGlobalContainerStack()
  22. extruder = mycura.extruderList
  23. accel_print = extruder[0].getProperty("acceleration_print", "value")
  24. accel_travel = extruder[0].getProperty("acceleration_travel", "value")
  25. jerk_print_old = extruder[0].getProperty("jerk_print", "value")
  26. jerk_travel_old = extruder[0].getProperty("jerk_travel", "value")
  27. self._instance.setProperty("x_accel_limit", "value", round(accel_print))
  28. self._instance.setProperty("y_accel_limit", "value", round(accel_print))
  29. self._instance.setProperty("x_jerk", "value", jerk_print_old)
  30. self._instance.setProperty("y_jerk", "value", jerk_print_old)
  31. ext_count = int(mycura.getProperty("machine_extruder_count", "value"))
  32. machine_name = str(mycura.getProperty("machine_name", "value"))
  33. if str(mycura.getProperty("machine_gcode_flavor", "value")) == "RepRap (RepRap)":
  34. self._instance.setProperty("jerk_cmd", "value", "reprap_flavor")
  35. else:
  36. self._instance.setProperty("jerk_cmd", "value", "marlin_flavor")
  37. firmware_flavor = str(mycura.getProperty("machine_gcode_flavor", "value"))
  38. # Warn the user if the printer is an Ultimaker-------------------------
  39. if "Ultimaker" in machine_name or "UltiGCode" in firmware_flavor or "Griffin" in firmware_flavor:
  40. Message(text = "<NOTICE> [Limit the X-Y Accel/Jerk] DID NOT RUN because Ultimaker printers don't have sliding beds.").show()
  41. # Warn the user if the printer is multi-extruder------------------
  42. if ext_count > 1:
  43. Message(text = "<NOTICE> 'Limit the X-Y Accel/Jerk': The post processor treats all extruders the same. If you have multiple extruders they will all be subject to the same Accel and Jerk limits imposed. If you have different Travel and Print Accel they will also be subject to the same limits. If that is not acceptable then you should not use this Post Processor.").show()
  44. # Warn the user if Accel Control is not enabled in Cura. This keeps the script from being able to increase the Accel limits-----------
  45. if not bool(extruder[0].getProperty("acceleration_enabled", "value")):
  46. Message(title = "[Limit the X-Y Accel/Jerk]", text = "You must have 'Enable Acceleration Control' checked in Cura or the script will exit.").show()
  47. def getSettingDataString(self):
  48. return """{
  49. "name": "Limit the X-Y Accel/Jerk (all extruders equal)",
  50. "key": "LimitXYAccelJerk",
  51. "metadata": {},
  52. "version": 2,
  53. "settings":
  54. {
  55. "type_of_change":
  56. {
  57. "label": "Immediate or Gradual change",
  58. "description": "An 'Immediate' change will insert the new numbers immediately at the Start Layer. A 'Gradual' change will transition from the starting Accel to the new Accel limit across a range of layers.",
  59. "type": "enum",
  60. "options": {
  61. "immediate_change": "Immediate",
  62. "gradual_change": "Gradual"},
  63. "default_value": "immediate_change"
  64. },
  65. "x_accel_limit":
  66. {
  67. "label": "X MAX Acceleration",
  68. "description": "If this number is lower than the 'X Print Accel' in Cura then this will limit the Accel on the X axis. Enter the Maximum Acceleration value for the X axis. This will affect both Print and Travel Accel. If you enable an End Layer then at the end of that layer the Accel Limit will be reset (unless you choose 'Gradual' in which case the new limit goes to the top layer).",
  69. "type": "int",
  70. "enabled": true,
  71. "minimum_value": 50,
  72. "unit": "mm/sec² ",
  73. "default_value": 500
  74. },
  75. "y_accel_limit":
  76. {
  77. "label": "Y MAX Acceleration",
  78. "description": "If this number is lower than the Y accel in Cura then this will limit the Accel on the Y axis. Enter the Maximum Acceleration value for the Y axis. This will affect both Print and Travel Accel. If you enable an End Layer then at the end of that layer the Accel Limit will be reset (unless you choose 'Gradual' in which case the new limit goes to the top layer).",
  79. "type": "int",
  80. "enabled": true,
  81. "minimum_value": 50,
  82. "unit": "mm/sec² ",
  83. "default_value": 500
  84. },
  85. "jerk_enable":
  86. {
  87. "label": "Change the Jerk",
  88. "description": "Whether to change the Jerk values.",
  89. "type": "bool",
  90. "enabled": true,
  91. "default_value": false
  92. },
  93. "jerk_cmd":
  94. {
  95. "label": "G-Code Jerk Command",
  96. "description": "Marlin uses M205. RepRap might use M566.",
  97. "type": "enum",
  98. "options": {
  99. "marlin_flavor": "M205",
  100. "reprap_flavor": "M566"},
  101. "default_value": "marlin_flavor",
  102. "enabled": "jerk_enable"
  103. },
  104. "x_jerk":
  105. {
  106. "label": " X jerk",
  107. "description": "Enter the Jerk value for the X axis. Enter '0' to use the existing X Jerk. This setting will affect both the Print and Travel jerk.",
  108. "type": "int",
  109. "enabled": "jerk_enable",
  110. "unit": "mm/sec ",
  111. "default_value": 8
  112. },
  113. "y_jerk":
  114. {
  115. "label": " Y jerk",
  116. "description": "Enter the Jerk value for the Y axis. Enter '0' to use the existing Y Jerk. This setting will affect both the Print and Travel jerk.",
  117. "type": "int",
  118. "enabled": "jerk_enable",
  119. "unit": "mm/sec ",
  120. "default_value": 8
  121. },
  122. "start_layer":
  123. {
  124. "label": "From Start of Layer:",
  125. "description": "Use the Cura Preview numbers. Enter the Layer to start the changes at. The minimum is Layer 1.",
  126. "type": "int",
  127. "default_value": 1,
  128. "minimum_value": 1,
  129. "unit": "Lay# ",
  130. "enabled": "type_of_change == 'immediate_change'"
  131. },
  132. "end_layer":
  133. {
  134. "label": "To End of Layer",
  135. "description": "Use the Cura Preview numbers. Enter '-1' for the entire file or enter a layer number. The changes will end at your 'End Layer' and revert back to the original numbers.",
  136. "type": "int",
  137. "default_value": -1,
  138. "minimum_value": -1,
  139. "unit": "Lay# ",
  140. "enabled": "type_of_change == 'immediate_change'"
  141. },
  142. "gradient_start_layer":
  143. {
  144. "label": " Gradual From Layer:",
  145. "description": "Use the Cura Preview numbers. Enter the Layer to start the changes at. The minimum is Layer 1.",
  146. "type": "int",
  147. "default_value": 1,
  148. "minimum_value": 1,
  149. "unit": "Lay# ",
  150. "enabled": "type_of_change == 'gradual_change'"
  151. },
  152. "gradient_end_layer":
  153. {
  154. "label": " Gradual To Layer",
  155. "description": "Use the Cura Preview numbers. Enter '-1' for the top layer or enter a layer number. The last 'Gradual' change will continue to the end of the file.",
  156. "type": "int",
  157. "default_value": -1,
  158. "minimum_value": -1,
  159. "unit": "Lay# ",
  160. "enabled": "type_of_change == 'gradual_change'"
  161. }
  162. }
  163. }"""
  164. def execute(self, data):
  165. mycura = CuraApplication.getInstance().getGlobalContainerStack()
  166. extruder = mycura.extruderList
  167. machine_name = str(mycura.getProperty("machine_name", "value"))
  168. print_sequence = str(mycura.getProperty("print_sequence", "value"))
  169. acceleration_enabled = bool(extruder[0].getProperty("acceleration_enabled", "value"))
  170. # Exit if acceleration control is not enabled----------------
  171. if not acceleration_enabled:
  172. Message(title = "[Limit the X-Y Accel/Jerk]", text = "DID NOT RUN. You must have 'Enable Acceleration Control' checked in Cura.").show()
  173. data[0] += "; [LimitXYAccelJerk] DID NOT RUN because 'Enable Acceleration Control' is not checked in Cura.\n"
  174. return data
  175. # Exit if 'one_at_a_time' is enabled-------------------------
  176. if print_sequence == "one_at_a_time":
  177. Message(text = "<NOTICE> [Limit the X-Y Accel/Jerk] DID NOT RUN. This post processor is not compatible with 'One-at-a-Time' mode.").show()
  178. data[0] += "; [LimitXYAccelJerk] DID NOT RUN because Cura is set to 'One-at-a-Time' mode.\n"
  179. return data
  180. # Exit if the printer is an Ultimaker-------------------------
  181. if "Ultimaker" in machine_name:
  182. Message(text = "<NOTICE> [Limit the X-Y Accel/Jerk] DID NOT RUN. This post processor is for bed slinger printers only.").show()
  183. data[0] += "; [LimitXYAccelJerk] DID NOT RUN because the printer doesn't have a sliding bed.\n"
  184. return data
  185. type_of_change = str(self.getSettingValueByKey("type_of_change"))
  186. accel_print = extruder[0].getProperty("acceleration_print", "value")
  187. accel_travel = extruder[0].getProperty("acceleration_travel", "value")
  188. jerk_print_old = extruder[0].getProperty("jerk_print", "value")
  189. jerk_travel_old = extruder[0].getProperty("jerk_travel", "value")
  190. if int(accel_print) >= int(accel_travel):
  191. accel_old = accel_print
  192. else:
  193. accel_old = accel_travel
  194. jerk_travel = str(extruder[0].getProperty("jerk_travel", "value"))
  195. if int(jerk_print_old) >= int(jerk_travel_old):
  196. jerk_old = jerk_print_old
  197. else:
  198. jerk_old = jerk_travel_old
  199. #Set the new Accel values----------------------------------------------------------
  200. x_accel = str(self.getSettingValueByKey("x_accel_limit"))
  201. y_accel = str(self.getSettingValueByKey("y_accel_limit"))
  202. x_jerk = int(self.getSettingValueByKey("x_jerk"))
  203. y_jerk = int(self.getSettingValueByKey("y_jerk"))
  204. if str(self.getSettingValueByKey("jerk_cmd")) == "reprap_flavor":
  205. jerk_cmd = "M566"
  206. x_jerk *= 60
  207. y_jerk *= 60
  208. jerk_old *= 60
  209. else:
  210. jerk_cmd = "M205"
  211. # Put the strings together-------------------------------------------
  212. m201_limit_new = f"M201 X{x_accel} Y{y_accel}"
  213. m201_limit_old = f"M201 X{round(accel_old)} Y{round(accel_old)}"
  214. if x_jerk == 0:
  215. m205_jerk_pattern = r"Y(\d*)"
  216. m205_jerk_new = f"Y{y_jerk}"
  217. if y_jerk == 0:
  218. m205_jerk_pattern = r"X(\d*)"
  219. m205_jerk_new = f"X{x_jerk}"
  220. if x_jerk != 0 and y_jerk != 0:
  221. m205_jerk_pattern = jerk_cmd + r" X(\d*) Y(\d*)"
  222. m205_jerk_new = jerk_cmd + f" X{x_jerk} Y{y_jerk}"
  223. m205_jerk_old = jerk_cmd + f" X{jerk_old} Y{jerk_old}"
  224. type_of_change = self.getSettingValueByKey("type_of_change")
  225. #Get the indexes of the start and end layers----------------------------------------
  226. if type_of_change == 'immediate_change':
  227. start_layer = int(self.getSettingValueByKey("start_layer"))-1
  228. end_layer = int(self.getSettingValueByKey("end_layer"))
  229. else:
  230. start_layer = int(self.getSettingValueByKey("gradient_start_layer"))-1
  231. end_layer = int(self.getSettingValueByKey("gradient_end_layer"))
  232. start_index = 2
  233. end_index = len(data)-2
  234. for num in range(2,len(data)-1):
  235. if ";LAYER:" + str(start_layer) + "\n" in data[num]:
  236. start_index = num
  237. break
  238. if int(end_layer) > 0:
  239. for num in range(3,len(data)-1):
  240. try:
  241. if ";LAYER:" + str(end_layer) + "\n" in data[num]:
  242. end_index = num
  243. break
  244. except:
  245. end_index = len(data)-2
  246. #Add Accel limit and new Jerk at start layer-----------------------------------------------------
  247. if type_of_change == "immediate_change":
  248. layer = data[start_index]
  249. lines = layer.split("\n")
  250. for index, line in enumerate(lines):
  251. if lines[index].startswith(";LAYER:"):
  252. lines.insert(index+1,m201_limit_new)
  253. if self.getSettingValueByKey("jerk_enable"):
  254. lines.insert(index+2,m205_jerk_new)
  255. data[start_index] = "\n".join(lines)
  256. break
  257. #Alter any existing jerk lines. Accel lines can be ignored-----------------------------------
  258. for num in range(start_index,end_index,1):
  259. layer = data[num]
  260. lines = layer.split("\n")
  261. for index, line in enumerate(lines):
  262. if line.startswith("M205") or line.startswith("M566"):
  263. lines[index] = re.sub(m205_jerk_pattern, m205_jerk_new, line)
  264. data[num] = "\n".join(lines)
  265. if end_layer != -1:
  266. try:
  267. layer = data[end_index-1]
  268. lines = layer.split("\n")
  269. lines.insert(len(lines)-2,m201_limit_old)
  270. lines.insert(len(lines)-2,m205_jerk_old)
  271. data[end_index-1] = "\n".join(lines)
  272. except:
  273. pass
  274. else:
  275. data[len(data)-1] = m201_limit_old + "\n" + m205_jerk_old + "\n" + data[len(data)-1]
  276. return data
  277. elif type_of_change == "gradual_change":
  278. layer_spread = end_index - start_index
  279. if accel_old >= int(x_accel):
  280. x_accel_hyst = round((accel_old - int(x_accel)) / layer_spread)
  281. else:
  282. x_accel_hyst = round((int(x_accel) - accel_old) / layer_spread)
  283. if accel_old >= int(y_accel):
  284. y_accel_hyst = round((accel_old - int(y_accel)) / layer_spread)
  285. else:
  286. y_accel_hyst = round((int(y_accel) - accel_old) / layer_spread)
  287. if accel_old >= int(x_accel):
  288. x_accel_start = round(round((accel_old - x_accel_hyst)/25)*25)
  289. else:
  290. x_accel_start = round(round((x_accel_hyst + accel_old)/25)*25)
  291. if accel_old >= int(y_accel):
  292. y_accel_start = round(round((accel_old - y_accel_hyst)/25)*25)
  293. else:
  294. y_accel_start = round(round((y_accel_hyst + accel_old)/25)*25)
  295. m201_limit_new = "M201 X" + str(x_accel_start) + " Y" + str(y_accel_start)
  296. #Add Accel limit and new Jerk at start layer-------------------------------------------------------------
  297. layer = data[start_index]
  298. lines = layer.split("\n")
  299. for index, line in enumerate(lines):
  300. if lines[index].startswith(";LAYER:"):
  301. lines.insert(index+1,m201_limit_new)
  302. if self.getSettingValueByKey("jerk_enable"):
  303. lines.insert(index+2,m205_jerk_new)
  304. data[start_index] = "\n".join(lines)
  305. break
  306. for num in range(start_index + 1, end_index,1):
  307. layer = data[num]
  308. lines = layer.split("\n")
  309. if accel_old >= int(x_accel):
  310. x_accel_start -= x_accel_hyst
  311. if x_accel_start < int(x_accel): x_accel_start = int(x_accel)
  312. else:
  313. x_accel_start += x_accel_hyst
  314. if x_accel_start > int(x_accel): x_accel_start = int(x_accel)
  315. if accel_old >= int(y_accel):
  316. y_accel_start -= y_accel_hyst
  317. if y_accel_start < int(y_accel): y_accel_start = int(y_accel)
  318. else:
  319. y_accel_start += y_accel_hyst
  320. if y_accel_start > int(y_accel): y_accel_start = int(y_accel)
  321. m201_limit_new = "M201 X" + str(round(round(x_accel_start/25)*25)) + " Y" + str(round(round(y_accel_start/25)*25))
  322. for index, line in enumerate(lines):
  323. if line.startswith(";LAYER:"):
  324. lines.insert(index+1, m201_limit_new)
  325. continue
  326. data[num] = "\n".join(lines)
  327. #Alter any existing jerk lines. Accel lines can be ignored---------------
  328. if self.getSettingValueByKey("jerk_enable"):
  329. for num in range(start_index,len(data)-1,1):
  330. layer = data[num]
  331. lines = layer.split("\n")
  332. for index, line in enumerate(lines):
  333. if line.startswith("M205") or line.startswith("M566"):
  334. lines[index] = re.sub(m205_jerk_pattern, m205_jerk_new, line)
  335. data[num] = "\n".join(lines)
  336. data[len(data)-1] = m201_limit_old + "\n" + m205_jerk_old + "\n" + data[len(data)-1]
  337. return data