Table of contents |

# Controlling KMotionCNC from KFLOP/Kogna

User KFLOP/Kogna C Programs can request actions to be performed by the KMotionCNC Application running on the PC by setting various command codes into special persist.UserData variables. The special UserData variables are being continuously uploaded with the Bulk Status record that KMotionCNC requests at approximately 10 times per second to update the DROs and so forth. If a command code is uploaded, KMotionCNC attempts to perform the requested action, then changes the UserData Command Value to a result code. Zero indicates success and a negative value indicates an error code (all commands are positive). So the typical process involves:

1. KFLOP/Kogna stores command into a Persist Var to request an action
2. The command is uploaded to KMotionCNC with the next status request
3. KMotionCNC performs the action
4. KMotionCNC clears the Persist Var to indicate completion
5. KFLOP/Kogna detects the command Var has been cleared to know the action was successful and complete

The Status uploads several Persist Vars which permits additional parameters to be uploaded with the command to KMotionCNC if required by the action to be performed. If extra parameters or data is required, then one of the uploaded parameters will specify where that data is located.

The number of UserData variables has now been expanded from 100 to 200 Variables and Variables 100-107 are the special vars constantly uploaded with bulk status. This is defined in the PC-DSP.h file as:

    	#define PC_COMM_PERSIST 100 	// First Persist Variable that is uploaded in status
    	#define N_PC_COMM_PERSIST 8 	// Number of Persist Variables that are uploaded in status

Currently supported actions include:

- EStop
- Halt
- Execute
- Single Step
- Set FRO
- Inc/dec FRO
- Set X,Y,Z,A,B,C DROs
- Push a User Defined Action Button
- Execute an M Code
- Display a Message Box
- Get/Set GCode #Vars
- Execute a MDI line of GCode

A new example called KFLOPtoPCCmdExamples.c is included which demonstrates how to invoke these actions from a KFLOP User C Program. The following helper functions are included in the example that simplify invoking the actions by setting the proper persist variables. They are:

    	// Trigger a message box on the PC to be displayed
    	// defines for MS Windows message box styles and Operator
      	// response IDs are defined in the KMotionDef.h file
      	int MsgBox(char *s, int Flags)

    	// put the MDI string (Manual Data Input - GCode) in the
    	// gather buffer and tell the App where it is
    	int MDI(char *s)

    	// Put a Float as a parameter and pass the command to the App
    	int DoPCFloat(int cmd, float f)

    	// Put an integer as a parameter and pass the command to the App
    	int DoPCInt(int cmd, int i)

    	// Pass a command to the PC and wait for it to handshake
    	// that it was received by either clearing the command
    	// or changing it to a negative error code
    	int DoPC(int cmd)

The Example code to make use of the helper functions is in the example as:

       main()
       {
       		int Answer;
    		double *pD = (double *)persist.UserData;

    		DoPC(PC_COMM_ESTOP);
    		DoPC(PC_COMM_HALT);
    		DoPC(PC_COMM_EXECUTE);
    		DoPC(PC_COMM_SINGLE_STEP);
    		DoPCFloat(PC_COMM_SET_FRO,0.25f);
    		DoPCFloat(PC_COMM_SET_FRO_INC,1.1f);
    		DoPCFloat(PC_COMM_SET_X,0.0);
    		DoPCFloat(PC_COMM_SET_Y,0.0);
    		DoPCFloat(PC_COMM_SET_Z,1.25);
    		DoPCInt(PC_COMM_USER_BUTTON,3);
    		DoPCInt(PC_COMM_MCODE,3);

    		Answer = MsgBox("Hello World",MB_YESNO|MB_ICONEXCLAMATION);
    		if (Answer == IDYES)
    			printf("Answer is Yes\n");
    		else
    			printf("Answer is No\n");

    		MDI("G0 X1.2 Y2.2 Z3.3");

    		// put 3 double values in the persist vars

    		pD[10] = 123.456;
    		pD[11] = 1000.0;
    		pD[12] = 999.9;

    		// transfer up to the GCode Vars
    		SetVars(100,3,10);  // Upload 3 to GCode 100 from persist 10

    		MDI("#100 = [#100 + 1]");

    		// read them back into different persist Vars
    		GetVars(100,3,13);  // Download 3 from GCode 100 to persist 13

    		printf("%f %f %f\n",pD[13],pD[14],pD[15]);
    	}