Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Script for Z offset

    Scheduled Pinned Locked Moved
    Gcode meta commands
    5
    11
    356
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ericlmccormickundefined
      ericlmccormick
      last edited by

      Whenever I swap nozzles, I find that I need to adjust my offsets each time. I use a gauge to get my height and then I update my G31 command accordingly to adjust for the difference but this is a pain.

      I would like to make a script that pulls my current Z height, compares it to my known height from my gauge and my stored offset, then adjusts the stored offset automatically. I could see this able being very helpful if you have multiple printheads. I know how to set a variable, and save a variable to a file. But I can't seem to figure out how to pull just my z height. Using M114 pulls everything. Is there a way to parse the sting and save just the Z component to a variable? It would be nice if something like "M114 Z" would return just Z, but that doesn't seem to work.

      jay_s_ukundefined 1 Reply Last reply Reply Quote 0
      • jay_s_ukundefined
        jay_s_uk @ericlmccormick
        last edited by

        @ericlmccormick you get it from the object model
        https://github.com/Duet3D/RepRapFirmware/wiki/Object-Model-Documentation#sensorsprobestriggerheight

        Owns various duet boards and is the main wiki maintainer for the Teamgloomy LPC/STM32 port of RRF. Assume I'm running whatever the latest beta/stable build is

        ericlmccormickundefined 1 Reply Last reply Reply Quote 0
        • ericlmccormickundefined
          ericlmccormick @jay_s_uk
          last edited by

          @jay_s_uk I am not looking for the stored position of the the trigger height as I set that with the G31 commend. I am looking to extract the Z component of the tool position.

          I used a 0.5 gauge and I have to hard code my offset anytime I change the nozzle. However, If I set my offset 1.0 initially and set it to a variable z_height. Then I adjust my z height to my gauge, and my tool height returns 0.3. I want to have a scripts that does z_height = z_height + tool position - 0,5 (gauge thickness).

          jay_s_ukundefined 1 Reply Last reply Reply Quote 0
          • jay_s_ukundefined
            jay_s_uk @ericlmccormick
            last edited by

            @ericlmccormick so you want the z motor position?
            I'm not sure what you mean by the z component

            Owns various duet boards and is the main wiki maintainer for the Teamgloomy LPC/STM32 port of RRF. Assume I'm running whatever the latest beta/stable build is

            ericlmccormickundefined 1 Reply Last reply Reply Quote 0
            • ericlmccormickundefined
              ericlmccormick @jay_s_uk
              last edited by

              @jay_s_uk type in M114 in the console and look at your results. I just finished a print an mine currently says X:300.000 Y:290.000 Z:7.060 E:0.000 E0:6993.7 Count 24000 23200 2782 Machine 300.000 290.000 7.110 Bed comp -0.155

              I want to be able to take the 7.060 number and set that to a variable

              jay_s_ukundefined 1 Reply Last reply Reply Quote 0
              • jay_s_ukundefined
                jay_s_uk @ericlmccormick
                last edited by

                @ericlmccormick ok, well M114 is just the current axis position which is available under move.axis[].machine position (IIRC). You can then feed it back into the probe or whatever else you want to do using G31 or the appropriate gcode

                Owns various duet boards and is the main wiki maintainer for the Teamgloomy LPC/STM32 port of RRF. Assume I'm running whatever the latest beta/stable build is

                ericlmccormickundefined 1 Reply Last reply Reply Quote 0
                • ericlmccormickundefined
                  ericlmccormick @jay_s_uk
                  last edited by

                  @jay_s_uk using move.axis[2].machine position, I was able to get the value I was looking for. This is a huge step to getting this working.
                  However, I am now running into a new issue. Once var z_height is loaded from a file, trying to update that variable returns an error saying that the variable already exist. I am not an expert coder, but when I have programmed in other languages the past, you could just call up the variable and give it a new value. Do you know how to give an existing variable a new value?

                  OwenDundefined T3P3Tonyundefined 2 Replies Last reply Reply Quote 0
                  • OwenDundefined
                    OwenD @ericlmccormick
                    last edited by

                    @ericlmccormick
                    You have to use SET in cases where the variable has already been created
                    Also access to any variable or global value is in the format
                    var.myVar

                    set var.z_height = 1234
                    echo var.z_height
                    
                    1 Reply Last reply Reply Quote 0
                    • T3P3Tonyundefined
                      T3P3Tony administrators @ericlmccormick
                      last edited by

                      @ericlmccormick

                      In addition to what @OwenD has already pointed out, you may find the following logic useful:

                      if !exists(global.myVariable)
                        global myVariable = move.axis[2].machine position
                      else
                        set global.myVariable = move.axis[2].machine position
                      

                      It's helpful for global variables especially where it might not be known if this is the first time the ste assigned.

                      www.duet3d.com

                      1 Reply Last reply Reply Quote 0
                      • mikeabuilderundefined
                        mikeabuilder
                        last edited by

                        I've spent a fair bit of time working on this same problem and I'll describe the macros I've come up with to help me.

                        Step 1 - Manual adjustment
                        My first step in setting a z-offset is to use a feeler gauge to get the nozzle at .2mm (I don't use paper for this, a good set of feeler gauges is less than $10). After doing this, I run my first Macro that adjusts the z offset so that the current tool position is .2mm.

                        ; This macro adjusts the zoffset of the current tool to make the current z value 0.2mm
                        ; optional input is Z = value we want Z to be after changing the z offset
                        ; use case:
                        
                        var target_z_vale = 0.2
                        
                        M291 P"This macro will reset the current tool offset to make the current tools Z value 0.2mm." S3 T0
                        
                        ;If no tools is selected, prompt the user
                        if state.currentTool =-1    ; menas there is no tool selected 
                          M291 P"No tool selected. Select tool first." S1 T0
                          M99
                        
                        ; Read the current tool offset
                        var current_Z_offset = tools[state.currentTool].offsets[2]
                        
                        ; read the current z value
                        var current_z = move.axes[2].machinePosition
                        
                        ; calculate z offset needed to make current Z = 0.2mm
                        var new_z_offset = 0.2 - var.current_z
                        
                        ; build the command string
                        var command = "G10 P"^{state.currentTool}^" Z"^{var.new_z_offset}
                        
                        
                        ; Create the replacement for the tooloffset file 
                        echo >{"tool"^state.currentTool^"_Z_offset.g"} {"; This macro sets the Z offset for tool # "^state.currentTool}
                        echo >>{"tool"^state.currentTool^"_Z_offset.g"} {var.command^"  ;changed: "^state.time}
                        
                        
                        ; run the new z_offset file
                        M98 P{"0:/sys/tool"^state.currentTool^"_Z_offset.g"}
                        

                        I have a personal peeve about using M500 to store parameters, so I write little macro files. On line 24, you'll see I build the G10 command to set the new z-offset. Then, on lines 28 and 29, I write that command to a file. I also include the date and time the file is created in a comment. Finally, I run that macro (line 33) to set the new z-offset.

                        And since I have a problem with M501 to restore settings, I have a line in my config.g file to run that macro on startup.

                        Having the date and time in the macro means I can look at it to verify that a new z-offset really got saved.

                        Step 2 - folding baby steps into the z-offset
                        My second step in setting z-offset if to use baby stepping when the first layer is printing to get it dialed in better. This macro is something I run after a print finishes to "roll" those baby steps into the z-offset.

                        ; Macro to 
                        ;  1. Read the current babysteps
                        ;  2. Read the Z offset on the current tool
                        ;  3. Create a command to adjust the tool z offset to use zero babysteps 
                        ;  4. Overwrite the zoffset file for this tool with the command
                        ;  5. Set baby steps to 0
                        ;  6  Run the new z-offset file
                        
                        M291 P"This macro will reset the current tool offset incorporate the currently active babysteps." S3 T0
                        
                        ;If no tools is selected, prompt the user
                        if state.currentTool =-1    ; menas there is no tool selected 
                          M291 P"No tool selected. Select tool first." S1 T0
                          M99
                        
                        
                        ; Read the current baby steps
                        var baby_steps = move.axes[2].babystep  ; Positive babystep means the bed is farther from the nozzle
                        
                        ; Read the current tool offset
                        var current_Z_offset = tools[state.currentTool].offsets[2]
                        
                        ; Calculate the new tool offset
                        var z_offset_new = var.current_Z_offset - var.baby_steps
                        
                        ; build the command string
                        var command = "G10 P"^{state.currentTool}^" Z"^{var.z_offset_new}
                        
                        ; clear the baby stepping
                        M290 R0 S0
                        
                        ; Create the replacemetn fro the tooloffset file 
                        echo >{"tool"^state.currentTool^"_Z_offset.g"} {"; This macro sets the Z offset for tool # "^state.currentTool}
                        echo >>{"tool"^state.currentTool^"_Z_offset.g"} {var.command^"  ;changed: "^state.time}
                        
                        
                        ; run the new z_offset file
                        M98 P{"0:/sys/tool"^state.currentTool^"_Z_offset.g"}
                        

                        This macro operates similarly to the first one, but makes an adjustment based on the current baby steps. It's important to clear the baby steps (line 30) in addition to changing the z-offset.

                        If you have multiple tools, and want to adjust z-offsets based on the baby steps differently for each tool, you need to include it in the tool change process. That's another macro and I'll leave it out for now.

                        1 Reply Last reply Reply Quote 2
                        • ericlmccormickundefined
                          ericlmccormick
                          last edited by

                          I ended up solving this with a few lines of code and it works really well.
                          Step 1.Created a file called saved_var.g with a single line in it

                          global z_offset = 1
                          

                          Step 2. Added the following line to the end of config.g

                          M98 P"saved_var.g" 	;Load saved z offset
                          

                          Step 3. In homez.g, I replaced my G31 code with

                          G31 P1000 X-32 Y0 Z{global.z_offset} 
                          

                          Step 4. I made a Marco with the following

                          set global.z_offset =  global.z_offset - move.axes[2].machinePosition + 0.5
                          echo >"saved_var.g" "global z_offset = " ^ global.z_offset
                          

                          Step 5. Reboot, Home All, bring the nozzle to the correct height using my gauge. Then run the macro.

                          Note: I used a metal ruler that happens to be exactly 0.5mm. If you use something else, then you need to replace the 0.5 with whatever thickness gauge you use.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Unless otherwise noted, all forum content is licensed under CC-BY-SA