LimitXYAccelJerk.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. from ..Script import Script
  13. from cura.CuraApplication import CuraApplication
  14. import re
  15. from UM.Message import Message
  16. class LimitXYAccelJerk(Script):
  17. def initialize(self) -> None:
  18. super().initialize()
  19. # Get the Accel and Jerk and set the values in the setting boxes--
  20. mycura = CuraApplication.getInstance().getGlobalContainerStack()
  21. extruder = mycura.extruderList
  22. accel_print = extruder[0].getProperty("acceleration_print", "value")
  23. accel_travel = extruder[0].getProperty("acceleration_travel", "value")
  24. jerk_print_old = extruder[0].getProperty("jerk_print", "value")
  25. jerk_travel_old = extruder[0].getProperty("jerk_travel", "value")
  26. self._instance.setProperty("x_accel_limit", "value", round(accel_print))
  27. self._instance.setProperty("y_accel_limit", "value", round(accel_print))
  28. self._instance.setProperty("x_jerk", "value", jerk_print_old)
  29. self._instance.setProperty("y_jerk", "value", jerk_print_old)
  30. ext_count = int(mycura.getProperty("machine_extruder_count", "value"))
  31. machine_name = str(mycura.getProperty("machine_name", "value"))
  32. if str(mycura.getProperty("machine_gcode_flavor", "value")) == "RepRap (RepRap)":
  33. self._instance.setProperty("jerk_cmd", "value", "reprap_flavor")
  34. else:
  35. self._instance.setProperty("jerk_cmd", "value", "marlin_flavor")
  36. firmware_flavor = str(mycura.getProperty("machine_gcode_flavor", "value"))
  37. # Warn the user if the printer is an Ultimaker-------------------------
  38. if "Ultimaker" in machine_name or "UltiGCode" in firmware_flavor or "Griffin" in firmware_flavor:
  39. Message(text = "<NOTICE> [Limit the X-Y Accel/Jerk] DID NOT RUN because Ultimaker printers don't have sliding beds.").show()
  40. # Warn the user if the printer is multi-extruder------------------
  41. if ext_count > 1:
  42. 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()
  43. def getSettingDataString(self):
  44. return """{
  45. "name": "Limit the X-Y Accel/Jerk (all extruders equal)",
  46. "key": "LimitXYAccelJerk",
  47. "metadata": {},
  48. "version": 2,
  49. "settings":
  50. {
  51. "type_of_change":
  52. {
  53. "label": "Immediate or Gradual change",
  54. "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.",
  55. "type": "enum",
  56. "options": {
  57. "immediate_change": "Immediate",
  58. "gradual_change": "Gradual"},
  59. "default_value": "immediate_change"
  60. },
  61. "x_accel_limit":
  62. {
  63. "label": "X MAX Acceleration",
  64. "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).",
  65. "type": "int",
  66. "enabled": true,
  67. "minimum_value": 50,
  68. "unit": "mm/sec² ",
  69. "default_value": 500
  70. },
  71. "y_accel_limit":
  72. {
  73. "label": "Y MAX Acceleration",
  74. "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).",
  75. "type": "int",
  76. "enabled": true,
  77. "minimum_value": 50,
  78. "unit": "mm/sec² ",
  79. "default_value": 500
  80. },
  81. "jerk_enable":
  82. {
  83. "label": "Change the Jerk",
  84. "description": "Whether to change the Jerk values.",
  85. "type": "bool",
  86. "enabled": true,
  87. "default_value": false
  88. },
  89. "jerk_cmd":
  90. {
  91. "label": "G-Code Jerk Command",
  92. "description": "Marlin uses M205. RepRap might use M566.",
  93. "type": "enum",
  94. "options": {
  95. "marlin_flavor": "M205",
  96. "reprap_flavor": "M566"},
  97. "default_value": "marlin_flavor",
  98. "enabled": "jerk_enable"
  99. },
  100. "x_jerk":
  101. {
  102. "label": " X jerk",
  103. "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.",
  104. "type": "int",
  105. "enabled": "jerk_enable",
  106. "unit": "mm/sec ",
  107. "default_value": 8
  108. },
  109. "y_jerk":
  110. {
  111. "label": " Y jerk",
  112. "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.",
  113. "type": "int",
  114. "enabled": "jerk_enable",
  115. "unit": "mm/sec ",
  116. "default_value": 8
  117. },
  118. "start_layer":
  119. {
  120. "label": "From Start of Layer:",
  121. "description": "Use the Cura Preview numbers. Enter the Layer to start the changes at. The minimum is Layer 1.",
  122. "type": "int",
  123. "default_value": 1,
  124. "minimum_value": 1,
  125. "unit": "Lay# ",
  126. "enabled": "type_of_change == 'immediate_change'"
  127. },
  128. "end_layer":
  129. {
  130. "label": "To End of Layer",
  131. "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.",
  132. "type": "int",
  133. "default_value": -1,
  134. "minimum_value": -1,
  135. "unit": "Lay# ",
  136. "enabled": "type_of_change == 'immediate_change'"
  137. },
  138. "gradient_start_layer":
  139. {
  140. "label": " Gradual From Layer:",
  141. "description": "Use the Cura Preview numbers. Enter the Layer to start the changes at. The minimum is Layer 1.",
  142. "type": "int",
  143. "default_value": 1,
  144. "minimum_value": 1,
  145. "unit": "Lay# ",
  146. "enabled": "type_of_change == 'gradual_change'"
  147. },
  148. "gradient_end_layer":
  149. {
  150. "label": " Gradual To Layer",
  151. "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.",
  152. "type": "int",
  153. "default_value": -1,
  154. "minimum_value": -1,
  155. "unit": "Lay# ",
  156. "enabled": "type_of_change == 'gradual_change'"
  157. }
  158. }
  159. }"""
  160. def execute(self, data):
  161. mycura = CuraApplication.getInstance().getGlobalContainerStack()
  162. extruder = mycura.extruderList
  163. machine_name = str(mycura.getProperty("machine_name", "value"))
  164. print_sequence = str(mycura.getProperty("print_sequence", "value"))
  165. # Exit if 'one_at_a_time' is enabled-------------------------
  166. if print_sequence == "one_at_a_time":
  167. 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()
  168. data[0] += "; [LimitXYAccelJerk] DID NOT RUN because Cura is set to 'One-at-a-Time' mode.\n"
  169. return data
  170. # Exit if the printer is an Ultimaker-------------------------
  171. if "Ultimaker" in machine_name:
  172. Message(text = "<NOTICE> [Limit the X-Y Accel/Jerk] DID NOT RUN. This post processor is for bed slinger printers only.").show()
  173. data[0] += "; [LimitXYAccelJerk] DID NOT RUN because the printer doesn't have a sliding bed.\n"
  174. return data
  175. type_of_change = str(self.getSettingValueByKey("type_of_change"))
  176. accel_print_enabled = bool(extruder[0].getProperty("acceleration_enabled", "value"))
  177. accel_travel_enabled = bool(extruder[0].getProperty("acceleration_travel_enabled", "value"))
  178. accel_print = extruder[0].getProperty("acceleration_print", "value")
  179. accel_travel = extruder[0].getProperty("acceleration_travel", "value")
  180. jerk_print_enabled = str(extruder[0].getProperty("jerk_enabled", "value"))
  181. jerk_travel_enabled = str(extruder[0].getProperty("jerk_travel_enabled", "value"))
  182. jerk_print_old = extruder[0].getProperty("jerk_print", "value")
  183. jerk_travel_old = extruder[0].getProperty("jerk_travel", "value")
  184. if int(accel_print) >= int(accel_travel):
  185. accel_old = accel_print
  186. else:
  187. accel_old = accel_travel
  188. jerk_travel = str(extruder[0].getProperty("jerk_travel", "value"))
  189. if int(jerk_print_old) >= int(jerk_travel_old):
  190. jerk_old = jerk_print_old
  191. else:
  192. jerk_old = jerk_travel_old
  193. #Set the new Accel values----------------------------------------------------------
  194. x_accel = str(self.getSettingValueByKey("x_accel_limit"))
  195. y_accel = str(self.getSettingValueByKey("y_accel_limit"))
  196. x_jerk = int(self.getSettingValueByKey("x_jerk"))
  197. y_jerk = int(self.getSettingValueByKey("y_jerk"))
  198. if str(self.getSettingValueByKey("jerk_cmd")) == "reprap_flavor":
  199. jerk_cmd = "M566"
  200. x_jerk *= 60
  201. y_jerk *= 60
  202. jerk_old *= 60
  203. else:
  204. jerk_cmd = "M205"
  205. # Put the strings together-------------------------------------------
  206. m201_limit_new = f"M201 X{x_accel} Y{y_accel}"
  207. m201_limit_old = f"M201 X{round(accel_old)} Y{round(accel_old)}"
  208. if x_jerk == 0:
  209. m205_jerk_pattern = r"Y(\d*)"
  210. m205_jerk_new = f"Y{y_jerk}"
  211. if y_jerk == 0:
  212. m205_jerk_pattern = r"X(\d*)"
  213. m205_jerk_new = f"X{x_jerk}"
  214. if x_jerk != 0 and y_jerk != 0:
  215. m205_jerk_pattern = jerk_cmd + r" X(\d*) Y(\d*)"
  216. m205_jerk_new = jerk_cmd + f" X{x_jerk} Y{y_jerk}"
  217. m205_jerk_old = jerk_cmd + f" X{jerk_old} Y{jerk_old}"
  218. type_of_change = self.getSettingValueByKey("type_of_change")
  219. #Get the indexes of the start and end layers----------------------------------------
  220. if type_of_change == 'immediate_change':
  221. start_layer = int(self.getSettingValueByKey("start_layer"))-1
  222. end_layer = int(self.getSettingValueByKey("end_layer"))
  223. else:
  224. start_layer = int(self.getSettingValueByKey("gradient_start_layer"))-1
  225. end_layer = int(self.getSettingValueByKey("gradient_end_layer"))
  226. start_index = 2
  227. end_index = len(data)-2
  228. for num in range(2,len(data)-1):
  229. if ";LAYER:" + str(start_layer) + "\n" in data[num]:
  230. start_index = num
  231. break
  232. if int(end_layer) > 0:
  233. for num in range(3,len(data)-1):
  234. try:
  235. if ";LAYER:" + str(end_layer) + "\n" in data[num]:
  236. end_index = num
  237. break
  238. except:
  239. end_index = len(data)-2
  240. #Add Accel limit and new Jerk at start layer-----------------------------------------------------
  241. if type_of_change == "immediate_change":
  242. layer = data[start_index]
  243. lines = layer.split("\n")
  244. for index, line in enumerate(lines):
  245. if lines[index].startswith(";LAYER:"):
  246. lines.insert(index+1,m201_limit_new)
  247. if self.getSettingValueByKey("jerk_enable"):
  248. lines.insert(index+2,m205_jerk_new)
  249. data[start_index] = "\n".join(lines)
  250. break
  251. #Alter any existing jerk lines. Accel lines can be ignored-----------------------------------
  252. for num in range(start_index,end_index,1):
  253. layer = data[num]
  254. lines = layer.split("\n")
  255. for index, line in enumerate(lines):
  256. if line.startswith("M205") or line.startswith("M566"):
  257. lines[index] = re.sub(m205_jerk_pattern, m205_jerk_new, line)
  258. data[num] = "\n".join(lines)
  259. if end_layer != -1:
  260. try:
  261. layer = data[end_index-1]
  262. lines = layer.split("\n")
  263. lines.insert(len(lines)-2,m201_limit_old)
  264. lines.insert(len(lines)-2,m205_jerk_old)
  265. data[end_index-1] = "\n".join(lines)
  266. except:
  267. pass
  268. else:
  269. data[len(data)-1] = m201_limit_old + "\n" + m205_jerk_old + "\n" + data[len(data)-1]
  270. return data
  271. elif type_of_change == "gradual_change":
  272. layer_spread = end_index - start_index
  273. if accel_old >= int(x_accel):
  274. x_accel_hyst = round((accel_old - int(x_accel)) / layer_spread)
  275. else:
  276. x_accel_hyst = round((int(x_accel) - accel_old) / layer_spread)
  277. if accel_old >= int(y_accel):
  278. y_accel_hyst = round((accel_old - int(y_accel)) / layer_spread)
  279. else:
  280. y_accel_hyst = round((int(y_accel) - accel_old) / layer_spread)
  281. if accel_old >= int(x_accel):
  282. x_accel_start = round(round((accel_old - x_accel_hyst)/25)*25)
  283. else:
  284. x_accel_start = round(round((x_accel_hyst + accel_old)/25)*25)
  285. if accel_old >= int(y_accel):
  286. y_accel_start = round(round((accel_old - y_accel_hyst)/25)*25)
  287. else:
  288. y_accel_start = round(round((y_accel_hyst + accel_old)/25)*25)
  289. m201_limit_new = "M201 X" + str(x_accel_start) + " Y" + str(y_accel_start)
  290. #Add Accel limit and new Jerk at start layer-------------------------------------------------------------
  291. layer = data[start_index]
  292. lines = layer.split("\n")
  293. for index, line in enumerate(lines):
  294. if lines[index].startswith(";LAYER:"):
  295. lines.insert(index+1,m201_limit_new)
  296. if self.getSettingValueByKey("jerk_enable"):
  297. lines.insert(index+2,m205_jerk_new)
  298. data[start_index] = "\n".join(lines)
  299. break
  300. for num in range(start_index + 1, end_index,1):
  301. layer = data[num]
  302. lines = layer.split("\n")
  303. if accel_old >= int(x_accel):
  304. x_accel_start -= x_accel_hyst
  305. if x_accel_start < int(x_accel): x_accel_start = int(x_accel)
  306. else:
  307. x_accel_start += x_accel_hyst
  308. if x_accel_start > int(x_accel): x_accel_start = int(x_accel)
  309. if accel_old >= int(y_accel):
  310. y_accel_start -= y_accel_hyst
  311. if y_accel_start < int(y_accel): y_accel_start = int(y_accel)
  312. else:
  313. y_accel_start += y_accel_hyst
  314. if y_accel_start > int(y_accel): y_accel_start = int(y_accel)
  315. m201_limit_new = "M201 X" + str(round(round(x_accel_start/25)*25)) + " Y" + str(round(round(y_accel_start/25)*25))
  316. for index, line in enumerate(lines):
  317. if line.startswith(";LAYER:"):
  318. lines.insert(index+1, m201_limit_new)
  319. continue
  320. data[num] = "\n".join(lines)
  321. #Alter any existing jerk lines. Accel lines can be ignored---------------
  322. if self.getSettingValueByKey("jerk_enable"):
  323. for num in range(start_index,len(data)-1,1):
  324. layer = data[num]
  325. lines = layer.split("\n")
  326. for index, line in enumerate(lines):
  327. if line.startswith("M205") or line.startswith("M566"):
  328. lines[index] = re.sub(m205_jerk_pattern, m205_jerk_new, line)
  329. data[num] = "\n".join(lines)
  330. data[len(data)-1] = m201_limit_old + "\n" + m205_jerk_old + "\n" + data[len(data)-1]
  331. return data