Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: thespindoctor on February 13, 2018, 11:32:48 AM

Title: Lua programming of voltage meter
Post by: thespindoctor 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
Title: Re: Lua programming of voltage meter
Post by: DazTheGas 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
Title: Re: Lua programming of voltage meter
Post by: thespindoctor 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.
Title: Re: Lua programming of voltage meter
Post by: thespindoctor 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
Title: Re: Lua programming of voltage meter
Post by: joeaverage 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
Title: Re: Lua programming of voltage meter
Post by: joeaverage 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
Title: Re: Lua programming of voltage meter
Post by: joeaverage 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
Title: Re: Lua programming of voltage meter
Post by: thespindoctor 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.
Title: Re: Lua programming of voltage meter
Post by: joeaverage 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
Title: Re: Lua programming of voltage meter
Post by: thespindoctor 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 %




Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 17, 2018, 12:24:54 PM
Hi,
that code looks to have the 57E device read an analogue voltage and convert it to digital. Then Mach takes that digital value and converts it into
a PWM, ie an analogue voltage and output it. Why?

You want an onscreen voltmeter do you not?

Craig
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 17, 2018, 12:58:51 PM
Craig, yes I do want an onscreen voltmeter.  I realize now that that code is making a PWM but could I just change the code but using the reading and writing to the register to make it work? 

I do see the voltages in the register diagnostics so I know it is set up correctly.  I just have to get the LUA code correct and into a meter.
Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 17, 2018, 02:25:18 PM
Hi,
if you have a register with the numeric representation of the input voltage that's all you need.

The problem you have is that the on-screen voltmeter has a preprogrammed range of data sources and your register is not one of them. In fact
no registers are in the list.

What I was proposing was that you cheat. Use the register value that you want to display to populate one of the variables that IS on the list.
May I suggest 'C SoftLimit+'. You could repeatedlty write a new value to the C axis positive softlimit and the display that softlimit as is provided
in the Code list for the onscreen voltmeter.

A sketch, this is in the PLC script:
1) Get the register value
2) Set the C softlimit value  with mc.mcAxisSetSoftlimitMax()=register value

I don't know that it would work but its the only way I can think of that you can make the onscreen voltmeter display a register value.

Craig
Title: Re: Lua programming of voltage meter
Post by: DazTheGas on February 17, 2018, 03:11:24 PM
The Meters can be set using the "scr" class functions,

Lets assume you have you register sorted and have assigned a it to a local value as it shows in the pokeys manual from page 31 onwards.

All you need to do now is send that value to the meter, if say for instance your meter is called amtr and you want to set it to 75 then use scr.SetProperty("amtr","Value","75")

DazTheGas
Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 17, 2018, 03:20:43 PM
Hi Daz,
kool that makes it easy. Just put the 'scr' assignment function in the PLC script.

Craig
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 17, 2018, 04:08:51 PM
Thanks guys I will see if I can make it work!
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 18, 2018, 09:29:52 AM
Got 4 meters working great.  Key was that the CNC4PC configuration setup for the Pokeys57E already has registers going for the 4 analog inputs.
I realized that the pin setups were correct once I could see the voltages active in the reg diagnostics for the Pokeys device in Mach4.
Next, with help of the LUA Scripting Guide showing how to read register values, I read each value using the following function placed in the Screen load script.
The labels used to access the registry were listed in the registry diagnostics "CNC4PCMotionDevice" and the name of each input "Analog input 41" was there too.

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

Then in the PLC script I put a pair of lines for each of the 3 meters changing the values 41,42,43,44 or the Pokeys pins for each analog input.  Each meter was setup in the screen with labels Meter1, Meter2, Meter3 etc.

    regval=ReadAnalog("Analog input 41")
    scr.SetProperty("Meter1","Value",regval)

The PLC is now updating the meters realtime but I put it all in a do loop if (machEnabled == 1) then update the meters so it only updates when Mach is enabled.

Happy now!
Title: Re: Lua programming of voltage meter
Post by: DazTheGas on February 18, 2018, 12:25:20 PM
Loops in the PLC are bad, try a simpler way.

if machEnabled == 1 then
-- update meters
end

DazTheGas
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 18, 2018, 01:06:00 PM
Daz

Thanks, that is what I did.

However I added a loop to sum the values every 20 sweeps then updated a second guage with average per second.

Is there an easier way to average?
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 18, 2018, 01:08:06 PM
Actually no loops just if then else
Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 19, 2018, 06:32:14 AM
Hi,
could you not store, in a table, the last twenty results and average those, I think the average function can be built into the table.
Then on each run thru the PLC one new data point is added to the table and the oldest, the 21st, gets dropped. Running average?

Craig
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 06:46:45 AM
Craig,

I set up an if statement with a new counter called sweep in the PLC script so that
each time it sweeps, it reads the analog input then if sweep is <=20 it adds that value to a new variable.
Then when sweeps is over 20, it averaged that variable zeros it then set thes sweep counter back to 0.

Seems to work great with the value set to another gauge.
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 06:48:10 AM
Having trouble with getting an LED to light up with these analog inputs.
Is there a trick here?
Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 19, 2018, 07:07:45 AM
Hi,
what LED? LEDs are on or off, an analogue value is in this case a 12 bit numerical value, how is an LED supposed to reflect that?

Craig
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 07:14:11 AM
Ok, that makes sense.  I will try a work around to turn on the led if the value of the input is over 0.

Thanks!
Title: Re: Lua programming of voltage meter
Post by: joeaverage on February 19, 2018, 07:19:00 AM
Hi,
you could turn the LED on if greater than 50% and off if less than 50%

if (value>=2048) then
     LED=on
else
     LED=off
end

Craig
Title: Re: Lua programming of voltage meter
Post by: DazTheGas on February 19, 2018, 08:45:29 AM
Think that should be if value ~= 0 then turn on LED

DazTheGas

Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 09:42:58 AM
No matter what I do I can’t get an Led to turn on.  I am trying to set property value to “On” even with a separate button. I believe I saw a past discussion in the past about Leds being difficult.

I am happy with my new command of meters and guages though!  Thanks for the help!
Title: Re: Lua programming of voltage meter
Post by: Chaoticone on February 19, 2018, 10:20:08 AM
No problem toggling a LED here.

You want to set it to 1 or 0

Code: [Select]
local TestState = scr.GetProperty("ledTest1", "Value")

if TestState == "1" then
scr.SetProperty("ledTest1", "Value", "0")
else
scr.SetProperty("ledTest1", "Value", "1")
end
Title: Re: Lua programming of voltage meter
Post by: DazTheGas on February 19, 2018, 10:23:44 AM
Try a different way,

1.  Add LED to screen.
2. Assign it to an output thats not being used IE Output 63
3. Turn On and Off using mc.mcSignalSetState (mc.mcSignalGetHandle ( inst ,mc.OSIG_OUTPUT63  ), 1) = ON and mc.mcSignalSetState (mc.mcSignalGetHandle ( inst,mc.OSIG_OUTPUT63  ), 0) = OFF

I have many LED`s setup in this way.

DazTheGas
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 10:58:19 AM
So nice to solve this, thanks!
Title: Re: Lua programming of voltage meter
Post by: Chaoticone on February 19, 2018, 11:06:45 AM
 :)
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 05:13:56 PM
Now it is working great with the mc.SignalSetState method per Daz assigning an unused input to the LED such as input63 as he suggested.
For those trying trying to use this, note the change to ISIG_INPUT from OSIG_OUTPUT ( 1=on 0=off)  This code is in my PLC script.
As Daz suggested, I tried but do not have a ~ sign on my keyboard so stuck with >=.1 instead of ~= 0.  It works better because there is noise below .1v

  if tonumber(regval) >.1 then
        mc.mcSignalSetState (mc.mcSignalGetHandle ( inst ,mc.ISIG_INPUT63  ), 1)
    else
        mc.mcSignalSetState (mc.mcSignalGetHandle ( inst,mc.ISIG_INPUT63  ), 0)
    end
    --

I could not get reliable function with the scr.SetProperty method.  Sometimes it would work and sometimes it would switch to off and stay off. 
Title: Re: Lua programming of voltage meter
Post by: TOTALLYRC on February 19, 2018, 05:37:43 PM
As Daz suggested, I tried but do not have a ~ sign on my keyboard so stuck with >=.1 instead of ~= 0.  It works better because there is noise below .1v

If youneed a ~ try the holding the  alt key and then type in 126. Should give you a Tilde(~) once you let go.


Mike
Title: Re: Lua programming of voltage meter
Post by: thespindoctor on February 19, 2018, 05:40:56 PM
Thanks Mike but it works! ~~~ but I had to use the num pad.  It does not work using the number keys across the top of the keyboard though.
Title: Re: Lua programming of voltage meter
Post by: TOTALLYRC on February 19, 2018, 08:41:43 PM
Glad it works. IIRC there is a number for every symbol that you would every need.
I always use the number pad.I just tried it the other way and it doesn't work here either.

Mike