Machsupport Forum
Third party software and hardware support forums. => HiCON Motion Controller => Topic started by: QCMIRD on October 05, 2015, 03:42:12 PM
-
I need help getting an Mcode set up, it would function something like this:
function m100()
Check INPUT1
IF Active (LaserReady) THEN
Activate OUTPUT1 (FireLaser, Note: Signal must be active for at least 50msec.)
Wait for INPUT1 to go Inactive
Deactivate OUTPUT1
Wait for INPUT1 to return to Active
Return to Gcode
Else
Display Error 'Laser Not Ready'
END
As you can probably see I only need to check the INPUT1 the first time M100 is called in the Gcode. I’m not sure how to write this and was hoping someone could help.
-
Hello,
You should be able to find a "Scripting Manual.pdf" within your Mach4 installation directory (Mach4Hobby/Docs/Scripting Manual.pdf). In that manual, a good example can be found on page 18.
Additionally, a "LuaCalls.txt" file can be found in this same directory which contains all the available scripting commands.
local inst = mc.mcGetInstance()
local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEFWD)
local spinstate = mc.mcSignalGetState(hsig)
if (spindstate == 0) then
mc.mcSignalSetState(hsig, 1)
else
mc.mcSignalSetState(hsig, 0)
end
In the case of your "Laser Ready", you can use one of the generic signals (e.g. ISIG_INPUT0 etc.). The same goes for the output that you'd want to activate (e.g. OSIG_OUTPUT0 etc.)
For waiting, you can keep checking the state of your input under a while loop and using a sleep.
input1State = mc.mcSignalGetState(input1Handle)
while input1State <> 0 do
wx.wxMilliSleep(100)
input1State = mc.mcSignalGetState(input1Handle)
end
mc.mcSignalSetState(output1Handle, 0)
I do recommend that you go through the documents that I had mentioned above, otherwise my explanation may make little sense without prior knowledge of how lua scripting works in Mach4.
-Marc
Vital System Inc.
-
Mach4CoreAPI is the document you want to use. The LuaCalls.txt is just a listing of the functions with zero explanation of what they do or how to use them.
Using loops in your code is risky business and can easily lead to the system hanging.
The PLC has very fast loop times so additional loops inside the PLC are generally not needed except in extremely critical time sensitive events.
You can monitor 'state changes' (first time thru M100, etc) by adding variables to hold a previous state. If you create variables in the screen load script, they are global (perpetually available from anywhere) and will maintain the last value assigned, so adding to the previous example, you can do something like
M100FirstPass = true <= put these in the Screen Load script
function M100()
if (M100FirstPass == true) then
CheckINPUT1()
M100FirstPass = false <= this line will prevent any more passes thru this routine
end
end