Mach3 Plugin



      KMotion/KFlop                                           Mach3        


Mach3 is a popular CNC program available for purchase through ArtSoftControls.

Traditionally Mach3 has relied on little hardware support and performed low level motion and even stepper motor step pulses directly by the PC.  The pulses were output using the PC's parallel port.  This required a special Microsoft Windows Kernel driver with a high interrupt rate and was limited to 50~100K steps per second.

Mach3 has the capability for adding "plugins" that allow additional functionality and hardware support.  DynoMotion has developed a plugin that allows Mach3 to work with a KMotion or KFLOP Motion Controller.  The offloads most of the real-time requirements from the PC, allows USB connectivity, much higher step rates, and allows easily adding other motor types including brushless servos.

This is an overview of the overall setup process of getting Mach3 and KMotion to play together.

1. Install Mach3
2. Install KMotion
3. Within KMotion configure/tune your motors
4. Within KMotion create a configuration and initialization program
5. Within Mach3 - configure plugin (enter name of KMotion Init program )
6. Within Mach3 - configure IO Bits. 
7. Within Mach3 - configure motor tuning (set resolution, speeds, and acceleration)

Note: when using encoder feedback see also: Mach3 Encoder Setup

Note: for configuring probing see also: Mach3 G31 Probe Setup

Note: for information regarding operating steppers in closed loop see also: Closed Loop Steppers

Note: For passing Parameters see also: Passing DROs

Note: For Rigid Tapping see also: Mach3 Rigid Tapping


1. Install Mach3

Mach3 should be installed before KMotion so that the KMotion installation program can copy the necessary files (Dynomotion.dll) into the Mach3 plugin directory.  The KMotion installation will also add a registry entry under "App Paths" so that Mach3.exe will have access to the necessary KMotion DLL libraries, programs, and data files

It is not necessary to load the Mach3 Kernel driver.

If KMotion has already been installed, it should be re-installed to make the necessary links between the programs.



2. Install KMotion


At the end of the KMotion installation an option to install the Mach3 Plugin will be displayed. 

Check the Mach3 Plugin option and select Next. 


You will be prompted to enter the directory where Mach3 was installed.

A check will be made that a valid Mach3.exe exists in that directory, and the Dynomotion.dll will be copied to the plugins subdirectory.



3. Within KMotion configure/tune your motors


Follow the normal procedures to use the KMotion Executive program to configure and tune all of your motor axes.  The simplest method to configure the system is to enter and change values in the various KMotion Executive Screens, download them to the KMotion Board, and test and tune for correct operation using the Step Response Screen as well as Console commands. 



4. Within KMotion create a configuration and initialization program


After each axis is functioning properly, the configuration for that axis can be copied to the clipboard as a valid C program code. 


See the circled button below.


C Code to configure axis 0 might look like the code shown below.  Detailed knowledge of C programming is not required to paste these into a User Program.  The configuration for each axis should be pasted into a single program.  Later this program may be executed at any time to completely configure the KMotion Board.  The KMotion Executive program would not be needed to do this.  The RESET button from within Mach3 will be configured to execute this program.





Besides C code to configure each axis, other commands such as those shown highlighted below may be used to enable each axis (and set the destination) and to define which axis channels are in the coordinated motion system.  The DefineCoordSystem(0,1,-1,-1); statement defines a two axis coordinate system where X is axis channel 0, Y is axis channel 1, and the Z and A axes are not used.

The circled button can be used to save, compile, download, and run the C program in one step.  Note that once a C program has been executed to change configuration settings within the KMotion Board, the values in the KMotion Executive screens may be different from the current settings within the board.  To synchronize the screens with what is actually in the board the channel should be "Uploaded".

Additional initialization operations may also be added to the C Progam.  These might include brushless motor phase finding, homing, activating IO bits, etc..


After the Initialization program is finalized and working it should be saved in a known location so that Mach3 may be configured to execute it as described in the next step.



5. Within Mach3 - configure plugin (enter name of KMotion Init program)


We are now ready to Execute Mach3.  The first time Mach3 is executed after adding the Dynomotion Plugin Mach3 will prompt the user which Motion Device should be used.  The Dynomotion Plugin should be selected.  After the Mach3 Application comes up use the Config|Config Plugins menu to bring up the Config Plugins dialog shown below.




Next click in the circled area to bring up the Dynomotion Plugin Configuration Screen.



Specify the KMotion Initialize User Program that was created in step 4 above (you may browse to the file using the >> button).


Options to automatically run the program on Mach3 Startup or only once after Mach3 Startup may also be selected.


Mach3 generates Spindle Messages for On, Off, and Speed Changes.  Specify a KMotion Spindle Speed User Program to handle and perform appropriate actions for the various Mach3 Spindle Messages.  A default SpindleMach3.c program is included in the default installation that will simply print the messages and desired speeds requested by Mach3.  Note that speed is a relative fraction of the max currently selected pulley's speed.  Because KFlop/KMotion is likely to be controlling the motor speed regardless of the selected pulley, this is usually more appropriate.  An appropriate User Program to perform the appropriate actions to actually drive the spindle should be created to replace the default program that simply prints the requested operation.  There are included examples for Spindles controlled by a DAC output (SpindleMach3DAC.c) and for Spindles controlled by Step/Dir outputs (SpindleMach3Jog.c).


Spindle Speed measurement and Single point threading is also supported.  A quadrature encoder is required on the spindle.  Specify the Sensor Type as 1 to enable the Spindle measurement.  Configure the Axis Channel that is configured to read the encoder (Note this is not the encoder channel, rather it is the axis that has the encoder channel configured as its input).  Specify the Update time, Tau, and counts/rev.  See here for more information. 


Mach3 Home requests may be passed to a User Program to activate any desired Home Sequence.  An example skeleton program which just prints the Mach3 home requests is included as HomeMach3.c.  Another example for use with encoders is described here.


Mach3 permits a general purpose mechanism to make custom calls to any Plugin that are passed through to a KFlop Custom Notify User Program.   Placing a NotifyPlugins(10xxx) call in VB Script of either a Screen Button or Macro command may then trigger behavior in KMotion/KFlop.  Message Codes from 10000 to 10999 will be sent to KFlop (all others are ignored).  Plugin executions are automatically queued and executed in the sequence they were requested (the previous must complete before the next is launched).  An example skeleton program which just prints the Mach3 notify requests is included as NotifyMach3.c.  Another example for use with encoders is described here.


An I/O bit may be defined that is activated when Mach3 goes into the offline state.  Neg True Polarity may be checked if it is desirable for the bit to be driven low in the off-line state rather than high.


The Windows Buffer ahead Time may be changed to keep more or less buffering ahead in the KMotion Board.  Using a value too small may cause the buffer to run empty and "starve" for data if Windows becomes non-responsive for longer than that amount of time.  Using a value too large will cause feed rate changes and feed hold commands to be less responsive.


The plugin can be configured to Automatically run Initialize on Mach Startup if desired.  Caution:This option should be left off if there is any potential danger with unexpected machine motion when launching Mach3.


After setting al parameters press OK to save the parameters into the Mach3 XML parameter file and return to the main Mach Screen.  Pushing Mach3's Reset button will now execute the Initialize program.





6. Within Mach3 - configure IO Bits


Mach3 was originally designed to directly control Step/Dir drives connected to a PC Parallel Port.  When using KFLOP this is no longer the case.  Select the Menu Config|Ports & Pins to bring up the Dialogs shown below.  When using the KMotion Plugin, Pin Numbers now correspond to KMotion IO Bit Numbers rather than Parallel port Pins.  Some of the terminology on the screen may be misleading as it was designed expecting to use a PC Parallel Port.  For IO bit numbers less than 128 specify Port#1 and use the IO Bit number as the Pin.  For IO bit numbers 128 or larger, subtract 128 from the bit number and specify Port #2 instead of Port #1.  Extended Virtual IO bits 1024-1151 may also be accessed by specifying Port #3 and subtracting 1024 from the bit number (note:  the first 32 Virtual IO will consume less USB bandwidth because they are uploaded in the KFLOP Bulk Status record so use the first 32 if possible).


KFLOP handles all real-time I/O (Step/Dir, Analog, Limits, etc...) and is configured within KFLOP.  So within Mach3 Step/Dir Outputs are not used and should all be configured to non-KFLOP Outputs (ie Port 0 Pin 0).  See below.  Note that the Enable for each axis in your system must be enabled otherwise Mach3 will not generate motion for the axis.





Mach3 has an EStop (Emergency Stop) Input to allow the hardware to inform Mach3 that an EStop has occurred so that it will stop any running Job and enter Emergency Stop Mode.  If your system is continuously in Emergency Stop Mode and can not be Reset out of Emergency Stop Mode, then it is likely that the configured EStop Input is continuously Active.  If your hardware has a EStop signal interfaced to a KFLOP Input configure the corresponding KFLOP input IO number and the appropriate polarity.  If your system does not have an EStop signal configure the EStop Input to an unused IO bit that wil never be active.  See below for how to configure to KFLOP IO Bit 46 (a KFLOP LED) which is normally always high.



Various M Codes may be used within Mach3 to activate IO bits on KFLOP.  Note Mach3 typically defaults after an initial install with Output #1 enabled for spindle control on Pin0 (as shown below).  Bit 0 is often used on a KMotion board as an Encoder input.  Having Mach configure the IO bit as an output will cause the encoder to be inoperable.  Disable the output if this is the case. 


7. Within Mach3 - configure motor tuning (set resolution, speeds, and acceleration)  


Select Config|Motor Tuning to bring up the Motor Tuning Dialog.  This screen allows the setting of Machine resolution, Max Velocity, and Max Acceleration for each axis in use.  Note that the sliders are typically not very useful since the max step rates are much higher using a KMotion Motion Controller than with the original PC Generated Steps.  It's usually best to just enter values directly into the cells.




After the parameters have been entered and saved, Press OK to return to the main screen.

That's it!  Reset should now properly initialize the machine.  Jogging and G Code should now be functional. 


Refer to the Mach3 documentation for more information on advanced features. 


Here is how a GCode file appears on Mach3 Mill.


Here the same example GCode file cut (or more accurately burned) into a piece of wood using a Harbor Freight Mini-mill driven with KMotion and Mach3.