Jump to content

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

      • Help Us Improve Cura – Join the Ultimaker Research Program
        🚀 Help Shape the Future of Cura and Digital Factory – Join Our Power User Research Program!
        We’re looking for active users of Cura and Digital Factory — across professional and educational use cases — to help us improve the next generation of our tools.
        Our Power User Research Program kicks off with a quick 15-minute interview to learn about your setup and workflows. If selected, you’ll be invited into a small group of users who get early access to features and help us shape the future of 3D printing software.

        🧪 What to Expect:
        A short 15-minute kickoff interview to help us get to know you If selected, bi-monthly research sessions (15–30 minutes) where we’ll test features, review workflows, or gather feedback Occasional invites to try out early prototypes or vote on upcoming improvements
        🎁 What You’ll Get:
         
        Selected participants receive a free 1-year Studio or Classroom license Early access to new features and tools A direct voice in what we build next
        👉 Interested? Please fill out this quick form
        Your feedback helps us make Cura Cloud more powerful, more intuitive, and more aligned with how you actually print and manage your workflow.
        Thanks for being part of the community,

        — The Ultimaker Software Team
        • 0 replies
      • Cura 5.10 stable released!
        The full stable release of Cura 5.10 has arrived, and it brings support for the new Ultimaker S8, as well as new materials and profiles for previously supported UltiMaker printers. Additionally, you can now control your models in Cura using a 3D SpaceMouse and more!
          • Like
        • 18 replies
    ×
    ×
    • Create New...