Hello Guest it is March 28, 2024, 04:39:24 AM

Author Topic: Scripts to control my VFD over Modbus  (Read 4588 times)

0 Members and 1 Guest are viewing this topic.

Scripts to control my VFD over Modbus
« on: August 13, 2015, 11:13:49 PM »
I have been fooling around for a couple of days, and finally got my Modbus VFD control working from Mach4.

I thought it might be useful to others to see the scripts that worked for me, though the parameters will probably be different for your VFD.

In screen Load Script:
(This section sets the VFD to be controlled by the modbus)

local hregCtrl = mc.mcRegGetHandle(inst, "modbus0/oCtrlMode")
mc.mcRegSetValue(hregCtrl, 2)
--set VFD control to Comms
local hregRef = mc.mcRegGetHandle(inst, "modbus0/oRefSel")
mc.mcRegSetValue(hregRef, 5)
--set VFD Reference to Comms
local hregCtrlWordEn = mc.mcRegGetHandle(inst, "modbus0/oCtrlWordEnable")
mc.mcRegSetValue(hregCtrlWordEn, 1)
--enable VFD Control Word


In PLC Script:
(This section takes the commanded spindle speed and converts it into a frequency value for the VFD)

local inst = mc.mcGetInstance()
local SPD = mc.mcSpindleGetCommandRPM(inst)
--what speed is currently called for
local cRange = mc.mcSpindleGetCurrentRange(inst)
--What Pulley range is currently active 0-19
local MaxRPM = mc.mcSpindleGetMaxRPM(inst, cRange)
--returns the MAX RPM for that pulley range
local Sdir = mc.mcSpindleGetDirection(inst)
local Dir = Dir
if (Sdir == -1) then
    Dir = -1;
else
    Dir = 1;
end
Dir = tonumber(Dir)
local Freq = SPD/MaxRPM*60*100*Dir
--60Hz is my max VFD Frequency; my VFD requires the frequency multiplied by 100
local hregFreq = mc.mcRegGetHandle(inst, "modbus0/oPreset1")
mc.mcRegSetValue(hregFreq, Freq)


In Signal Script:
(this ssection looks for Spindle FWD or REV commands and controls the VFD accordingly)

if ((sig == mc.OSIG_SPINDLEFWD) or (sig == mc.OSIG_SPINDLEREV)) then
    if (state == 1) then
        local inst = mc.mcGetInstance ();
        local hregCtrlWord = mc.mcRegGetHandle(inst, "modbus0/oCommsCtrlWord")
        mc.mcRegSetValue(hregCtrlWord, 8)
    else
        local inst = mc.mcGetInstance ();
        local hregCtrlWord = mc.mcRegGetHandle(inst, "modbus0/oCommsCtrlWord")
        mc.mcRegSetValue(hregCtrlWord, 0)
    end
end


In Screen Unload Script:
(This section sets the VFD back to front-panel control)

local hregCtrl = mc.mcRegGetHandle(inst, "modbus0/oCtrlMode")
mc.mcRegSetValue(hregCtrl, 0)
--set VFD control to keypad
local hregRef = mc.mcRegGetHandle(inst, "modbus0/oRefSel")
mc.mcRegSetValue(hregRef, 0)
--set VFD Reference to keypad
local hregCtrlWordEn = mc.mcRegGetHandle(inst, "modbus0/oCtrlWordEnable")
mc.mcRegSetValue(hregCtrlWordEn, 0)
--disable VFD Control Word
Re: Scripts to control my VFD over Modbus
« Reply #1 on: November 14, 2015, 04:07:04 PM »
Awander;
Thanks for posting. I plan to dig into this in a month or so for my machine.  What brand of VFD did you program this for?

Thanks.

Oh, assume the smiley face with glasses is "8)"

Re: Scripts to control my VFD over Modbus
« Reply #2 on: November 14, 2015, 08:07:33 PM »
yup, the forum auto-:corrected: the "eight, close parentheses"

My VFD is a Hopewind HV300.
Re: Scripts to control my VFD over Modbus
« Reply #3 on: November 17, 2015, 10:45:03 AM »
Andy, very cool but I have an addition that will make this nice and simple:

local maxrpm = mc.mcSpindleGetMotorMaxRPM(inst);
local rpm = mc.mcSpindleGetMotorRPM(inst);


with this "motorRPM" you can simply get the Max and use the current to set the value of the motor.. with this there is NO need to look at the range you are in. I think it was one of the better idea's i have had LOL

Tell me if that works for you please or if you need some more help from me.


Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com