Hello Guest it is October 04, 2023, 11:20:01 PM

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rhtuttle

451
Mach4 General Discussion / Mach4/Lua script do's and Dont's
« on: March 13, 2017, 04:05:26 PM »
I am hoping someone can explain the do's and don't's of Mach4/Lua and  macro scripts and GUI's and how they interplay. 

As I understand it there are two instances of Mach4 Lua code running simultaneously.  One for the GUI(screen load script) and one for running macros/gcode/movement?

I was told to create a module for code that is to be used by both the screen and macro scripts.  I have some code that I put in the mach4hobby\modules directory and named it rtMyModulse.lua. It contains 3 functions at this point.

My ScrrenLoadScript contains:  rt=require "rtMyModule" in the load modules section which successfully instantiates rt for use in the screen.

The first two functions work as expected from both the a screen button and a call to a macro (m9002.mcs example below).

The third function rtProbeX(dir) also works in a button script: rt.rtProbeX(1).

However, when I call m9000 from the mdi, or from a button using: mc.mcCntlMdiExecute(inst,"m9000"), it calls rtProbeX and executes the first part past the 'quick strike' and then fails.

Can a macro run several mcCntlExecuteGcodeWait?
Can a function in a module be called by a macro and execute several mcCntlExecuteGcodeWait?
Can you mcCntlSetLastError from a macro?
Can a function in a module be called by a macro and execute mcCntlSetLastError?

I've read that Lua does not have real threads but do the Gui and macro scripts not communicate/syncronize through callbacks or coroutine?

Are there any whitepapers on Mach4 structure and interplay?

rtMyModule.lua
Code: [Select]
ocal rtModule = {}
  local inst = mc.mcGetInstance()
  local profile = mc.mcProfileGetName(inst)
  local path = mc.mcCntlGetMachDir(inst)
 
  function rtModule.rtMessage(msg)
    wx.wxMessageBox(msg)
  end
 
  function rtModule.rtAudio(fileName)
    local audio=wx.wxSound(path.."\\Sounds\\"..fileName..".wav",inst)
    audio:Play()
  end

  function rtModule.rtAudioCountDown(StartNum,EndNum)
    local i=StartNum
    while i>=EndNum do
      rtModule.rtAudio(""..i)
      wx.wxMilliSleep(900)
      i=i-1
    end 
  end
 
 function rtModule.rtProbeX(Dir)
  local r1,r2,r3,retVal,rc
  --  local inst=mc.mcGetInstance()
  local xStart,rc

  xStart=mc.mcAxisGetPos(inst,0)-- Save starting X pos
  -- mc.mcCntlSetLastError(inst,"")
  mc.mcCntlGcodeExecuteWait(inst,"G31 X0 F4")  -- Quick strike

  r1,rc=mc.mcAxisGetProbePos(inst,0,false)
  mc.mcCntlGcodeExecuteWait(inst,'G01 X'..tostring(mc.mcAxisGetPos(inst,0)+0.030*Dir)..' F4') --Back off
  mc.mcCntlGcodeExecuteWait(inst,'G31 X0 F0.5')  -- First slow strike
  r1,rc=mc.mcAxisGetProbePos(inst,0,false)
  mc.mcCntlGcodeExecuteWait(inst,'G01 X'..tostring(mc.mcAxisGetPos(inst,0)+0.030*Dir)..' F4') --Back off
  mc.mcCntlGcodeExecuteWait(inst,'G31 X0 F0.5') -- Second slow strike
  r2,rc=mc.mcAxisGetProbePos(inst,0,false)
  mc.mcCntlGcodeExecuteWait(inst,'G01 X'..tostring(mc.mcAxisGetPos(inst,0)+0.030*Dir)..' F4')  --Back off
  mc.mcCntlGcodeExecuteWait(inst,'G31 X0 F0.5') -- Third slow strike
  r3,rc=mc.mcAxisGetProbePos(inst,0,false)

  mc.mcCntlGcodeExecuteWait(inst,'G01 F8 X'..xStart)--return to starting position
  retVal=(r1+r2+r3)/3  -- average the strikes
  --  mc.mcCntlSetLastError(inst,string.format("%1.4f - %1.4f - %1.4f ",r1,r2,r3))
  return retVal
end

return rtModule



m9000.mcs
Code: [Select]
--m9000 lathe centering probe

function m9000(hVars)
    local xDis=1
    local zDis=.25
    local zStart
    local zEnd
    local xPos
    local xNeg
    local flag, rc
    local rt = require "rtMyModule"
    local inst = mc.mcGetInstance() -- Get the current instance
    local nilPoundVar = mc.mcCntlGetPoundVar(inst,0)
    local message = ""

    if hVars ~= nil then
      flag, rc = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_X)
      if (flag == 1) then
        xDis, rc = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_X)
        if xDis==nil then
          xDis=1
        end
      end
      flag, rc = mc.mcCntlGetLocalVarFlag(inst, hVars, mc.SV_Z)
      if (flag == 1) then
        zDis, rc = mc.mcCntlGetLocalVar(inst, hVars, mc.SV_Z)
        if zdis==nil then
          zDis=0.25
        end
      end
    end

    mc.mcAxisSetPos(inst,0,0)
    zStart,rc=mc.mcAxisGetPos(inst,2)
    zEnd=zStart-zDis
    mc.mcCntlGcodeExecuteWait(inst,'G0 F1 X'..xDis..'\nG0 F1 Z'..zEnd)
    --mc.mcCntlSetLastError(inst, "Probing for Positive X" )
    xPos= rt.rtProbeX(1)
    mc.mcCntlGcodeExecuteWait(inst,'G0 Z'..zStart)
    mc.mcCntlGcodeExecuteWait(inst,'G0 X'..-xDis)
    mc.mcCntlGcodeExecuteWait(inst,'G0 Z'..tostring(zStart-zDis))
    --mc.mcCntlSetLastError(inst, "Probing for Negative X" )
    xNeg= rt.rtProbeX(-1)
    mc.mcCntlGcodeExecuteWait(inst,'G0 Z'..zStart)
    mc.mcCntlGcodeExecuteWait(inst,'g01 f8 X'..tostring(2*xPos-(xPos-xNeg)))
    mc.mcAxisSetPos(inst,0,0) 
end

if (mc.mcInEditor() == 1) then
    m9000(0)
end


m9002.mcs
Code: [Select]
function m9002()  --testing load rtMyModule
 local inst = mc.mcGetInstance()
 
 mc.mcCntlGcodeExecuteWait(inst,"g1F4 x1")
 
 rt = require "rtMyModule"
 
 if rt==nil then

    wx.wxMessageBox("rt==nil")

  else

    rt.rtMessage('Another Hello')

  end

end



if mc.mcInEditor()==1 then
  m9002()
end

Can anyone enlighten this old dog as to why this fails and what the rules and regulations are for scripting screen use and macro use?

RT

452
General Mach Discussion / Re: Question on Xbox 360 Plug-In on Mach 3
« on: March 11, 2017, 11:20:25 AM »
Not sure which zero you want to go to but:

Create a new macro, lets say m888.m1s

In it write

code("G0 Z0")

then configure you xBox controller A button to custom macro#1 and put 888 into the Custom Macro#1 box.

You can put as many moves or commands as you want:

code("G0 Z1")
code("G0 X0")
code("G0 Y0")
code("G0 Z0")

HTH

RT

453
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 11, 2017, 11:01:43 AM »
Quote
When the required tool is selected and the turret rotates I need it to run back against it's stop for every tool selected, when it's there then the DRO needs to be reset maybe

I don't know what is 'right' but if that is what you want to do then in each place where I commented out the line like this one for tool 4:

REM    SetOemDRO(803,4)

you would remove the REM and change it to:

   SetOemDRO(803,135)

I assume you are using stepper motors and backing up against the stop until the stepper loses steps. then I guess this would work.  Personally I would make your moves
G0 A138
while isMoving()
wend
G0 A135

No need to then reset the DRO.


Like I said I don't have a turret so I don't know what the proper way to use it.

Good luck

RT


454
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 10, 2017, 06:41:54 PM »
What version of Mach3 are you running?

455
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 10, 2017, 06:39:31 PM »
George,

If the speed setting is 1% then that is why your turret ref is so slooooow.  Change it to higher number!


I can only assume that you copy and pasted the code incorrectly so I have attached the file.  It runs in my editor

456
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 10, 2017, 05:34:17 PM »
Hi George, got any hair left?

First, in config ->homing/limits, check to see what 'Speed %' you have for the A axis.

Second, the screen set you are using is custom.  In your code you keep setting  DRO 803 to the new tool number.  that is the A axis dro not the current tool number.

I have taken your code and modified it a bit and it works for me when I step through it.  See if it works for you.  I have commented out the second line which I used to test it in the script editor.  If you want to step through with the editor debugger just take the REM out , step through it(don't try to step while it is moving the  dro wait until it stops) when done you should see the dro at the right angle and the tool numer changed in the box right and below.  Change the NEWTOOL number and test again.

Code: [Select]
NEWTOOL = GetSelectedTool()
REM NEWtool = 4
If NEWTOOL = GetCurrentTool() Then  ' If the tool called for is the same as already in then toolchange ignored
  NEWTOOL = NEWTOOL
ElseIf NEWTOOL > 8 Then            'If tool called is greater than 8 then code is stopped and message telling you why is displayed
 DoOemButton(1003)
 MsgBox("Tool number too high, file will rewind")
 DoOemButton(1002)
ElseIf NEWTOOL < 1 Then            ' If tool called is less than 1 then code is stopped and message telling you why is displayed
 DoOemButton(1003)
 MsgBox("Tool number too low, file will rewind") 'Tool number called is too high so code will stop and rewind so editing can take place
 DoOemButton(1002)
Else
  code("G90")
  If NEWTOOL = 1 Then
    Code("G0A0")
    While IsMoving()
    Wend
    Code("G0A-358")
    While IsMoving()
    Wend
REM    SetOemDRO(803,1)
  ElseIf NEWTOOL = 2 Then
    Code("G0A45")
    While IsMoving()
    Wend
    Code("G0A-43")
    While IsMoving()
    Wend
REM    SetOemDRO(802,2)
  ElseIf NEWTOOL = 3 Then
    Code("G0A90")
    While IsMoving()
    Wend
    Code("G0A-88")
    While IsMoving()
    Wend
REM    SetOemDRO(803,3)
  ElseIf NEWTOOL = 4 Then
    Code("G0A135")
    While IsMoving()
    Wend
    Code("G0A-133")
    While IsMoving()
    Wend
REM    SetOemDRO(803,4)
  ElseIf NEWTOOL = 5 Then
    Code("G0A180")
    While IsMoving()
    Wend
    Code("G0A-178")
    While IsMoving()
    Wend
REM    SetOemDRO(803,5)
  ElseIf NEWTOOL = 6 Then
    Code("G0A225")
    While IsMoving()
    Wend
    Code("G0A-223")
    While IsMoving()
    Wend
REM    SetOemDRO(806,6)
  ElseIf NEWTOOL = 7 Then
    Code("G0A270")
    While IsMoving()
    Wend
    Code("G0A-268")
    While IsMoving()
    Wend
REM    SetOemDRO(803,7)
  ElseIf NEWTOOL = 8 Then
    Code("G0A315")
    While IsMoving()
    Wend
    Code("G0A-313")
    While IsMoving()
    Wend
REM    SetOemDRO(803,8)
  End If
  SetCurrentTool(NEWTOOL)       ' Set Tool number DRO to new tool
End If
    

In config->general Config make sure that 'Rot 360 Rollover' is ticked and that 'Ang Short Rot on G0' is not ticked.
If this doesn't do it I am at a loss.

HTH

RT

457
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 10, 2017, 10:41:21 AM »
George,

I think it's time to re-post the the macro and the code you are using to call the macro.

458
General Mach Discussion / Re: Question on Xbox 360 Plug-In on Mach 3
« on: March 09, 2017, 06:39:24 PM »
On my machine that button says GOTO Z and it is not an editable button.  You may be using a custom screen so select operator->Edit Button Script and see if that button blinks meaning it is editable.  If so check out the code there.

On the stock screen like mine that button means to go to safe z as defined in your config.  If that is what you want to do or if you find in the button script on yours   doOEMButton(some number) then for your xBox A button select OEM Button #1 and then on the left side enter either the cutom button code from your button script or 17 for safeZ  in the Custom OEM Code #1

Otherwise write a macro and select custom Macro#1 and fill the macro number on the left.

HTH

RT

459
General Mach Discussion / Re: Inconsistant ATC Positioning
« on: March 09, 2017, 06:24:24 PM »
Ports and Pins->input signals
A Home enabled would be a green check mark
Just like you did for x and z axis.

460
Mach4 General Discussion / Re: Lua Macro Parameters
« on: March 09, 2017, 12:27:01 PM »
Thank you all for your guidance on this issue.

Daz, the loadScreen path and the load_modules paths are exactly the same.

I don't understand why but the loadScreen found and loaded the module but the load_modules failed because there was no
mach4 dir/profiles/my profile/modules folder.

Once I created the modules folder under the profile folder it worked.

I haven't tried it yet but I assume if I eliminated that as part of the package altogether it would have worked.

RT