Jump to content
Ultimaker Community of 3D Printing Experts

Convert fan speed to servo movement (post processing script)


troyproffitt
 Share

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
     Share

    • Our picks

      • New here? Get ahead with a free onboarding course
        Hi,
         
        Often getting started is the most difficult part of any process. A good start sets you up for success and saves you time and energy that could be spent elsewhere. That is why we have a onboarding course ready for
        Ultimaker S5 Pro Bundle, Ultimaker S5, Ultimaker S3 Ultimaker 2+ Connect.   
        They're ready for you on the Ultimaker Academy platform. All you need to do to gain access is to register your product to gain free access. 
        Ready? Register your product here in just 60 seconds.
          • Like
        • 11 replies
    ×
    ×
    • Create New...