Hello Guest it is April 26, 2024, 01:29:10 AM

Author Topic: PLC Script not updating all registers?  (Read 354 times)

0 Members and 1 Guest are viewing this topic.

PLC Script not updating all registers?
« on: February 28, 2023, 03:21:12 PM »
Hi everyone,

I have a number of registers that I'm reading from my VFD (via modbus) that have some small scaling calculations done to them and eventually become values for a diagnostics tab in my screenset.

For some reason, only some of the code appears to be getting executed (the "Set target ROM and DRO" block).  What am I missing here? 

Code: [Select]
-- ************************* BrSp 02/22/23 **************************************
---------------------------------------------------------
--          set Target RPM and DRO
---------------------------------------------------------

RPM=mc.mcSpindleGetCommandRPM(inst);  -- Use mcSpindleGetCommandRPM
local hRPM=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/SetRPM");
mc.mcRegSetValue(hRPM,RPM);

--Spindle Override Check
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;

-- Calc and set the frequency to the VFD
local freq=RPM/6*10;  -- ie 24000 rpm /6 = 400Hz but WJ200VFD wants 4000 as input (no decimals)
local hsetHz=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/SetHz");
mc.mcRegSetValue(hsetHz,freq);
local hfreq=mc.mcRegGetHandle(inst,"MbVFD/W_VFDSpd_Hz");
mc.mcRegSetValue(hfreq,freq);

-- Get the actual RPM from the VFD
local aFreq=mc.mcRegGetHandle(inst,"MbVFD/R_VFDRPM_Hz");
local hActRPM=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/ActRPM");
local readBackRPM=aFreq/10*6;
mc.mcSpindleSetSensorRPM(inst,readBackRPM);
mc.mcRegSetValue(hActRPM,readBackRPM);

---------------------------------------------------------
--          scale VFD readbacks
---------------------------------------------------------

local iVolts=mc.mcRegGetHandle(inst,"MbVFD/R_VFDVolt_V");
local hVolts=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/Volts_S");
local readBackVolts=iVolts/10;
mc.mcRegSetValue(hVolts,readBackVolts);

local iPwr=mc.mcRegGetHandle(inst,"MbVFD/R_VFDpwr_KW");
local hPwr=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/KW_S");
local readBackPwr=iPwr/10;
mc.mcRegSetValue(hPwr,readBackPwr);

local iCur=mc.mcRegGetHandle(inst,"MbVFD/R_VFDcurr_A");
local hCur=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/Curr_S");
local readBackCur=iCur/10;
mc.mcRegSetValue(hCur,readBackCur);

local iTemp=mc.mcRegGetHandle(inst,"MbVFD/R_VFDTemp_C");
local hTemp=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/Temp_S");
local readBackTemp=iTemp/10;
mc.mcRegSetValue(hTemp,readBackTemp);

-- **************************************************************************

The Current value "R_VFDcurr_A" is a perfect example.  The value coming back from the VFD is 0, as is the register it is stored in.  But somehow, the Curr_S register is not being updated.  The script posted above is being executed correctly for the "SetRPM" and "SetHz" values.

Any thoughts?
If I set the initial values for the Curr_S register to a value, that value is quickly overwritten by the value you see and then is static.
Re: PLC Script not updating all registers?
« Reply #1 on: March 01, 2023, 08:30:43 AM »
Hi, it looks like you are not reading the value of the register before performing calculations. You are performing calculations on the handle of the register which is just a number.. hence why everything executes correctly.

See below

Code: [Select]
---------------------------------------------------------
--          scale VFD readbacks
---------------------------------------------------------

local iVoltsHANDLE=mc.mcRegGetHandle(inst,"MbVFD/R_VFDVolt_V");

local iVolts = mc.mcRegGetValue(inst, iVoltsHANDLE) ----------------Missing this

local hVolts=mc.mcRegGetHandle(inst,"iRegs0/MbVFD/Volts_S");
local readBackVolts=iVolts/10;                                  ---------------- Now calculations will be performed on the value of the register just read
mc.mcRegSetValue(hVolts,readBackVolts);
Re: PLC Script not updating all registers?
« Reply #2 on: March 01, 2023, 10:41:31 AM »
Awesome.  I knew it was something super simple!

Thanks
Re: PLC Script not updating all registers?
« Reply #3 on: March 05, 2023, 04:24:50 PM »
That worked a treat.

One related problem now:  Some of the registers that were created in modbus don't actually exist where they are supposed to be (or anywhere else)

My ini file showing where the registers should exist (take R_VFDalarm and R_VFDfOK for example)
Code: [Select]
[ModbusDevice]
[ModbusDevice/MbVFD]
Type=2
Interval=500
Retry=3
Timeout=100
EnronMode32=0
SwapIntWords=0
SwapFloatWords=0
UseBaseZero=1
InitialState=1
Desc=WJ200 VFD
IPAddr=10.9.9.77
TcpPort=502
Func0=function0
Func1=function1
Func2=function2
Func3=function3
Func4=function4
Func5=function5
Func6=function6
Func7=function7
Func8=function8
Func9=function9
Func10=function10
[ModbusDevice/MbVFD/function0]
Desc=r_Spinspd
Type=4
SlaveAddr=1
MbAddr=1
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=1
Res0=R_VFDSpd_Hz
[ModbusDevice/MbVFD/function0/R_VFDSpd_Hz]
Desc=Read Spindle Speed (Hz)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function1]
Desc=w_Spinspd
Type=6
SlaveAddr=1
MbAddr=1
NbrRegs=1
BitPack=0
Bits=0
ReadAs=0
InitialState=1
ScanDenominator=1
Res0=W_VFDSpd_Hz
[ModbusDevice/MbVFD/function1/W_VFDSpd_Hz]
Desc=Write Spindle Speed (Hz)
Type=256
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function2]
Desc=r_VFDTemp
Type=4
SlaveAddr=1
MbAddr=4120
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=5
Res0=R_VFDTemp_C
[ModbusDevice/MbVFD/function2/R_VFDTemp_C]
Desc=Read VFD heatsink temp (dec C *10)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function3]
Desc=r_AL_FR
Type=0
SlaveAddr=1
MbAddr=24
NbrRegs=2
BitPack=0
Bits=0
ReadAs=0
InitialState=1
ScanDenominator=1
Res0=R_VFDalarm
Res1=R_VFDfOK
[ModbusDevice/MbVFD/function3/R_VFDalarm]
Desc=VFD Alarm Signal
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function3/R_VFDfOK]
Desc=VFD Setpoint Frequency reached
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function4]
Desc=r_tripinfo
Type=4
SlaveAddr=1
MbAddr=17
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=10
Res0=R_VFDtpInfo
[ModbusDevice/MbVFD/function4/R_VFDtpInfo]
Desc=VFD Trip error "factor"
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function5]
Desc=r_OutFreq
Type=4
SlaveAddr=1
MbAddr=4097
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=1
Res0=R_VFDRPM_Hz
[ModbusDevice/MbVFD/function5/R_VFDRPM_Hz]
Desc=VFD Output Freq (dec Hz *100)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function6]
Desc=r_Etime
Type=4
SlaveAddr=1
MbAddr=4117
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=20
Res0=R_VFDeTim
[ModbusDevice/MbVFD/function6/R_VFDeTim]
Desc=VFD Total elapsed run time (dec Hours)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function7]
Desc=r_StatusC
Type=0
SlaveAddr=1
MbAddr=14
NbrRegs=5
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=1
Res0=R_VFDrun
Res1=R_VFDrtDir
Res2=R_VFDready
Res3=R_VFDIGN
Res4=R_VFDtrip
[ModbusDevice/MbVFD/function7/R_VFDrun]
Desc=15: VFD Read Run state
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function7/R_VFDrtDir]
Desc=16: VFD Rotation Direction (0 FWD, 1 REV)
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function7/R_VFDready]
Desc=17: VFD Ready Status
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function7/R_VFDIGN]
Desc=18: ignore
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function7/R_VFDtrip]
Desc=19: VFD Trip (0 for trip, 1 normal)
Type=1
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function8]
Desc=r_Mon1
Type=4
SlaveAddr=1
MbAddr=4098
NbrRegs=2
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=2
Res0=R_VFDcurr_A
Res1=R_VFD_Dir
[ModbusDevice/MbVFD/function8/R_VFDcurr_A]
Desc=4099: VFD Output current (dec A *10)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function8/R_VFD_Dir]
Desc=4010: VFD Act Rot Dir- 0: stop, 1:FWD, 2:REV
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function9]
Desc=r_Mon2
Type=4
SlaveAddr=1
MbAddr=4112
NbrRegs=2
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=2
Res0=R_VFDVolt_V
Res1=R_VFDpwr_KW
[ModbusDevice/MbVFD/function9/R_VFDVolt_V]
Desc=4113: VFD Output Voltage (dec V*10)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function9/R_VFDpwr_KW]
Desc=4114: VFD Power Monitor (dec kW*10)
Type=128
RegIndex=0
BitIndex=0
[ModbusDevice/MbVFD/function10]
Desc=r_trip2
Type=4
SlaveAddr=1
MbAddr=27
NbrRegs=1
BitPack=0
Bits=0
ReadAs=1
InitialState=1
ScanDenominator=10
Res0=R_VFDtpInfo2
[ModbusDevice/MbVFD/function10/R_VFDtpInfo2]
Desc=FD Trip error "factor" for second last trip
Type=128
RegIndex=0
BitIndex=0

As you can see, the Modbus window shows them being updated, however they don't exist in the register diagnostics window (nor in the pull-down menus in the screen editor)

What am I missing here?

Thanks!