Table of contents |

# Mach3 Plugin - with Encoders

The following describes the use of linear glass scale encoders or rotary shaft encoders with Mach3. The setup process described apply for KFLOP/Kogna operating in open loop mode with encoders as well as closed loop control.

KFLOP/Kogna should first be wired and configured such that the encoders are functional and scaled (using the InputGain0 parameter) so that the encoder counts match the commanded position in units of μsteps. To verify that this is properly configured the KMotion.exe Step Response Screen may be used for verification. The Plot of Position Error should show small errors (typically <200 μSteps) for a Move Plot if properly configured.

Since the encoder position is already scaledwithin KFLOP/Kogna to match the μsteps/Unit scale of the motor, the Mach3 Encoder resolution should be set to the same value as the Motor "Tuning" as shown below. The Encoder/MPG screen is opened using the Config|Ports and Pins Menu. The Port and Pin definitions are not relevant when using KFLOP/Kogna and should be set to some unused Port.

## Zero Buttons

Mach3 "Zeros" a DRO by adjusting the currently selected work offset such that the DRO will read zero. Since the glass scales are the best reference, the commanded position is adjusted to match the encoder position, before Mach3 is told to compute the new work offset.

    	NotifyPlugins (10100) 	'tell KFlop to set command to encoder
    	Sleep 300	 	'make sure mach updates
    	DoOEMButton (1008) 	'calculate work offset

    	NotifyPlugins (10101) 	'tell KFlop to set command to encoder
    	Sleep 300		'make sure mach updates
    	DoOEMButton (1009)	'calculate work offset

    	NotifyPlugins (10102) 	'tell KFlop to set command to encoder
    	Sleep 300 		'make sure mach updates
    	DoOEMButton (1010)	'calculate work offset

In the Config|Config Plugins|Dynomotion set an appropriate KFLOP/Kogna User Program that will process the NotifyPlugin Message Codes to set KFLOP's/Kogna's internal Commanded Destination to the Current Encoder Positions. Typical program for 3 axes shown below. Note that the Message code is defined to be passed to the KFlop User Program via persist.UserData[6]

## Example File: <Install Dir>\C Programs\NotifyZeroEncoderMach3.c

    	#include "KMotionDef.h"
    	//Plugin calls for Mach3 NotifyPlugins Commands
    	#define X 0
    	#define Y 1
    	#define Z 2

    	main()
    	{
    		int msg = persist.UserData[6]; // Mach3 notify Message 10000-10999

    		printf("Mach3 Notify Call, Message = %d\n",msg);

    		if (msg==10100)
    		{
    			// adjust the commanded position to match the glass scale encoder
    			DisableAxis(X);
    			EnableAxisDest(X,chan[X].Position);
    		}
    		if (msg==10101)
    		{
    			// adjust the commanded position to match the glass scale encoder
    			DisableAxis(Y);
    			EnableAxisDest(Y,chan[Y].Position);
    		}
    		if (msg==10102)
    		{
    			// adjust the commanded position to match the glass scale encoder
    			DisableAxis(Z);
    			EnableAxisDest(Z,chan[Z].Position);
    		}
    	}

## REF Buttons

Mach3 REF buttons are used to set the initial Machine coordinates either by simply Zeroing them or performing a home operation into a switch.

The REF X, REF Y, REF Z etc... buttons may require editing using a screen editor. We recommend the one written by Klaus Dietz.

The Ref buttons should be edited to perform the standard Mach3 Ref operations. See the settings selected for the Ref buttons shown below when using Klaus' free Mach Screen Editor. The standard Ref operations for Mach3 is to request the Plugin to perform the Home Operation (actually labeled purge in the plugin).

The Dynomotion Plugin passes these Home requests to KFLOP/Kogna to handle with a Home User Program. In the Config|Config Plugins|Dynomotion set an appropriate KFLOP/Kogna User Home Program. A flag variable is also passed to tell which axis is to be homed (Note that the flags is defined to be passed to the KFLOP/Kogna User Program via persist.UserData[5]). In the case with encoders, both the Encoder Position and the Commanded Destination should be zeroed. Prior to Zeroing if any homing motion (to a switch for example - See: SimpleHome3Axis.c in the C Programs directory) may also be added into the program.

Note: if Homing Inputs are enabled in Mach3 | Config | Ports and Pins | Input Signals | X Home, Y Home, Z Home, A Home, B Home, C Home then Mach 3 will NOT call the Plugin to do Homing. Please make sure these Inputs are NOT enabled.

## Example File: <Install Dir>\C Programs\HomeEncoderMach3.c

    	#include "KMotionDef.h"

    	//Plugin calls for Mach3 Home (actually Purge) Commands>
    	//Called from Mach3 "REF" command
    	//in this case just Zero the measured position (encoder)
    	//and set the commanded destination to zero

    	#define X 0
    	#define Y 1
    	#define Z 2

    	main()
    	{
    		int flags = persist.UserData[5]; // Mach3 flags bit0=X, bit1=Y, Bit2=Z, etc...

    		printf("Mach3 Home Call, flags = %d\n",flags);

    		if (flags & 1)
    		{
     			DisableAxis(X);
     			Zero(X);
     			EnableAxisDest(X,0.0);
     		}
     		if (flags & 2)
    		 {
     			DisableAxis(Y);
     			Zero(Y);
     			EnableAxisDest(Y,0.0);
     		}
     		if (flags & 4)
     		{
     			DisableAxis(Z);
     			Zero(Z);
     			EnableAxisDest(Z,0.0);
     		}
    	}