G32 sync spindle to Z axis lathe

Moderators: TomKerekes, dynomotion

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

Re: G32 sync spindle to Z axis lathe

Post by TomKerekes » Tue Aug 13, 2019 5:55 pm

Hi turbothis,
with the more accurate counts/degree and counts/rev on the traj planner is this still good?

#define FACTOR (118724.6/60.0)
Actually I see now the file you posted doesn't use FACTOR. Change it in the files that do. That's why we recommend using a header file to define things so things only need to be changed in one place and it will then be automatically changed in every program that includes it. Greatly reduces the chance for errors.
Regards,

Tom Kerekes
Dynomotion, Inc.

turbothis
Posts: 309
Joined: Fri Mar 15, 2019 4:07 pm
Location: southern oregon

Re: G32 sync spindle to Z axis lathe

Post by turbothis » Tue Aug 13, 2019 6:07 pm

great
can you help explain how to do this?
i am easily confused with where all this stuff goes and why
i have the header file........

deleted due to size wow!
Last edited by turbothis on Tue Aug 13, 2019 9:44 pm, edited 1 time in total.

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

Re: G32 sync spindle to Z axis lathe

Post by TomKerekes » Tue Aug 13, 2019 6:20 pm

Hi turbothis,

No, that is our KMotionDef.h header file that contains definitions of all of KFLOP's built in available functions, variables, and definitions. It is best not to ever change it.

You should create your own header file with only definitions you need for your system. An example of this is MySpindleDefs.h

Code: Select all

#define SPINDLEAXIS 6			// Axis Channel to Jog to rotate Spindle
#define FACTOR (1000/60.0)  	// to convert RPM to counts/sec (counts/rev / 60.0sec)
#define SPINDLECW_BIT 154   	// bit to activate to cause CW rotation
#define SPINDLECCW_BIT 155		// bit to activate to cause CCW rotation
#define SPEEDVAR 99				// global persistant variable to store latest speed
#define STATEVAR 98				// global persistant variable to store latest state (-1=CCW,0=off,1=CW)
#define KMVAR PC_COMM_CSS_S 	// variable KMotionCNC will pass speed parameter (113)
#define USE_POS_NEG_VOLTAGE 0 	// 0 = output Magnitude, 1 = output positive and negative speed 
Regards,

Tom Kerekes
Dynomotion, Inc.

turbothis
Posts: 309
Joined: Fri Mar 15, 2019 4:07 pm
Location: southern oregon

Re: G32 sync spindle to Z axis lathe

Post by turbothis » Tue Aug 13, 2019 7:10 pm

Gotcha
Where do I want to store this header file?

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

Re: G32 sync spindle to Z axis lathe

Post by TomKerekes » Tue Aug 13, 2019 7:17 pm

Anywhere you want. If you put all your C Programs in the same place and the header there also you will be able to include it by name without needing to specify the folder path.
Regards,

Tom Kerekes
Dynomotion, Inc.

turbothis
Posts: 309
Joined: Fri Mar 15, 2019 4:07 pm
Location: southern oregon

Re: G32 sync spindle to Z axis lathe

Post by turbothis » Tue Aug 13, 2019 9:10 pm

ok i have a folder on my desktop
in it i put the C programs and the "MySpindleDefs.h" . nothing else in there.....
so in the C programs for onCWjog and onCCWjog it has the #include "MySpindleDefs.h" at the start.
does the C program search the whole computer to find exactly that file? hence the quotes? or how does it get used?

also you said

"Actually I see now the file you posted doesn't use FACTOR. Change it in the files that do"

are you saying i need to add the "MySpindleDefs.h" to all my C programs?

this....
#define FACTOR (118724.6/60.0)

turns into this?????
#define FACTOR (118328.889/60.0)

my H file i have been using (or trying to use)

Code: Select all

#include "KMotionDef.h"

#define SPINDLEAXIS 2
#define FACTOR (118724.6/60.0) //1000 counts/sec / 1000 counts/rev = 1 RPS = 60 RPM
#define SPINDLECW_BIT 154
#define SPINDLECCW_BIT 155
#define SPEEDVAR 99
#define STATEVAR 98
#define KMVAR 1


// desired speed is passed from KMotionCNC in variable KMVAR
// save in user variable STATEVAR whether it was off, CW, or CCW (0,1,-1)
// save in user variable SPEEDVAR the last desired speed

main()
{
	// spin down
	
	ClearBit(SPINDLECW_BIT);
	ClearBit(SPINDLECCW_BIT);
	Jog(SPINDLEAXIS,0);
	printf("Jogging Spindle Stop\n");
	persist.UserData[STATEVAR] = 0;  // remember we are Off
	while (!CheckDone(SPINDLEAXIS)) ;
	DisableAxis(SPINDLEAXIS);
}


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

Re: G32 sync spindle to Z axis lathe

Post by TomKerekes » Tue Aug 13, 2019 10:45 pm

Hi turbothis,
does the C program search the whole computer to find exactly that file? hence the quotes? or how does it get used?
No it does not search the whole computer. If a complete path is specified it only looks exactly where specified. For example for:
#include "C:\Users\turbothis\Desktop\MyPrograms\MySpindleDefs.h"

then exactly what file to be included is completely specified and that file will be included. This method is not normally a good idea because if you ever move the programs to another folder then the files will need to be edited to specify the new location.

If the folder path is excluded and only the name given like this:
#include "MySpindleDefs.h"

then the compiler (not the program btw) first looks into the same folder where the file that is including it is in.

if not found it will then look into <KMotion Install folder>\DSP_KFLOP\ folder where KFLOP's standard headers are. This is how KMotionDef.h is found for example.

You shouldn't include the main function into your header file. See the example I provided. The idea is to include only things that are common and other programs will need. You have included code to stop your Spindle. Can you see how it would then not make sense to include it into a program to Start your spindle? But then all your Spindle Programs are likely to need to know what axis is used to control your spindle. So that makes sense to define that in the header.

Another way of looking at it would be to imagine not using header files. Create all the programs needed. Then look for things that are duplicated in two or more programs. Then remove all the commonality from the programs, place it into a separate file, then include the new file into all the programs needing it. Its a way of simple text substitution.
are you saying i need to add the "MySpindleDefs.h" to all my C programs?
yes, the programs that may need to know things about your Spindle.

this....
#define FACTOR (118724.6/60.0)

turns into this?????
#define FACTOR (118328.889/60.0)
yes

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

turbothis
Posts: 309
Joined: Fri Mar 15, 2019 4:07 pm
Location: southern oregon

Re: G32 sync spindle to Z axis lathe

Post by turbothis » Tue Aug 13, 2019 11:44 pm

ya that helps a lot!
i will play around with it some more

turbothis
Posts: 309
Joined: Fri Mar 15, 2019 4:07 pm
Location: southern oregon

Re: G32 sync spindle to Z axis lathe

Post by turbothis » Wed Aug 14, 2019 12:43 am

ok i think i killed it (negative way)
the spindle now just runs off way past the speed commanded

my spindle jog C program

Code: Select all

#include "KMotionDef.h"
#include "MySpindleDefs.h"


int   *css_mode = &persist.UserData[PC_COMM_CSS_MODE];			// Mode 1=Normal RPM mode. 2=CSS

// desired speed is passed from KMotionCNC in variable KMVAR
// save in user variable STATEVAR whether it was off, CW, or CCW (0,1,-1)
// save in user variable SPEEDVAR the last desired speed

main()
{
	float speed = *(float *)&persist.UserData[KMVAR];  // value stored is actually a float 
	float LastState = persist.UserData[STATEVAR];  // get last state 
	
	persist.UserData[SPEEDVAR] = persist.UserData[KMVAR];  // Always save the last desired speed 
	
	if (LastState==0 || *css_mode == 2)  
	{
		// if spindle is off (or CSS mode) and User Changes the speed 
		// just save the desired speed
		
		return 0;
	}
	
	// spindle is already on, so ramp to new speed
	if (USE_POS_NEG_VOLTAGE)
		Jog(SPINDLEAXIS,speed * FACTOR * LastState);
	else
		Jog(SPINDLEAXIS,speed * FACTOR);
		
	printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);
}
my CCW C program

Code: Select all

#include "KMotionDef.h"
#include "MySpindleDefs.h"

int   *css_mode = &persist.UserData[PC_COMM_CSS_MODE];			// Mode 1=Normal RPM mode. 2=CSS

// desired speed is passed from KMotionCNC in variable KMVAR
// save in user variable STATEVAR whether it was off, CW, or CCW (0,1,-1)
// save in user variable SPEEDVAR the last desired speed

main()
{
	if (!chan[SPINDLEAXIS].Enable) EnableAxis(SPINDLEAXIS);
	float speed = *(float *)&persist.UserData[SPEEDVAR];  // value stored is actually a float 
	float LastState = persist.UserData[STATEVAR];  // get last state 
	
	if (LastState==1)  
	{
		// if spindle was CW now we want CCW 
		// spin down
		
		ClearBit(SPINDLECW_BIT);
		ClearBit(SPINDLECCW_BIT);
		Jog(SPINDLEAXIS,0);
		while (!CheckDone(SPINDLEAXIS)) ;
	}
	
	// turn spindle on CCW and ramp to new speed
	SetBit(SPINDLECCW_BIT);
	
	LastState = -1;
	
	if (*css_mode != 2)
	{
		// spindle is already on, so ramp to new speed
		if (USE_POS_NEG_VOLTAGE)
			Jog(SPINDLEAXIS,speed * FACTOR * LastState);
		else
			Jog(SPINDLEAXIS,speed * FACTOR);
		
		printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);
	}	
	persist.UserData[STATEVAR] = -1;  // remember we are CCW
}

and the spindle header

Code: Select all

#define SPINDLEAXIS 2			// Axis Channel to Jog to rotate Spindle
#define FACTOR (118328.889/60.0)  	// to convert RPM to counts/sec (counts/rev / 60.0sec)
#define SPINDLECW_BIT 154
#define SPINDLECCW_BIT 155
#define SPEEDVAR 99				// global persistant variable to store latest speed
#define STATEVAR 98				// global persistant variable to store latest state (-1=CCW,0=off,1=CW)
#define KMVAR PC_COMM_CSS_S 	// variable KMotionCNC will pass speed parameter (113)
#define USE_POS_NEG_VOLTAGE 1 	// 0 = output Magnitude, 1 = output positive and negative speed 

Moray
Posts: 282
Joined: Thu Apr 26, 2018 10:16 pm

Re: G32 sync spindle to Z axis lathe

Post by Moray » Wed Aug 14, 2019 9:29 am

I think your header is the problem, as the KMVAR is different.

In your old defines, KMVAR was simply defined as 1, whereas in Tom's example, KMVAR is defined as PC_COMM_CCS_S, which must also be defined elsewhere, otherwise the compiler should throw an error.

Post Reply