Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: markgeigel on October 13, 2017, 01:36:13 PM

Title: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 01:36:13 PM
I'm interfacing build 3481 of Mach4 to a Hitachi NES1 VFD. I've got them communicating and I can write registers fine from LUA. What I can't seem to do is write coils from LUA since Mach4 does not create registers for coils how do I access it from LUA?

Thanks

Mark

Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 01:57:01 PM
Hi Mark,
have you enabled the Modbus plugin?

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 02:07:12 PM
Hi Craig:

I have modbus communicating to the VFD, I can write a frequency to the VFD because that uses the write registers function. And that function when setup in Configure/Modbus automatically creates an instance register that I can access from LUA. And that is working. However the write coil function which is just working on a bit field when setup in modbus does not create any kind of variable automatically. So I don't know how to tie that to my LUA script. I went through all the scripts you did when you helped someone else get a VFD working but he was using only registers. My NES1 needs coils for stop/run and direction but uses registers for the frequency setting.
I really appreciate the help.

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 02:32:39 PM
Hi,
you need to add a function to your Modbus connection.

Configure/Plugins/Modbus.....with your connection highlighted, probably Modbus0 unless you have a whole swag of them! There is a row of four
icons at the top of the window...Make New Connection, Delete Connection, Add a Function, Delete a function. The Add a function icon should be active
with the others greyed out. The function setup panel allows you to write to and read from coils, registers and blocks of registers. Enjoy...

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 02:37:05 PM
Hi Craig:

I have done that already. When you do that for a register in modbus you automatically get an instance variable that you can access in LUA. But when you do the same thing for a coil you don't. So the question is once you have the write coil function defined in Modbus how do you actually set it's value in LUA?

Thanks

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 02:39:27 PM
Hi,
rereading your post and I think I misunderstood your question.

You can set individual coils in the VFD ergo you must have a working Modbus function to do so. If I understand your question is how do you
set a bit in Lua that will eventually transmitted to the VFD? Is this correct?

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 03:07:29 PM
Hi Craig:

See attached files. I have a working modbus connection to the VFD but I'm only able to set register values not coil values.

In the screenshots my setFrequency function I defined in Configure/Modbus causes Mach4 to automatically create the spndlFrequency0 and spndlFrequency1 registers. The script in the screenshot (most of which you wrote lol) does indeed set the frequency in the VFD and it then appears in the modbus diagnostic window and registers diagnostics window when I press the button that the script is attached to. I set the register value and it gets sent to the VFD without error.

So registers work fine for me with the VFD from what I can tell.

What I don't know how to do is change the coil value from 0 to 1

Mach does not create a register that I can get an instance to in LUA script for me for the coil like it does for the write register function.

to make my test button start the spindle I need to do the following

1) set the frequency (I have that working since it uses registers and I now know how to do that)

2) Set the direction by writing to coil 2 either a 1 or 0 depending on if I want fwd or rev

3) set the spindle to run by writing a 1 to coil 1 (that is my sop/run function I have defined in modbus)

I don't know how to do steps 2 or 3 show me some code obi-wan

Sorry for being a pain.

Mark

PS:

If I press the button that executes the script in the screenshot I do see the spndlFrequency0 and spndlFrequency1 values change in the modbus tree on the register diagnostics and I also see them change in the diagnostic window for Modbus (see screenshot)

So I have registers working but don't know how to set a coil to 0 or 1 in LUA after I have it the function defined in modbus

is that any clearer?



Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 03:13:35 PM
forgot to add these screenshots
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 03:29:03 PM
Hi Mark,
I'm having exactly the same issue. The Modbus function to write a to a coil seems to be there but not a register name therefore no handle
therefore no way to set the bit.

I wonder if we are confusing a coil and a register? My thinking suggests that a coil is just one bit of a register, therefore to set a coil in your
target device you need to write to that one bit in that register. Sounds like you are thinking the same way.

I'm now wondering if that is correct...what if 'I want to set a coil in my device then "write coil" function' ie that the function 'write coil' actually
sets a bit in the device and presumably some other function clears it?

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 03:36:40 PM
Hi Craig:

Now we are on the same page. Simpson36 who seems to have worked a lot with modbus asked the same question in a previous thread but then 2 hours later said "never mind, figured it out" but never posted what he figured out :)

but on the hitachi the coil numbers are 1 and 2 and guess what that is the same address used for the setFrequency register so unless there is some addressing translation scheme that I don't know about I'm stuck.

Here was the thread asking the same question from Simpson367 https://www.machsupport.com/forum/index.php/topic,27158.msg209066.html#msg209066 (https://www.machsupport.com/forum/index.php/topic,27158.msg209066.html#msg209066)
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 03:43:31 PM
Hi Mark,
found this:
http://www.simplymodbus.ca/FC05.htm (http://www.simplymodbus.ca/FC05.htm)
which seems to suggest that the embedded data within the function determines whether the coil is set or unset.
At this stage Mach4 is not offering a register for us to apply that data. Simpsons36 post is maddening!

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 06:21:22 PM
Hi Mark,
think I've tumbled to the solution.

What gave it away is: have a look on the Modbus function page where you have declared your registers, the type is listed as Mach Output Register
whereas on the read coil function page it is listed as Mach Output. You will note that I declared a name, fwdrevbit, as the name of the input
to the modbus function which is a Mach output. In Machs outputs page I elected to use Output#1 but could have in fact any valid Mach output...

A picture is worth a thousand words.

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 13, 2017, 06:24:28 PM
Nice catch. Makes perfect sense. I'm away from the machine right now but I'll try it over the weekend and I really appreciate the help

you rock :)

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 13, 2017, 06:29:23 PM
Hi,
sorry I used Output#0 not 1 as in previous post.

Not sure at the moment when trying to set Output#0/fwdrevbit what should be used when trying to get the signal handle. I suspect Output#0, fwdrevbit
is an alias. I know the ESS plugin make good use of aliases but its the first time I've seen another plugin do the same.

The question in my mind is where the alias is valid? One question answered but another three new ones!

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 14, 2017, 10:25:56 AM
Craig:

I have not tried using the alias yet. That does indeed make the spindle spin. On the hitachi the frequency is from 0-40000 via modbus since the resolution is .01 hz. It works fine with the frequency <32767 but when I try to use something closer to 24,000 RPM of course then I need the 2nd register value to be updated. So far when I do that I get an error from the VFD I need to look at that a bit more. But it's working a LOT better. Thanks and I'll post something again when I get this all working since I'm sure there are other Hitachi VFD people who will want to do this.

Thanks again

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 14, 2017, 01:46:40 PM
Hi,
and having frequency resolution to 0.01 Hz is useful why? Your spindle is asynchronous and the frequency displayed or
commanded is not the actual rpm anyway.

Craig
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 14, 2017, 03:02:13 PM
Don't ask me why ask Hitachi :)

Anyway as it turns out (verified all this with a tachometer) the Hitachi NE-S1 has two registers for the frequency but you have to set the high order one to zero and then populate the low order one with the desired frequency 0-400hz multiplied by 100. So for the full 24,000 RPM on my router you just stuff the 40000 into the low register and the VFD treats it like an unsigned integer and it works fine. I guess they allocated two registers just to be safe and allow for future products. None of this is documented in the Hitachi manual of course other than the fact that there are two registers and that the resolution is .01 HZ.

I still have to hook up the VFD trip stuff so that Mach knows when the VFD throws and error.

Getting closer, fun fun fun.

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: markgeigel on October 14, 2017, 03:21:03 PM
Later this weekend I'm going to implement all the stuff you did with Zuxztah in this thread: http://www.machsupport.com/forum/index.php/topic,34023.0.html (http://www.machsupport.com/forum/index.php/topic,34023.0.html)

After that I guess I'll have to revisit the E-Stop situation a bit.

Thanks!!!

Mark
Title: Re: writing modbus coils using LUA for Mach4? how?
Post by: joeaverage on October 14, 2017, 03:27:36 PM
Hi Mark,
glad to help.

Craig