Hello Guest it is March 29, 2024, 09:19:34 AM

Author Topic: MPG thru modbus  (Read 3053 times)

0 Members and 1 Guest are viewing this topic.

MPG thru modbus
« on: August 15, 2015, 11:27:55 PM »
I have a modbus pendant with an MPG. The MPG increments or decrements a holding register.

I have mapped the modbus register into MACH4, but how do I use that register to move an axis?
Re: MPG thru modbus
« Reply #1 on: September 09, 2015, 11:01:42 AM »
As recommended, i looked at Brian's mousewheel jog example, and I am still as much in the dark as ever. There are not enough comments in the code for me to figure out what it is doing.

Also, it seems that you can set the MPGAxis, but how do you display the selected Axis?

Has anyone got an MPG working that they could share code for?
Re: MPG thru modbus
« Reply #2 on: September 10, 2015, 12:47:12 PM »
OK, Brian sent me some code snippets over on the yahoo group, and my Modbus MPG is now working in Mach4, and it’s working better than it ever did in Mach3!

For the benefit of anyone else who wants to do this, here is the lua code I am using. This will have to be changed to match whatever registers you are storing things like MPG Selected Axis, MPG Step Size, and MPG Count.

*In my Screen Load Script:
mc.mcMpgSetAccel(inst, 0, 25);  --set up MPG parameters
mc.mcMpgSetRate(inst, 0, 100);
mc.mcMpgSetCountsPerDetent(inst, 0, 4);

*In my PLC Script:

**near the beginning:
testcount = testcount + 1  --this is standard in the latest Mach4 screens

**in the PLC First Run Section:
-------------------------------------------------------
--  PLC First Run
-------------------------------------------------------
if (testcount == 1) then –this section is included in the latest Mach4 screens, just add the lines below
    …
    local inst = mc.mcGetInstance()
    local hregiPEncoderInit = mc.mcRegGetHandle(inst, "modbus1/iPEncoder")
    local iPEncoderInit = mc.mcRegGetValue(hregiPEncoderInit) --get value from pendant Encoder Register
    local Init_EncoderPos = iPEncoderInit –not sure if so many levels of variables are necessary here
    last_EncoderPos = Init_EncoderPos
    …
End

**near the end:
--This section for setting MPG Axis based on modbus pendant Axis switch
local hregipAxis = mc.mcRegGetHandle(inst, "modbus1/ipAxis")
local ipAxis = mc.mcRegGetValue(hregipAxis)
--get value from pendant Axis switch
if (ipAxis == 2) then
    mc.mcMpgSetAxis(inst, 0, 0);
--    set Axis to "x"
elseif (ipAxis == 4) then
    mc.mcMpgSetAxis(inst, 0, 1);
--    set Axis to "y"
elseif (ipAxis == 8) then
    mc.mcMpgSetAxis(inst, 0, 2);
--    set Axis to "z"
elseif (ipAxis == 16) then
    mc.mcMpgSetAxis(inst, 0, 3);
--    set Axis to "a"
elseif (ipAxis == 32) then
    mc.mcMpgSetAxis(inst, 0, 4);
--    set Axis to "b"
elseif(ipAxis == 64) then
    mc.mcMpgSetAxis(inst, 0, 5);
--    set Axis to "c"
--"T" is "0"
--"Off" is "1"
end

--This section for setting MPG Increment based on modbus pendant Step switch
local hregipStep = mc.mcRegGetHandle(inst, "modbus1/ipStep")
local ipStep = mc.mcRegGetValue(hregipStep)
--get value from pendant Step switch
if (ipStep == 1) then
    mc.mcMpgSetInc(inst, 0, .01);
elseif (ipStep == 2) then
    mc.mcMpgSetInc(inst, 0, .001);
elseif (ipStep == 4) then
    mc.mcMpgSetInc(inst, 0, .0001);
elseif (ipStep == 8) then
    mc.mcMpgSetInc(inst, 0, .01)--for "V" mode
--set MPG increment
end

--This section for moving selected axis based on MPG register changes
--local inst = mc.mcGetInstance()
local inst = mc.mcGetInstance()
local hregiPEncoder = mc.mcRegGetHandle(inst, "modbus1/iPEncoder")
local iPEncoder = mc.mcRegGetValue(hregiPEncoder) --get value from pendant Encoder Register
local inc_pos = (last_EncoderPos - iPEncoder); --Calculate the inc position of the axis
mc.mcMpgMoveCounts(inst, 0, inc_pos) --move the number of counts
last_EncoderPos = iPEncoder