Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: CRS on January 18, 2017, 03:51:02 AM

Title: Lua success with external buttons to Mach4
Post by: CRS on January 18, 2017, 03:51:02 AM
Hi guys, I posted a little while back looking for some enlightenment with creating Lua scripts to get all my front panel buttons, MPGs and potentiometers talking to Mach4.

I am very happy to say that I have a large percentage of those buttons working with help of other members of this forum, articles and uploaded scripts.

Matevž from PoLabs did a great tutorial on how to connect up an exterior potentiometer to the Feed Rate Override as well as the Lua script that accompanies it. I copied that for my FRO as well as the Spindle Speed Override and both work very well.

As a quick reference the original PoLabs file from Matevž is in the attached pdf.

For anyone who has an interest here is the link to the PoLabs tutorial.
http://blog.poscope.com/mach4-tutorial-fro-using-analog-input/

Just one thing I can’t get worked out. The below line sets the FRO percentage.

local percent = analog/1*250 --Calculate percentage from 0% to 250%

My challenge is that the Spindle OVR goes from 50% up to 150%.  I have tried many embellishments, but can’t get it happening.  The spindle override must be expressed as a decimal percentage, so the max number is 1.5 (150%) and the minimum is 0.5 (50%).

Does anyone know how to express a percentage range in that line?

Thank to anyone who can enlighten.

Craig Smith
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on January 18, 2017, 03:59:06 AM
Whoops last line of the pdf needed correction

Craig.
Title: Re: Lua success with external buttons to Mach4
Post by: joeaverage on January 21, 2017, 05:42:40 PM
Hi Craig,
I see DTG has already replied with a solution that uses a lookup table.

I think this might also work: the PoKeys yields an 12 bit number and ranges from 0 to 4096.

ratio=0.5+(pokeyvalue/4096)
percent=ratio*100

Craig.
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on January 21, 2017, 06:50:50 PM
Hi Craig, thanks for getting back to me.

Yes DTG sent through a great response including file and video.  He is quite amazing.  I have spent the last two days trying to apply that, with no success.  I think I must be doing something wrong.  I was about to sit and write a response to DTG when I noticed your post.

He said there are only two variables for me to change/add.  The first is the name of my controller which goes in the table.  I still haven’t figured that out.  Does he mean the PoKeys, the ESS or the screen set.

I noticed that on his video, the name of the Screen Set is “machduino” which matched what he had in that line of code in the table, however there are capitols in that line of code. “MachDuino”.  I have tried all three options with no success.  My screen set is wx4

The other thing is the name of the register, which goes into those lines inserted into the PLC script.  I assume that’s the name of the input when you look in the Register Diagnostics in Mach, which in my case is “Analog pin 42”.

No errors show up, but the override slider in Mach is stuck hard to the bottom.  I thought maybe the table is somehow incorrect for the PoKeys although DTG said it was the resolution of the pot.

You mention 0 to 4096, DTG’s table goes from 0 to 1023, could that have anything to do with it, or are they totally separate things?  Remember this is not my strong point.  Not sure I actually have one to be honest.

Now to your suggestion. And by the way thank you Craig.  I have no idea where that should go.  Screen Load Script, PLC or in a separate file.  Remember Lua for me is all new and may as well be Chinese.

Sorry to ask for more guidance Craig, if you can point in the right direction, that would be great.

How’s the weather in NZ.  We actually have a great day.  Strange for Melbourne.
Craig.
Title: Re: Lua success with external buttons to Mach4
Post by: joeaverage on January 21, 2017, 10:28:46 PM
Hi Craig,
believe it or not we've had a genuine NE gale and about 3in of rain over the last 24 hours, not complaining about the rain!

Quote
“MachDuino”.  I have tried all three options with no success.  My screen set is wx4

The name is the name of the screenset you are using. wx4 is a default screenset that ships with Mach4. It is recommended that
you copy it and rename it to something individual say 'Craigwx4Set' or similar. Any changes to the set say buttons, macros and
so on will be saved to that set. If you update Mach4 with a later build wx4 will be overwritten and any changes you made to it
will be lost whereas your individually named screenset will be retained. The underlying code that selects and loads the screen is LUA
and LUA is case sensitive, your screenset is case matched but otherwise follows the rules for LUA variables, ie doesn't start with a
number or a LUA keyword.

Code: [Select]
You mention 0 to 4096, DTG’s table goes from 0 to 1023, could that have anything to do with it,
This depends on the PoKeys board. If memory serves it uses a 12 bit ADC, ie 0 to 4096. If it uses a 10 bit ADC, like DTGs Arduino,
the range would be 0 to 1024.

Code: [Select]
The other thing is the name of the register, which goes into those lines inserted into the PLC script
Most of the buttons and switches you have used on your control board have two distinct states. Mach4 runs the signal script each and
every time a signal changes state. You don't have to poll them every once and a while. An analogue signal is converted to a digital number
and communicated via Modbus by TCP/IP and the Modbus function runs every 25ms or so. The Modbus plugin creates and defines a register
in the Regfile and the register is updated with the digital representation of your analogue value everytime the Modbus function runs ie every 25ms.
You can read the register any time you like but I imagine you wish to see it live so you would put the code necessary to read and display it in the
PLC script as it runs every 40ms or so.

Craig
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on January 21, 2017, 11:25:05 PM
I once sailed a regatta in Wellington, now there is some serious wind.  Forecast: 50 to 55 knots from the north turning to 55 to 60 knots from the south in the afternoon.  And it was raining.  That was fun!

Okay, I get it about copying screen set.   I have read that before, thanks for the reminder.

Sorry Craig I am still missing something. Just nice and slow.  In this line of code in DTG’s SpindleOVR.lua file:

local hreg = mc.mcRegGetHandle(inst, string.format("MachDuino/%s", regname))

I didn’t get what you said.  Is “MachDuino” what he is calling his controller?  If so does that mean I should be putting Craigwx4 (or whatever) as mine?   And what about the “/%s” is that a part of it?  Or it just must be there after the controller name..?

If not, what is my controller?  My PoKeys57E?  Which is called PoKeys_25172.

Yes I know, I’m a bit thick.  I am still stuck back on the first online Lua tutorial about how to print “Hello World”.  I breath epoxy resin and solvents all day long, so I can blame that.

Thank you for this Craig.
Craig #2
Title: Re: Lua success with external buttons to Mach4
Post by: joeaverage on January 22, 2017, 12:08:07 AM
Hi Craig,
look in the API.chm ('Help Docs' button File Ops tab with M4 enabled), I have it almost permanently on the taskbar so I can refer to it;

LUA Syntax:
hReg, rc = mc.mcRegGetHandle(
      number mInst,
      string path)
So the correct way to get the register handle is:

inst=mc.mcGetInst()
hreg=mc.mcRegGetHandle(inst, "<your register, whatever you called it>")


Its got nothing to do with the screenset. Go to Diagnostics/Regfile and expand the different entries until you find where the PoKeys plugin
or maybe the Modbus plugin has put the register. For instance I have a macro that uses an instance register called ALcode. Its handle:

hreg=mc.mcRgGetHandle(inst,"iRegs0/ALcode")

ie the path is iRegs0/ALcode.

If you cant find the register then you haven't created it yet. Solve that first.

Craig
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on January 22, 2017, 04:40:10 PM
Hi Craig

Thanks for taking the time to think about this.  Looking at the Register Diagnostics, my controller is "PoKeys_25172" and the Register is "Analog input 42".

So the line should read.
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_25172", "Analog input 42"))

I heard back from DTG and he thinks the issue lies in the table. He read that the Pokeys analog is a value between 0v and 3.3v, which I also read.  So he will redo the table to reflect that and see how that goes.  He also showed me how the above line of code should look.

You mentioned 0 to 4096.  I wonder which one will do the job the 0-3.3V or 0-4096?  I started to try and make a table from 0 to 4096 in Excel, but couldn’t get the numbers to propagate with brackets around them.  Must be a way.   I’ll let you know when Daz posts the table for the 3.3V.

Craig.


Title: Re: Lua success with external buttons to Mach4
Post by: joeaverage on January 22, 2017, 05:24:18 PM
Hi Craig,
0-3.3V is the analogue input voltage range of pin 42. It will be converted to a 12 bit number in the range 0-4096.
You can convert that back into a number representing a voltage and read a table indexed by that number if you wish,
but why?

Just read the 12 bit number. Your OV ratio is simple:

ratio=0.5+(value/4096) where value is the contents of the register ie 0-4096.

Craig
Title: Re: Lua success with external buttons to Mach4
Post by: DazTheGas on January 22, 2017, 06:04:01 PM
Not as straight forward, now if they used the resolution of 4096 that would be too simple!! but instead here`s an extract from the manual.

Quote
you should see the value change from 0 to 1 as you turn the potentiometer. Value of 0 equals to a voltage of 0 V, while the value of 1 equals to voltage of +3.3 V on the analog input pin


DazTheGas
Title: Re: Lua success with external buttons to Mach4
Post by: joeaverage on January 22, 2017, 06:22:23 PM
Hi,
Ah!, that makes sense, haven't read that bit. Still it makes the calculation even easier:

ratio=0.5+value

ie with knob full CCW, voltage =0 therefore value=0 therefore ratio=0.5
    with knob full CW, voltage =3.3 therefore value=1 therefore ratio=1.5

and isn't that what we're trying to achieve?

Craig
Title: Re: Lua success with external buttons to Mach4
Post by: thosj on March 23, 2017, 06:59:40 PM
Did you guys ever figure this out some way other than the table way?

I have a PoKeys 57E. This is a lua file being run from the PLC script.

here is my code, and my analog pot works but goes DOWN to 0% and UP to 150%. Usable, but not perfect.
That 40*60 was derived by guess and by gosh!! Not the scientific method.


Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set SRO value
function SetSRO(analog)
    local percent = analog/40*60 --calculate percentage
    local inst = mc.mcGetInstance()
    mc.mcSpindleSetOverride(inst, percent)
end

--Main body -
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "44" --Analog input pin number on PoKeys

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetSRO(analogVal) -- Set SRO value in %

It seems the line that says:

local percent = analog/40*60 --calculate percentage

Should say something like:

local percent = analog/((1*100) +.5) --calculate percentage

Do the 0 to 100, add .5, inferred to in the post above, but it doesn't work. I've tried different ways of doing the math, but apparently I can't do that or there's another convention I haven't discovered.
I've tried:

local percent = analog/1*100+.5
local percent = .5 + analog/1*100
local percent = (.5) + (analog/1*100)

I probably have to do the math outside that line, but I tried a couple ways to do that and can't figure it out!!

I'm sure I'm missing some basic programming convention here, but.....I can't get it.

Any ideas?

Tom
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on March 23, 2017, 09:58:26 PM
Hi Tom, I had to take a short break my mill conversion, you know how domestic stuff happens. So all I learned about Lua from November through January has slipped away and will take a while to get it back.

That doesn’t matter though because regarding the matter of the analog pot, I received an email from Matevž Bošnak from PoLabs and he explained how to do it.  This is the email.
Hi, Craig

Although the analog inputs are 12-bit, the analog input registers contain a value between 0 and 1.
To get the 50-150% range, simply use
local percent = 50 + 100 * analog
And guess what, it works.  I hope this is what you wanted.  I did actually post all this in another thread somewhere.  Not to worry though.

With the analog input into the Pokeys 57E, I find the reaction time lags a bit, which gets better if you slide the Filter Value for that input towards fast, but then the slider in Mach seams to jump around a lot.  So I set the filer value slower and the dithering became less, but the reaction time suffered.  It is quite usable though.

Let me know if this makes sense Tom

Regards,

Craig.
Title: Re: Lua success with external buttons to Mach4
Post by: thosj on March 24, 2017, 06:18:02 AM
Hey, thanks Craig.

I'll give that a go later today, I'm sure it'll work.

I guess it's simply the old My Dear Aunt Sally, multiply, divide, add, subtract!!! I couldn't logic it out even knowing that, it seems  >:(

I'll have to look into the filter value on the input. Where is that, in the PoKeys plugin settings or in Mach settings? I'll figure it out, I'm not at the machine just now. My spindle DOES seem to glitch a little, by the sound, with the pot working, but let me get the pot to 50% to 150% and see what I think. I actually disabled it figuring I'd just use the screen slider, once I heard the spindle glitch, or dither, or whatever it is/was. I'll play with that a bit.

Thanks for the help!

Tom
Title: Re: Lua success with external buttons to Mach4
Post by: Stuart on March 24, 2017, 07:04:22 AM
Tom

I use BMDAS.  for the same operations  ;D

Brackets  Multiplication act you get the rest

good thread following with interest
Title: Re: Lua success with external buttons to Mach4
Post by: CRS on March 24, 2017, 07:24:22 AM
Hi Tom, the filtering is in the Pokeys configuration software.  See page 41 of the attached manual.

Have to go.

Craig.
Title: Re: Lua success with external buttons to Mach4
Post by: thosj on March 24, 2017, 08:55:36 AM
Thanks, Craig, I'll give that a go later today.

Stuart, I know about the Brackets part of the formula (!), not in My Dear Aunt Sally, but assumed (not THAT'LL get me!! Makes as ass of..........) That's how I got distracted trying to dope out the formula required to get 50% to 150%. Tried brackets and if brackets in LUA are normal parens, I couldn't seem to get it. Perhaps brackets of any type don't work like that here.....or brackets want to be square brackets or curly brackets? I guess not being a real programmer gets me stuck pretty easily. One thing I know, mental block or whatever, I could read thru VB script in Mach3 and dope it out, but with LUA, I'm having a harder time doing that, so my learning has been kind of halting!!! It's either mental block or old age, I don't remember which  >:D
Title: Re: Lua success with external buttons to Mach4
Post by: Stuart on March 24, 2017, 09:16:06 AM
You will sort it Tom

I was brought up on C++  but Lua has me scraping the noggin ( well it is 70 years old ) then on to a sub set of C++ used in BAS ( Building Automated Systems ) for computer centre HVAC

it not the Lua that has me troubled, its were the code goes and the interaction with the screen load and the PLC

See you around here and warp9

 
Title: Re: Lua success with external buttons to Mach4
Post by: thosj on March 24, 2017, 04:25:39 PM
Well, Craig, I can't get Matevž code to work. I get nothing from the pot using this:

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set SRO value
function SetSRO(analog)
    local percent = 50+100*analog --calculate percentage
    --local percent = analog/40*60 --calculate percentage
    local inst = mc.mcGetInstance()
    mc.mcSpindleSetOverride(inst, percent)
end

--Main body -
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "44" --Analog input pin number on PoKeys

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetSRO(analogVal) -- Set SRO value in %

If I uncomment my old line and comment the 50+100*analog line, the pot works. With the 50+100*analog line it doesn't work AT ALL, no movement although the pot is working in Regfile.

So what's wrong with that line? I've tried all manner of spaces, nothing. Mathematically, that formula works, analog * 100 plus 50.

If you're using this, can you post up your actual lua file code so I can just try that to try and learn what's wrong with my code??

Tom
Title: Re: Lua success with external buttons to Mach4
Post by: Wallerawang on March 24, 2017, 08:47:34 PM
Hi Tom
A short shaky video of how to achieve what you want. Just push pause on the code screen to read the code.
Steve

https://youtu.be/A_dMgzaLku0
Title: Re: Lua success with external buttons to Mach4
Post by: thosj on March 25, 2017, 11:43:25 AM
Got it, sheesh, finally!  ;D

Thanks to Wallerwang (?), and I don't know why Craig's code didn't work, it worked mathematically!!

Here's the code that actually works for me, for the cutters/pasters among us! You'd only need to change the PoKeys Device and PoKeys Analog Input to yours.

This is my Speed Override, 50% to 150% to match Mach slider

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set SRO value
function SetSRO(analog)
    local percent = analog/1*1.0+0.5 --calculate percentage
    local inst = mc.mcGetInstance()
    mc.mcSpindleSetOverride(inst, percent)
end

--Main body -
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "44" --Analog input pin number on PoKeys

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetSRO(analogVal) -- Set SRO value in %

This is my Feed Override, 0% to 250% to also match the Mach slider.

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set FRO value
function SetFRO(analog)
    local percent = analog/1*250 --calculate percentage from 0% to 250%
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

--Main
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "45" --Analog input pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in %

Both of these are LUA files, stored, in my case, in the Mach4 Industrial directory and run with code in the PLC script, which runs the files every 50ms or whatever that interval is.

Here's the PLC script lines, way near the end.

Code: [Select]
-------------------------------------------------
--My Stuff!!
-------------------------------------------------
--Runs the PoKeys FRO and SRO lua files as shown!

dofile("C:\\Mach4Industrial\\PoKeys_analog_FRO.lua")
dofile("C:\\Mach4Industrial\\PoKeys_analog_SRO.lua")

--------------------------------------------------

--This is the last thing we do.  So keep it at the end of the script!
machStateOld = machState;
machWasEnabled = machEnabled;

Now, being old fashioned, I'd like the Rapid Override, 0% to 100%, to follow the Feed Override which is 0% to 250% with the one PoKeys Analog Input pot. Wonder if I can have two LUA files running, reading the same PoKeys Analog Input and moving two sliders? Or might it be better to have one file reading the input and updating two things? I had Mach3 doing this but don't remember how. Must have written Brains, eh?

It's always somethin', ain't it?  :o

Tom