Machsupport Forum

Mach Discussion => Mach4 General Discussion => Mach4 Plugins => Topic started by: Skateboss on October 03, 2016, 04:46:22 PM

Title: Pokeys Mach4 plugin LUA reference for LCD
Post by: Skateboss on October 03, 2016, 04:46:22 PM
Hi,

Is there a reference manual available anywhere for the Mach4 version of the pokeys plugin that will give me an idea of how to communicate with the LCD and also set the PWM duty cycle etc for the contrast and backlight pins via LUA code?

Any info is much appreciated as I have been searching for days and cant seem to find any way of doing this.

Thanks in advance,

Chris.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on November 03, 2016, 01:57:39 PM
Chris,

I have some of the same questions.  He is what I have discovered after many hours of playing.

If you look in MACH4 and go to the Diagnostics tab at the top then go down to Regfile - Newfangled Solutions, you can click there and a new window will open.  It shows all the registers available and if you have installed the Pokeys plugin you will find a + next to something like PoKeys_45617.  Now press the + on this group of registers and you will find all the registers accessible in the Pokeys device.  Now slide down near the bottom of the long list and you will see four entries.

LCD Line1:
LCD Line2:
LCD Line3:
LCD Line4:

Double click on any LCD line and you can actually enter any value and it will immediately show up on the LCD screen.  I have tested and verified this part works perfect.

Now I need to figure out how to take something like say the MACH4 DRO and write that to these registers.  This is something I am trying to figure out for a POKEYS pendant.  Hopefully this gets you pointed in the right direction.

Russ

Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 04, 2016, 01:07:51 PM
Chris,
You can adjust the PWM duty cycle inside the plugin inside of Mach4 for the Pokey.  That does adjust the LCD contract on my pendant.  Right now I am working on a way to copy the DROs inside of MACH4 to the pokeys plugin register that holds the LCD display.  I have not been able to get it to work yet.  Hoping someone know exactly how to access those registers via LUA.  All the stuff I have tried does not work in LUA for the plugin registers.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 09:25:08 AM
Look in the API help file at Mach registers.

I think you will use mc.mcRegSetValueString(number hReg, string value)
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 10:56:31 AM
Stakeboss,

Here is now you can get Mach4 to update an LCD screen attached to the Pokeys unit.

Go into the Screen Editor
click on the X DRO on the screen, then look at the left you will see droCurrentX
Now look at the window below this window and click on the second little box which is the events tab.
Now click on the first line below that called "On Update Script", now click on the box with the three dots to the right, that will open a blank Lua Page.

Now you need to copy the LUA code below and paste it on that blank LUA page.  Please update the serial number of your Pokeys device, and then save and exit the screen editor.
Now exit Mach4 and then start it up again.  Now the LCD will update each time you have the DROs update in Mach4.

DazTheGas helped me get this working, now the next part is to get a formatted output, because this will display the DRO on the screen for example 23.1234, but on the LCD it will be 23.123456.

I am working on how to do a formatted output that knows the screen is 20 digits long and I need it to follow the standard DRO format which is %4.4f

As soon as I get that part working I will post it as well.  This will at least get your LCD working.  Have Fun

Russ


local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX","Value")
local Ycoords = scr.GetProperty("droCurrentY","Value")
local Zcoords = scr.GetProperty("droCurrentZ","Value")
local Acoords = scr.GetProperty("droCurrentA","Value")
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
mc.mcRegSetValueString(hreg, tostring(Xcoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 2"))
mc.mcRegSetValueString(hreg, tostring(Ycoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 3"))
mc.mcRegSetValueString(hreg, tostring(Zcoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 4"))
mc.mcRegSetValueString(hreg, tostring(Acoords))

Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 11:08:33 AM
This might work.........

replace
mc.mcRegSetValueString(hreg, tostring(Xcoords))

with
mc.mcRegSetValueString(hreg, string.format("%4.4f", Xcoords)
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 11:46:25 AM
Chaoticone,

Nope, what that does is print out %4.4f on the LCD screen.

I think the issue is the values are being retrieved and they are floating point.  Then you send it to the display as a string so it does not know how to handle the %f.4f.  I even removed the quotes and that gives me an LUA error.

There must be some other trick, I will keep looking at the manual.  Must have missed something somewhere.

Russ

Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 11:53:26 AM
Might try this.....

local Xcoords = scr.GetProperty("droCurrentX","Value")
Xcoords = string.format("%4.4f", Xcoords)

If that works, do the same for Y and Z.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 04:03:36 PM
Chaoticone,

That is exactly what I was going to attempt.  The following code kind of works.  Let me explain.
When you start up Mach4 many times it does not write to the LCD display, not exactly sure why.
If you load the roadrunner TAP file and enable the machine and start running the LCD display suddenly starts following the On Screen DROs. 
These are displayed correctly for example screen shows 23.4567, now the LCD shows 23.4567, so that part is great.  Still need to figure out how to clear the display prior to these writes as it only updates the 4%.4f digits.

The next issue is also strange.  If I stop the Gcode, then press zero X that updates on the LCD display just fine, but if I try that to Y,Z, or A they do not update to zero.

Not sure if I need to grab the instance before each get handle, but that is not required in the screen load scripts.  Not positive.  Scratching my head on this one.

The other thing I would like to display X:  before the actual data on each line, so the axis shows.

Getting closer thanks for the help Chaoticone.

Russ



local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX","Value")
local Ycoords = scr.GetProperty("droCurrentY","Value")
local Zcoords = scr.GetProperty("droCurrentZ","Value")
local Acoords = scr.GetProperty("droCurrentA","Value")
Xcoords = string.format("%4.4f", Xcoords)
Ycoords = string.format("%4.4f", Ycoords)
Zcoords = string.format("%4.4f", Zcoords)
Acoords = string.format("%4.4f", Acoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
mc.mcRegSetValueString(hreg, tostring(Xcoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 2"))
mc.mcRegSetValueString(hreg, tostring(Ycoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 3"))
mc.mcRegSetValueString(hreg, tostring(Zcoords))
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 4"))
mc.mcRegSetValueString(hreg, tostring(Acoords))
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 04:39:41 PM
OK, are you putting all of this code in the X DRO on updates script? If so, you will need to put it in each DROs on update script respectively.

Code: [Select]
This goes in the X DROs on update script.....
local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX","Value")
Xcoords = string.format("X%4.4f", Xcoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
mc.mcRegSetValueString(hreg, tostring(Xcoords))

This goes in the Y DROs on update script......
local inst = mc.mcGetInstance()
local Ycoords = scr.GetProperty("droCurrentY","Value")
Ycoords = string.format("Y%4.4f", Ycoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 2"))
mc.mcRegSetValueString(hreg, tostring(Ycoords))
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 05:08:54 PM
I have another idea that I think will work better all around. Let me know if your interested.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 05:40:10 PM
Chaoticone,

Thanks for your comments, yes I had it all in xDRO, moved it to each did not realize each DRO had a separate update script.  That works.

I will have to search how to concatenate strings so I can get the AXIS: with a couple spaces before it prints out the coordinates.

Another thing I notice is when you start up Mach4, this never updates the LCD display.  If you enable Mach4 it does not update the LCD display.

If you jog it updates the LCD display immediately, if you load a gcode file and start to run it updates the LCD display immediately.

I have not been able to find a command to send via the POKEYS unit to clear the LCD display in the very beginning so the POKEYS on MACH4 message gets erased prior to the coordinates getting display, but I will keep looking.  I know I can build a string of "   " 20 spaces and send that which might be a work around.

Much much closer and thank you very much Chaoticone, I knew I was missing several things so between you and DazTheGas I am getting much much closer.


Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 05:41:12 PM
YES, I am very interested.  Any suggestions are totally welcome.  I know many people have tried to get this kind of stuff working so hopefully this study will help many others struggling with some of these same issues. 

Let me know what you have and I will give it a try.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 05, 2016, 06:03:23 PM
OK, the reason the LCD does not update when Mach starts up is because the DROs are not updating and that script only runs when the DROs update (value changes). Anything that changes the value of the DROs should update the LCD.

Quote
I have not been able to find a command to send via the POKEYS unit to clear the LCD display in the very beginning so the POKEYS on MACH4 message gets erased prior to the coordinates getting display, but I will keep looking.  I know I can build a string of "   " 20 spaces and send that which might be a work around.

Not familiar with it but yes, sending an empty string should clear it.

OK, my idea is to build a function that updates the LCD. It would basically be all of the script you put in each dro combined into a single function in the screen load script. Then in the PLC script check to see if the DROs have changed and if they have, run the function. Also, to initialize the LCD when Mach starts run the function once in first run of PLC script. Look in the router screen sets PLC script for the Update fractional DROs section to see how it would work.

Another option that may be even better is to update the LCD to the values in the DROs in the first run of the PLC script and leave the onupdate scripts where they are (no need for a function in the screen load script then and will only run once when Mach is launched).

This should have put an X in front of the X value in the LCD display..........
Xcoords = string.format("X%4.4f", Xcoords)

This should put a X, a : and a space..........
Xcoords = string.format("X: %4.4f", Xcoords)
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 06:23:20 PM
Thank you for the suggestions.  I will play a while and let you know what happens.  Think I tried the "X: %4.4f" but got an error, will try it again.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 05, 2016, 06:29:59 PM
Just tried to update the Xcoords = string.format("X:  %4.4f",Xcoords)

That worked this time because it was working with the string not a value so thanks that works perfect.  I will work on the function you suggest and look at the other suggestion as well.  YOU ARE AWESOME...  Thank You so Much

Russ


Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 06, 2016, 04:41:16 PM
Chaoticone,

I tried to send the clear charter 0x01 to the LCD screen but that does not work, it just outputs a decimal 1.

Then I tried 0x20 which is HEX for a space, and what comes out on the LCD is 32.  So apparently writing to registers in Hex they get converted to decimal?

local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
                 mc.mcRegSetValue(number hReg, 0x01)


Then I tried to send a string of 20 spaces, like clr = "               "
but that pukes as well.  Still searching the LUA manuals must be some trick I am missing somewhere.

Russ




Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 06, 2016, 05:13:31 PM
After playing, my guess is the Plugin has defined these LCD registers as ASCII, so when you send HEX they get converted to DECIMAL ASCII

0x20 which is traditionally a space comes out as 32, which is decimal conversion of the hex number.  But clearly that is not ASCII number so this must happen automatically due to the plugin defines.

Will play again after I think about this a while.  LOL

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 06, 2016, 07:13:10 PM
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
                 mc.mcRegSetValue(number hReg, 0x01)

In line 2, is "number hReg" right? Shouldn't it just be hReg?

Seems to me this should clear it.........

local hreg = mc.mcRegGetHandle(inst, "PoKeys_32062/LCD Line 1")
mc.mcRegSetValue(hReg, "                    ")
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 06, 2016, 08:15:38 PM
Nope, but that is because setvalue is expecting a value not a string.  I also tried setstringvalue which should work, but I got and error.  Will test this again in the morning

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DazTheGas on December 07, 2016, 02:57:02 AM
Chaotione is right, lose the Number it should be just hReg, and also moving to havin a function in the screen load script would be much cleaner, it could be called from the dro update with (axis,lcd) and also set to run on 1st loop of the plc.

DazTheGas
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 07, 2016, 08:33:28 AM
Yeah that was a cut and paste error I only had hreg in the actual code.  I will play with this stuff again this morning.  Thanks

RUss
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 07, 2016, 08:36:49 AM
Daz,
I did not actually have the number in the real code just hreg.  As soon as I get it to blank a line on the screen correctly, I will modify what is current in the dro update code and move that to screen load and have dro update code call that code.  I am not sure how to do the PLC aspect for one time do you just put a loop counter inside the PLC script?

I will confirm the details as soon as I get a line on the LCD to clear.  The current code does update the LCD correctly so now I have all three axis update when running gcode or jogging.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 07, 2016, 09:23:22 AM
Guys,

Here is the code that actually works to clear a line on the LCD.

local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
                 mc.mcRegSetValueString(hreg, "                    ")

You can use mcRegSetValue and then try and send a string that just gives LUA errors.

I also tried other variations to clean it up for a function and they failed.

I tried, but this gives a LUA error when executed.

local clrlcd = "                  "

local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
                 mc.mcRegSetValueString(hreg, clrlcd)

I also tried to get the handle as follows in lines above the actual setvaluestring code.  But this approach also fails.

local hlcd1, rc = mc.mcRelogGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
                       mc.mcRegSetValueString(hlcd1, "                    ")


LUA seems to be very picky about certain things.  I wrote in C for years and things that worked in C do not work in LUA.  So this is a big learning experience.

Now that I have some code to clear a line on the LCD I will attempt to turn this into a function in the screen load script.

Really appreciate your help guys.

Russ


Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 07, 2016, 11:33:30 AM
Chaoticone, DazTheGas

I wrote an LCD initialize function in the Screen load script.   Guys thanks for all your suggestions, hopefully this will help anyone else attempting to get an LCD to work with a pokeys device.

Russ


--------------------------------------------------------------
-- Yaskawa Pendant Clear LCD Display
--------------------------------------------------------------
function YPenLCD()
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
        mc.mcRegSetValueString(hreg, "X:  0.0000          ")  -- X zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 2"))
        mc.mcRegSetValueString(hreg, "Y:  0.0000          ")  -- Y zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 3"))
        mc.mcRegSetValueString(hreg, "Z:  0.0000          ")  -- Z zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 4"))
        mc.mcRegSetValueString(hreg, "A:  0.0000          ")  -- A zero display
end

Then I added a function call in the PLC script that runs one time.  This basically clears the screen and shows each axis set at 0.0000, and if something other than 0.0000 was saved when you exit Mach4 it updates that when the machine comes up so this is working much better.  I can probably clean up the xDRO update scripts and do all of that in the screen load script as Daz suggested as well.

for name,number in pairs (DROTable) do -- for each paired name (key) and number (value) in the DRO table
        local droName = (DROTable[name]) -- make the variable named droName equal the name from the table above
        --wx.wxMessageBox (droName)
        local val = mc.mcProfileGetString(inst, "PersistentDROs", (droName), "NotFound") -- Get the Value from the profile ini
        if(val ~= "NotFound")then -- If the value is not equal to NotFound
            scr.SetProperty((droName), "Value", val) -- Set the dros value to the value from the profile ini
        end -- End the If statement
    end -- End the For loop
    ---------------------------------------------------
    YPenLCD() -- initialize the Yaskawa LCD display when you first start.

end
-------------------------------------------------------

Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 07, 2016, 03:21:09 PM
Good, glad your making progress.

This might work too.

Put this in your screen load script....

--------------------------------------------------------------
-- Yaskawa Pendant Clear LCD Display
--------------------------------------------------------------
function YPenLCD(Axis, Line)
    local CurPos = scr.GetProperty("droCurrent" .. tostring(Axis), "Value")
    local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line" .. tostring(Line)))
    mc.mcRegSetValueString(hreg, string.format(tostring(Axis) .. ":  " .. tostring(CurPos) .. "          "))
end

This in you PLCs first run....
YPenLCD(X, 1)
YPenLCD(Y, 2)
YPenLCD(Z, 3)
YPenLCD(A, 4)

This in your X DRO update event.......
YPenLCD(X, 1)

This in your Y DRO update event.......
ThiYPenLCD(Y, 2)

etc, ect. for Z and A DROs
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 07, 2016, 03:41:53 PM
Chaoticone,

Thanks, actual this was next on my agenda.  LOL   Passing the axis and line int he function is a great idea.  The only thing not working on the pendant at this point is the LED matrix, which apparently in a bug in the pokeys plugin for Mach4.  I will get this implemented and then let you know if all works as expected.  Thanks again you are fantastic.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 08, 2016, 05:47:58 PM
NP, that may not be exactly right (I did not get to test it) but you get the idea. Would make it a lot cleaner.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 11, 2016, 04:07:34 PM
Chaoticone,
I did not get a chance to test it this weekend was gone out of state for the last four days.  Will get this all tested tomorrow after I recover from my trip.  LOL

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 15, 2016, 04:34:31 PM
Chaoticone,

Well I tested your suggestion and what happens is each line of the LCD gets updated with

nil:
nil:
nil:
nil:

Seems like it is putting the nil return value on the screen.  This must be the line of code that is causing the issue.  You ask for the property of the droCurrent and that must be return Value of nil would be my guess.

Russ



 local CurPos = scr.GetProperty("droCurrent" .. tostring(Axis), "Value")
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 15, 2016, 04:47:42 PM
Where your calling the function try putting the axis letter and line number in double quotes........ like this.

YPenLCD("X", "1")

I would just try in the plc scripts first run to start with. If when mach opens the LCD displays right (before any axis moves) you got it.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 15, 2016, 05:17:28 PM
That fixed part of it, then I also did that same thing in the update scripts for each DRO.

Now it almost works correctly,

now I get 6 digits after the decimal point where I had four previously. 

So like

X:  12.123456     instead of  X:  12.1234

must need to put the %4.4f somewhere...

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 15, 2016, 05:30:20 PM
Try replacing your function with this..........

function YPenLCD(Axis, Line)
    local CurPos = scr.GetProperty("droCurrent" .. Axis, "Value")
    CurPos = string.format("%4.4f", CurPos)
    local hreg = mc.mcRegGetHandle(inst, "PoKeys_32062/LCD Line" .. Line)
    mc.mcRegSetValueString(hreg, Axis .. ":  " .. CurPos .. "          ")
end
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 15, 2016, 06:43:04 PM
I actually replaced the code with the following, which just added one line, which seemed to work but it broke other things.

function YPenLCD(Axis, Line)
    local CurPos = scr.GetProperty("droCurrent" .. tostring(Axis), "Value")
    CurPos = string.format("%4.4f", CurPos)
    local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line " .. tostring(Line)))
    mc.mcRegSetValueString(hreg, string.format(tostring(Axis) .. ":  " .. tostring(CurPos) .. "          "))
end


Now if I try and load a gcode file and start it seems to go and then will just get hung.  Z also updated very slow, very strange.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 16, 2016, 10:15:58 AM
Very strange,

With the latest change to put everything in a function to clean it up, sudden Mach4 would star trek.  I tried to run gcode file like the roadrunner and it would just sit and never start.  It is like one of t he functions would send it to never never land, but this makes no sense.  I commented out all the code and and Mach4 works again.  Then I removed the comments from the original codes prior to turning it into a function and everything worked again.  This is very bizarre.

Looking at the code nothing in there should have caused Mach4 to crash.  I rebooted and tried it several times with the same results.  One time the LCD went totally blank so I power cycled the LCD display and the ESS and got everything back but the problem reoccurred.  I will have to study this again to determine why this happens.

I still do not really understand the method to run the debugger when you are working with the screen load script.  I debug it and it starts but many of the variables just show errors when you step through the codes start at the very top of the screen load script the first few variables all show a value, but as you do down further they all show errors.  Nothing has changed in the upper section of the screen load script so clearly I am doing something wrong when running the debugger.  I have used C debuggers and assembly debuggers and this debugger seemed to work similar but must be something I am not doing to get it to work correctly.

Russ
 
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 16, 2016, 04:15:45 PM
Sounds like there is something wrong in some of your script somewhere.

You can take the script that's in the function and put it in a wizard........... then open that wizard and step through it to debug. That's usually what I do. I keep a wizard named test in my wizards directory just to play in.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 16, 2016, 04:58:17 PM
Chaoticone,

Thanks for the suggestion on using a wizard.  I will try that to get to the bottom of this issue.  Very strange everything was getting better and better but when I adjusted the DRO value and put it into the 4.4f format that for some strange reason seemed to cause it to star trek.  The strange part is it actually did adjust the DRO output correctly but somehow seemed to get MACH4 is a strange state where nothing would work correctly.  Never got any errors from the LUA code, so does not seem to be associated with a typo or something.  I will test in a wizard to see what might be wrong.

CurPos = string.format("%4.4f", CurPos)

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on December 16, 2016, 05:02:25 PM
Would be interesting to see if replacing your function with the last I posted fixes it.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on December 17, 2016, 09:05:48 AM
Chaoticone,

I will give that a go today.  LUA seems pretty touchy on some things and very forgiving on other aspects.  That single line only replaced a sting value with a formatted string value.  That line by itself should not have done anything that would allow MACH4 to crash.  I am learning as I go with this LUA stuff.  I was a C programmer for years, and this is similar to C, with the lack of Switch/Case and others but it does have other attributes.  Oh well always good to learn new things.  Thanks for all your help and suggestions.

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: mundsen on December 31, 2017, 08:49:55 PM
Any more progress on this? a final working code/setup for the LCD on Pokeys57CNC?
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on January 01, 2018, 08:49:30 AM
YES, I got it all working correctly.  I will post the code shortly, have to run out to the shop and look at the code.  Forgot exactly been a few days.  LOL

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: mundsen on January 01, 2018, 09:19:45 AM
Great, thank you :-)
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on January 01, 2018, 06:52:03 PM
Sorry for the delay been buried in College Football today.  LOL


LUA Code for X DRO

local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX", "value")
local hreg = mc.mcRegGetHandle (inst, string.format("Pokeys_32062 / LCD Line 1"))
Xcoords = string.format("X: %4.4f", Xcoords)
mc.mcRegSetValue(hreg, tostring (Xcoords).. "     ")


LUA Code for Y DRO

local inst = mc.mcGetInstance()
local Ycoords = scr.GetProperty("droCurrentY", "value")
local hreg = mc.mcRegGetHandle (inst, string.format("Pokeys_32062 / LCD Line 2"))
Ycoords = string.format("Y: %4.4f", Ycoords)
mc.mcRegSetValue(hreg, tostring (Ycoords).. "     ")



LUA Code for Z DRO

local inst = mc.mcGetInstance()
local Zcoords = scr.GetProperty("droCurrentZ", "value")
local hreg = mc.mcRegGetHandle (inst, string.format("Pokeys_32062 / LCD Line 3"))
Zcoords = string.format("Z: %4.4f", Zcoords)
mc.mcRegSetValue(hreg, tostring (Zcoords).. "     ")





Here is the code that goes in the main LUA loop

--------------------------------------------------------------
-- Yaskawa Pendant Clear LCD Display
--------------------------------------------------------------
function YPenLCD()
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 1"))
        mc.mcRegSetValueString(hreg, "X:  0.0000          ")  -- X zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 2"))
        mc.mcRegSetValueString(hreg, "Y:  0.0000          ")  -- Y zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 3"))
        mc.mcRegSetValueString(hreg, "Z:  0.0000          ")  -- Z zero display
        local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line 4"))
        mc.mcRegSetValueString(hreg, "A:  0.0000          ")  -- A zero display
end

--function YPenLCD(Axis, Line)
--    local CurPos = scr.GetProperty("droCurrent" .. tostring(Axis), "Value")
--    CurPos = string.format("%4.4f", CurPos)
--    local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_32062/LCD Line " .. tostring(Line)))
--    mc.mcRegSetValueString(hreg, string.format(tostring(Axis) .. ":  " .. tostring(CurPos) .. "          "))
--end


Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: mundsen on January 02, 2018, 02:12:27 PM
I know where to ad the DRO code, but need a hint about how to insert in the Lua main loop (novise when it comes to Mach4 scripting..)
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on January 02, 2018, 03:15:47 PM
The first thing you do is go the the OPERATOR tab at the top of the Mach4 screen, then select "LUA SCRIPT"  it is about the forth or fifth item on the list under Operator.

This will show you the current LUA code you are running.

I have attached my code, the entire script was designed to put the DROs on my LCD inside my Yaskawa pendant.  You will not need lots of this code, but if you print this out and highlight the sections that update the LCD display you will understand what you need to add to your LUA script.  Back up your initial script before you make any changes, that way you can always go back.

The other item I should have mentioned on my DRO update code you need to change the serial number to the serial number of your POKEYS device.  You can find that under diagnostics screen you will find the POKEYS device listed.


Hope this helps.


Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on January 06, 2019, 04:07:51 PM
Hi,

I have similar problems trying to display the X and Z DRO readings on a 4x20 LCD display. I'm running a Pokeys57 card with Mach4. At start-up the LCD does display "Pokeys plugin for Mach4" and I have done all the basic checks to ensure the LCD is working.
In the screen script for the X DRO I have placed:

local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX", "value")
local hreg = mc.mcRegGetHandle (inst, string.format("Pokeys_Lathe / LCD Line 1"))
Xcoords = string.format("X: %4.4f", Xcoords)
mc.mcRegSetValue(hreg, tostring (Xcoords).. "     ")

However, when I enable and run Mach4, and jogging the X axis the axis move but the X DRO stays fixed at 0.00 and I get the error message:

C:\Mach4Hobby\ScreenScript.lua 764
Bad argument #2 to 'format' (number expected, got string)
stack traceback:
[C]:\Mach4Hobby\ScreenScript.lua:764 in function C:\Mach4Hobby\ScreenScript.lua760

I have to remove the code, not only comment it out, to get the X DRO going again.

Any help will be greatly appreciated.

Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: cncman172 on January 07, 2019, 08:20:39 AM

This line just gets you the handle, where is the code that populates the string?

local hreg = mc.mcRegGetHandle (inst, string.format("Pokeys_Lathe / LCD Line 1"))


This line populates the Xcoords with a string.
Xcoords = string.format("X: %4.4f", Xcoords)


I am running to catch a plan this morning but will respond again with the complete program, you have something wrong...

Russ
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on January 07, 2019, 09:08:03 AM
Hi Russ,

I'm still very new to Lua programming - your help is very much appreciated.

Thanks Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on January 14, 2019, 09:19:00 AM
Hi Russ,

I still have my set of problems to get the LCD going correctly.
If I place the simpler code as a Lua script attached to the “droCurrentZ” the following happens:

local inst = mc.mcGetInstance()

local Xcoords = scr.GetProperty("droCurrentX","Value")
local Zcoords = scr.GetProperty("droCurrentZ","Value")

local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_Lathe/LCD Line 1"))
mc.mcRegSetValueString(hreg, tostring(Xcoords))

local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_Lathe/LCD Line 3"))
mc.mcRegSetValueString(hreg, tostring(Zcoords))

The droCurrentX is displayed and updated on the LCD every time the Z axis is moved (this behaviour does make sense to me);
The droCurrentZ is displaying a zero value, even if the Z axis are moved around(?)

Then I removed all the code except for:

local inst = mc.mcGetInstance()

This line seems to be causing the droCurrentZ to be kept at zero. This is difficult for me to understand.

If I use the same lines of code above  in the droCurrentX script the effect of X and Z is reversed.

Thanks Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 04, 2019, 08:01:33 AM
Hi,

got to implement all the code for outputting the axis DRO values to the Pokeys56U LCD. But the values of the axis DROs and the LCD are updated only once when the axis are moved and no error messages apeared. What could be the reason for this effect?

Any help is appreciated

Kind regards
Wolfgang
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on March 14, 2019, 02:46:35 PM
Hi,

I went to the Screen Editor, clicked on the X DRO value – the variable droCurrentX highlighted on the LHS. Then I clicked on Properties -> Events, went to RHS (….) of On Update Script, then a new Lua file opened.
I typed the following code in this new LUA file:
local inst = mc.mcGetInstance()
local Xcoords = scr.GetProperty("droCurrentX","Value")
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_Lathe"))
mc.mcRegSetValueString(hreg, tostring(Xcoords))
where PoKeys_Lathe is the name of my Pokeys57 card.

I then saved and exited the screen editor. After enabling Mach4 I moved the X-axis – the axis moved as before but the DRO X stayed constant on the value displayed previously. Fur debugging purposes I removed lines 2 to 4 (leaving only local inst = mc.mcGetInstance()) but the problem persisted.

At this point I'm stuck, any help will be very much appreciated,

Kind regards

Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 16, 2019, 04:36:12 AM
Hi,

same effect to me...

Even 2 hyphens as comment in this code section of the On Update Scripts are inhibiting the update of the DRO values. It seems, that this is a more as a matter of principle.
At the moment I'm working with Mach4Home build 4095 which is licensed.

Maybe some of the experts have an idea what could be wrong?

Thank you in advance,

Kind regards
Wolfgang
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: bryannab on March 20, 2019, 09:25:04 AM
Hello all,

Try adding the code below (thanks to Brett for the code!) to your On Update Script, with your custom stuff in the middle:


local val = select(1,...) -- Get the system value.
val = tonumber(val) -- The value may be a number or a string. Convert as needed.

--Do your stuff here
local inst = mc.mcGetInstance()
mc.mcCntlSetLastError(inst, "On update ran")

return val -- the script MUST return a value, otherwise, the control will not be updated.

^^ that last line is the most important. This was tested in our latest development version (4124) and worked like a dream, though you will need to add in your custom scripts and test it with your setup as well.

Hope that helps!

-Bryanna
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 22, 2019, 11:23:07 AM
Hello,

with the recommendations of Bryanna the output of the DRO to the LCD of the PoKeys is working.

What I've found is the fact, that the values on the LCD are lagging the DRO values when I use this code:
local Xcoords = scr.GetProperty("droCurrentX","Value")

The correct value which is identical to the DRO on screen is val by itself.
So we can simply write:
local Xcoords = val

Sometimes the changes in the On Update Script are showing no effect after saving and leaving the editor.
Only when closing and restarting Mach4 the changes in the code where recognized.

Hope this is of help for somebody.

Wolfgang


Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on March 24, 2019, 04:26:11 AM
Dear All,

Thank you for all your help thus far.

As indicated above, starting and ending the Update Script (droCurrentX) in the correct way seems to be very important. I replaced my initial code with the following:

local val = select(1,...)
val = tonumber(val)
local inst = mc.mcGetInstance()
local Xcoords = val
local hreg = mc.mcRegGetHandle(inst,string.format("Pokeys_Lathe/LCD Line 1"))
mc.mcRegGetValueString(hreg,tostring(Xcoords))
return val

Now when I move the axis of the lathe, the droCurrentX continuously update on my Mac4 screen but my LCD still only indicates "Pokeys for Mach4" - thus the register does not seem to be updating? But when I go to Diagnostic -> RegFile -> Pokeys_Lathe - LCD Line 1, I am able to enter a value and the value is displayed on the 1st line of my LCD.

Any suggestions will be highly appreciated,

Kind regards Danie


Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 27, 2019, 12:01:44 PM
Hello Danie,

the code  of the On Update Script which is working seemlessly on my machine looks like here:

local val = select(1,...)
Xcoords = val
val = tonumber(val)
local inst = mc.mcGetInstance()
mc.mcCntlSetLastError(inst, "On update ran")
local Xcoords = string.format("X:% 10.4f", Xcoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_34497/LCD Line 1"))
mc.mcRegSetValueString(hreg, Xcoords.."        ")
return val

Not too much differences to your code. Maybe this helps a little.

Kind regards
Wolfgang
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on March 28, 2019, 01:31:53 AM
What version of Mach4 are you running?

The reason I ask is DROs have had the capacity to be linked to a register in Dev. versions for a while. That would eliminate the need for any of the code if I'm not overlooking something. Linking it in the screen if the DRO value changes the register updates. If the register value changes it updates the DRO. If your just doing a one-one no code needed. The only time you need to do and scripting to write to the register would be if you wanted to do some math or formatting or something.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 28, 2019, 01:23:19 PM
At the moment I'm using Build 4132.

Kind regards,
Wolfgang
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Chaoticone on March 28, 2019, 01:54:26 PM
At the moment I'm using Build 4132.

Kind regards,
Wolfgang

OK, try this. Comment out all of your on update and on modify script for that DRO. In the properties tab for that DRO towards the bottom of the list you will see Register. Use the dropdown to select the register you want the DRO linked to. Should be all there is to it.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 28, 2019, 02:38:27 PM
Did what you recommended. I've completely removed the code in the On Update Script and linked the DRO register to the PoKeys LCD register from the drop down list. But the LCD doesn't update when moving the axis.
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on March 28, 2019, 03:48:09 PM
Dear Wolfgang

Tonight I can report, with joy in my heart, that your code is displaying the X and Z coordinates on my lathe. Understanding all the Lua code with still take some time but I am working on that. I'm using Build 3804.

Thank you so much for all your help,

Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Elektroniker on March 29, 2019, 11:43:57 AM
Hello Danie,

I'm glad to read that your problem is solved.

Kind regards,
Wolfgang
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: tbadger on July 14, 2019, 10:38:40 PM
A few on the board seem to have gotten something working here. Can anyone help write up the final resolution to getting the output to the LCD?
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on July 15, 2019, 02:02:38 AM
Hi
My LCD is working well, will post the code in a day or two,

Regards
Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: tbadger on July 15, 2019, 09:56:59 AM
Hi
My LCD is working well, will post the code in a day or two,

Regards
Danie

Thanks Danie, looking forward to seeing your solution.

Trent
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on July 17, 2019, 02:30:58 AM
Dear Trent

Please see my steps, generated with great help from the Forum members. I run a lathe with Mach4 and the name of my card is Pokeys_Lathe:

Go into the Screen Editor: Click on the X DRO on the screen, then look at the left and you will see droCurrentX highlighted slightly. Click on droCurrentX to highlight it.
Now look at the window (Properties)  below this window and click on the second little box which is the Events Tab. Now click on the first line below that called "On Update Script", then click on the box with the three dots to the right,  that will open a blank Lua Page.

Now copy the LUA code below for the X DRO below and paste it on that blank LUA page.  Please update the serial number of your Pokeys device.

local val = select(1,...)
Xcoords = val
val = tonumber(val)
local inst = mc.mcGetInstance()
mc.mcCntlSetLastError(inst, "On update ran")
local Xcoords = string.format("X:% 10.4f", Xcoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_Lathe/LCD Line 1"))
mc.mcRegSetValueString(hreg, Xcoords.."        ")
return val

Follow the same procedure for updating the Z axis DRO. Remember to update the serial number of your Pokeys device again.

local val = select(1,...)
Zcoords = val
val = tonumber(val)
local inst = mc.mcGetInstance()
mc.mcCntlSetLastError(inst, "On update ran")
local Zcoords = string.format("Z:% 10.4f", Zcoords)
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_Lathe/LCD Line 2"))
mc.mcRegSetValueString(hreg, Zcoords.."        ")
return val

Now exit Mach4 and then start it up again.  Now the LCD should update each time you have the DROs updated in Mach4. I still struggle to understand what each line of code is doing.

Please let me know how its going.

Kind regards

Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: tbadger on July 21, 2019, 02:19:06 PM
 :)  8)

Danie, you ROCK!  I've followed the instructions exactly as you provided and the LCD is now displaying the DRO info as I had hoped.

Thanks, I hope that I can someday provide similar input to solve a similar problem.

Trent
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: DanieGouws on July 21, 2019, 02:28:23 PM
Dear Trent,

I am so glad it is working!! I struggled for months with this problem.

Kind regards
Danie
Title: Re: Pokeys Mach4 plugin LUA reference for LCD
Post by: Tim28 on October 14, 2021, 10:44:48 AM
Hello. I need help. Have a Pokeys57cnc and want to build a handwheel. there is also a 20x4 LCD. Unfortunately, I am not a LUA expert. I did the following. LCD shows the last values ​​when starting mach4. Problems. at startup it shows Pokeys plugin for mach4. Doesn't go away either. DRO does not change in the main menu of mach4 when moving or jogging. LCD also does not change anything. I added the scripts to update under Screen editor under individual DROs can someone help me? With best regards Tim