Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: dude1 on May 02, 2015, 05:12:40 AM

Title: need some lua help
Post by: dude1 on May 02, 2015, 05:12:40 AM
I am going through the M4 scripting manual have got up to the automatic tool height section

how do i set the getregiser function in a M code header file

its this bit
A brief run through: As before the first step in the script is getting and defining necessary variables and the current state of the machine. Register values are retrieved using the GetRegister() function created in section 4.3. It is not defined in this script so it must be defined in an M code header file or a loaded module to allow access to it.

I think I have everything else working
Title: Re: need some lua help
Post by: poppabear on May 02, 2015, 09:12:01 AM
You may have to copy/paste the GetRegister() function from the header file and put it directly into your script.
At least in the scripter, when debugging, it errors out and will not "find" that function in the header file.
It MAY work if calling from the MDI, but, I have not tested that.
Title: Re: need some lua help
Post by: dude1 on May 02, 2015, 04:45:50 PM
ok I tryed adding this it did not work

function GetRegister(regname)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
return mc.mcRegGetValueString(hreg)
end

is there a way of defining it as a macro

----------------------------------------------------------------------------
-- Auto Tool Setting Macro
-----------------------------------------------------------------------------
--[[
 Requires the following instance registers to be defined
 TS_XPos-----------X position of probe (machine position)
 TS_YPos-----------Y position of probe (machine position)
 TS_Type-----------Offset type (1 or 2)
 TS_TouchPos-------Z position of touch off surface (machine position)
 TS_ProbeH---------Height of probe above touch off surface
 TS_DefaultL-------Default tool length guess
 TS_Retract--------Retract distance after probe touch
 Offset Type 1-----Length of tool from gauge line to tip
 Offset Type 2-----Distance from tip of tool to the touch position
]]
--The function GetRegister() must be defined for use by macros  (its this bit I cant work out how to do)
Title: Re: need some lua help
Post by: poppabear on May 02, 2015, 08:44:08 PM
Just in case you did not, you have to have the register defined, before you call it.........

Scott
Title: Re: need some lua help
Post by: dude1 on May 02, 2015, 09:22:34 PM
thats the bit I dont now how to do
Title: Re: need some lua help
Post by: dude1 on May 02, 2015, 09:24:05 PM
at a guess

function GetRegister(TS_XPos)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s",TS_XPos ))
return mc.mcRegGetValueString(hreg)
end
Title: Re: need some lua help
Post by: pbarratt on May 02, 2015, 10:09:07 PM
I don't know if you can define them through statements but you can define them by opening up Regfile and adding them.  That's how I'm doing it.

Peter
Title: Re: need some lua help
Post by: dude1 on May 02, 2015, 11:59:54 PM
not quite sure how to do that ever
Title: Re: need some lua help
Post by: dude1 on May 03, 2015, 01:36:41 AM
I tried adding them to regfile it did not work

I think I have to do what`s at the bottom of page 16 top of 17 in the scripting manual
Title: Re: need some lua help
Post by: poppabear on May 03, 2015, 08:58:06 AM
you have to pass: TS_XPos, as a string: "TS_XPos"

Scott
Title: Re: need some lua help
Post by: pbarratt on May 03, 2015, 10:48:43 AM
Sorry, I thought you said they weren't defined yet.  Once they are, you can follow the instructions to access them as you say.

Peter
Title: Re: need some lua help
Post by: dude1 on May 03, 2015, 03:45:20 PM
I can find them its just the code needs them available for m code I worked out how to do it for one not all of them to together
Title: Re: need some lua help
Post by: dude1 on May 03, 2015, 10:11:51 PM
this is what i have come up with probley wrong

Code: [Select]
function VariableParamFunc(...)
local TS_XPos = 0; local TS_YPos = 0; local TS_Type = 0; local TS_TouchPos = 0; local mInst = 0;
    local inst = mc.mcGetInstance(mInst);
TS_XPos, TS_YPos, TS_Type, TS_TouchPos = select(1, ...);
    if TS_XPos == nil then TS_XPos = "nil"; end
    if TS_YPos == nil then TS_YPos = "nil"; end
    if TS_Type == nil then TS_Type = "nil"; end 
    if TS_TouchPos == nil then TS_TouchPos = "nil"; end
mc.mcCntlSetLastError(  inst,
                            " TS_XPos = " .. tostring( TS_XPos)..
                            " TS_YPos = ".. tostring(TS_YPos)..
                            " TS_Type = ".. tostring(TS_Type)..
                            " TS_TouchPos = ".. tostring(TS_TouchPos));
end

if (mc.mcInEditor() == 1) then
    VariableParamFunc();
    VariableParamFunc(2,3,4);
end

--You can pass as many params as you want, by putting in the function
--variables that get them via the order them come in at.
--this example handles three.
--Scott
Title: Re: need some lua help
Post by: dude1 on May 04, 2015, 07:42:54 AM
right this lua is a pita

what I think I need to do is use this

function GetRegister(regname)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
return mc.mcRegGetValueString(hreg)
end

and have it like this

function GetRegister(TS_XPos)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s, TS_XPos",10))

end

function GetRegister(TS_YPos)
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s, TS_Pos",10))

so on

then end with this
return mc.mcRegGetValueString(hreg)
end

it compiles and runs and debugs with no problem

then at top of m1005 put in path to it or not

but when I run m1005 it compiles but does not run as it cant find the Register`s

I have put them in as iregs and gregs that did fa I could find and read them using one of Scott examples this one Quicky Wizard and/or Macro variable value viewer function.

the thing I am getting stuck on is m1005 finding them I meant to put them in a use macro or module.

with what I have put in here how wrong am I

also Scott is there something you or ya bro have posted that is the correct way of doing just pointers please, if I can`t work this out by the end of the week I might ask for the answer

for the time being I will just use the macro b to do tool height probing it works but I would rather use the code in the manual it`s a bit safer 

and i will be doing the first cut with M4 Friday or monday just got to fix a bobo
Title: Re: need some lua help
Post by: poppabear on May 04, 2015, 07:47:49 AM
You may have already done this but,

In the Register plugin, in the "instance" tab, that is where you add your register name and the value of that register.

i.e.
you will have under the "Name" and "Initial Value" columb:
TS_XPos             2.0
TS_YPos             1.5
etc....

Then in your code that you getting those values:

function GetRegister(regname)
   local inst = mc.mcGetInstance()
   local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
   return mc.mcRegGetValueString(hreg)
end

local TS_XPosVal = tonumber(GetRegister("TS_XPos"));
local TS_YPosVal = tonumber(GetRegister("TS_YPos"));
etc.

Scott
Title: Re: need some lua help
Post by: dude1 on May 04, 2015, 08:01:04 AM
thank`s scott my brains burt for to day I will get back to it tommrow I have done half of that i have put the registers in

then i will do what you have suggested.
Title: Re: need some lua help
Post by: machiner on May 04, 2015, 10:09:34 AM
I always wondered what is the difference between global registers and instance registers, when to use one or the other, etc ;  i guess most programmers know about that , but is there a simple explanation somewhere for regular users ?
Title: Re: need some lua help
Post by: dude1 on May 06, 2015, 05:30:44 AM
this is what I have so far its still not working

local macroname = "m1005"
function GetRegister(regname)


local rc
local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
return mc.mcRegGetValueString(hreg)
end

local TS_XPosVal = tostring(GetRegister("TS_XPos"));
local YPosVal = tostring(GetRegister("TS_YPos"));
local TypeVal = tostring(GetRegister("TS_Type"));
local TouchPosVal = tostring(GetRegister("TS_TouchPos"));
local ProbeHVal = tostring(GetRegister("TS_ProbeH"));
local RetractLVal = tostring(GetRegister("TS_RetractL"));
local DefaultVal = tostring(GetRegister("TS_DefaultL"));


--regval = GetRegister("TS_Type")
--wx.wxMessageBox(regval)

if (mc.mcInEditor() == 1) then
    GetRegister ();
end

I have put them in the Register and can call them using
egval = GetRegister("TS_Type")
wx.wxMessageBox(regval)

I keep getting this
[string "C:\Mach4Hobby\Macros\m1005.mcs"]:34: attempt to call global 'GetRegister' (a nil value)
stack traceback:
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:34: in function 'm1005'
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:103: in main chunk


mcLua ERROR: Lua: Error while running chunk
Title: Re: need some lua help
Post by: poppabear on May 06, 2015, 08:45:53 AM
Try this instead:

function m1005()   
   --local macroname = "m1005"
   function GetRegister(regname)
      local rc;
      local inst = mc.mcGetInstance();
      local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname));
      return mc.mcRegGetValueString(hreg);
   end
   local TS_XPosVal = tostring(GetRegister("TS_XPos"));
   local YPosVal = tostring(GetRegister("TS_YPos"));
   local TypeVal = tostring(GetRegister("TS_Type"));
   local TouchPosVal = tostring(GetRegister("TS_TouchPos"));
   local ProbeHVal = tostring(GetRegister("TS_ProbeH"));
   local RetractLVal = tostring(GetRegister("TS_RetractL"));
   local DefaultVal = tostring(GetRegister("TS_DefaultL"));
   --regval = GetRegister("TS_Type");
   --wx.wxMessageBox(regval);
end

if (mc.mcInEditor() == 1) then
    m1005();
end
Title: Re: need some lua help
Post by: dude1 on May 06, 2015, 03:47:15 PM
I should of been more correct I get this with the m1005 tool height probing from the scripting manual

I keep getting this
[string "C:\Mach4Hobby\Macros\m1005.mcs"]:34: attempt to call global 'GetRegister' (a nil value)
stack traceback:
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:34: in function 'm1005'
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:103: in main chunk


mcLua ERROR: Lua: Error while running chunk

having the reg in reg or the macro I can call them but the tool height probing macro cant find them
Title: Re: need some lua help
Post by: dude1 on May 07, 2015, 12:17:20 AM
do i need to put in a address for this
unction m1005()   
   --local macroname = "m1005"
   function GetRegister(regname)
      local rc;
      local inst = mc.mcGetInstance();
      local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname));
      return mc.mcRegGetValueString(hreg);
   end
   local TS_XPosVal = tostring(GetRegister("TS_XPos"));
   local YPosVal = tostring(GetRegister("TS_YPos"));
   local TypeVal = tostring(GetRegister("TS_Type"));
   local TouchPosVal = tostring(GetRegister("TS_TouchPos"));
   local ProbeHVal = tostring(GetRegister("TS_ProbeH"));
   local RetractLVal = tostring(GetRegister("TS_RetractL"));
   local DefaultVal = tostring(GetRegister("TS_DefaultL"));
   --regval = GetRegister("TS_Type");
   --wx.wxMessageBox(regval);
end

if (mc.mcInEditor() == 1) then
    m1005();
end

so the tool height probing macro can find it what is m1005 from the manual
Title: Re: need some lua help
Post by: dude1 on May 07, 2015, 03:54:38 AM
I have played some more I add this into the Auto Tool Setting Macro from the manual

function GetRegister(regname)
      local rc;
      local inst = mc.mcGetInstance();
      local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname));
      return mc.mcRegGetValueString(hreg);
   end

now the Get touch off parameters work but when it gets to the Check Probe part it fails it comes up with this
[string "C:\Mach4Hobby\Macros\m1005.mcs"]:48: wxLua: Expected a 'number' for parameter 2, but got a 'nil'.
Function called: 'mcSignalGetHandle(number, nil)'
01. mcSignalGetHandle(number, number, lightuserdata)
stack traceback:
   [C]: in function 'mcSignalGetHandle'
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:48: in function 'm1005'
   [string "C:\Mach4Hobby\Macros\m1005.mcs"]:109: in main chunk

I am running it in sim I have defind a probe input
Title: Re: need some lua help
Post by: poppabear on May 07, 2015, 07:45:49 AM
Function called: 'mcSignalGetHandle(number, nil)'--is your error
the "nil" is the parameter 2, you need to have something like:   mc.ISIG_INPUT1 in there.

Scott
Title: Re: need some lua help
Post by: dude1 on May 07, 2015, 03:27:25 PM
I think they did this with bits missing so people will learn a thing or two more than just copy and past code to its correct spot it`s not fun but I know how it was for you and ya bro to do now.

thanks for the help scott
Title: Re: need some lua help
Post by: dude1 on May 08, 2015, 03:10:56 AM
thank you very much Scott I I think i got it working your hints are very good now to the next one