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:
--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:
--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:
Shutdown();
CycleStop();
Added line to leftup code for reset button:
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:
---------------------------------------------------------------
-- 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:
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:
---------------------------------------------------------
-- 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:
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:
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:
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!