Hello Guest it is March 29, 2024, 09:37:55 AM

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

0 Members and 1 Guest are viewing this topic.

Offline CRS

*
  •  61 61
    • View Profile
Lua success with external buttons to Mach4
« 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

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua success with external buttons to Mach4
« Reply #1 on: January 18, 2017, 03:59:06 AM »
Whoops last line of the pdf needed correction

Craig.
Re: Lua success with external buttons to Mach4
« Reply #2 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.
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua success with external buttons to Mach4
« Reply #3 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.
Re: Lua success with external buttons to Mach4
« Reply #4 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline CRS

*
  •  61 61
    • View Profile
Re: Lua success with external buttons to Mach4
« Reply #5 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
Re: Lua success with external buttons to Mach4
« Reply #6 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline CRS

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


Re: Lua success with external buttons to Mach4
« Reply #8 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Lua success with external buttons to Mach4
« Reply #9 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
New For 2022 - Instagram: dazthegas