Jump to content

Convert fan speed to servo movement (post processing script)


troyproffitt

Recommended Posts

Posted · Convert fan speed to servo movement (post processing script)

I am attempting to use a CPAP fan as a parts cooler for my 3d printer.  Since it needs a controller to function (ESC), on my Duet WIFI 2, I cannot just plug it into the fan0 port on the controller board.  Good news is that I was able to re-purpose one of the heater pins to behave like a servo and then I can use an M42 command to turn on / off the CPAP fan and increase / decrease the fan speed.  For example

CPAP fan at 0%

M42  P8 S0.4

CPAP fan at 100%

M42  P8 S0.7

 

What I am hoping to accomplish is to re-use the post processing script InsertAtLayerChange to convert the fan_speed value from the M106 (or possibly reference the CURA keyword cool_fan_speed) to an M42 command.  Here is my math to get the appropriate range:

 

M42 P8 S({cool_fan_speed}/250)+.4)

 

I'd like this to be evaluated at every layer.  I am not familiar with the Python syntax, but I was hoping someone could help me figure out how to modify the current Insert at Layer Change script to add in this additional functionality.

 

Here is the original script:

# Copyright (c) 2020 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
# Created by Wayne Porter

from ..Script import Script

class InsertAtLayerChange(Script):
    def __init__(self):
        super().__init__()

    def getSettingDataString(self):
        return """{
            "name": "Insert at layer change",
            "key": "InsertAtLayerChange",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "insert_location":
                {
                    "label": "When to insert",
                    "description": "Whether to insert code before or after layer change.",
                    "type": "enum",
                    "options": {"before": "Before", "after": "After"},
                    "default_value": "before"
                },
                "gcode_to_add":
                {
                    "label": "G-code to insert.",
                    "description": "G-code to add before or after layer change.",
                    "type": "str",
                    "default_value": ""
                }
            }
        }"""

    def execute(self, data):
        gcode_to_add = self.getSettingValueByKey("gcode_to_add") + "\n"
        for layer in data:
            # Check that a layer is being printed
            lines = layer.split("\n")
            for line in lines:
                if ";LAYER:" in line:
                    index = data.index(layer)
                    check_fan_speed = data.index(cool_fan_speed)
                    if self.getSettingValueByKey("insert_location") == "before":
                        layer = gcode_to_add + layer
                    else:
                        layer = layer + gcode_to_add 

                    data[index] = layer
                    break
        return data

 

Thanks for your help,

-Troy

  • Link to post
    Share on other sites

    Posted (edited) · Convert fan speed to servo movement (post processing script)

    So if I was able to use the S value off the M106, the formula would be something like this:

    (<fan_value>/850)+.4

    So if M106 S255 , I'd like to add a row that said

    M42 P8 S((255/850)+.4) 

     

    Edited by troyproffitt
  • Link to post
    Share on other sites

    Posted (edited) · Convert fan speed to servo movement (post processing script)

    I ended up figuring out how to create the script on my own.  I also added some additional functionality by providing parameters for min / max servo range values and also letting the user provide the pin value on the M42 command.

    Hope this helps others that are trying to use a berdair or other servo type air source:

     

    # Copyright (c) 2020 Ultimaker B.V.
    # Cura is released under the terms of the LGPLv3 or higher.
    # Created by Wayne Porter
    
    from ..Script import Script
    
    class convertFantoServo(Script):
        def __init__(self):
            super().__init__()
    
        def getSettingDataString(self):
            return """{
                "name": "Convert Fan to Servo",
                "key": "convertFantoServo",
                "metadata": {},
                "version": 2,
                "settings":
                {
                   "minServo":
                    {
                        "label": "Servo Min Travel Value.",
                        "description": "Servo Min Travel Value.",
                        "type": "str",
                        "default_value": ".4"
                    },
                    "maxServo":
                    {
                        "label": "Servo Max Travel Value.",
                        "description": "Servo Max Travel Value.",
                        "type": "str",
                        "default_value": ".7"
                    },
                    "servoPin":
                    {
                        "label": "Servo pin assigned in M42.",
                        "description": "What pin on the controller board your servo is connected to.",
                        "type": "str",
                        "default_value": "8"
                    }
                }
            }"""
    
        def execute(self, data):
            minServo = float(self.getSettingValueByKey("minServo"))
            maxServo = float(self.getSettingValueByKey("maxServo"))
            servoPin = self.getSettingValueByKey("servoPin")
            servoRange = maxServo - minServo
            conversionVal = 255 / round(servoRange, 3)
            for layer in data:
                # Check that a layer is being printed
                lines = layer.split("\n")
                for line in lines:
                    if "M106" in line:
                        index = data.index(layer)
                        getFanval = line.replace("M106 S", "")
                        check_fan_speed = (float(getFanval) / conversionVal) + minServo
                        layer = "M42 P" + servoPin + " S" + str(round(check_fan_speed, 3)) + "\n" + layer
                        data[index] = layer
                        break
            return data

     

    Edited by troyproffitt
  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    • Our picks

      • Introducing Universal Cura Projects in the UltiMaker Cura 5.7 beta
        Strap in for the first Cura release of 2024! This 5.7 beta release brings new material profiles as well as cloud printing for Method series printers, and introduces a powerful new way of sharing print settings using printer-agnostic project files! Also, if you want to download the cute dinosaur card holder featured below, it was specially designed for this release and can be found on Thingiverse! 
          • Like
        • 10 replies
      • S-Line Firmware 8.3.0 was released Nov. 20th on the "Latest" firmware branch.
        (Sorry, was out of office when this released)

        This update is for...
        All UltiMaker S series  
        New features
         
        Temperature status. During print preparation, the temperatures of the print cores and build plate will be shown on the display. This gives a better indication of the progress and remaining wait time. Save log files in paused state. It is now possible to save the printer's log files to USB if the currently active print job is paused. Previously, the Dump logs to USB option was only enabled if the printer was in idle state. Confirm print removal via Digital Factory. If the printer is connected to the Digital Factory, it is now possible to confirm the removal of a previous print job via the Digital Factory interface. This is useful in situations where the build plate is clear, but the operator forgot to select Confirm removal on the printer’s display. Visit this page for more information about this feature.
          • Like
        • 0 replies
    ×
    ×
    • Create New...