Hello Guest it is July 12, 2020, 03:41:58 PM

Author Topic: Lua programming of voltage meter  (Read 2729 times)

0 Members and 1 Guest are viewing this topic.

Lua programming of voltage meter
« on: February 13, 2018, 11:32:48 AM »
Is it possible to use LUA to program a graphic voltmeter in Mach 4?  I am trying to visualize voltage output from 4 piezoelectric weight sensors, using Mach 4 as a data acquisition system.
I have a Pokeys57U and a breakout board so I know the 4 analog inputs are there but I am not sure how to make the software side work. Any suggestions would be appreciated.

Thanks

Offline DazTheGas

*
  •  752 752
  • DazTheGas
    • View Profile
Re: Lua programming of voltage meter
« Reply #1 on: February 13, 2018, 11:41:20 AM »
You could use the meter and set its value from a register that is updated by the pokeys analog signal.

DazTheGas
Theres always time for a choccy biccy
Re: Lua programming of voltage meter
« Reply #2 on: February 13, 2018, 12:17:24 PM »
Woo Hoo, thanks Daz!

I will see if I can figure out how to get a meter set up.

Thanks again.
Re: Lua programming of voltage meter
« Reply #3 on: February 16, 2018, 05:57:31 PM »
So I now have my Pokeys57E set up with analog voltage input on pin 44.   I think the configuration is correct for Mach4 Input3.

Now I have a gauge set up with screen editor.  What do I do to link the gauge to the analog input?  Not sure how to use the register.  Is there an example to look at in the manuals?

Thanks
Re: Lua programming of voltage meter
« Reply #4 on: February 16, 2018, 06:23:35 PM »
Hi,
the 57E will need to communicate a digital word to your PC over its Ethernet connection. You will have to read the manual as to how to cause the 57E to that
and to where it would store its value.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua programming of voltage meter
« Reply #5 on: February 17, 2018, 01:05:49 AM »
Hi,
page 31 and 32 of the PoKeys Mach4PluginManual show you how to get Machs register updated by the 57E and introduces the Register Diagnostics to check.

Once you've got that bit sorted you may need a PLC script to update your voltmeter.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua programming of voltage meter
« Reply #6 on: February 17, 2018, 06:58:39 AM »
Hi,

Quote
I think the configuration is correct for Mach4 Input3

No, I don't think so, the 57E produces a 12 or maybe 14 bit digital word to represent the numerical value of the voltage on its input pin 44. How would the 57E
possibly pass a multibit value via a single Mach input?
The 57E maintains quite a few registers and the digital value is placed in one of those registers. My reading of the manual suggests that the 57E plugin creates and
updates those registers. You can use Diagnostic/Regfile to expand the 57E registers and view them.

Quote
Now I have a gauge set up with screen editor.  What do I do to link the gauge to the analog input?  Not sure how to use the register.  Is there an example to look at in the manuals?

This is not going to be as straight forward as you think. The recent build of Mach has a voltmeter icon built in. What you may not have noticed is an entry 'Code' in the
properties list. If you click on it and view the dropdown list you have ALL the intended possible sources for the voltmeter to display, there appears to be well over a hundred
entries but none of them are a simple register value but rather a DRO or position variable or similar.

There are two possibilities:
1) Take the register value and use it to populate say an out-of-band axis DRO which is on the 'Code' list and use that to control the provided voltmeter icon.
2) Craft your own voltmeter, I note that wxLua has a 'PercentComplete' graphic which could be used.

Its not clear to me that either option is best, might be a case of trying both to see which one works best. Probably a useful excerise in Lua coding.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua programming of voltage meter
« Reply #7 on: February 17, 2018, 10:01:11 AM »
Thanks Craig for working on this for me. I am struggling but the Pokeys 57E has 4 analog inputs.
It should be easy...  I am not sure my config is correct yet to do the programming since inputs do not show up in the Code box as sn option.
Re: Lua programming of voltage meter
« Reply #8 on: February 17, 2018, 12:04:41 PM »
Hi,
if I understand correctly they wont.

Have you had a look in the Diagnostics/Regfile to see if the registers are there?

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua programming of voltage meter
« Reply #9 on: February 17, 2018, 12:08:43 PM »
ok, I will assume that they will not show up in the CODE properties list and will proceed with programming a function per an example that I found that I am r- posting here.  I have to edit it for my purposes but maybe it will help someone else too.  Thanks

--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 Write value from analog register
function WriteRegister(device, outPin, newValue)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/PWM %s duty", device, outPin))
    return mc.mcRegSetValueString(hreg, newValue)
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 = "PoKeys_30290" --Change this to the name of your PoKeys device
local analogPin = "41" --Analog input pin number
local outPin = "18" --Analog output pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
outVal = analogVal
WriteRegister(device, outPin, outVal)  -- Set Output Value
SetFRO(analogVal) -- Set FRO value in %