From ae6c16422f1107139fa2f36cbd31360b37312ff7 Mon Sep 17 00:00:00 2001 From: jake-b <1012393+jake-b@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:30:06 -0300 Subject: [PATCH] mcp4018: Add SET_DIGIPOT command to mcp4018 implementation (#5737) Added a SET_DIGIPOT command to the mcp4018 implementation. Previously the mcp4018 was read only, and set at the time of configuration. This allows you to change the value during a print, which is needed for some older printers that need to lower the stepper current during preheating. Signed-off-by: Jake Bordens --- config/sample-macros.cfg | 47 ++++++++++++++++++++++++++++++++++++++++ docs/G-Codes.md | 14 ++++++++++++ klippy/extras/mcp4018.py | 15 ++++++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/config/sample-macros.cfg b/config/sample-macros.cfg index 3590268c..7b68f5d0 100644 --- a/config/sample-macros.cfg +++ b/config/sample-macros.cfg @@ -261,3 +261,50 @@ gcode: {% if 'U' in params %} EXCLUDE_OBJECT RESET=1 NAME={params.U} {% endif %} + +###################################################################### +# G130: Set digital potentiometer value +###################################################################### + +# The macro below uses the MCP4018 SET_DIGIPOT command to implement +# a `G130` as used on classic Mightyboard-based printers such as +# The Makerbot Replicator 2/2X. +# +# The `G130` command can be used to lower the stepper current +# during preheating and raise the current again prior to starting +# the print. This is necessary for printers with smaller power +# supplies that needed all the power to heat the bed. +# +# This macro requires one or more [mcp4018] configuration sections: +# (x_axis_pot, y_axis_pot, z_axis_pot, a_axis_pot, b_axis_pot) +# +# Example: G130 X20 Y20 Z20 A20 B20 ; Lower stepper Vrefs while heating + +[gcode_macro G130] +gcode: + M400 + {% if ('X' in params) and ('mcp4018 x_axis_pot' in printer.configfile.config) %} + {% set x_value = params['X']|float %} + {% set x_axis_pot_scale = printer.configfile.config["mcp4018 x_axis_pot"].scale|float %} + SET_DIGIPOT DIGIPOT=x_axis_pot WIPER={ x_axis_pot_scale * (x_value / 127.0)} + {% endif %} + {% if ('Y' in params) and ('mcp4018 y_axis_pot' in printer.configfile.config) %} + {% set y_value = params['Y']|float %} + {% set y_axis_pot_scale = printer.configfile.config["mcp4018 y_axis_pot"].scale|float %} + SET_DIGIPOT DIGIPOT=y_axis_pot WIPER={ y_axis_pot_scale * (y_value / 127.0)} + {% endif %} + {% if ('Z' in params) and ('mcp4018 z_axis_pot' in printer.configfile.config) %} + {% set z_value = params['Z']|float %} + {% set z_axis_pot_scale = printer.configfile.config["mcp4018 z_axis_pot"].scale|float %} + SET_DIGIPOT DIGIPOT=z_axis_pot WIPER={ z_axis_pot_scale * (z_value / 127.0)} + {% endif %} + {% if ('A' in params) and ('mcp4018 a_axis_pot' in printer.configfile.config) %} + {% set a_value = params['A']|float %} + {% set a_axis_pot_scale = printer.configfile.config["mcp4018 a_axis_pot"].scale|float %} + SET_DIGIPOT DIGIPOT=a_axis_pot WIPER={ a_axis_pot_scale * (a_value / 127.0)} + {% endif %} + {% if ('B' in params) and ('mcp4018 b_axis_pot' in printer.configfile.config) %} + {% set b_value = params['B']|float %} + {% set b_axis_pot_scale = printer.configfile.config["mcp4018 b_axis_pot"].scale|float %} + SET_DIGIPOT DIGIPOT=b_axis_pot WIPER={ b_axis_pot_scale * (b_value / 127.0)} + {% endif %} diff --git a/docs/G-Codes.md b/docs/G-Codes.md index afa808c8..fb9e4a7a 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -746,6 +746,20 @@ scheduled to run after the stepper move completes, however if a manual stepper move uses SYNC=0 then future G-Code movement commands may run in parallel with the stepper movement. +### [mcp4018] + +The following command is available when a +[mcp4018 config section](Config_Reference.md#mcp4018) is +enabled. + +#### SET_DIGIPOT + +`SET_DIGIPOT DIGIPOT=config_name WIPER=`: This command will +change the current value of the digipot. This value should typically +be between 0.0 and 1.0, unless a 'scale' is defined in the config. +When 'scale' is defined, then this value should be between 0.0 and +'scale'. + ### [led] The following command is available when any of the diff --git a/klippy/extras/mcp4018.py b/klippy/extras/mcp4018.py index fd04aa66..c7d3d312 100644 --- a/klippy/extras/mcp4018.py +++ b/klippy/extras/mcp4018.py @@ -68,17 +68,30 @@ class SoftwareI2C: class mcp4018: def __init__(self, config): + self.printer = config.get_printer() self.i2c = SoftwareI2C(config, 0x2f) self.scale = config.getfloat('scale', 1., above=0.) self.start_value = config.getfloat('wiper', minval=0., maxval=self.scale) config.get_printer().register_event_handler("klippy:connect", self.handle_connect) + # Register commands + self.name = config.get_name().split()[1] + gcode = self.printer.lookup_object('gcode') + gcode.register_mux_command("SET_DIGIPOT", "DIGIPOT", self.name, + self.cmd_SET_DIGIPOT, + desc=self.cmd_SET_DIGIPOT_help) def handle_connect(self): self.set_dac(self.start_value) def set_dac(self, value): val = int(value * 127. / self.scale + .5) self.i2c.i2c_write([val]) - + cmd_SET_DIGIPOT_help = "Set digipot value" + def cmd_SET_DIGIPOT(self, gcmd): + wiper = gcmd.get_float('WIPER', minval=0., maxval=self.scale) + if wiper is not None: + self.set_dac(wiper) + gcmd.respond_info("New value for DIGIPOT = %s, wiper = %.2f" + % (self.name, wiper)) def load_config_prefix(config): return mcp4018(config)