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 ==

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 ==

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