SpindleOnCWJogDir.c not working :'(

Moderators: TomKerekes, dynomotion

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Mon Apr 19, 2021 4:45 pm

Hello Tom,

I desperatly need your help, I have been trying for many hours to run my spindle without success :

*Step Dir output
*Channel 4
*Encoder 2500ppr
*Jogging is working in the console, VFD receives properly the pulses
*M3 set with SpindleOnCWJogDir.c

If I type M3 S500 in KMotionCNC the VFD will not receive anything, in the Kmotion console the printf("Jogging Spindle %f counts/sec\n",speed * FACTOR); would show 0, so I guess for an unknown (yet) reason the speed command does pass through.

Any idea what I did wrong in my setup?

Cordially

Francois

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

Re: SpindleOnCWJogDir.c not working :'(

Post by TomKerekes » Mon Apr 19, 2021 5:02 pm

Hi Francois,

What persist Var is the S Action configured to pass the Speed to the C program in? What persist Var is the C program expecting the Speed to be in?
Regards,

Tom Kerekes
Dynomotion, Inc.

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

Re: SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Mon Apr 19, 2021 5:17 pm

Hello Tom,

Thanks for your help, I am bit lost (not really/at all a programmer), here is the C program used (fully original from installation) :

#include "KMotionDef.h"

#define SPINDLEAXIS 4
#define FACTOR (2048/60.0) //1000 counts/sec / 100 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()
{
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 CCW now we want CW
// spin down

ClearBit(SPINDLECW_BIT);
ClearBit(SPINDLECCW_BIT);
Jog(SPINDLEAXIS,0);
while (!CheckDone(SPINDLEAXIS)) ;
}

// turn spindle on CW and ramp to new speed
SetBit(SPINDLECW_BIT);

// spindle is already on, so ramp to new speed
Jog(SPINDLEAXIS,speed * FACTOR);
printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);

persist.UserData[STATEVAR] = 1; // remember we are CW
}

In KMotionCNC, the variable set is 1.

Cordially

Francois

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

Re: SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Mon Apr 19, 2021 5:37 pm

I found a nice page : https://dynomotion.com/Help/KMotionCNC/ ... Params.htm

By reading it and checking again the code, I would think that KMVAR is not even needed, but SPEEDVAR should be 1, am I correct?

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

Re: SpindleOnCWJogDir.c not working :'(

Post by TomKerekes » Mon Apr 19, 2021 6:07 pm

There are 4 actions and programs involved with the Spindle. M3, M4, M5 and S. For OnCW, OnCCW, Off, and Speed respectively.

The program you posted is for OnCW (M3) which doesn't pass the Speed. What Program do you have assigned to the S Action in KMotionCNC | Tool Setup | M0-M30 ? Post your 4 Programs and any included files.

You might also read this.
Regards,

Tom Kerekes
Dynomotion, Inc.

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

Re: SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Mon Apr 19, 2021 6:57 pm

Hello Tom,

Here are the different codes, with the threads and variable as in the manual :
thread 2 for all
variable 1 for M3, M4, M5
variable 113 for S

M3, M4, M5 do not need variable then (could be -1) ? And variable for S should be 1?

M3

Code: Select all

#include "KMotionDef.h"

#define SPINDLEAXIS 4
#define FACTOR (-500.0/2007) // -500 counts = 2007 RPM

// desired speed is passed in variable 1
// save in user variable 99 the last speed setting
// save in user variable 98 whether it was off, CW, or CCW (0,1,-1)
// save in user variable 97 the last desired speed


main()
{
	float speed = *(float *)&persist.UserData[97];  // value stored is actually a float 
	float LastSpeed = *(float *)&persist.UserData[99];  // get last speed setting 
	float LastState = persist.UserData[98];  // get last state 
	
	if (LastState==-1)  
	{
		// if spindle was CCW now we want CW 
		// spin down
		
		ClearBit(154);
		ClearBit(155);
		LastSpeed = 0.0;
		Jog(SPINDLEAXIS,0);
		while (!CheckDone(SPINDLEAXIS)) ;
	}
	
	// turn spindle on CW and ramp to new speed
	
	SetBit(154);
	
	// spindle is already on, so ramp to new speed
	
	if (LastSpeed != speed)
	{
		LastSpeed = speed;
		while (!CheckDone(SPINDLEAXIS)) ;
	}
	
	*(float *)&persist.UserData[99] = LastSpeed;  // save the last speed
	persist.UserData[98] = 1;  // remember we are CW
}


M4

Code: Select all

#include "KMotionDef.h"

#define SPINDLEAXIS 4
#define FACTOR (2048/60.0) //1000 counts/sec / 100 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()
{
	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);
	
	// spindle is already on, so ramp to new speed
	Jog(SPINDLEAXIS,-speed * FACTOR);
	printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);
	
	persist.UserData[STATEVAR] = -1;  // remember we are CCW
}


M5

Code: Select all

#include "KMotionDef.h"

#define SPINDLEAXIS 4
#define FACTOR (2048/60.0) //1000 counts/sec / 100 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)) ;
}

S

Code: Select all

include "KMotionDef.h"

#define SPINDLEAXIS 4
#define FACTOR (2048/60.0) //1000 counts/sec / 100 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()
{
	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)  
	{
		// if spindle is off and User Changes the speed 
		// just save the desired speed
		
		return 0;
	}
	
	// spindle is already on, so ramp to new speed
	Jog(SPINDLEAXIS,LastState * speed * FACTOR);
	printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);
}

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

Re: SpindleOnCWJogDir.c not working :'(

Post by TomKerekes » Mon Apr 19, 2021 7:00 pm

M3, M4, M5 do not need variable then (could be -1) ? And variable for S should be 1?
Correct.
Regards,

Tom Kerekes
Dynomotion, Inc.

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

Re: SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Wed Apr 21, 2021 3:26 pm

Hello Tom,

Thanks, it works !

I have now another concern, there are some offsets between desired speed, VFD monitored frequency, and Kmotion monitored speed, let me give some examples :

Gcode / Spindle speed in KMotionCNC / Command Frequency of the VFD / Monitored VFD frequency/ Speed according VFD

M3 S300 / around 261rpm / 9.99Hz / 10.02Hz / 299rpm

M3 S750 / around 695rpm / 25.11Hz / 25.09Hz / 752rpm

M3 S1500 / around 1413rpm / 49.98Hz / 50Hz / 1499rpm

M3 S3000 / around 2910rpm / 100Hz / 100.14Hz / 2999rpm

There are big variations of speed on KmontionCNC, +/-10rpm which does not seem so normal to me (also at 0 speed), encoder is 2500ppr, all cables shielded and grounded.

Any idea of the problem?

Cordially

Francois

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

Re: SpindleOnCWJogDir.c not working :'(

Post by TomKerekes » Wed Apr 21, 2021 5:19 pm

Hi Francois,

What kind of encoder do you have? Differential? Single ended? How/where is it connected?

Check if it is counting correctly. Zero the Position and mark the physical spindle position. Then move/run the Spindle. Then move the Spindle back to the same position. Does the Position read within several counts of a multiple of 10000 counts?
Regards,

Tom Kerekes
Dynomotion, Inc.

cemoa9
Posts: 82
Joined: Mon Jun 01, 2020 11:01 am

Re: SpindleOnCWJogDir.c not working :'(

Post by cemoa9 » Wed Apr 21, 2021 6:35 pm

Hello Tom,

This is a single ended encoder, it is connected in parallel to the VFD feedback card and to the IO38-39-40 (IO36-37 being used to step-dir the VFD). But it was before connected to the ouput of the VFD feedback card with the same result.

I already checked roughly if it was counting properly and it was (with the VFD pulse counting), but I will perform tomorrow a more accurate test with Kmotion, should I use the position from the status?

The fact that there is a lot of oscillations even with spindle stops makes me think about noise issue.

Cordially

Francois

Post Reply