Hello Guest it is December 10, 2019, 01:18:47 PM

Author Topic: Read droGageBlockT  (Read 369 times)

0 Members and 1 Guest are viewing this topic.

Re: Read droGageBlockT
« Reply #10 on: May 25, 2019, 02:11:06 AM »
absolutely, im up for any solution you have. although in the meantime I have gone and created a new instance register.I can change it to global if that's better. I was just kind of messing around here trying different thing. ive done this all on a pc with a demo running so I don't screw the one running the machine up anymore than I have already lol. I also changed the code of the drogageblockt code to the script im posting below. running the register diagnostic, the register changes then I input a number in the drogageblockt DRO.

could I now reference that register in my macro(I hadn't made it this far yet) and use it this way? or will it give me the same trouble I had before.

local inst = mc.mcGetInstance()

local val = scr.GetProperty("droGageBlockT", "Value")  this line was already present
local hreg = mc.mcRegGetHandle(inst, "iRegs0/TSoffset") --added this line
mc.mcProfileWriteString(inst, "PersistentDROs", "droGageBlockT", string.format (val))
mc.mcRegSetValueString(hreg, tostring(val)) --added this line.
Re: Read droGageBlockT
« Reply #11 on: May 25, 2019, 02:22:45 AM »
ive made it this far and its returning the numbers as it should. again this is in the script editor so who knows if it will actually work on the machine. if you see any issues please don't hesitate to point them out.

function m6()
   
   local inst = mc.mcGetInstance()
   local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
   local selectedtool = 1 --mc.mcToolGetSelected(inst)
   local currenttool = 2 --mc.mcToolGetCurrent(inst)
   local xstart = mc.mcAxisGetPos(inst, 0) --X Axis--
   local ystart = mc.mcAxisGetPos(inst, 1) --Y Axis--
   local hreg = mc.mcRegGetHandle(inst, "iRegs0/TSoffset")
   local val = mc.mcRegGetValueString(hreg)
   local tsoffset = tonumber(val)
Re: Read droGageBlockT
« Reply #12 on: May 25, 2019, 02:58:47 AM »
This is what I came up with. It is working in the editor. Well see in the morning if it will run on the machine. I would like to add a little more on the math end though dealing with negative numbers. Well see how it does on the machine and go from there. I changed a few things to make it easier to change variables if I need to as well. again, if anyone sees any issues, or has some advice on what I could do to improve it. Don't hesitate to post.





function m6()
   
   local inst = mc.mcGetInstance()
   local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
   local selectedtool = 1 --mc.mcToolGetSelected(inst)
   local currenttool = 2 --mc.mcToolGetCurrent(inst)
   local xstart = mc.mcAxisGetPos(inst, 0) --X Axis--
   local ystart = mc.mcAxisGetPos(inst, 1) --Y Axis--
   local xchangepos = 12
   local ychangepos = -46.0857
   local xprobepos = .6831
   local yprobepos = -46.0857
   local reg = mc.mcRegGetHandle(inst, "iRegs0/TSoffset") --Get register handle
   local regval = mc.mcRegGetValueString(reg) --Get the string
   local tsoffset = tonumber(regval) --The value set in the Tool Offset Gage Block Height
   
   
   if selectedtool == currenttool then
       
      mc.mcCntlSetLastError(inst, "No Tool Changed Required")
   
   else
     
      mc.mcCntlGcodeExecuteWait(inst, "G53 G0 Z-0.2\nG53 G0 X"..xchangepos.." Y"..ychangepos.."") --Move the Z to Home.Then to the X, Y Coords for our tool change.
      mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedtool) .. "   Previous Tool == " .. tostring(currenttool)) --Specify which tools changed--
          wx.wxMessageBox("Remove Dust Boot and Change Tool \nThen Press Ok")
      mc.mcCntlGcodeExecuteWait(inst, "G53 G0 X"..xprobepos.."Y"..yprobepos.."") --Move to Tool Setter position--
          wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue")
     mc.mcToolSetCurrent(inst, selectedtool) --change selected tool to current
      mc.mcCntlSetLastError(inst, "Probing in Progress!")
      mc.mcCntlGcodeExecuteWait(inst, " G91 G31 Z-5. F10.")--probe the new tool

local ProbedZ = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_Z) -- Z Probe position in Machine coords
     mc.mcCntlGcodeExecute(inst, string.format('G ' .. posmode))--return to pre macro mode G90, or G91
     mc.mcCntlGcodeExecuteWait(inst, "G00 G53 Z-0.2")--Retract
       
 
local NewOffset = math.abs(ProbedZ - tsoffset)
     mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset)
     mc.mcCntlSetLastError(inst, string.format("Auto tool setting complete, Offset = %.4f", NewOffset))
     mc.mcCntlGcodeExecuteWait(inst,"G53 G0 X"..xchangepos.."")
         wx.wxMessageBox("Toolchange Complete.\nReplace dust boot")
     mc.mcCntlGcodeExecuteWait(inst, "G90 G0 X"..xstart.."Y"..ystart)
   
     end
   
   

end

if (mc.mcInEditor() == 1) then
 m6()
end
Re: Read droGageBlockT
« Reply #13 on: May 25, 2019, 11:05:55 AM »
 --To my understanding your trying to use these two Probing Screens Dro's
 --Make sure you have created registers GageBlock and GageBlockT before you can write and read them.
 
   local GageBlock = scr.GetProperty("droGageBlock", "Value")
   local GageBlockT = scr.GetProperty("droGageBlockT", "Value")

   WriteReg('iRegs0/GageBlock',tonumber(GageBlock));
   WriteReg('iRegs0/GageBlockT',tonumber(GageBlockT));
   
 --Never tried to share the same named variable but worth a test
 --if not just change them. (more of a scripting timing and use issue id expect)
 --Just have to make sure any changes to the Dro's get reread and rewritten to the registers
 --I do remember if you try to read the registers to soon after writing to them they are not available.
 --so a dwell might need to be used if in the same script
 --But in most cases registers are updated in the PLC or button script and then used in the M code.
 
   local GageBlock = ReadReg("iRegs0/GageBlock");
   local GageBlockT = ReadReg("iRegs0/GageBlockT");
Re: Read droGageBlockT
« Reply #14 on: May 25, 2019, 03:11:33 PM »
Thanks you both for your lending your knowledge. The only time I want to write to the register is when I input a new number into the tool offset gage block height DRO manually.

-so I can measure the top of my stock with my touch probe.
-zero my z for my work offset
-move over to the tool setter position
-probe it to get the offset in height of the material top and the tool setter top.
-input that number into the droGageBlockT location, have that write to the register to be used in the m6 macro.
-then proceed with an m6 to change to the first tool I want to use.
-the m6 will then read that droGageBlockT and use it to set my tool offset

If that makes any sense at all. I have no way to change tools repeatably on my router. So I cant setup a normal tool table like I'm used to. This was the only solution I could come up with to automate changing tools and setting their offsets mid program without jogging around and manually zeroing with each tool change.

when I tested this manually all the math came out correct and my tool heights were set correctly. I guess that doesn't mean it will work when I try and automate it.

I just created the registers on my machines PC. Loaded the new m6 macro to it. I'm going to give it a go. The script worked in the editor again, but my last one did as well and failed when an m6 was called. which joe explained very well as to why that happened.

 
Re: Read droGageBlockT
« Reply #15 on: May 25, 2019, 07:17:23 PM »
ok so now the code runs perfectly fine. But all the offsets are wrong. Its driving me crazy lol. I thought I could reference the same thing as the "touch" function when setting TLO. If I input my "tool setter height" in the touch function the same as I do in the "gage block dro" on the offsets tab. the touch function sets my offsets correctly. I run the M6 and its not even close…
Re: Read droGageBlockT
« Reply #16 on: May 25, 2019, 07:42:05 PM »
Hi,
I experimented a little on my laptop along the lines of your proposal and I had a similar problem.
I haven't really sorted out why.

What I found was that just because I entered a new number into the DRO that did not necessarily translate  into the update
of the iRegs0 register that I was using as a relay.

I altered the code of the 'On Update' event script to copy the data from the DRO to the destination register. The data WOULD
be copied, I found, if I hit the <Set Z> or <Set Tool> button. I concluded therefore that the 'On Update' script runs
reliably ONLY AFTER one of the two buttons is pressed.

The only way I can see to overcome this problem is to put some code into the the PLC script that copies the data from the
DRO to the register. I am loathe to do so because of the software overhead that represents but there may not be much choice.

The second question remains, lets say we did bite the bullet and put code in the PLC script to keep the register updated,
or as I call it 'in lockstep' with the DRO.....does that code run during Gcode interpreter sessions.  It would really only make sense
that the PLC script should run irrespective of which chunk (GUI or Mach core) is in control. This is the true measure of my
ignorance of how Mach4 works, but the truth is I don't know whether that is the case or not.

In some respects while I endeavor to help others by contributing on the forum, I in fact have to learn about Mach and I do so
by others posing questions that I never thought to ask. Thus its contributing to the forum that aids my learning curve.

Craig
My wife left with my best friend...
     and I miss him!
Re: Read droGageBlockT
« Reply #17 on: May 25, 2019, 08:28:42 PM »
Hi,
I wrote some code to put in the PLC script. It seems to work, that is to say that if you type in a new number
into the droGageBlockT and <enter> the PLC chunk will in very short order, within milliseconds, update the iRegs0/droRelay
register. As the PLC script runs multiple times per second, 20 times per second by default, the register should always reflect
the value of the DRO.

Code: [Select]
--Cause iRegs0/workZMax register to be updated by GageBlockDRO
local inst=mc.mcGetInstance()
local hreg=mc.mcRegGetHandle(inst,"iRegs0/droRelay")
local val = scr.GetProperty("droGageBlockT", "Value") 
mc.mcRegSetValue(hreg,tonumber(val))
--End of chunk

The question still remains...'will this short chunk maintain the updates when the Gcode Interpreter chunk is in control?'
Try it out and let us now.

Craig
My wife left with my best friend...
     and I miss him!
Re: Read droGageBlockT
« Reply #18 on: May 25, 2019, 10:59:07 PM »
Mine seems to update the register when I input a new number and just press enter. Pretty reliably. I will still use your code to have the reassurance I don't need to check it every time I do change the value. I applicate the time you've spent helping me resolve these issues too. I've learned quite a bit, not only about the lua end of mach, but just the way everything operates in general.


The last issue that is haunting me are these damn offsets.
-If i turn on the machine.
-home it.
-go to my workpiece, zero my g54 z. im not really concerned about x and y until i sort these offsets out.
-go to the tool setter position.
-probe it for height. its about .775 above my work z currently. 
-set that value in both the "touch plate height" in the touch menu, and the tool offset gage height(which is where our register is referencing)
-use the tool setter to set the touch probe height with the "touch" function. it writes 0 into TLO
-then i call a m6 t2 from mdi
-it runs though the m6 function as it should
-but my offsets are way off.

now if i do the same process starting over. then change to tool 2 manually and probe it with the "touch" function as well, it sets my offset correctly. The only math i need the m6 macro to do is subtract my "droGageBlockT" value from the z probe position. I have tried a couple different expressions to get it to return the correct number. Im doing something wrong it seems like. I went into the script for the "touch" function. It seems to do no more than what im trying to achieve with mine. subtracting the "touch plate height" from the z position. which in this case the touch plate height is the same as our gage block height register. what am i doing wrong here. I'll reference below the touch script im referring to.


function TouchOffZNeg1()   --Set tool length offset
   GetPreState()
   local CurTool = mc.mcToolGetCurrent(inst) --Current Tool Num
   if (CurTool > 0) and (CurTool < 255) then
      GetMachPos('Z')
      TouchOff('Z', -1) -- Do a touch move in the X negative direction
      local ZPos = mc.mcAxisGetProbePos(inst, mc.Z_AXIS, 0)
      ToMachPos('Z')
      local GageBlock = ToffPlate
      local OffsetVal = ZPos - GageBlock
      OffsetState = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_8) --Current Height Offset State
      
      mc.mcCntlGcodeExecuteWait(inst, "G49")
      mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, CurTool, OffsetVal)
      mc.mcCntlSetLastError(inst, string.format("Tool %.0f Height Offset Set: %.4f", CurTool, OffsetVal))
      if (OffsetState ~= 49) then
         mc.mcCntlMdiExecute(inst, string.format("G%.1f", OffsetState))
      end
   else
   mc.mcCntlSetLastError(inst, ('Cannot set a height for an invalid tool number. Call a valid tool and try again.'))
   end
   mc.mcCntlGcodeExecute(inst, string.format('G ' .. m_CurAbsMode .. '\nF ' .. m_CurFeed)) --Set mode and feed back
end

Re: Read droGageBlockT
« Reply #19 on: May 26, 2019, 10:14:48 AM »
id start with mc.mcCntlSetLastError print out of each variable in the script then look at the history to see what is not correct.
but I suspect as id mentioned before its a timing issue and a sleep of a few seconds (maybe 3) needs to be used then look at the variables again in the same script. As it takes a little time for all the registers to update.