Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: barneskite28 on October 31, 2021, 12:49:00 PM

Title: Modbus control huanyang GT vfd
Post by: barneskite28 on October 31, 2021, 12:49:00 PM
Hi guys,
  Just upgraded my VFD to huanyang GT, got modbus connected and talking thanks to the little tutorial
https://caldwellfam.net/bill/wp-content/uploads/2019/04/Connecting-a-VFD-to-Mach4-through-the-Modbus-v5.pdf

I have copied the script as instructed and i have the VFD turning on with the spindle on button, but the spindle is not turning - cant seem to set the RPM.  I dont quite get what i should be looking at!

I can control via regfile initially but now when i enter new values the actual value does not change
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 01, 2021, 04:43:34 PM
So using the REGFILE i can start, stop and set rpm of the spindle.  But the GUI does not have this effect?  If entered m3 s2000 in mdi, the VFD comes on but no forward signal and 0 rpm........  PLEASE HELP
MB3 ESS
Huanyang GT
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 01, 2021, 05:25:25 PM
Hi,
in the m3 macro you need to edit it to put a valid number into the register.

Modbus exchanges data with Mach exclusively through the registers. If you want Mach to provide good Modbus data then you must program
Mach to do so.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 01, 2021, 07:35:48 PM
Hi,
  Thanks but I am not using an m3 macro, I have added the lua script as above.  When i add the lua code I cannot set the speed using regfile.

If i need an m3 macro would that not only start the spindle after the m3 cmd?  if the on screen button is pressed would that still work??

I'm guessing that lua is the right way to go but I'm no programmer, just a dumb mech engineer!
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 01, 2021, 07:36:28 PM
Do you know of any good examples that i can find or documentation?
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 01, 2021, 11:37:58 PM
Hi,
all very well but that script does not seem to work, all in all it seems a complicated way to go about a simple task.

If you enter a valid number into the register 'wrtvfdspeed0' what happens?. It should be communicated to the VFD and if the VFD is turned on by the
other control registers then it will rotate at the speed nominated by the value you just entered.

If you had two-three-four lines of code in the PLC script updating  the register wrtvfdspeed0 from the current S value which is #2132.
Every time you enter a block Snnnn either MDI or Gcode job, and hence #2132 is updated the PLC script will update wrtvfdspeed0 every few milliseconds and
Bob's your Auntie!!!

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 01, 2021, 11:52:03 PM
Hi,

Quote
If i need an m3 macro would that not only start the spindle after the m3 cmd?  if the on screen button is pressed would that still work??

You always had to use m3, OR the on-screen button press.....which IS an m3, so no change. The only change is that you require the commanded
speed be placed in the appropriate register....and the MODBUS plugin will look after transmitting that to the VFD.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 02, 2021, 03:14:16 AM
So if I use regfile and update the register manually, the spindle turns on, changes speed etc.
When I say I'm not using M3, what I mean is I'm not using a separate M3 macro.
What script to have in the plc script is my problem, as I mentioned I'm no coder. I have tried to figure it out using PMC but no luck.
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 02, 2021, 06:56:23 AM
Hi,

Quote
So if I use regfile and update the register manually, the spindle turns on, changes speed etc.

Exactly, that proves if the register is updated then everything works.

Quote
When I say I'm not using M3, what I mean is I'm not using a separate M3 macro.

You ARE using m3, and note it is m3 NOT M3. m3 is a Mach4 built-in macro, it's there whether you like it or not. You may also write your own m3.
When Mach encounters an m3 call it will in the first instance looks in the macros directory of your profile, if it finds an m3 there it uses it, if not it searches further
up the directory tree until it finds an m3 to use, usually the built-in m3. This is a time when you MUST use proper notation, ie m3 not M3. Get used
to using lowercase, without leading zeros and without whitespace, this is how the Gcode interpreter parses the Gcode. When the interpreter is looking for
m3 for example its likely to miss M3, it's looking look a lowercase m.

So you have two choices:
1) Write your own m3. It will live in the macros folder of your profile. It will need to include everything that the built-in m3 includes PLUS whatever
else you want, which in your case would include updating the three registers that control the VFD.
2) Use the provided script, but which for whatever reason fails to update the speed register by adding a little code in the PLC script.

The script will be something like this:

local registerHandle=mc.mcRegGetHandle(inst,.....Path)  -- Substitute Path with the full path to register wrtvfdspeed0
local speed=mc.mcGetPoundVar(inst, 2132)
speed = speed*...........    -- You may need to do some arithmetic on the pound variable to bring into into line with the number the VFD expects.
mc.mcRegSetValue(registerHandle,speed)

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 02, 2021, 03:57:45 PM
Ok, i know i AM using M3, what i meant was i am using the built in M3 as opposed to a separate macro file.
If i put this script in an M3 macro would the command to write the register function if the gcode gave an change of rpm? if i enter separate commands in the MDI i can make this change with simply s5000, so in this case would not call the M3?
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 02, 2021, 07:43:46 PM
Hi,
Writing your own m3 would result in the speed register being updated only at each occasion m3 is called. Quasi continuous updates ocurr with the code in the plc script and is therefore preferred.
Craig
Title: Re: Modbus control huanyang GT vfd
Post by: SwiftyJ on November 03, 2021, 10:35:46 AM
A couple of things to check:
Did you add the .lua file to the PMC and enable it through the screen editor?
Are your register names for the modbus exactly the same as it shows in the instruction? The PMC is writing to these specific register paths so it needs to match.

Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 05:48:07 AM
A couple of things to check:
Did you add the .lua file to the PMC and enable it through the screen editor?
Are your register names for the modbus exactly the same as it shows in the instruction? The PMC is writing to these specific register paths so it needs to match.

Yes i have checked the functions are exact as in the script file.  i added the Lua file to the pmc scripts in screen editor.  I feel like its partly working as it is 'activating' the vfd, it just wont spin!

Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 05:51:17 AM
Hi,
Writing your own m3 would result in the speed register being updated only at each occasion m3 is called. Quasi continuous updates ocurr with the code in the plc script and is therefore preferred.
Craig

Yes thats what i thought.. i would like this to function as it was - i.e. not need an m3 cmd to update the speed
 
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 05:53:11 AM
This is the lua file i'm using here

Code: [Select]
-----------------------------------------------------------------------------
--   Name: PMC Module
--Created: 03/27/2015
-----------------------------------------------------------------------------
-- This is auto-generated code from PmcEditor. Do not edit this file! Go
-- back to the ladder diagram source for changes in the logic

-- U_********* symbols correspond to user-defined names. There is such a symbol
-- for every internal relay, variable, timer, and so on in the ladder
-- program. I_********* symbols are internally generated.

local VFD_Spindle_controller = {}

local hReg, hSig, rc
local mInst = 0

-- Generated function for MachAPI.
local function Read_Register(path)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local value, rc = mc.mcRegGetValue(hReg)
        if (rc == mc.MERROR_NOERROR) then
            return value
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Register(path, value)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        rc = mc.mcRegSetValue(hReg, value)
        if (rc == mc.MERROR_NOERROR) then
            return
        end
    end
    return
end

-- Generated function for MachAPI.
local function Read_Signal(path)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local state = 0;
        state, rc = mc.mcSignalGetState(hSig)
        if (rc == mc.MERROR_NOERROR) then
            return state
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Signal(path, v)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        mc.mcSignalSetState(hSig, v)
    end
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CW()
    return Read_Signal(1142)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CCW()
    return Read_Signal(1143)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_ON()
    return Read_Signal(1141)
end

-- Call this function to retrieve the PMC cycle time interval
-- that you specified in the PmcEditor.
function VFD_Spindle_controller.GetCycleInterval()
    return 10
end


-- Call this function once per PLC cycle. You are responsible for calling
-- it at the interval that you specified in the MCU configuration when you
-- generated this code. */
function VFD_Spindle_controller.PlcCycle()
    mInst = mc.mcGetInstance()

    if     (Read_U_b_FSpindle_CCW() == 0 and Read_U_b_FSpindle_CW() == 1 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 1)
    elseif (Read_U_b_FSpindle_CCW() == 1 and Read_U_b_FSpindle_CW() == 0 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 2)
    else --if Read_U_b_FSpindle_ON() == 0) then
       Write_Register("modbus0/wrtvfdcmd0", 5)
    end

    local spindle_rpm = scr.DroRead(mInst, 27)
    Write_Register("modbus0/wrtvfdspeed0", spindle_rpm/12000*10000)
end

return VFD_Spindle_controller
Title: Re: Modbus control huanyang GT vfd
Post by: Bill_O on November 04, 2021, 07:52:41 AM
I could be very wrong but don't the variables "path" and "value" need to be defined what they are?
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 08:30:55 AM
Maybe, i'm not sure too.... i did see that but i dont have the knowledge of Lua to say so
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 08:31:50 AM
the modbus resister addresses are present below, so i figured this path was supposed to be there
Title: Re: Modbus control huanyang GT vfd
Post by: SwiftyJ on November 04, 2021, 10:39:34 AM
Try replacing this line

local spindle_rpm = scr.DroRead(mInst, 27)

with this:

local spindle_rpm = mc.mcSpindleGetCommandRPM(mInst)

I'd edit the lua file with mach closed
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 04, 2021, 01:55:40 PM
replaced that line of code to no avail....

one thing i have noticed is that when i have the lua script enabled, i cannot write to the speed register using regfile anymore...
As soon as i uncheck the script in screen editor, i can control it again
Title: Re: Modbus control huanyang GT vfd
Post by: SwiftyJ on November 05, 2021, 04:14:17 AM
That will be because the PMC is continuously writing to that register every couple of milliseconds. So whatever value you enter is being instantly overwritten.

I modified the script on my system to update another register I created, and when I enter S1000 into the MDI the register updates. So I am not sure why this isn't working on your system..
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 05, 2021, 06:12:56 AM
i STILL CANT GET A RESOLUTION TO THE REQUIRED SCRIPT TO MAKE THIS OPERATE PROPERLY!
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 12:05:49 AM
Hi,
have you thought about putting some code in the PLC script instead?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 04:50:50 AM
Hi,
have you thought about putting some code in the PLC script instead?

Craig
Yes definitely thought about it, but i am unsure as to how to write the code!  This is the main problem and why i'm here asking for help! if i was a scripting guru i would have written the solution by now lol
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 05:06:46 AM
I have used the pmc editor a couple of times, but i am not sure how the structure should look to acheive the desired result
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 06:36:37 AM
Hi,
I gave you the outline of what is required in post #8. Did you try it?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 06:44:36 AM
Hi,
I gave you the outline of what is required in post #8. Did you try it?

Craig
I pasted that code into the lua file for the spindle control, but I had no success with that unfortunately
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 06:46:22 AM
Hi,
I gave you the outline of what is required in post #8. Did you try it?

Craig
I noticed you have posted about this kind of thing a while ago - Do you use modbus spindle control yourself?  If so do you mind sharing the script?
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 06:48:05 AM
Hi,

Quote
I pasted that code into the lua file for the spindle control, but I had no success with that unfortunately

But it doesn't go in the spindle control file, wherever it is and whatever it is, it goes in the PLC script. The code is not completed,
you have to add some details, you'll not just be able to paste it in as is.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 06:51:56 AM
Hi,

Quote
I noticed you have posted about this kind of thing a while ago - Do you use modbus spindle control yourself?  If so do you mind sharing the script?

I don't use MODBUS on my machine but a friend of mine has a CNC pressbrake which I helped him the interface Mach4 to the PLC of the machine.
It does not have a spindle .

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 06:54:15 AM
Hi,
first please post a screen shot of the Regfile diagnostic, I need to see the register path.
Second, what number do you have to set in the register to cause a given speed?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 10:12:08 AM
Hi,

Quote
I pasted that code into the lua file for the spindle control, but I had no success with that unfortunately

But it doesn't go in the spindle control file, wherever it is and whatever it is, it goes in the PLC script. The code is not completed,
you have to add some details, you'll not just be able to paste it in as is.

Craig

ok, so i pasted this  code into the Lua file that i posted earlier in this thread, which is called as a PLC script (selected in screen editor). This LUA file is called spindle control - hope this clarifies!

Unfortunately i am not so familiar with coding, scripting and writing macros so forgive my ignorance!
I have attached a screen grab of the regfile and modbus window, and below the code as is at the moment

Code: [Select]
-----------------------------------------------------------------------------
--   Name: PMC Module
--Created: 03/27/2015
-----------------------------------------------------------------------------
-- This is auto-generated code from PmcEditor. Do not edit this file! Go
-- back to the ladder diagram source for changes in the logic

-- U_********* symbols correspond to user-defined names. There is such a symbol
-- for every internal relay, variable, timer, and so on in the ladder
-- program. I_********* symbols are internally generated.

local VFD_Spindle_controller = {}

local hReg, hSig, rc
local mInst = 0

-- Generated function for MachAPI.
local function Read_Register(path)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local value, rc = mc.mcRegGetValue(hReg)
        if (rc == mc.MERROR_NOERROR) then
            return value
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Register(path, value)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        rc = mc.mcRegSetValue(hReg, value)
        if (rc == mc.MERROR_NOERROR) then
            return
        end
    end
    return
end

-- Generated function for MachAPI.
local function Read_Signal(path)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local state = 0;
        state, rc = mc.mcSignalGetState(hSig)
        if (rc == mc.MERROR_NOERROR) then
            return state
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Signal(path, v)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        mc.mcSignalSetState(hSig, v)
    end
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CW()
    return Read_Signal(1142)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CCW()
    return Read_Signal(1143)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_ON()
    return Read_Signal(1141)
end

-- Call this function to retrieve the PMC cycle time interval
-- that you specified in the PmcEditor.
function VFD_Spindle_controller.GetCycleInterval()
    return 10
end


-- Call this function once per PLC cycle. You are responsible for calling
-- it at the interval that you specified in the MCU configuration when you
-- generated this code. */
function VFD_Spindle_controller.PlcCycle()
    mInst = mc.mcGetInstance()

    if     (Read_U_b_FSpindle_CCW() == 0 and Read_U_b_FSpindle_CW() == 1 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 1)
    elseif (Read_U_b_FSpindle_CCW() == 1 and Read_U_b_FSpindle_CW() == 0 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 2)
    else --if Read_U_b_FSpindle_ON() == 0) then
       Write_Register("modbus0/wrtvfdcmd0", 5)
    end

    local spindle_rpm = scr.DroRead(mInst, 27)
    Write_Register("modbus0/wrtvfdspeed0", spindle_rpm/12000*10000)
end

return VFD_Spindle_controller
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 10:15:27 AM
(http://)
Hi,
first please post a screen shot of the Regfile diagnostic, I need to see the register path.
Second, what number do you have to set in the register to cause a given speed?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 03:39:58 PM
Hi,
OK. The PMC module has been around for about three years now, maybe more. Prior to its introduction the only way to have Mach
execute blocks of code repeatedly was to use the PLC, and I must confess its what I am still most familiar with.

The PMC module is a ladder logic module. Its designed to replicate Mach3 Brains and ladder logic is the predominant means of programming
industrial Programmable Logic Controllers. As it is a visual representation it is widely appreciated as the simplest means of programming.

The PMC module in Mach4 takes the visual representation, ie the rung of the ladder, and converts that into  chunks of Lua code, and its that
Lua code that simulates the logic implied by the ladder rung. The Lua code blocks are auto generated and are not easy to read or interpret,
really they are seldom ever viewed by human operators, we'd just open the PMC editor and look at the ladder diagram. It comes as no surprise
that you are having difficulty in reading and understanding the Lua code, it was never meant to be human readable.

You have posted  screen shots of the Modbus plugin set-up pages, which is useful, but still actually removed from what I asked for.
If you open the Regfile diagnostic you will see a tree diagram of ALL the registers in Machs core, and that is what I would prefer. Having said that
the shots you have posted, and the PM both confirm that the path to the speed register is:

modbus0/wrtvfdspeed

Now the speed command is a little different. In my own VFD if I choose to use Modbus comms I would supply a number related to frequency rather
than speed. For example my VFD goes to 400Hz, and the spindle does its full 24000 rpm at the frequency. The number I have to send is the frequency
IN TENTHS, thus if I send the number 4000 the spindle goes at full speed and if I send a number 2000 the spindle goes at half speed.
Machs core stores the speed as rpm. Thus if I interrogate Mach after issuing an s18000 for instance, Mach will respond with a number 18000.
To convert that into a number my VFD can use I would do the following:

RegisterValue=(MachSpeed  / 24000 )*4000

Just to double check:
RegisterValue=(18000/24000)*4000
=3000, which to my VFD means 300Hz which indeed causes the spindle to rotate at 18000rpm.

It would appear that your VFD it somewhat different in that you must issue a number related to percentage of full speed, IN ONE-HUNDREDTHS:
100%=10000 and
50%=5000

Do I have this correct? If so you will need a slightly different mathematic expression to convert the number that Mach reports to the number
that the VFD expects:

RegisterValue=(MachSpeed /12000) *10000                     given that 100% speed of your spindle is 12000rpm?

As a sanity check lets say you issue an s9000:
RegisterValue= (9000/12000) *10000
=7500

There are two ways to interrogate Machs core to get the current commanded speed. The way I am most familiar with is:
local speed=mc.mcGetPoundVar(inst, 2132)    So this statement gets the value of the pound variable #2132, which is where Mach stores its commanded
speed.

swifty posted this method:
local spindle_rpm = mc.mcSpindleGetCommandRPM(mInst)
This method, which I was previously unaware of, is much better and more elegant and I would suggest you adopt it also.
Thus we should have some draft code:

Code: [Select]
local registerHandle=mc.mcRegGetHandle(mInst,"modbus0/wrtvfdspeed")
local commandedSpeed=mc.mcSpindleGetCommandRPM(mInst)
local registerValue=(commandedSpeed/12000)*10000
mc.mcRegSetValue(registerHandle,registerValue)

Note here I am asumming that 'mInst' has already been evaluated in the PLC and stored as a local variable. If not you'll have to add a line:
local mInst=mc.mcGetInstance()

Please note that I have not tested this code, my machine is at work, and we are shifting premises and have been for the last two days, in fact
as soon as I finish this post I'm leaving to continue with that. At the moment I cannot even plug my machine in, let alone run it
and test some code.

May I suggest running it in the Lua editor before you put it into the PLC. Faulty code in the PLC can cause you grief. You want/need to de-bug any code
before you put it in there.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 06, 2021, 05:32:45 PM
should i bin the rest of the code or append this into it?

Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 11:48:45 PM
Hi,
I would guess at least a part of the code that you have installed is working, ie the VFD turns on and off and you can select FWD or REV,
so to bin it would be to 'throw the baby out with the bath water'.

What I would suggest is to put the proposed code in a macro so that you can run it in the editor.  Once you have established that it works
to your satisfaction and you understand it thoroughly then and only then install it in the PLC script.

The reason I suggest this is because a macro executes just once, if there is a problem with it you can stop it, and/or single step through the code
until you understand what's going on. If you put it directly in the PLC script it will run every few milliseconds, and if it does something strange you may have
to crash Mach to get it to stop, not ideal.

For no particular reason lets call the macro m666:

Code: [Select]
function m666()
     local mInst=mc.mcGetInstance()
     local registerHandle=mc.mcRegGetHandle(mInst,"modbus0/wrtvfdspeed")
     local commandedSpeed=mc.mcSpindleGetCommandRPM(mInst)
     local registerValue=(commandedSpeed/12000)*10000
     mc.mcRegSetValue(registerHandle,registerValue)
end
if(mc.mcInEditor()==1)then
     m666()
end

Try installing that in the macros folder of your current profile. You need to single step through it using the debug functions of the editor.
Using single step is the sure-fire best way to understand how your code works, and even more importantly when it does  not work!

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 06, 2021, 11:51:08 PM
Hi,
I have just got back from work, been shifting stuff all day and I'm buggered again! I'm beginning to think that going to Church on a Sunday
may not be such a bad idea after all.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 07, 2021, 07:22:27 AM
Hi,
I would guess at least a part of the code that you have installed is working, ie the VFD turns on and off and you can select FWD or REV,
so to bin it would be to 'throw the baby out with the bath water'.

What I would suggest is to put the proposed code in a macro so that you can run it in the editor.  Once you have established that it works
to your satisfaction and you understand it thoroughly then and only then install it in the PLC script.

The reason I suggest this is because a macro executes just once, if there is a problem with it you can stop it, and/or single step through the code
until you understand what's going on. If you put it directly in the PLC script it will run every few milliseconds, and if it does something strange you may have
to crash Mach to get it to stop, not ideal.

For no particular reason lets call the macro m666:

Code: [Select]
function m666()
     local mInst=mc.mcGetInstance()
     local registerHandle=mc.mcRegGetHandle(mInst,"modbus0/wrtvfdspeed")
     local commandedSpeed=mc.mcSpindleGetCommandRPM(mInst)
     local registerValue=(commandedSpeed/12000)*10000
     mc.mcRegSetValue(registerHandle,registerValue)
end
if(mc.mcInEditor()==1)then
     m666()
end

Try installing that in the macros folder of your current profile. You need to single step through it using the debug functions of the editor.
Using single step is the sure-fire best way to understand how your code works, and even more importantly when it does  not work!

Craig

So i have done as you suggested, i have saved the macro into the macro folder as m666 an if i go to the mdi, enter m3 s2000, i have the same result - power but no rotation.
then i enter m666 to call the macro (screen dro displays 2000 rpm) nothing happens so no better  :(
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 07, 2021, 07:23:02 AM
Hi,
I have just got back from work, been shifting stuff all day and I'm buggered again! I'm beginning to think that going to Church on a Sunday
may not be such a bad idea after all.

Craig
wow must be bad lol
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 07, 2021, 09:49:05 AM
I can confirm it is still the speed register not working/updating
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 07, 2021, 12:56:01 PM
Hi,
the macro is not really a means of changing the speed, it would have to run multiple times a secoond to do that, the macro is so you
can step through it one instruction at a time until you can understand what it does. Have you tried single stepping?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 07, 2021, 01:01:22 PM
Hi,
I can see one mistake:
the path is not modbus0/wrtvfdspeed it is:
modbus0/function2/wrtvfdspeed0

Change the path in the macro and single step through it.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 07, 2021, 01:11:00 PM
Hi,
this is a classic case where I did not consult the register diagnostic and thus had the wrong path......and the path must be EXACTLY character
for character correct.

When you single step through the code and the once the local 'registerHandle=mc.mcRegGetHandle(mInst,"modbus0/wrtvfdspeed")'
statement executes hover the cursor over 'registerHandle' and the editor should display the value.....does the number displayed look like a genuine
memory address? If it does then the chances are that the statement has executed correctly if not then it has not found the correct handle ie the
path is STILL not correct.

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 08, 2021, 02:04:47 PM
Hi Joe,
  unfortunately the path does not include the 'function0', but i tried it anyways and still the same :(
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 08, 2021, 06:23:59 PM
Hi,
have you single stepped through the code?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: joeaverage on November 08, 2021, 06:28:20 PM
Hi,
the whole point about putting the code in a macro IS NOT TO EFFECT THE SPEED.....it is a means for you put put the code
in a 'sandbox' so you can play with it until you understand it and get it to work, just throwing random corrections at the code will not do it.

Double check the Register Diagnostics, it shows that function2 Is included in the path.......what evidence do you use to support the contention that its
not part of the path?

Craig
Title: Re: Modbus control huanyang GT vfd
Post by: barneskite28 on November 14, 2021, 01:38:24 PM
Hi,
the whole point about putting the code in a macro IS NOT TO EFFECT THE SPEED.....it is a means for you put put the code
in a 'sandbox' so you can play with it until you understand it and get it to work, just throwing random corrections at the code will not do it.

Double check the Register Diagnostics, it shows that function2 Is included in the path.......what evidence do you use to support the contention that its
not part of the path?

Craig
If you check the tree, the path is modbus0/wrtvfd..... etc.  I got the code finally working with the help of James, massive thankyou! i will post the working version here
Code: [Select]
-----------------------------------------------------------------------------
--   Name: PMC Module
--Created: 03/27/2015
-----------------------------------------------------------------------------
-- This is auto-generated code from PmcEditor. Do not edit this file! Go
-- back to the ladder diagram source for changes in the logic

-- U_********* symbols correspond to user-defined names. There is such a symbol
-- for every internal relay, variable, timer, and so on in the ladder
-- program. I_********* symbols are internally generated.

local VFD_Spindle_controller = {}

local hReg, hSig, rc
local mInst = 0

-- Generated function for MachAPI.
local function Read_Register(path)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local value, rc = mc.mcRegGetValue(hReg)
        if (rc == mc.MERROR_NOERROR) then
            return value
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Register(path, value)
    hReg, rc = mc.mcRegGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        rc = mc.mcRegSetValue(hReg, value)
        if (rc == mc.MERROR_NOERROR) then
            return
        end
    end
    return
end

-- Generated function for MachAPI.
local function Read_Signal(path)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        local state = 0;
        state, rc = mc.mcSignalGetState(hSig)
        if (rc == mc.MERROR_NOERROR) then
            return state
        end
    end
    return 0
end

-- Generated function for MachAPI.
local function Write_Signal(path, v)
    hSig = 0
    rc = mc.MERROR_NOERROR
    hSig, rc = mc.mcSignalGetHandle(mInst, path)
    if (rc == mc.MERROR_NOERROR) then
        mc.mcSignalSetState(hSig, v)
    end
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CW()
    return Read_Signal(1142)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_CCW()
    return Read_Signal(1143)
end

-- Generated function for signal read.
local function Read_U_b_FSpindle_ON()
    return Read_Signal(1141)
end

-- Call this function to retrieve the PMC cycle time interval
-- that you specified in the PmcEditor.
function VFD_Spindle_controller.GetCycleInterval()
    return 10
end


-- Call this function once per PLC cycle. You are responsible for calling
-- it at the interval that you specified in the MCU configuration when you
-- generated this code. */
function VFD_Spindle_controller.PlcCycle()
    --mInst = mc.mcGetInstance()

    if     (Read_U_b_FSpindle_CCW() == 0 and Read_U_b_FSpindle_CW() == 1 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 1)
    elseif (Read_U_b_FSpindle_CCW() == 1 and Read_U_b_FSpindle_CW() == 0 and Read_U_b_FSpindle_ON() == 1) then
       Write_Register("modbus0/wrtvfdcmd0", 2)
    else --if Read_U_b_FSpindle_ON() == 0) then
       Write_Register("modbus0/wrtvfdcmd0", 5)
    end

    local registerHandle=mc.mcRegGetHandle(mInst,"modbus0/wrtvfdspeed0")
local commandedSpeed=mc.mcSpindleGetCommandRPM(mInst)
-- mc.mcCntlSetLastError(mInst, tostring(commandedSpeed))
local registerValue=(commandedSpeed/12000)*10000
mc.mcRegSetValue(registerHandle,registerValue)
end

return VFD_Spindle_controller

The big problem was for some reason the register was not getting written. Maybe an internal mach problem