Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Zuxztah on January 14, 2017, 09:03:39 AM

Title: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 14, 2017, 09:03:39 AM
Hello guys I'm totally lost..
 
As I've decided to switch from Mach3 to Mach4,
to get rid of the old dying parallel port, A new problem have showed up, probably not just one,

I have very little experience with modbus, very very basic knowledge and nothing more and I really need some help to either understand, or to get it working,

I got the numbers hex/Deci,s registers and so on, but I don't know what to do with them?

There is no test modbus feature in Mach 4 that could help me determine the functionality of the settings,

And how to bind them to the spindle start/stop/freq buttons in the software?

Lua script/plc editor? Way out of my comfort zone,

I want to learn but..  I don't often find moments to research and learn,

And my English isn't the best as you guys probably have noticed.

Thanks.
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 14, 2017, 08:05:22 PM
Hi Zuxztah,
what motion controller are you using? The one I use has features built in to control a spindle. I imagine every controller
on the market has some features to turn the spindle on and off at the least and most have a PWM output that can
be used to control the speed.

You could probably use MODBUS or similar but is more complex and maybe not necessary when getting started.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 15, 2017, 05:32:28 AM
Hello! Yes it's possible to run it trough pwm, I use 2 Different Chinese bob's, can't remember the name C10 maybe? at the moment I got everything hooked up with a Smoothstepper (ESS)

I could just wire up the start/stop cable and then control the speeds from my portable vfd panel,
But I want that automatic speed change feature, and smooth running operation with modbus, 0-10v  dosen't seem to run as reliable as Modbus

The biggest problem I have is to bind all the functions from mach4's buttons to send commands via modbus?

The LUA code that sends the commands to VFD when M4 or M5 is pressed  or when the frequency is changed?

I can run the vfd with any modbus communication software/serial monitor it's just the lua in Mach that's itching my brain, just like the old brains in Mach 3, I'm not able to figure it out,

 attachment is a mess, a old picture without the ESS


And some other pictures of the machine  :)

https://imgur.com/gallery/qc15q
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 16, 2017, 06:03:08 AM
Hi Zuxztah,
Quote
, and smooth running operation with modbus, 0-10v  dosen't seem to run as reliable as Modbus

I'm not sure about that, many users run their spindles that way without problem.

If you want to run over MODBUS I can probably sketch it out, will use the manual for my Delta VFD which I imagine will be
similar to yours.

You need to set up your VFD

02 - 00
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 16, 2017, 01:50:40 PM
I really appreciate the help!

I want to run modbus to save I/O on the breakout boards and to eliminate EMI noise, haven't had any noise problems with modbus so far,


 I'm running modbus trough USB (USB - RS485) if you want I could give you access trough teamviewer or something to change all the settings directly in (Mach) on my computer, and if you want you could contact me via Facebook/skype or something if you want fast response, if you prefer that, it's important though that we post all the information and settings here on the forum  :)

The settings I use is:

P101: 5 RS485 communication (Frequency setting selection)
P103: 1 "STOP" ("STOP"Key lock mode valid)
P700: 38400 bps (Communication speed)
P701: 8N1 FOR RTU (Communication mode)
P702: 1 (Communication address/Slave address)
 
COM port 1 or 3 don't remember at the moment, and I'm also running the VXRouter.screenset

I had everything working in Mach 3 with some help from a guy on the CNCzone forum,

I can start and stop the spindle via decimal start:8192 and by sending decimal command:

10 Start CW
01 Stop
06 Start CCW
12 change Direction (While running)

And frequenc:  Start:8193 by sending decimal command 100-400 or 1000-4000 not entirely sure at the moment (Frequency in Hz)
100hz = 6000RPM
200hz = 12000RPM
400hz =240000RPM


The only thing that wasn't working was RPM feedback to the RPM window in Mach 3 (actual RPM)

The problem I experience now is that there isn't any test modbus function in Mach 4 so I'm not sure if it's working or not when I'm trying to add a modbus function, and I'm not familiar at all with LUA/PLC or any code at all (Linking to the M commands and conversion from RPM to Hz and  actual running speed feedback to Mach 4 (RPM window)

I've used a external serial monitor and sent all the Deci commands with good results,

attached all the manuals for the VFD,

Thanks again!

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 20, 2017, 01:30:58 AM
Hi Zuxztah,
the manual files are empty. Sounds like you know how to programme your VFD.

If you read thru the manual you might find another function something like (my Delta VFD manual):
(3) 08H: loop detection
This command is used to detect if the communication between master device (PC or PLC) and AC
motor drive is normal. The AC motor drive will send the received message to the master device.

This function you could use to test your link. The link and the VFD will have to be present and working. You may need to
disconnect the spindle while you experiment if it would present a danger.

If you follow my suggestion there should be two registers in Mach4; spinCntrl and freq. They can be observed by opening
the Regfile Diagnostics page and expanding Modbus0 using Mach4 Diagnostic tab. If you click on a register you can also
manually edit it. As the MODBUS function are executed every 25ms or so any edits will be written to the VFD.

All that is required now are some LUA scripts to auto update the registers. If you have not already done so read chapter4 of the
'Mach4 Scripting Manual' which can be opened by clicking the 'Help Docs' button in the File Ops tab with Mach4 enabled.
I will re-read them myself and come back with some suggestions.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 20, 2017, 03:08:39 AM
Hi!. I'll try to post the manuals again, used the phone to attach the pdf's and that's probably the cause to not uploading properly,
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 21, 2017, 05:56:17 AM
Hello! I've been fiddling with some of the modbus settings and set up a function with 4 (Write) Multiple Registers
Start CW
Stop
Start CCW
Cange Dir
And one single register for frequency control

, I'll post some pictures :)

Functions at 8192 doesn't seem to be responding? Reply time out.

And there isn't any value to add the (Decimal code) that Mach will send to the VFD (I guess it's in the lua code that determine what Decimal command to send)

And there isn't any way that I know of to send a command to the vfd to determine if the modbus setup is really working? More than the diagnostics window.

https://postimg.org/gallery/10d73mala/ (https://postimg.org/gallery/10d73mala/)
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 21, 2017, 03:46:43 PM
Hi Zuxztah,
if I understand correctly you only require two registers to control your spindle:

slave reg 8192- contains the bit pattern to turn on or off the spindle and its direction
slave reg 8193- contains a numeric value for the frequency

I believe also that your MODBUS connection window (your first pic) should have 'use zero base addressing' checked.
I propose that the Mach4 registers are modbus0/spinCntrl  and modbus0/freq.
Your frequency setting function (your second pic) should have the slave address 8193 and the Modbus register should be modbus0/freq.

Your spindle control function (your third pic) is inconsistent. The function selected is 'write multiple registers', you only require 1 register word.
The slave address (the VFD is the slave device) should be 8192 and the Modbus register should be modbus0/spinCntrl.

In order to control your spindle you need to load the required bit pattern into modbus0/spinCntrl, ie one word transmitted but different values
for the required spindle function.

Does that make any sense?

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 21, 2017, 04:33:24 PM
Hi, I don't know what I'm doing, no experience at all, the only obstacle now is = everything but at the moment  modbus0/spinCntrl and modbus0/freq, what are those? Script in screen editor or a txt/config file in the Mach folder?

I'm stating to understand

I thought I had to write to  modbus register (8192) with multiple registers and then define Reg 1 - 2 - 3 accordingly to the buttons in Mach via script in screen editor,

Like in this picture (IMG_0268)

Reg 1 is defined to button for  Spindle start Clockwise (sends Decimal 10 for start)
Reg 2 is defined to button for Spindle stop rotation (sends Decimail 01) and so on, but I've clearly got everything wrong, hahaha :P

Forgive me for my bad/basic English,

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 21, 2017, 05:27:12 PM
Hi Zuxztah,
have a look at Diagnostics/Regfile. You will see a number of registers. Most of them are defined and/or created by various plugins
but you can define your own as well.

Try this: Configure/Plugins/Regfile- you should see a page with two tabs 'Instance Registers' and 'Global Registers'. Try creating an instance
register (page icon with green cross) and a new register will be formed which you can name, provide a default value and make persistent.
Make one called RegMyTrial with a default of 100 and make it persistent. Click Apply and exit.

Now inspect that new register by Diagnostic/Regfile and expand iRegs0 and you should see the register you created RegMyTrial and its value.
You can edit its value by double clicking. This register could now be accessed or written to by any script in Mach4. For instance you might
store a value of the X co-ordinate when running one of your macros say m100 but later you could recall that number in another macro say m101.
Of course this is just an example, read chapter 4 of the Mach4 Scripting Manual that is in the 'Help Docs' button of the File Ops tab when Mach4
is enabled. It explains the usage of registers. Note also that you could store either a number or a string. I make use of the string store to
record a path and filename in one of my macros which can later be used to open that file in another macro.

For your spindle control problem you are going to allow the MODBUS plugin to define two new registers. They will be seen at Diagnostics/Regfile
and expand modbus0. You could manually edit them there or you could programmatically edit them when you hit a button or execute a script.
You would also be able to see the registers by Diagnostics/Modbus and expanding but note that the only registers displayed there are those which
have been created or owned by the Modbus Plugin, it is not the right place to edit them however.

Once you've got your 485 comm link working and the two new registers defined in the Regfile then you can go on to work out how to manipulate
them.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 22, 2017, 12:12:11 PM
Finally it works!  I've been changing the ballscrews on the machine so haven't been able to check the modbus settings until now, and it worked first try? That's just pure luck!

I've posted a new album on postimg.org: https://postimg.org/gallery/prwl03vi/

I guess the Reg file thing you told me to do "RegMyTrial" was for explaining purposes? For me to find the registry editor?

I can now control the Spindle via Register diagnostics by changing the value modbus0/spinCntrl and modbus0/freq with my old decimal commands I talked about earlier:)
Modbus0/Cntrl
8192:
10 Start CW
01 Stop
06 Start CCW
12 change direction

Modbus0/freq
8193:
1000-4000




Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 22, 2017, 01:29:25 PM
Hi Zuxztah,
kool!

Code: [Select]
I guess the Reg file thing you told me to do "RegMyTrial" was for explaining purposes? For me to find the registry editor?
Yes.

Couple of things I see that could do with refinement or experimentation:
Both your Modbus functions have slave address 0 and Modbus address 8192 or 8193. Try the other way around.
The VFD is the slave device and the address of the registers internal to the VFD are 8192 and 8193. Try it and tell
me if it works, I can't test it without a 485 link.
Second item: the two registers are continuous so you could have just one function

Modbus function: Multiple write
Slave address: 8192
Modbus address:0
Nmbr reg:2

ie each time the Modbus function runs it sends both the control word AND the frequency word. It means that the CRC check which must
happen within the Modbus plugin only has to run once.

Another idea that you may wish to experiment with is reading the actual spindle speed from the VFD, in this instance you are not
writing data to the VFD but reading from it. Don't have your VFD manual on screen at the moment but there is a register that has
the current speed, I'm going to guess 8150:

Modbus function: Read single holding reg
Slave address: 8150
Modbus address:0

So you would end up with another register in the Regfile called something like ActSpeed which could be read and then displayed on the screen.

The next item on the agenda is being able to manipulate your control words from a button on screen or an M code. I suggest you have
a look thru some of DTGs videos to get some ideas how to use the 'Edit Screen' function.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 22, 2017, 01:39:06 PM
Hi! I tired the other way around this morning I understand that the vfd is the slave unit, in my vfd settings I can choose to set vfd slave adress, which is set to 1 (if I had another vfd it would be slave adress 2)

If you want and have your computer available at the moment then you could could download teamviewer (remote access) and try the settings yourself, super simple and no installation is needed, just download teamviewer 12 and choose run only, and then I send you my I'd and pass,

It is one way operation, I'll need to have your I'd and password to be able to do anything with your computer, it's safe :) no worries :)

The current setup works great, if I go into Regfile diagnostics and change the value on modbus0/spinCntrl to 10 the Spindle revs up, and 01 stops it,

And readout would also be awesome :) I'll try it as you mentioned (8150)
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 22, 2017, 02:14:28 PM
hi Zuxztah,
8150 was just my guess, read thru your manual to find the actual register number.

For instance my Delta VFD:

Quote
210CH Motor speed (rpm)
- would need to convert 210C to decimal...

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 22, 2017, 02:25:32 PM
Hi Zuxztah,
just re-reading your last post and I think you are correct, ie the Slave address is the VFD device number. I wish I could
experiment but I don't have a 485 link. Further my machine is out in the garage and I've pretty much be told, doctors orders' to
stay the hell out of there!

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 22, 2017, 03:35:02 PM
I must be really really stupid... my brain doesn't work as it should.. not  what it used to be, due to the disease..

I came up with 0004H = 4 Deci
Or 001B = 27 Deci,
 Tested and confirmed to not work with read holding registers either 16bit or 32bit

The manual is a mess.., at least for me, I find the other data addresses for the previous functions Start and freq:

2000H = 8192
2001H = 8193

And the content Bit1~Bit0 (10B Start) (01B stop) and so on..  but to read the motor speed seems different.

Some pictures of the manual: https://postimg.org/gallery/1g8bophy6/

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 22, 2017, 04:30:08 PM
Hi Zuxztah,
maybe try 'Read Input Register', looks to me like you have the right address.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 22, 2017, 04:34:25 PM
It's just reading the current motor speed state? No further action is needed? Just setup up the modbus configuration and then look for a value in the regfile diagnostics tab?
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 22, 2017, 05:44:54 PM
Hi,
not quite. The Modbus functions run every 25ms or so, this means that the Regfile register is near enuf to a live value of the
actual motor speed. That register will have to be read and displayed on a regular basis so the display becomes 'live'.

The PLC script is the ideal way to achieve this. The PLC script runs about every 40ms. In that script you will insert some code
that reads the register, probably with some sort of calculation and formatting and display it on the screen. Lets say the register
you have created is called modbus0/ActSpeed

Append to PLC script:
hreg=mc.mcGetRegHandle(inst,"modbus0/ActSpeed")
local speed=mc.mcRegGetValueString(hreg)   --  have read it as a string just to be on safe side
local RPM =tonumber(speed)/10*60   --   convert speed to a number, divide by ten to get frequency and multiply by sixty to get rpm
scr.scrSetProp(droTrueSpindle,RPM)   -- not sure about this statement just yet will have to do some more reading/experimenting

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 24, 2017, 06:50:44 AM
I tried input registers with the numbers I got from the manual but didn't work, don't know what to try and what to do.. there must be a way to read the motor speed, but how? Try to read the manual and see if you understand?
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 24, 2017, 10:56:10 AM
Hi,
I agree the manual is not clear, by my reading the address should be 0004hex. What I suspect is that you have to
prepend with another number to get the data address. Try emailing the manufacturer.

May I suggest don't bother with reading the speed at the moment, just concentrate on controlling the spindle.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 24, 2017, 11:09:42 AM
Hi!
I'm currently waiting for skype contact, they will probably answer tonight as usual, China time zone and all
they say that it is standard protocols but that doesn't help, because we don't know what registers they use for their functions? It's may be possible to figure that out?.
I could try to use a modbus sniffer? And se what data it transmits? I may only work if I call for that function first?

I made one modbus function with 16 (registers read output holding)  and then checked modbus diagnostics and found a bunch of 1-0s in a row for every Reg?

0
0
0
0
1
0
0
0
0
1
1
1
0
0

Something like that? And they changed when I started the Spindle / changed frequency? Any idea what it could be? Binary?
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 24, 2017, 02:28:24 PM
Hi Zuxztah,
not sure but don't think so. You may have noted in one of the examples included in the communication pdf you posted
it wrote to and address 2102 hex. Can you find anywhere in the lists such an address? I can't and yet it used as an example,
ie it must exist. My guess it that if you wish to write to the parameters, ie set some feature of the VFD, then you use the
command address as listed. If you wish to read data from the VFD addresses will be in the range 2100 hex to who knows.

This is from my Delta VFD manual:
Code: [Select]
2103H Output frequency (H)
2104H Output current (A*********.X)
2105H DC-BUS Voltage (U*********.X)
2106H Output voltage (E*********.X)
2107H Step number of Multi-Step Speed Operation
2108H Step number of PLC operation
2109H Content of external TRIGGER
210AH Power factor angle
Status
monitor
Read only 210BH Estimated torque ratio (*********.X)
210CH Motor speed (rpm)
210DH PG pulse (low word) /unit time (Pr.10-15)
210EH PG pulse (high word) /unit time (Pr.10-15)
210FH Output power (KW)
2110H Reserved
2200H Feedback Signal (*********.XX %)
Chapter 5 Parameters

Note the address block of 2100 hex to 2200 hex. I very much suspect your VFD is the same.

Still recommend you don't get hung up on it. You stated earlier that you had this VFD working over Modbus with Mach3 but without speed
feedback. With current knowledge you don't have speed feedback in Mach4 either, big deal.

You still have to write the LUA scripts necessary to give you programmatic control of your spindle.

To date you have really only used the Modbus and Refile plugins to set up your 485 comm link. You have yet to write any code and I suspect
you will find it rather harder and more confusing than what you have done to date. Don't get hung up on a detail.

Craig

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 24, 2017, 03:28:41 PM
Hi, That's true, I'll probably never sort the lua script out by my self, probably in a Year or two with a hard time learning and long nights in front of the computer, but it doesn't feel like it's worth the effort, it's worth it but at the same time it feels like I reinvent the wheel? I know that it will be useful in the future for other projects and such. I know that there is people with knowledge who is glad help :) like you! All the effort you already have put in to this is incredible! :)
 
the basic modbus functions is working and the script is not, I want to learn but have tried many many times, arduino, python and other languages and It feels like I never learn anything? Especially after I got sick.
my brain is totally corrupted.. the unknown disease should be named "Teflon brain" with a hint of failing nervous system and bad memory, haha. And that's a True fact.
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 24, 2017, 05:21:01 PM
Hi Zuxztah,
I have the same problem with less organic excuse.

The good news tho is that you only need four separate chunks of code each no more that half a dozen lines long. I don't claim to be
good enuf to write them and have them work straight off aside from which you would learn nothing in the process.

With the Program Run Tab on screen with Mach4 disabled, click Edit Screen on the Operator menu.
Click the Spindle CW/Stop button on the right hand side of the page. This should highlight the 'button code' in the screen elements tree on
the upper left and will show either the 'properties' or 'events' of the button at the lower left. With the 'properties' displayed you can change colours,
sizes, texts, enabled states and all that sort of stuff.

What I want you to have a look at tho is the 'events' associated with the button. You will note, assuming the default screen, that the left-up script
is the function 'SpinCW()'. The 'left-up' means the up-click event of the lefthand mouse button whereas the 'left-down' refers to the down-click event.
If you click on that and click on the rightmost extension button an editor window will open with the script for the button.
In this case it's pretty boring, it just the function SpinCW(), but notice the code underneath, all commented out but is in fact a copy of the code which
runs when SpinCW() is called. You may note that all it does is either set the spindle direction signal to off or set it to 1, ie CW. This won't work for you
because your using Modbus to control you spindle. You will have to substitute SpinCW() with code that turns your spindle CW at a given speed.

Just as a matter of interest the LUA editor display window is just a fraction of the LUA SCRIPT. All of the buttons, LEDs, DROs and so on all have their
own little bits of code associated with them. They are all gathered up into one script, the LUA SCRIPT, and it is that conglomerate which runs. You can view
the LUA script on the Oeratrator menu 'Lua Script'. Its quite a sizeable file, about 1000 lines on my machine. In there you will find the Screen load script, Screen
Unload script, Signal script and others besides. Over a period of time as you customise your machine, like your Modbus control, your code will end up
in this file as well. You can view it but you can't run or edit it. If you wish to make alterations you will have to open each button or what have you and edit
its script/code there. Note also that if you have say a button script open in the LUA editor then the conglomerate file can't be bought together because a
piece of it is still open in the editor. If you wish to run the code you need to save any edits and commonly close the editor window before the complete LUA
script can be compiled and run. Don't worry you will forget from time to time and Mach4 will hang as a result. You will have to close Mach4, sometimes
with the help of the 'Task Manager,' and reopen.

Open 'Lua Script' per the above and have a look at lines 136-165. These are the 'event scripts' for the two spindle buttons on screen.

With the left-up script in the Lua Editor rather than delete SpinCW() just comment it out, ie put '--' in front of it. Means that if you wish to unwind what you've
done at a later date its easy to do.

Type in this code (before or after the comments doesn't matter):
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
      local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
      mc.mcRegSetValue(hregcontrol,01)      -- substitute 01 with whatever bit pattern is required to turn the spindle clockwise
      local hregfrequency=mc.mcRegGetHandle(inst,"modbus0/freq")
      mc.mcRegSetValue(hregfrequency,2000)   -- the value 2000 ie 200Hz will as this script grows and gets more sophisticated will be variable but for now....
else
      local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
      mc.mcRegSetValue(hregcontrol,,00)   - substitute 00 with whatever bit pattern is required to stop the spindle
end

Please note that I haven't tested this code, without a working 485 link I can't. Note also that some changes could be made to make it better but
try and read it thru and understand what is trying to do.

If sigState=1 (it must have been 0 before), then we want to turn the spindle on, ie update your two Modbus registers to turn the spindle CW and set
a  frequency. If sigState~=1 (it must have been 1 before), then we want to turn the spindle off.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 24, 2017, 05:51:47 PM
Hi :)

Seems pretty easy when you explain, I understand the part with edit button script and so on, but the code writing is the really hard plart for me, I understand the basics of the code when I read it but typing it from scratch is another problem much like me an English, I understand  the language pretty good but trying to talk or write English is damn hard sometimes, the grammar and so on Is pretty messed up heheh. I'll follow your posted guide here tomorrow after work and see if I get any result :)

By the way I'm running the vx routerset (screen set) if it makes any difference, I know that I followed an old guide to make limit switches to work properly in Mach 4 with ESS (Ethernet Smooth Stepper) the screen load script was different, may be caused of an erlier build/ version of Mach.

For time being it won't listen to "S command"  (S18000 M3)? it's necessary Edit the script for calculating the Spindle rpm from frequency, basically it will listen to S1000-4000 if the script is set to transmit to 8193 function via modbus0/freq

Small steps to success :) ill buy the Mach 4 license tomorrow, didn't know before if it was worth it because of lack of knowledge but now it seems like there is a solution on its way :)

Either way it's worth it because Mach 3 no longer have any updates, feels like it's worth upgrading to get updates/fixes  and reliability :)

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 24, 2017, 06:11:32 PM
Hi Zuxztah,
I adopted the approach I did so that I could explain how it works and give you the chance to learn it too.

Glad that you are familiar with the Edit Screen function, you are going to use it a lot while you get this setup.

You are also correct that as I've written it spindle speed is an issue. I did it this way for simplicity... at the beginning...
it will change!

What will evolve is two small button scripts for the two buttons, three m functions; m03, m04 and m05 that take over from
the standard Mach4 functions by the same name and another small piece of code in the PLC script to maintain spindle speed
in step with your Gcode and/or MDI.

Let me know when you've digested and maybe got that previous lot of code working and will make a start on the rest. Good news
is if you understand what I've done then the rest will be easy.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 26, 2017, 07:55:46 AM
Hi! I've tried the script now, had to change some small things to get it to compile,  :)

Code: [Select]
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
      local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
      mc.mcRegSetValue(hregcontrol,10)      -- substitute 01 with whatever bit pattern is required to turn the spindle clockwise
      local hregfrequency=mc.mcRegGetHandle(inst,"modbus0/freq")
      mc.mcRegSetValue(hregfrequency,1000)   -- the value 2000 ie 200Hz will as this script grows and gets more sophisticated will be variable but for now....
else
      local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
      mc.mcRegSetValue(hregcontrol,01)   -- substitute 00 with whatever bit pattern is required to stop the spindle
end

but when I comment out SpinCW () with (--) it won't trigger the green led bar in Mach anymore, and the script gives no response, note that I edited the button Left Up Script,

tried to change the Reg file to add modbus0/spinCntrl and modbus0/freq

the Operator tab (Lua Script) doesn't seem to implement anything from the button script

Is it really Decimal value the script/mach wants? maybe it's hex?

I'll add some pictures for you to look at if I'm getting it all right, probably not,

https://postimg.org/gallery/1wc50bmou/


Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 26, 2017, 10:32:30 AM
Hi!
Here's a video on the functions I got so far. by editing Regfile values, so that's what we need to link the button scripts to,

https://youtu.be/YzcnwxyAG0Q

Watch out for the Volume in the video, sounds terrible, (runout vibrations/ Unbalanced endmill)
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 26, 2017, 04:33:14 PM
Hi Zuxztah,
struggling to get it to work myself.

As it turns out if you execute:
mc.mcSpindleSetDirection(inst, 1) the CW LED operates,
mc.mcSpindleSetDirection(inst, -1)  the CCW LED operates and
mc.mcSpindleSetDirection(inst, 0)  both LEDs extinguish.

So getting the LEDs to work it just a case of executing one of the above at the right time.

What I really want is three customised macros m03, m04 and m05. I've written the macros and they work when debugging
but they do not 'overwrite' the Mach4 internal functions of the same name as I expected. I've put out a call for help on another
thread.

The CW button script will look like this:
Code: [Select]
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then;
    local rc = mc.mcCntlGcodeExecute(inst,"m03");
else;
    local rc = mc.mcCntlGcodeExecute(inst,"m05");
end;

Once I've worked out how to call the customised m03 (or m05 as the case may be) I post back.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 26, 2017, 05:53:00 PM
Hi Zuxztah,
think I may have some working code:
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 26, 2017, 06:05:58 PM
Hi Zuxztah,
whoops, mistroke...

As I was saying I may have some working code for you to try...

Substitute your CW button left-up script with:

Code: [Select]
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then;
    local rc = mc.mcCntlGcodeExecuteWait(inst,"m5");
else;
    local rc = mc.mcCntlGcodeExecuteWait(inst,"m3");
end;

Substitue your CCW button left-up script with:

Code: [Select]
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then;
    mc.mcCntlGcodeExecuteWait(inst,"m5");
else;
    mc.mcCntlGcodeExecuteWait(inst,"m4");
end;

Now you need three macros in your Profile/Macros folder...

Named m3:

Code: [Select]
function m3();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/spinCntrl");
mc.mcSpindleSetDirection(inst,1);
mc.mcRegSetValue(hcont,10);
end
if (mc.mcInEditor() == 1) then
    m3()
end

Named m4:
Code: [Select]
function m4();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/spinCntrl");
mc.mcSpindleSetDirection(inst,-1);
mc.mcRegSetValue(hcont,10);  -- substitute 10 with whatever number is required to make your spindle run CCW
end
if (mc.mcInEditor() == 1) then
    m4()
end

Named m5:

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 26, 2017, 06:11:39 PM
Hi,
done it again...

Named m5:
Code: [Select]
function m5();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/spinCntrl");
mc.mcSpindleSetDirection(inst, 0);
mc.mcRegSetValue(hcont,01);
end
if (mc.mcInEditor() == 1) then
    m5()
end
.

Once you've got these installed you should be able to turn the spindle on and off from the CW or CCW buttons on the screen or
alternately from calling m3/m4/m5 from MDI or Gcode.

Have not as yet set the speed, will work on that shortly.

Craig

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 27, 2017, 04:33:32 AM
Hi Zuxztah,
last instalment of code.
This needs to go at the end (or near the end) of your PLC script:
Code: [Select]
---------------------------------------------------------
--          set Target RPM DRO
---------------------------------------------------------
RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,0) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,0) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));

Note that I have a DRO (dro(128)) that I display the target RPM. Also I used pound variable 2132 to get the current commanded speed,
there is an API function but it didn't work for me.

Just as a matter of interest do you use the 'Always on Top' feature of the Diagnostics/Regfile expand modbus0? Its the pin icon, click it
and you can view your two Modbus registers as you fiddle with the buttons/MDI/Speed_OverRide. Really handy.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 27, 2017, 04:40:09 AM
Hi,
see an error, try this instead:
Code: [Select]
RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));

Didn't have 'range' set correctly in the second mcSpindleGetMaxRPM(inst,range) or mcSpindleGetMinRPM(inst,range)  statements.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 27, 2017, 04:46:37 AM
Hi Zuxztah,
might be able to take  :( off your original post and swap it for  :)

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 06:43:19 AM
Hello! super big thanks for all this effort! I don't know how to thank you enough! this looks very promising :)  I'm currently adding all those scripts :) trying to figure out how to add macros to the macro folder, I know the directory but not what extension the file should have? or if its possible to edit it trough mach software itself?

I guess the file shouldn't have an extension name like .txt

The directory C:\Mach4Hobby\Profiles\Mach4ESS\Macros only have one file, mcLua.mcc and it's not possible to edit the file trough mach (Edit/Debug scripts.) due to the extension name.

The Edit/Debug scripts function do only allow .mcs .lua .wx.lua formats.

at the moment when I press Spindle CW / Stop and Spindle CCW / Stop the program binds up and needs to be restarted probably due to the missing macros (m3-m4-m5) ill see if I manage to sort it out :) hopefully before you read this post XD


(I do actually have the Reg file diagnostics window always on top) to determine if the modbus values changes while pressing the buttons



Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 07:39:22 AM
Hi! managed to add m3. m4. m5 macros, but no response in the Register Diagnostics? I think the "code" may be wrong in the part of finding the right register/path?

at the moment the code is:

Code: [Select]
local hcont=mc.mcRegGetHandle(inst,"modbus0/spinCntrl");
what if the path is wrong? if maybe the code should look something like this:

Code: [Select]
local hcont=mc.mcRegGetHandle(inst,"modbus0/modbus0/spinCntrl");
it's all just a wild guess, but it feels like the script/code isn't finding the right path?

first of, the registry path: modbus0 (named Modbus Function) (the expandable register) and then the function modbus0/SpinCntrl: gets equal to modbus0/modbus0/SpinCntrl:0

if we hade a custom registry we would have to put the path for iRegs0 and then the function?

Example: iRegs0/Test1 in the Scripting manual

the pictures explain what I mean, it's probably not working because of the name I chose to use with my modbus setup? (modbus0)

Pictures: https://postimg.org/gallery/1zsvad7ha/

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 02:08:10 PM
Hi Zuxztah,
looking at your screenshot I would say you are correct. You must use the register path as you named them. Why did you name them that way?

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 02:37:19 PM
It works now! :) I Edited the modbus configuration and also found out that I had a uppercase letter on SpinCntrl that was the problem:)

Video of it working:
https://youtu.be/yTqMH4h2jYE

Now to the last script that you told me to place close to the end in the PLC script? where do I put it? in Lua script?

Code: [Select]
RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));



Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 02:41:06 PM
Why did you name them that way?

I have absolutely no idea, I'm a newbie you know xD
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 02:53:55 PM
Hi, I did just found out that the m3,m4,m5 commands doesn't listen to E-Stop? When the Spindle is running it continues to run even if I press the physical E-stop or "Disable" button in Mach,

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 02:55:11 PM
Hi Zuxztah,
with the screen editor open highlight the very very topmost item of the Screen Tree Manager, your screenset, presumably one you named.
Now open the Events tab and the various scripts are listed, including the PLC script. Open it in the editor as you would a button, ie click on it
and then the rightmost extension.

Scroll down to the last statements of script:

--This is the last thing we do.  So keep it at the end of the script!
machStateOld = machState;
machWasEnabled = machEnabled;

Clearly stated these must be at the end of the script. I put the speed code immediately in front of those:

Code: [Select]
---------------------------------------------------------
--          set Target RPM DRO
---------------------------------------------------------
RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));


--This is the last thing we do.  So keep it at the end of the script!
machStateOld = machState;
machWasEnabled = machEnabled;

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 03:09:44 PM
It seems to be working with S????? Command but not with the slider/SRO% functions on Mach screen,

If I send MDI

S12000 M3 it spins up at correct rpms (the vfd display shows 12000rpm) and if I change the S value it changes speed, but still no response or numbers in Mach (Spindle sliders/SRO%)
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 03:51:58 PM
Hi Zuztah,
couple of issues.

Easy one first: you may note in the code in the PLC script that sets the speed I put the following test:

local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;

My guess is that your machine does not have the Spindle OverRide enabled. Either delete the test or manually write the SpindleOverrideEnable bit:

mc.mcSpindleSetOverrideEnable(inst,1);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;

The next issue is a little more serious namely that the spindle doesn't turn off.

In Screen Edit mode have a look at the properties of the two screen buttons...
near the top of the properties list there are entries;

Enabled
Hidden
Enabled with Machine
Enabled States    -- which can be expanded.

Obviously you want the buttons enabled so check Enabled
Uncheck Hidden  -- so you can see it!
Uncheck Enabled with Machine  -- ie the buttons don't work with the machine disabled
Expand Enabled States and check ALL or individually check the states for which you wish the buttons to operate. You will have to experiment with
those to work out what they all mean, I sure as hell don't.

These properties define when the button works or not. It does not solve the problem of the spindle not responding to the Estop.
I think it may be necessary to modify the 'Disable' script to turn the spindle off. I will experiment some more and report back.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 04:27:21 PM
hello! :) It didn't work with
Code: [Select]
RPM=mc.mcSpindleSetOverrideEnable(inst,1);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;

or Without the RPM= at the beginning


But With this code it works, it listens to the S-Commands, but shows no RPM in the SRO window, If the SRO is supposed to show the running RPM,
Code: [Select]
RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));

I think the states of the buttons are correct, when I press disable then I'm not able to start the spindle, neither to send any commands via MDI, but if I press disable or press my physical E-Stop when the spindle is on the spindle continues in the same state as before.¨

The button scripts for SRO%:
On Update Script and
On Modify Script it's just empty,

I don't know if you also run wxRouter.set screen set? its different from the others, and probably only scripted for basic Analog functions?  
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 04:33:06 PM
Hi, Somehow the Spindle-Override started working, all the buttons work now.  :) :) ;)

The only problem is that SRO% stays put at 0 and the E-Stop response.
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 04:35:00 PM
Hi Zuxztah,
not entirely surprised, look at what you've witten:

 RPM=mc.mcSpindleSetOverrideEnable(inst,1);

You have set the RPM to the return code of the mcSpindleSetOverrideEnable function which doesn,t make any sense at all.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 04:40:26 PM
It's still using the first code you sent, I tried with and without RPM= For the latest code but didn't work,
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 04:52:53 PM
Hi Zuxztah,
I have a new DRO on my screen, I call it the Target DRO, ie the speed I hope the spindle will achieve. The DRO you are talking about is included
in the default Mach4 screen and is called 'droTrueSpindle'.

Note also the code I used to write to my DRO:
scr.SetProperty('dro(128)','Value',tostring(RPM));

ie Mach4 regards or calls my custom DRO  'dro(128)'. My DRO stays live all the time and tells me the speed the spindle will get to if I turn it on
whereas the droTrueSpindle tells me what speed its doing at the moment. If the spindle is not running then it will be zero.

The Estop and in fact a couple of other functions require some consideration. Firstly if you have a look at the "Cycle Stop()" function (in the LUA script):

---------------------------------------------------------------
-- Cycle Stop function.
---------------------------------------------------------------
function CycleStop()
    mc.mcCntlCycleStop(inst);
    mc.mcSpindleSetDirection(inst, 0);
    mc.mcCntlSetLastError("Cycle Stopped");
end

note that it 'stops' the spindle with the mc.mcSpindleSetDirection(inst, 0); statement. It won't stop your spindle tho, you have to change the
spinCntrl register to stop it. Your m5 macro will do it or wherever you require a spindle stop you will have to code a spinCntrl register update
to stop it.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 05:08:12 PM
Hi Zuxztah,
have you enabled the spindle rate over-ride, I suspect not. Also have you set up at least one range in Machs Spindle page?, I suspect not.
Without it the max and min spindle speeds are both zero. Have a look at the screen shot.

For the required spindle over-ride enable try this code:
Code: [Select]
---------------------------------------------------------
--          set Target RPM DRO
---------------------------------------------------------
RPM=mc.mcCntlGetPoundVar(inst,2132);
mc.mcSpindleSetOverrideEnable(inst,1);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/freq");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));


Whether you chose to add dro(128) is up to you.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 05:25:38 PM
Yes my Mach configuration looks identical, I added a new Dro window, and named it dro(128) instead of 25

And It seems to work perfect :) it shows target rpm :) and the overide seems to be working, it's only the E-stop that's living it's own life now :)
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 05:27:04 PM
Hi Zuxztah,
just noticed that the screen shot shows my custom DRO at the lower right hand corner. Note it reads 18000, my commanded or target spindle
speed but the TrueSpindle DRO above and slightly to the left of it shows zero. This IS as anticipated, the spindle is not running...

Also note that I set range 0 to have a min speed of 6000 rpm. My spindle is air cooled and it gets hot if you try to run it slow.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 05:50:41 PM
I understand what you mean, my minimal is also 6000 rpm max 24000rpm, it's not healthy to run those spindles under that,

I understand that 6000 = 0 but mine doesn't change at all, and by that I mean the SRO window, it's always 0
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 05:53:17 PM
Hi Zuxztah,
now thinking about the Estop.....

Certainly as a safety measure the spindle most stop when the Estop is activated. Many users have a totally separate electrical system that kills
the power supply to the spindle and commonly the servo/stepper drivers as well. I myself do not use that system but rely on Mach to shut the
spindle down. There is some philosophical debate about that. I suspect if you were using this machine in an industrial situation then an 'automatic'
electrical supply shutoff would be legally required.

In absence of such a setup then you are going to require Mach to do something specific to shut the spindle off.

Further Estop is not the only time you want to turn the spindle off. Consider what you expect to happen if you hit the 'Enable/Disable' button,
do you expect the spindle to stop?. I do. What about the 'Reset' button? The standard script which accompanies the button shuts the spindle
down using the same statement as above which of course will not work for your setup, ie Modbus controlled spindle. What about the 'FeedHold'
button?. Do you expect the spindle to stop then?. I do not want it to stop under FedHold but maybe you do.

To my knowledge the 'Enable/Disable' and 'Reset' buttons use the CycleStop() function. Two ways of achieving it are
1) Modify the CycleStop() function to include Modbus shut-off
2)Write your own function instead of CycleStop()

I'm not aware of any argument that suggests one is better than another.

That still does not address the Estop set up however. The only way I can think of to ensure the spindle stops when an Estop signal is
encountered is to 'trap' the signal with either the signal script and/or signal table and execute a spindle shutdown function.

What do you think?

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 06:04:38 PM
I've hooked up the Physical E-stop to kill the enable signal to the stepper drivers and to tell mach that E-stop is triggered, it would be a good idea to do that for the spindle also, but I don't know if it's a good idea to cut the power? I think about the internal electronics in the VFD? maybe it won't harm the electronics?

it's maybe not as bad as I think, it would be one thing if I had a brake (resistor) on the spindle, available to buy for my VFD,

Feed hold doesn't need any Spindle action? I'm not used to it anyway,

the 1 opinion seems like a good idea, (Modify the CycleStop() function to include Modbus shut-off)

by that you mean to send the stop command to VFD? (decimal 01 to 8192)


Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 06:19:59 PM
Hi Zuxztah,
I rather questioned the idea of shutting the power off the VFD as well. The manufacturer doesn't recommend it, the manual says to stop
the motor using the normal commands and THEN depower.

If you require an 'Emergency Shutdown' then you have to program you VFD to achieve that and will almost certainly require a brake resistor.

Quote
by that you mean to send the stop command to VFD? (decimal 01 to 8192)

Yes. Alternately you could code:
mc.mc.mcCntlGcodeExecuteWait(inst,"m5");
provided 'inst' is in scope at that point.

E-stop is a valid input signal but I can't find the ENUM for it just at the moment. May have to call on wiser heads.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 28, 2017, 06:30:06 PM
I have a small idea! there is a run/stop Membrane switch on the VFD, and I have an external panel. it is probably easy to solder 2 wires from a relay to trigger it when E-stop is switched via an output from mach I/O

The VFD parameters are programmed to stop the spindle no matter what if I press that button.

that's the easiest solution and it's not hard on the electronics

but it would be great if it was doable to fix it in the software?

what about:

Yes. Alternately you could code:
mc.mc.mcCntlGcodeExecuteWait(inst,"m5");
provided 'inst' is in scope at that point.

in the m5 macro?


Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 28, 2017, 07:32:22 PM
Hi Zuxztah,
I think its doable in software but can't get it to work at the moment. Will come back to it later when fresh.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 29, 2017, 05:16:31 AM
Hi Zuxztah,
made some progress with the help of Daz-the-Gaz and Dude1....

The left-up script for the Stop button
Code: [Select]
Shutdown();
CycleStop();

The left-up script for the Reset button
Code: [Select]
Shutdown();
local inst = mc.mcGetInstance()
mc.mcCntlReset(inst)
mc.mcSpindleSetDirection(inst, 0)
mc.mcCntlSetLastError(inst, 'diagReset')

And this in the ScreenLoad script
Code: [Select]
---------------------------------------------------------------
--  Spindle Shutdown
---------------------------------------------------------------
function Shutdown();
    local inst=mc.mcGetInstance();
    mc.mcScriptExecute(inst,"m5.mcs",0);
end;
---------------------------------------------------------------
-- Cycle Stop function.
---------------------------------------------------------------
function CycleStop()
    mc.mcSpindleSetDirection(inst, 0);
    mc.mcCntlSetLastError(inst,"Cycle Stopped");
    mc.mcCntlCycleStop(inst);
end

---------------------------------------------------------------

And add this left-up script to the Enable/Disable button
Code: [Select]
Shutdown()
Not sure yet whether an Estop trap is required. Need to get to my machine, maybe later in the week.

Craig
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 29, 2017, 05:35:58 AM
Hi. It seems to be working good!  ::)
The physical estop won't stop the Spindle, it triggers the cycle stop button, and shows the message (E-Stop condition!)

Stop button stops the Spindle, I'll have to press Disable/Enable to get the Spindle running again,(with m3 or m4 command/button afterwards)
Reset button stops the Spindle and I'm able to start it again with m3 & m4 command/button

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: joeaverage on January 29, 2017, 05:45:46 AM
Hi Zuztah,
kool, awfully close now. Just an E-stop trap and you're done! If you had known at the outset what it was going to take would
you have bothered?

Probably need to write up the process, at least as a guide rather than a blow by blow account so others can follow it.

Craig

Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on January 29, 2017, 06:01:42 AM
Hi, it's pretty easy but complicated at the same time, the code seems to be pretty simple if I had any programming experience beforehand, it is certainly worth the effort :) there will be other people that will encounter the same problems and the documentation gets  richer and richer:)

I don't know why, but I'm always choosing the hardest path to learn things, and it has for sure been fun and I've learned a lot on the way,

If you want you could write a guide, it would be much easier for other guys that's experimenting, my English isn't the best and try to write one would be disaster xD
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Zuxztah on February 05, 2017, 06:34:06 AM
Hi!
Wired the physical E-stop to my VFD control panel button and it seems to work great :)
It behaves just like it should, smooth and nice deceleration and the only required action to get it running again is to press enable and then reset  ;)

but now I have another problem... came with no reason? haven't changed anything by mistake... my Z-probe touch of won't work? no response at all when clicking the touch button in Mach Router screens, and when I close the program a fault code shows up on the screen.

the attached photo shows the fault code...


Title: Re: Mach 4 Automation Direct VFD Modbus
Post by: gidyupp on January 03, 2018, 03:17:15 PM
Hi,
I'm replying to this older thread as I used it as a guide to get an Automation direct VFD working via Modbus.  I had it working with Mach3 and struggled with Mach 4.  I figured I'd summarize the steps I took so future readers may have it a bit easier.  I'm no programmer and I have only a vague understanding of script code, so I probably won't be the best guy to offer support.

This is all based upon the work of joeaverage and Zuxztah, I appreciate the help!

First off, I set up the modbus (Configure>plugins>Modbus) as follows:
(https://www.dropbox.com/s/3t6zimggzzedzyw/modbus1.jpg?raw=1)
(https://www.dropbox.com/s/d87wf8addndgjyz/modbus2.jpg?raw=1)
(https://www.dropbox.com/s/b8zb3qgh612kw9k/modbus3.jpg?raw=1)

The registers for my particular VFD are:
2331  VFD Speed in Hz (includes an implied decimal place, so 4000 = 400Hz)
2332  VFD run command (0=stop, 1=run)
2333  VFD direction command (0=fwd, 1=rev)

8456  Feedback of actual motor speed in RPM (not required, I'm working on a DRO to display this)

Before you do anything else, make sure modbus comms are working by looking at the modbus diagnostic window (Diagnostic>Modbus-Newfangled Solutions)
The following scren shot shows my system running in reverse at 24000 RPM (400HZ):
The green indicators are good, if anything is red you need to fix it.
(https://www.dropbox.com/s/g0hq99lzz2ent4x/modbus4.jpg?raw=1)

To make the above communication work, you need to modify the following paramiters in the VFD (via the VFD keypad, this is straight out of the VFD manual):

P3.00: 03 or 04 Operation Determined by RS-485 interface.
Keypad STOP is enabled (03) or disabled (04).

P4.00: 05 Frequency determined by RS-485 communication
interface.

P9.00: xx Communication address 1-254

P9.01: 01 9600 baud data transmission speed

P9.02: 05 MODBUS RTU mode
<8 data bits, odd parity, 1 stop bit>.

To be continued....
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: gidyupp on January 03, 2018, 03:29:59 PM
Part 2:
The code to make it work
NOTE: SOME OF THIS HAS BEEN MODIFIED FROM THAT POSTED EARLIER IN THIS THREAD TO WORK FOR MY SITUATION, PARTICULARLY THE NAMES I GAVE THE MODBUS REGISTERS AND THE FACT THAT MY VFD USES SEPARATE REGISTERS FOR RUN AND DIRECTION. Again, I'm no programmer so if someone sees an error, please let me know.
 
Replaced leftup code for CW/Stop button:
Code: [Select]
--SpinCW()
--local inst = mc.mcGetInstance();
--local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
--local sigState = mc.mcSignalGetState(sigh);
--if (sigState == 1) then
--    mc.mcSpindleSetDirection(inst, 0);
--else
--    mc.mcSpindleSetDirection(inst, 1);
--end

local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then;
    local rc = mc.mcCntlGcodeExecuteWait(inst,"m5");
else;
    local rc = mc.mcCntlGcodeExecuteWait(inst,"m3");
end;

Replaced leftup code for CCW/Stop button:
Code: [Select]
--SpinCCW()
--local inst = mc.mcGetInstance();
--local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
--local sigState = mc.mcSignalGetState(sigh);
--if (sigState == 1) then
--    mc.mcSpindleSetDirection(inst, 0);
--else
--    mc.mcSpindleSetDirection(inst, -1);
--end

local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then;
    mc.mcCntlGcodeExecuteWait(inst,"m5");
else;
    mc.mcCntlGcodeExecuteWait(inst,"m4");
end;

Added line to leftup code for stop button:
Code: [Select]
Shutdown();
CycleStop();

Added line to leftup code for reset button:
Code: [Select]
Shutdown();
local inst = mc.mcGetInstance()
mc.mcCntlReset(inst)
mc.mcSpindleSetDirection(inst, 0)
mc.mcCntlSetLastError(inst, 'diagReset')

Added lines to the end of the screenload script:
Code: [Select]
---------------------------------------------------------------
--  Spindle Shutdown
---------------------------------------------------------------
function Shutdown();
    local inst=mc.mcGetInstance();
    mc.mcScriptExecute(inst,"m5.mcs",0);
end;
---------------------------------------------------------------
-- Cycle Stop function.
---------------------------------------------------------------
function CycleStop()
    mc.mcSpindleSetDirection(inst, 0);
    mc.mcCntlSetLastError(inst,"Cycle Stopped");
    mc.mcCntlCycleStop(inst);
end

---------------------------------------------------------------

Added to the Up Script for the enable button:
Code: [Select]
Shutdown()

Added lines to the end of the PLC script
This was added after the existing “end” statement and before the “--this is the last thing we do….” Line:
Code: [Select]

---------------------------------------------------------
--          set Target RPM DRO
---------------------------------------------------------

RPM=mc.mcCntlGetPoundVar(inst,2132);
local OVRenable=mc.mcSpindleGetOverrideEnable(inst);
  if OVRenable then;
    local OVR=mc.mcSpindleGetOverride(inst);
    RPM=RPM*OVR;
  end;
local range=mc.mcSpindleGetCurrentRange(inst)
if RPM>mc.mcSpindleGetMaxRPM(inst,range) then RPM=mc.mcSpindleGetMaxRPM(inst,range) end;
if RPM<mc.mcSpindleGetMinRPM(inst,range) then RPM=mc.mcSpindleGetMinRPM(inst,range) end;
local freq=RPM/6;
local hfreq=mc.mcRegGetHandle(inst,"modbus0/speed");
mc.mcRegSetValue(hfreq,freq);
scr.SetProperty('dro(128)','Value',tostring(RPM));

Added three macro files to the Profile/Macros folder:
m3.mcs:
Code: [Select]
function m3();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/run"); --run command (0=stop, 1=run)
local hcont1=mc.mcRegGetHandle(inst,"modbus0/direction");--added for direction control (0=fwd, 1==rev)
mc.mcSpindleSetDirection(inst,1);
mc.mcRegSetValue(hcont,1); --set to 1 to run
mc.mcRegSetValue(hcont1,0); --set to 0 for fwd
end
if (mc.mcInEditor() == 1) then
    m3()
end

m4.mcs:
Code: [Select]
function m4();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/run");--run command (0=stop, 1=run)
local hcont1=mc.mcRegGetHandle(inst,"modbus0/direction"); --added for direction control (0=fwd, 1==rev)
mc.mcSpindleSetDirection(inst,-1);
mc.mcRegSetValue(hcont,1); -- set to 1 to run
mc.mcRegSetValue(hcont1,1); --set to 1 for reverse
end
if (mc.mcInEditor() == 1) then
    m4()
end

m5.mcs:
Code: [Select]
function m5();
local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/run");
mc.mcSpindleSetDirection(inst, 0);
mc.mcRegSetValue(hcont,0);
end
if (mc.mcInEditor() == 1) then
    m5()
end

NOTE: I do not know if the hardwired E-stop function will stop the VFD through software.  Mine has a hardwired E-stop connected to the VFD to force it to shutdown so I'm set.  If your's is not hardwired to the VFD you may need to experiment.

I think that's all of it.
Good luck!
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: gidyupp on January 03, 2018, 03:40:13 PM
One thing I should have mentioned, The automation Direct VFD uses Modbus decimal registers starting at the standard address of 40,001, M4 assumes the address start at 40,000 so you only enter the index number, so 40001 becomes "1", 42331 becomes "2331", etc.  In other words, subtract 40,000 from the addresses shown in the AD manual.
Title: DRO Modbus Question
Post by: gidyupp on January 03, 2018, 03:49:06 PM
Now for a question for the Gurus:
I am trying to add a DRO to show the actual spindle speed as reported by the VFD.  As shown a couple of posts above, I have a modbus register setup (modbus0/actualRPM) that is working, but I need help with the code.
I have a DRO created named 'droTrueSpindle', and have the following code at the end of the PLC script:

Code: [Select]
local vfdspeed=mc.mcRegGetHandle(inst,"modbus0/actualRPM");
scr.SetProperty('droTrueSpindle','Value',tostring(vfdspeed));

What am I doing wrong?
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Xillianto on January 09, 2018, 09:26:22 AM
I have been checking that reset button, and it doesnt stop my spindle, only lowers RPM to 6000 for some reason.

So i added to the button when clicked


local inst=mc.mcGetInstance();
local hcont=mc.mcRegGetHandle(inst,"modbus0/cntrl");
mc.mcSpindleSetDirection(inst, 0);
mc.mcRegSetValue(hcont,01);

This tells spindel to stop on my VFD, might need to adjust to your VFD's (mactric VFD)

Steppes i have done as to solving Emergency button:

Connected S2 VFD innput to a relay on my controller board (pokeys57cnc)
Set S2 as an emergency swith (P318 - option 17)

Relay is active high and set to react after if mach4 is active or not (If you push emergency button, Mach4 will go inactive)
If it is active, it turns on the relay, and ofc the other way around if not.
so if i turn off my controller, spindle will be locked out in Emergency mode.
Title: Re: Mach 4 Machtric 2.2kw VFD Modbus
Post by: Mantis3347 on February 09, 2019, 02:11:53 PM
As for stopping the spindle when the E-Stop is triggered, this code in the Signals Library of the Edit Screen WX6/ScreenLoadScript works for me:

[mc.ISIG_EMERGENCY]=function(state)
    if (state == 1) then
        mc.mcScriptExecute(inst,"m5.mcs",0);
   end
end,

With this code my Teco 7300CV under Modbus turns off the spindle with an M5 command from Mach4.

-rick