Skip to content

Commit c00285b

Browse files
committed
0.1.5
* adjust M220 feed rate modifier calculations
1 parent 7f1ae5a commit c00285b

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

octoprint_bambu_printer/printer/bambu_virtual_printer.py

+45-13
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,51 @@ def _set_feedrate_percent(self, data: str) -> bool:
474474
gcode_command = commands.SEND_GCODE_TEMPLATE
475475
percent = int(data.replace("M220 S", ""))
476476

477-
if percent is None or percent < 1 or percent > 166:
478-
return True
479-
480-
speed_fraction = 100 / percent
481-
acceleration = math.exp((speed_fraction - 1.0191) / -0.814)
482-
feed_rate = (
483-
2.1645 * (acceleration**3)
484-
- 5.3247 * (acceleration**2)
485-
+ 4.342 * acceleration
486-
- 0.181
487-
)
488-
speed_level = 1.539 * (acceleration**2) - 0.7032 * acceleration + 4.0834
489-
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"
477+
def speed_fraction(speed_percent):
478+
return math.floor(10000 / speed_percent) / 100
479+
480+
def acceleration_magnitude(speed_percent):
481+
return math.exp((speed_fraction(speed_percent) - 1.0191) / -0.8139)
482+
483+
def feed_rate(speed_percent):
484+
return 6.426e-5 * speed_percent ** 2 - 2.484e-3 * speed_percent + 0.654
485+
486+
def linear_interpolate(x, x_points, y_points):
487+
if x <= x_points[0]: return y_points[0]
488+
if x >= x_points[-1]: return y_points[-1]
489+
for i in range(len(x_points) - 1):
490+
if x_points[i] <= x < x_points[i + 1]:
491+
t = (x - x_points[i]) / (x_points[i + 1] - x_points[i])
492+
return y_points[i] * (1 - t) + y_points[i + 1] * t
493+
494+
def scale_to_data_points(func, data_points):
495+
data_points.sort(key=lambda x: x[0])
496+
speeds, values = zip(*data_points)
497+
scaling_factors = [v / func(s) for s, v in zip(speeds, values)]
498+
return lambda x: func(x) * linear_interpolate(x, speeds, scaling_factors)
499+
500+
def speed_adjust(speed_percentage):
501+
if not 30 <= speed_percentage <= 180:
502+
speed_percentage = 100
503+
504+
bambu_params = {
505+
"speed": [50, 100, 124, 166],
506+
"acceleration": [0.3, 1.0, 1.4, 1.6],
507+
"feed_rate": [0.7, 1.0, 1.4, 2.0]
508+
}
509+
510+
acc_mag_scaled = scale_to_data_points(acceleration_magnitude,
511+
list(zip(bambu_params["speed"], bambu_params["acceleration"])))
512+
feed_rate_scaled = scale_to_data_points(feed_rate,
513+
list(zip(bambu_params["speed"], bambu_params["feed_rate"])))
514+
515+
speed_frac = speed_fraction(speed_percentage)
516+
acc_mag = acc_mag_scaled(speed_percentage)
517+
feed = feed_rate_scaled(speed_percentage)
518+
# speed_level = 1.539 * (acc_mag**2) - 0.7032 * acc_mag + 4.0834
519+
return f"M204.2 K{acc_mag:.2f}\nM220 K{feed:.2f}\nM73.2 R{speed_frac:.2f}\n" # M1002 set_gcode_claim_speed_level ${speed_level:.0f}\n
520+
521+
speed_command = speed_adjust(percent)
490522

491523
gcode_command["print"]["param"] = speed_command
492524
if self.bambu_client.publish(gcode_command):

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "OctoPrint-BambuPrinter"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "0.1.4"
17+
plugin_version = "0.1.5"
1818

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

0 commit comments

Comments
 (0)