Hello Guest it is March 28, 2024, 05:48:49 AM

Author Topic: Lua Macro Parameters  (Read 6442 times)

0 Members and 1 Guest are viewing this topic.

Lua Macro Parameters
« on: February 24, 2017, 12:10:04 PM »
The following code compiles and executes in the script editor.  After it executes the editor I get a message saying the editor has failed and is closed yet it is open.  hParam is always 'error' when run in the editor.  When I try to run in Mach4 mdi 'm6690 D.9' the last error line says 'Do MDI 1' and nothing happens.  What am I doing wrong and how do you track down/test this?

Code: [Select]
--Reel Seat Insert
--Turn from current Z down to 0 from current X down to parameter D or .850

function m6690(hParam)
  local xNow,zNow,maxVal
  local TS,TR,TT --timer values Mach3 niy
  local plungeFeed=6
  local roughDOC=0.015
  local roughFeed=15
  local roughSpeed=900
  local finishDOC=0.004
  local finishFeed=8
  local finishSpeed=1400
  local inst=mc.mcGetInstance()
  local endX=.850 --param1()
 
  local inst=mc.mcGetInstance()

  mc.mcCntlSetLastError(inst,'')

  if (hParam ~= nil) then
    local DFlag = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_D)
    if(DFlag == 1) then
      endX = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_D)
    end
  end

  xNow = mc.mcAxisGetPos(inst,0)
  zNow = mc.mcAxisGetPos(inst,2)
  if xNow<=endX then
    wx.wxMessageBox("start X is less than end Diameter of "..tostring(endX))
    return
  end
  if zNow<=0 then
    wx.wxMessageBox("start Z is less than or = to 0")
    return
  end
  --ts=Timer
 
--rough cuts 
  mc.mcCntlGcodeExecuteWait(inst,"M3 S"..tostring(roughSpeed))
  while (mc.mcAxisGetPos(inst,0)>(endX+finishDOC*2)) do
    currPos=mc.mcAxisGetPos(inst,0)
    maxVal=math.max(endX+2*finishDOC,currPos)
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(maxVal-roughDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(roughFeed).." Z0")
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    maxVal=math.max(endX+2*finishDOC,currPos)
    if currPos==endX+2*finishDOC then
      break
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(math.max(endX+2*finishDOC,currPos-roughDOC)))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(roughFeed).." Z"..tostring(zNow))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end 
    if currPos==endX+2*finishDOC then
      break
    end 
  end
  -- tr=Timer-ts
  --wx.wxMessageBox("Roughing Time: "..tostring(tr))

  --finish cuts
  mc.mcCntlGcodeExecuteWait(inst,"M3 S"..tostring(finishSpeed))

  while (mc.mcAxisGetPos(inst,0)>=(endX+finishDOC)) do
    currPos=mc.mcAxisGetPos(inst,0)
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(currPos-finishDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(finishFeed).." Z0")
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
        return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    if currPos==endX then
      break
    end 
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(currPos-finishDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(finishFeed).." Z"..tostring(zNow))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    if currPos==endX then
      break
    end 
  end
  mc.mcCntlGcodeExecuteWait(inst,"M5\nM9")
--te=Timer-ts
--wx.wxMessageBox("Roughing Time: "..tostring(tr).." - Total Time: "..tostring(te))
end
 
if (mc.mcInEditor() == 1) then
    m6690()
end   

Thanks for any help,

RT

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Lua Macro Parameters
« Reply #1 on: February 24, 2017, 06:13:32 PM »
It is hard to simulate a macro with parameters because hParam is a handle to the parameters which just does not exists in the editor environment.  What I do is just set the variable to what you want to test with and ignore the parameter retrieval stuff while developing.  e.g. Set endX to a value and start debugging. 

Code: [Select]
--Reel Seat Insert
--Turn from current Z down to 0 from current X down to parameter D or .850

function m6690(hParam)
  local xNow,zNow,maxVal
  local TS,TR,TT --timer values Mach3 niy
  local plungeFeed=6
  local roughDOC=0.015
  local roughFeed=15
  local roughSpeed=900
  local finishDOC=0.004
  local finishFeed=8
  local finishSpeed=1400
  local inst=mc.mcGetInstance()
  local endX=.850 --param1()
 
  local inst=mc.mcGetInstance()

  mc.mcCntlSetLastError(inst,'')

  if (mc.mcInEditor() == 1) then
    endX = .850 --param1()
  else
    if (hParam ~= nil) then
      local DFlag = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_D)
      if(DFlag == 1) then
        endX = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_D)
      end
    end
  end
  xNow = mc.mcAxisGetPos(inst,0)
  zNow = mc.mcAxisGetPos(inst,2)
  if xNow<=endX then
    wx.wxMessageBox("start X is less than end Diameter of "..tostring(endX))
    return
  end
  if zNow<=0 then
    wx.wxMessageBox("start Z is less than or = to 0")
    return
  end
  --ts=Timer
 
--rough cuts 
  mc.mcCntlGcodeExecuteWait(inst,"M3 S"..tostring(roughSpeed))
  while (mc.mcAxisGetPos(inst,0)>(endX+finishDOC*2)) do
    currPos=mc.mcAxisGetPos(inst,0)
    maxVal=math.max(endX+2*finishDOC,currPos)
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(maxVal-roughDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(roughFeed).." Z0")
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    maxVal=math.max(endX+2*finishDOC,currPos)
    if currPos==endX+2*finishDOC then
      break
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(math.max(endX+2*finishDOC,currPos-roughDOC)))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(roughFeed).." Z"..tostring(zNow))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end 
    if currPos==endX+2*finishDOC then
      break
    end 
  end
  -- tr=Timer-ts
  --wx.wxMessageBox("Roughing Time: "..tostring(tr))

  --finish cuts
  mc.mcCntlGcodeExecuteWait(inst,"M3 S"..tostring(finishSpeed))

  while (mc.mcAxisGetPos(inst,0)>=(endX+finishDOC)) do
    currPos=mc.mcAxisGetPos(inst,0)
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(currPos-finishDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(finishFeed).." Z0")
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
        return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    if currPos==endX then
      break
    end 
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(plungeFeed).." X"..tostring(currPos-finishDOC))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    mc.mcCntlGcodeExecuteWait(inst,"G01 F"..tostring(finishFeed).." Z"..tostring(zNow))
    if mc.mcSpindleGetSensorRPM(inst)==0 then
      wx.wxMessageBox("Spindle Stopped")
      return
    end
    currPos=mc.mcAxisGetPos(inst,0)
    if currPos==endX then
      break
    end 
  end
  mc.mcCntlGcodeExecuteWait(inst,"M5\nM9")
--te=Timer-ts
--wx.wxMessageBox("Roughing Time: "..tostring(tr).." - Total Time: "..tostring(te))
end
 
if (mc.mcInEditor() == 1) then
    m6690()
end   

Steve
Re: Lua Macro Parameters
« Reply #2 on: February 24, 2017, 07:57:44 PM »
Fresh restart of Mach4
Went old school and started a new macro and copied, pasted and tested line by line and it works. 

Either multiple failures confused Mach4 or the editor inserted an invisible character that didn't get copied.

Just another learning experience

RT
Re: Lua Macro Parameters
« Reply #3 on: February 25, 2017, 01:37:09 PM »
Newest wrinkle

From the MDI or Gcode file:

m6691                          runs as expected
m6691 d.9200               passes variable d and runs as expected

From a button left up script

mc.mcScriptExecute(0,'m6691.mcs',false)  locks up Mach4 gui

if it runs from the MDI and gcode file why not from a button call event?

I tested another macro m6692.mcs which contained only a wx.wxMessageBox('Hello') in the function call and it ran from the button.

I am missing some basic concept

RT

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Lua Macro Parameters
« Reply #4 on: February 25, 2017, 04:16:03 PM »
Use mc.mcCntlMdiExecute() from the button script.  It will be the same as running it in the mdi window.

Otherwise, the more proper way to run code from both the screen and a macro is to put the common code in a module.  Then require the module in both the screen script and the macro script.  Then all you need to do is call the lua module function.

Steve
Re: Lua Macro Parameters
« Reply #5 on: February 25, 2017, 04:44:23 PM »
Steve, thanks for taking time to look at this.

I understand your method and will implement that way of doing this.

But I would like to understand why the failure occurs doing the way I have it so I don't replicate spending 6 hours trying to find an error.

If I understand you, the mcExecuteScript is only to be used with modules? 

Could you point me to where I can study up on what and how to make and use modules?

TIA

RT

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Lua Macro Parameters
« Reply #6 on: February 25, 2017, 07:53:52 PM »
The first thing to understand is if you do ANYTHING from a GUI event that is long running, it will lock the GUI for that duration.  Why?  Because you have directed the thread that runs the GUI to run your code.  While it is doing that, it can't sit there and update DROs and work the buttons.  So the thing to do is hand off the task to another thread.  That is what mc.mcCntlMdiExecute() does.  It executes your code in a thread in the core. 

If you are running the stock screen set, take a look at the Modules directory.  mcMasterModule.lua.  That is a module file that is "required" by the screen load script.  This is the lua method of "including" common code.  Open up the screen load script in the screen editor and see how it is implemented. 

Otherwise, there is a bunch of really dry reading in the LUA programming manuals.  :(  The manuals are good, but damn...  painful to read.  LOL

Steve

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Lua Macro Parameters
« Reply #7 on: February 25, 2017, 09:19:42 PM »
Quote
Could you point me to where I can study up on what and how to make and use modules?

This may help
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Lua Macro Parameters
« Reply #8 on: February 27, 2017, 07:03:54 PM »
That example was very helpful, thanks for that.  It made a lot of the Lua documentation more understandable.

RT

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Lua Macro Parameters
« Reply #9 on: February 28, 2017, 09:02:02 AM »
No problem, glad it helped.  :)
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!