Exec/Wait from inside a c program

Moderators: TomKerekes, dynomotion

User avatar
cnc_freak
Posts: 55
Joined: Fri Apr 20, 2018 5:36 am

Re: Exec/Wait from inside a c program

Post by cnc_freak » Tue Jun 07, 2022 9:00 pm

I have send all the files in your email.
Regards.

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Exec/Wait from inside a c program

Post by TomKerekes » Tue Jun 07, 2022 9:47 pm

Your M3 Action is just setting a bit so of course it doesn't wait. Change it to a C Program with the wait in it.
Regards,

Tom Kerekes
Dynomotion, Inc.

User avatar
cnc_freak
Posts: 55
Joined: Fri Apr 20, 2018 5:36 am

Re: Exec/Wait from inside a c program

Post by cnc_freak » Wed Jun 08, 2022 7:32 am

I make i M3 handling c program "DoM3.c" which is called by M3 in KmotionCNC with an action Exec/wait.

Code: Select all

#include "KMotionDef.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"


#define PLASMA 				1068		// Operation Plasma
#define T_SELECT1 			1069
#define T_SELECT2 			1070
#define CUT_VLV1			154			//
#define M3					48


main()
{
	//-------------------------------------------
	//-     P L A S M A    O P E R A T I O N    -
	//-------------------------------------------
	if(ReadBit(PLASMA) && !ReadBit(T_SELECT1) && !ReadBit(T_SELECT2) ){
		if(!ReadBit(1030)){
			SetBit(1031);
			ScreenScript("WinMsg:Keyboard;;KeyPress;122");	
			SetBit(1030);
		}	
		while(ReadBit(1031));	// Bit 1031 is reset by Torch_zero_find, when it finishes
	}
		
	//-------------------------------------------
	//-  O X Y     F U E L  O P E R A T I O N   -
	//-------------------------------------------
	if(!ReadBit(PLASMA) && ReadBit(T_SELECT1) || ReadBit(T_SELECT2)){	
		SetBit(M3);
	}
}
This program "DoM3.c" sets a virtual bit (1031) and then "presses" the button 116, in order to execute the Torch_zero_find.c, which also happens, ok.
The "DoM3.c" should then wait, via

Code: Select all

while(ReadBit(1031));
until this bit is setted by Torch_zero_find.c when finishes, which is not happens. The execution of Torch_zero_find.c starts but the DoM3.c finishes before the Torch_zero_find.c does.
Why is this happen?

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Exec/Wait from inside a c program

Post by TomKerekes » Wed Jun 08, 2022 4:08 pm

That looks correct to me except you said Key 116 but it does 122.

Where are you setting Bit 1031? Are you possibly setting it somewhere else?

You might add printf statements to troubleshoot what is actually happening.

Please post all your programs (do not email them to me).
Regards,

Tom Kerekes
Dynomotion, Inc.

User avatar
cnc_freak
Posts: 55
Joined: Fri Apr 20, 2018 5:36 am

Re: Exec/Wait from inside a c program

Post by cnc_freak » Wed Jun 08, 2022 8:02 pm

This is the Torch_zero_find.c which is called from user button 9 key 122, not as i wrote by mistake 116.
The DoM3 is called by M3 via action Exec/wait from KmotionCNC.

Code: Select all

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"


int DoPC(int cmd);
int DoPCFloat(int cmd, float f);
int DoPCInt(int cmd, int i);
int SetVars(int poff, int varoff, int n);
int GetVars(int varoff, int n, int poff);


void MoveUp(double distance);
void MoveDown(double distance);
void ProbeDown();

// Globals
double *pD = (double *)persist.UserData,t1,t2,PreheatTime,PierceDelay;
float xRes, yRes, zRes;
int TMR1,TMR2;

#define PROBEBIT 1032  //143                       	
#define PROBESPEED 500.0f                
#define XAXIS 0                            	
#define YAXIS 1                            
#define ZAXIS 3                            	
#define UPSPEED -500.0f                   	
#define DOWNSPEED (PROBESPEED * 0.5f)     	
#define PIERCE_HEIGHT 5.0f					
#define PIERCE_DELAY 0.1f					
#define CUTTING_HEIGHT 4.0f                 
#define IGNITION_HEIGHT 0.1f               	
#define SWITCH_OFFSET 4.0f

#define M3					48
#define RESUME 				55	
					
#define PLASMA_ON 152
//#define ARC_IS_ON 139
#define ARC_IS_ON 1025

#define GAS_VLV			153			//
#define PRE_HI_VLV		153			//
#define CUT_VLV1		154			//
#define PRE_LOW_VLV		153			//
#define EXAUST_VLV		155


#define PLASMA 				1068		// The first torch position "Operation Plasma"
#define T_SELECT1 			1069		// "Oxy Fuel operation"
#define T_SELECT2 			1070		// "Oxy Fuel operation"


double ProbeBit,ProbeSpeed,Xaxis,Yaxis,Zaxis,UpSpeed,DownSpeed,PierceHeight,PlateThickness,CuttingHeight,IgnitionHeight;

main()
{
	char s[256];
	printf("\n\nStarting M3\n");
	// Download Variables from GCode #110 to persists 
	GetVars(110, 10, 10);
	ProbeSpeed = pD[10];		//#110
	UpSpeed = pD[11];			//#111
	DownSpeed = pD[12];			//#112
	PierceHeight = pD[13];		//#113
	PierceDelay = pD[14];		//#114
	CuttingHeight = pD[15];		//#115
	IgnitionHeight = pD[16];	//#116

	DoPCInt(PC_COMM_GETAXISRES, 50);

	//xRes = (*(float *)&persist.UserData[50])/25.4;
	//yRes = (*(float *)&persist.UserData[51])/25.4;
	zRes = (*(float *)&persist.UserData[52])/25.4;

	//printf("Axis Resolutions Found:\ntz: %f\n",zRes);
	//printf("Probe Speed: %f\nUp Speed: %f\nDown Speed: %f\nPierce Height: %f\n", ProbeSpeed,UpSpeed,DownSpeed,PierceHeight);
	//printf("Pierce Delay: %f\nCutting Height: %f\nIgnition Height: %f\n", PierceDelay,CuttingHeight,IgnitionHeight);
	

	//============================================
	//=     P L A S M A    O P E R A T I O N     =
	//============================================

	if(ReadBit(PLASMA) && !ReadBit(T_SELECT1) && !ReadBit(T_SELECT2) ){
		printf("PLASMA\n");
		// Find Z Height
		ProbeDown();
		ProbeUp();
		MoveUp(SWITCH_OFFSET);
		DoPCFloat(PC_COMM_SET_Z, 0);
		DoPCFloat(PC_COMM_SET_A, 0);	
		MoveUp(IgnitionHeight);
		printf("Turn on plasma\n");
		SetBit(PLASMA_ON);
		printf("Waiting for arc on\n");
		while(!ReadBit(ARC_IS_ON));
		MoveUp(PierceHeight);					// Move relative Up for piercing
		Delay_sec(PierceDelay);
		MoveDown(PierceHeight);					// Move down again relative where you started "Ignition Height"
		MoveDown(IgnitionHeight-CuttingHeight);	// Move down again relative where you started "Ignition Height"
		printf("Ok to move\n");
		SetBit(1024);
		DefineCoordSystem(0,1,4,-1);
		ClearBit(1031);
	}
	
	//===========================================
	//=  O X Y     F U E L  O P E R A T I O N   =
	//===========================================
	if((ReadBit(T_SELECT1) || ReadBit(T_SELECT2)) && !ReadBit(PLASMA) ){	
		//SetBit(M3);			// Start the internal M3
	}
}

//
// ============================================================
//
void MoveUp(double distance)
{
	//printf("Moving Up by %f\n", distance);
	MoveRel(ZAXIS, distance * zRes);
	while (!CheckDone(ZAXIS));
}

void MoveDown(double distance)
{
	//printf("Moving Down by %f\n", distance);
	MoveRel(ZAXIS, -1 * distance * zRes);
	while (!CheckDone(ZAXIS));
}

void ProbeDown()
{
	printf("Jogging Down at speed:%f\n", DOWNSPEED);
    Jog(ZAXIS, DOWNSPEED);
	printf("Waiting for probe to read high\n");
	while (ReadBit(PROBEBIT));   		
	printf("Stopping\n");
    Jog(ZAXIS, 0);
	printf("Waiting for Z motion to stop\n");
	while (!CheckDone(ZAXIS));
}

void ProbeUp()
{
	printf("Jogging Up at speed:%f\n", DOWNSPEED);
    Jog(ZAXIS, -1*DOWNSPEED);
	printf("Waiting for probe to read low\n");
	while (!ReadBit(PROBEBIT));   		
	printf("Stopping\n");
    Jog(ZAXIS, 0);
	printf("Waiting for Z motion to stop\n");
	while (!CheckDone(ZAXIS));
}


int SetVars(int varoff, int n, int poff)
{
	persist.UserData[PC_COMM_PERSIST+2] = n;       // number of elements
	persist.UserData[PC_COMM_PERSIST+3] = poff;    // persist offset (doubles)
	return DoPCInt(PC_COMM_SET_VARS,varoff);       // Var index and Cmd
}

int GetVars(int varoff, int n, int poff)
{
	persist.UserData[PC_COMM_PERSIST+2] = n;       // number of elements
	persist.UserData[PC_COMM_PERSIST+3] = poff;    // persist offset (doubles)
	return DoPCInt(PC_COMM_GET_VARS,varoff);       // Var index and Cmd
}

// put the MDI string (Manual Data Input - GCode) in the 
// gather buffer and tell the App where it is
int MDI(char *s)
{
	char *p=(char *)gather_buffer+GATH_OFF*sizeof(int);
	int i;
	
	do // copy to gather buffer w offset 0
	{
		*p++ = *s++;
	}while (s[-1]);
	
	// issue the command an wait till it is complete
	// (or an error - such as busy)
	return DoPCInt(PC_COMM_MDI,GATH_OFF);
}

// Put a Float as a parameter and pass the command to the App
int DoPCFloat(int cmd, float f)
{
	int result;
	persist.UserData[PC_COMM_PERSIST+1] = *(int*)&f;
	return DoPC(cmd);
}

// Put an integer as a parameter and pass the command to the App
int DoPCInt(int cmd, int i)
{
	int result;
	persist.UserData[PC_COMM_PERSIST+1] = i;
	return DoPC(cmd);
}

// 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)
{
	int result;
	
	persist.UserData[PC_COMM_PERSIST]=cmd;
	
	do
	{
		WaitNextTimeSlice();	
	}while (result=persist.UserData[PC_COMM_PERSIST]>0);
	
//	printf("Result = %d\n",result);

	return result;
}
Attachments
Tool_setup_3.JPG
Tool_setup_4.JPG

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Exec/Wait from inside a c program

Post by TomKerekes » Thu Jun 09, 2022 3:13 am

It seems to work properly for me. But I had to delete much of your code as I don't have your hardware.

There is one problem with the Keypress. With a screen script its Action has priority and overrides the button action so the C Program isn't called.

Like I suggested before use:

Code: Select all

			if (ScreenScript("Action:5;3;-1;0;0;0;Torch_zero_find.c"))
				printf("Screen Script Failed\n");
			else
				printf("Screen Script Success\n");	
which issues the Action directly without the need for a button. When I posted that previously somehow spaces were introduced which causes it not to work.Maybe that is why you decided to do it differently than what I suggested.

Please post your Initialization file so we can check for conflicts.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply