Hello Guest it is April 25, 2024, 03:40:32 AM

Author Topic: How Are Encoders Configured in Mach4?  (Read 2037 times)

0 Members and 1 Guest are viewing this topic.

How Are Encoders Configured in Mach4?
« on: February 21, 2021, 06:55:37 PM »
How do I configure incremental encodes in Mach4? I have an encoder on each side of the gantry used for racking detection. In Mach3 I would configure them as encoders on the “Encoders/MPGs” tab. In Mach 4 the tab is labelled “MPGs”. If I configure them as encoders on that tab and enable them they cause problems when jogging. Can I get counts from the encoders without enabling them or am I configuring them in the wrong place? I have not figured out how to get the counts from encoders in Lua. If you know that answer as well I would appreciate it.
Re: How Are Encoders Configured in Mach4?
« Reply #1 on: February 23, 2021, 11:39:45 AM »
Perhaps the way I worded my question was confusing because I have to believe there is an easy answer to this. The base question is: How do I configure encoders in Mach 4 that are not going to be used as MPGs?
Re: How Are Encoders Configured in Mach4?
« Reply #2 on: February 24, 2021, 06:56:10 AM »
Don't map them as MPG's. Encoder counts are usually in the plugin of the motion controller.

You can get the value from the plugin register if it's available. Maybe include the motion controller you are using so more people can help you.

If the encoder registers are not available in the hardware plugin, you can map the encoders as Aux Position for the motor and just uncheck the update planner option. Then get the value of the encoder counts using the LUA api.

Code: [Select]
-- screen load script
local xAxisAuxEncReg = mc.mcMotorGetAuxPosReg(inst, 0)

-- plc script
local xAxisAuxEncVal = mc.mcRegGetValue(xAxisAuxEncReg)
mc.mcCntlSetLastError(inst, tostring(xAxisAuxEncVal))
« Last Edit: February 24, 2021, 07:08:37 AM by compewter_numerical »
Re: How Are Encoders Configured in Mach4?
« Reply #3 on: February 24, 2021, 10:46:29 AM »
Thank you for the reply.

I am running an Ethernet SmoothStepper.

I don't have the Aux. Positions tab when selecting Control Configurations.

I can see the encoders on the encoders diagnostics screen. They are listed as Encoder_0 and Encoder_1. I see the counts change when the y-axis moves, so now I just need to figure out how to read those encoder registers using LUA. I assume I would put this in the PMC. My goal is to monitor the counts of the encoders on each side of the gantry and if they vary by more than a specific number of counts I will stop the machine. My goal is to have racking detection for the gantry so that nothing is damaged if one side stops being driven for some reason.

Thank you for your help!
Re: How Are Encoders Configured in Mach4?
« Reply #4 on: February 24, 2021, 01:40:45 PM »
You can access any register in Mach4. You can do it in the PMC or you can do it in LUA.

if you have the regfile plugin enabled in Mach4, goto Diagnostic->Regfile and navigate to ESS->Encoders->Encoder_#, 'right-click' the register and select copy path to clipboard. Then you can get the register handle and the value.

mc.mcRegGetHandle(inst, 'path/you/just/copied') -- the path should be in between quotes

ScreenLoad Script:
Code: [Select]
local essEncoder0Reg = mc.mcRegGetHandle(inst, 'ESS/Encoders/Encoder_0')

PLC Script:
Code: [Select]
local essEncoder0Val = mc.mcRegGetValue(essEncoder0Reg)
mc.mcCntlSetLastError(inst, tostring(essEncoder0Val))
« Last Edit: February 24, 2021, 01:43:02 PM by compewter_numerical »
Re: How Are Encoders Configured in Mach4?
« Reply #5 on: February 24, 2021, 03:58:35 PM »
Thank you! I will give this a try this evening. I have verified that I can see the values via the register diagnostics screen.

Now I have another newbie question. The reason I want to read these two encoders is so I can stop the machine if they differ by more than a given count. One encoder monitors the move on one side of the gantry and the other encoder monitors the opposite side. This will ensure that gantry is not damaged if one side stops being driven for whatever reason.

In order to monitor this at a given interval, I assume I place the code into the PMC folder of Mach4. Is that correct?

How do I set the interval for running this code? This relates to another post I made but have yet to get a response. There is an interval that can be specified for the PMC module when editing the main screen. However, code that I implemented (by starting with code someone else created) for ModBus spindle control had two areas that appear to control the PLC interval. One was the following function (however that function did not appear to be called anywhere in the code I was using for ModBus spindle control):

function VFD_Spindle_Controller.GetCycleInterval()
    return 10
end

The other area that I did not understand that may control the interval the code is called is at the end of the main function call for the ModBus spindle control: function VFD_Spindle_Controller.PlcCycle()

Can you educate me on this.

Thank you again!
Kent
Re: How Are Encoders Configured in Mach4?
« Reply #6 on: February 24, 2021, 04:16:42 PM »
The Lua code generated by the PMC contains a lot of functions that aren’t documented, to my knowledge.

You should choose either to program using the PMC or by writing Lua code in the screen editor.

There are many different places you can set the interval but to make it simple you should only change the PMC and PLC intervals in the screen editor. The PMC and PLC are two different things.

Here is a guide for the screen editor.
https://www.machsupport.com/wp-content/uploads/2014/05/Mach4%20Scripting%20Manual.pdf

If you look at my last post I labeled where you can put the code. (Screen load and PLC)


« Last Edit: February 24, 2021, 04:21:05 PM by compewter_numerical »
Re: How Are Encoders Configured in Mach4?
« Reply #7 on: February 24, 2021, 05:14:43 PM »
Thank you again and again for your helpful responses. It was relatively easy to get the basic operation of my machine converted from Mach3 to Mach4. The one basic function I needed was to get ModBus control of the spindle. I started with a fantastic document created by Bill Caldwell. He went into great detail for controlling a Huanyang VFD. I have a Hitachi VFD that uses a combination of coils and registers (Huanyang only uses registers) so I modified his code. He explained that he started in the PMC and then modified some of his code. I like to fully under stand the code I implement even if it starts out as someone else's. Much more than you wanted to know, but I wanted to give you perspective on where I am with my Mach4 learning journey.

When do you use PMC versus PLC? What is the PMC? I was obviously confused and thought that the PMC and PLC were one of the same.

I don't want to wear out your patience, so feel free to ignore these last questions. You have already been very helpful to me and have me well on my way to implementing the racking detection code!

Kent
Re: How Are Encoders Configured in Mach4?
« Reply #8 on: February 24, 2021, 05:33:49 PM »
When do you use PMC versus PLC? What is the PMC? I was obviously confused and thought that the PMC and PLC were one of the same.

The PLC is just one part of the screen script...you edit in the screen editor.

The PMC is a tool that is supposed to make programming in Mach4 easier.

If you are comfortable writing programs using Lua and the Mach API then use the screen editor. Else use the PMC.
Re: How Are Encoders Configured in Mach4?
« Reply #9 on: February 24, 2021, 07:24:35 PM »
Hopefully one last question. Where do you place the function for running this Lua code in the PLC ? There is no folder named PLC. Is the code actually placed in the PMC folder? I read the scripting manual and watched a couple videos and this basic question was not covered.