Hello Guest it is April 28, 2024, 04:33:38 AM

Author Topic: Writing macro would someone please show light  (Read 512 times)

0 Members and 1 Guest are viewing this topic.

Writing macro would someone please show light
« on: November 07, 2023, 01:18:34 PM »
Mach 4. I need to write a couple of simple macros to turn outputs on and off, a bit of a sleep time, with an input to boot. I have seen two types of code, one of them I know works fine I have put the script on a button after adding in some additional lines. But this one I came across and would be keen to know what its for, what it means as it has a hParam section which I do not understand.

So this is the one I am asking about:

function m162_setOutput(number)
   local Osig = mc.OSIG_OUTPUT0
   Osig = (Osig + number)
   local hReg = mc.mcSignalGetHandle(mc.mcGetInstance(), Osig)
   if( hReg ~= nil ) then
      mc.mcSignalSetState(hReg, true) --Turn the output on
   end
end

function m162(hParam) --The param is a P value called from Gcode. M162P3 for example.
   if (hParam ~= nil) then
      local inst = mc.mcGetInstance()
      local varP = 0
      local flagP, rc = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_P)
      if (flagP == 1) then --Check that the flag has been set so we do not get an unexpected value for mc.SV_P
         varP = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_P)
         m162_setOutput(varP)
      end
   end
end

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

And this is the one I would normally use:


----------Variables----------
local inst = mc.mcGetInstance() --Get the instance of Mach4
local brake = mc.OSIG_OUTPUT1 --Save the name of the Signal (as stored inside of Mach4)
local ActivateSignalTime = 5000 --Time in milliseconds that we want the signal to be active.
local hReg = 0 --The handle to the signal
local rc = 0

hSig, rc = mc.mcSignalGetHandle(inst, brake)
if (rc ~= 0) then --There was error   
   mc.mcCntlSetLastError(inst, "There was an error")
else
   rc = mc.mcSignalSetState(hSig, 1) --Turn the output on
   if (rc ~= 0) then --There was error   
      mc.mcCntlSetLastError(inst, "There was an error")
   else
      wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
      rc = mc.mcSignalSetState(hSig, 0) --Turn the output off
      if (rc ~= 0) then --There was error   
         mc.mcCntlSetLastError(inst, "There was an error")
      end
   end
end

I need to write a brake on/off M10/M11 macro set which is the reason for this. Like I said I have managed to get the multiple outputs into a button script. If its a Macro do I need to do anything much differently except for name it and put it into the Macros folder? Btw I use ChatGpt and managed to write (with a lot of back and forth) a turret change for my lathe in Mach 3

Offline cncmagic

*
  •  63 63
  • what me worry? heck...it ain't my machine anyway
    • View Profile
Re: Writing macro would someone please show light
« Reply #1 on: December 21, 2023, 05:44:34 PM »
if you are simply doing an ON, delay, then OFF, or some variation of this, you'd probably be better off using the PMC coding.. that's the MACH4 version of a plc.. for something this straightforward that would be the easiest to implement. Takes a little time to get used to it, and the documentation is a bit lean but much simpler than writing another M function most likely.  :-\
any semblance of information posted to anything remotely  close to accuracy is merely coincidence. Use at you own discretion.. or play the lottery.. same odds