Hello Guest it is May 09, 2021, 10:38:07 PM

### Author Topic: Lua success with external buttons to Mach4  (Read 5583 times)

0 Members and 1 Guest are viewing this topic.

#### joeaverage

• 6,403
##### Re: Lua success with external buttons to Mach4
« Reply #10 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
My wife left with my best friend...
and I miss him!

#### thosj

• 432
##### Re: Lua success with external buttons to Mach4
« Reply #11 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 registerfunction 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 valuefunction 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 devicelocal analogPin = "44" --Analog input pin number on PoKeysanalogVal = ReadRegister(device, analogPin) --Save analog register value in variableSetSRO(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

#### CRS

• 61
##### Re: Lua success with external buttons to Mach4
« Reply #12 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.

#### thosj

• 432
##### Re: Lua success with external buttons to Mach4
« Reply #13 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

#### Stuart

• 264
##### Re: Lua success with external buttons to Mach4
« Reply #14 on: March 24, 2017, 07:04:22 AM »
Tom

I use BMDAS.  for the same operations

Brackets  Multiplication act you get the rest

good thread following with interest

#### CRS

• 61
##### Re: Lua success with external buttons to Mach4
« Reply #15 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.

#### thosj

• 432
##### Re: Lua success with external buttons to Mach4
« Reply #16 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

#### Stuart

• 264
##### Re: Lua success with external buttons to Mach4
« Reply #17 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

#### thosj

• 432
##### Re: Lua success with external buttons to Mach4
« Reply #18 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 registerfunction 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 valuefunction 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 devicelocal analogPin = "44" --Analog input pin number on PoKeysanalogVal = ReadRegister(device, analogPin) --Save analog register value in variableSetSRO(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

#### Wallerawang

• 105
##### Re: Lua success with external buttons to Mach4
« Reply #19 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