Switching coordinate systems.

Moderators: TomKerekes, dynomotion

Post Reply
Alexanders
Posts: 61
Joined: Wed May 03, 2023 12:54 am

Switching coordinate systems.

Post by Alexanders » Wed Dec 11, 2024 9:33 am

I use the spindle as the C axis :

Code: Select all

DefineCoordSystem6(0,1,2,-1,-1,5);
or as a simple spindle:

Code: Select all

DefineCoordSystem6(0,1,2,-1,-1,-1);
How can C Program determine whether the axis is included in coordinated movements or not?

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

Re: Switching coordinate systems.

Post by TomKerekes » Thu Dec 12, 2024 12:57 am

You can check the current Axis settings with:

Code: Select all

// coordinate systems #0 - axis definitions
extern int CS0_axis_x; // Axis channel number to use as x
extern int CS0_axis_y; // Axis channel number to use as y  
extern int CS0_axis_z; // Axis channel number to use as z  
extern int CS0_axis_a; // Axis channel number to use as a  
extern int CS0_axis_b; // Axis channel number to use as b  
extern int CS0_axis_c; // Axis channel number to use as c  
extern int CS0_axis_u; // Axis channel number to use as u  
extern int CS0_axis_v; // Axis channel number to use as v  
Another option is for you to keep track of what you set it to in a global variable or Virtual Bit
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 61
Joined: Wed May 03, 2023 12:54 am

Re: Switching coordinate systems.

Post by Alexanders » Sun Aug 10, 2025 12:37 am

To avoid manually turning the C axis on and off,I did this automatically in the M3, M4, and M5 programs.
The Init program turns on the C axis. The M3 and M4 programs first turn off the C axis, then turn on the spindle as a spindle. The M5 program stops the spindle and activates the C axis.

Init.c (executed .out)

Code: Select all

  .......
 .......
  EnableAxis (0);
  EnableAxis (1);
  EnableAxis (2);
  EnableAxis (5); //Spindle
  DefineCoordSystem6(0,1,2,-1,-1,5); //Spindle as C axis
 .......
 .......
M3.c (executed .out)

Code: Select all

#include "KMotionDef.h"
#include "Milling_Machine_24K40_Hardware.h"
/*
  //==== All defined in "Milling_Machine_24K40_Hardware.h" ====
         #define FACTOR              (11468.8/60.0)             //1000 counts/sec / 100 counts/rev = 1 RPS = 60 RPM
         #define Last_SPEED          persist.UserData[2]        //save in user variable Last_SPEED the last desired speed
         #define Desired_SPEED       persist.UserData[3]        //desired speed is passed from KMotionCNC in variable Desired_SPEED. 
         #define Spindle_STATE       persist.UserData[4]        //save in user variable Spindle_STATE whether it was off, CW, or CCW (0,1,-1) 
*/

main()
{   
       DefineCoordSystem6(0,1,2,-1,-1,-1); //Spindle as Spindle (Off C axis)
        printf("M03 start\n");	
	
	float speed = *(float *)&Last_SPEED;  // value stored is actually a float 
	float LastState = Spindle_STATE;  // get last state 
        printf("Prewiew spindle state= %f\n",LastState);	
	
	if (LastState==-1)  
	{		
		Jog(Spindle,0);
		while (!CheckDone(Spindle)) StopCoordinatedMotion();
		ResumeCoordinatedMotion();
	}
 
	printf("Spindle speed= %f\n",speed);
	Jog(Spindle,speed * FACTOR);
	while ((ch5->last_vel <=(speed * FACTOR/100*95))||(ch5->last_vel >= (speed * FACTOR/100*105))) StopCoordinatedMotion(); //while speed window = 95- 105%   
	ResumeCoordinatedMotion();
	printf("Jogging Spindle %f counts/sec\n",speed * FACTOR);
	Spindle_STATE = 1;  // remember we are CW
}

M5.c (executed .out)

Code: Select all

#include "KMotionDef.h"
#include "Milling_Machine_24K40_Hardware.h"
/*
  //==== All defined in "Milling_Machine_24K40_Hardware.h" ====
         #define FACTOR              (11468.8/60.0)             //1000 counts/sec / 100 counts/rev = 1 RPS = 60 RPM
         #define Last_SPEED          persist.UserData[2]        //save in user variable Last_SPEED the last desired speed
         #define Desired_SPEED       persist.UserData[3]        //desired speed is passed from KMotionCNC in variable Desired_SPEED.
         #define Spindle_STATE       persist.UserData[4]        //save in user variable Spindle_STATE whether it was off, CW, or CCW (0,1,-1) 
*/

main()
{
	// spin down	
	Jog(Spindle,0); //Spindle stop
	while (!CheckDone(Spindle)) StopCoordinatedMotion(); //Spindle full stop	
        ResumeCoordinatedMotion();
	Spindle_STATE = 0;  // remember we are Off
	DefineCoordSystem6(0,1,2,-1,-1,5); //Spindle as C axis
	printf("Jogging Spindle Stop\n");	
}

Everything is working.
But there's a problem: when you first run a G code, even if the code does not contain spindle or C-axis control, a warning appears:
IMG_20250808_235839.jpg
Then everything gets more interesting. Periodically (sometimes), KMotionCNC does not run .C and .Out programs in KFLOP. Variables persist.UserData[] exchange is lost. Sometimes there is a complete loss of Kflop control via KMotionCNC.
However, KMotion.exe continues to control KFLOP. The console sometimes works. The problem can only be solved by restarting both the PC and KFLOP.

When this does not happen, the spindle and C-axis work and switch normally. I'm not sure if the issue is related to the spindle/C-axis switch, but I haven't noticed it before.

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

Re: Switching coordinate systems.

Post by TomKerekes » Tue Sep 09, 2025 4:54 pm

But there's a problem: when you first run a G code, even if the code does not contain spindle or C-axis control, a warning appears:
As it says this indicates that your V A J settings for the C Axis are such that it would take 4 seconds to stop. Most systems can stop in a fraction of a second. Are they correct?

Periodically (sometimes), KMotionCNC does not run .C and .Out programs in KFLOP. Variables persist.UserData[] exchange is lost.
What do you mean by this? Why are you using .out files? What happens exactly? What Version are you running?
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 61
Joined: Wed May 03, 2023 12:54 am

Re: Switching coordinate systems.

Post by Alexanders » Sat Sep 27, 2025 10:53 pm

As it says this indicates that your V A J settings for the C Axis are such that it would take 4 seconds to stop. Most systems can stop in a fraction of a second. Are they correct?
Yes, that's right. I have a heavy spindle that accelerates to 4000 rpm in 4 seconds. It can operate also as a C-axis and is controlled and feedback by a quadrature signal 8200 PPR. And it works great in both modes. However, if the C axis is activated immediately during initialization, the trajectory planner displays a message, even if the G code does not contain commands for the spindle or C axis. And I understand that too.
But after this message, problems may start. Or problems may not start.
What do you mean by this? Why are you using .out files? What Version are you running?
I use .out files to avoid unnecessary compilation. I've also used .C files, and they've worked just as well. I don't think this has anything to do with the problem.
I am using version 5.3.7 and Win10 22H1.
What happens exactly?
After receiving this message from the trajectory planner, there are three possible outcomes:
1. Everything is working fine.
2. KMotionCNC freezes. (rare)
3. KMotionCNC not freeze, and G codes are executed on the machine. But at the same time KMotionCNC does not run .C and .Out programs in KFLOP. Variables persist.UserData[] exchange is lost.

Restarting KMotionCNC does not solve the problem.
Restarting KFlop does not solve the problem.
Restarting both KMotionCNC and KFlop does not solve the problem.
Only restarting both the PC and KFlop.

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

Re: Switching coordinate systems.

Post by TomKerekes » Mon Sep 29, 2025 10:40 pm

If an axis in the Coordinate System takes 4 seconds to stop then KMotion requires more lookahead than this to be able to expect to have enough data to always be able to stop. So you might set the buffer time to 6 seconds. Or what might be better is to reduce the max velocity before including the axis in the Coordinate System. I'm assuming you don't need 4000RPM as an indexed axis?

Version 5.3.7 (and before) had a bug which left a file open when downloading programs to Kogna. After 256 files were left open Windows blocks any other files from being open. This caused all manner of errors. However this seems somewhat different from what you describe. But please upgrade to the latest Version.
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 61
Joined: Wed May 03, 2023 12:54 am

Re: Switching coordinate systems.

Post by Alexanders » Sat Oct 25, 2025 10:08 pm

But please upgrade to the latest Version.
Thank you! I updated from version 5.3.7 to version 5.4.1 and the problem did not occur.

But in version 5.4.1, there is another problem. The M99 code causes an error: G Code Error Bad character used. Other M-codes do not cause errors.
How can I fix this?

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

Re: Switching coordinate systems.

Post by TomKerekes » Sat Oct 25, 2025 10:45 pm

I'm not able to reproduce this. I run the example Subroutine.ngc without error. Does it run for you? Can you post a small GCode file that demonstrates the error?
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 61
Joined: Wed May 03, 2023 12:54 am

Re: Switching coordinate systems.

Post by Alexanders » Sun Oct 26, 2025 9:13 pm

Can you post a small GCode file that demonstrates the error?
Lubrication.ngc

Code: Select all

G53 G01 
G53 Z-1 F6000
G53 X-10 Y-10 
G53 X-610 Y-405
M99
This is a cyclically executed machine lubrication program. It worked on version 5.3.7 and earlier. I also use a similar program on other CNC systems.

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

Re: Switching coordinate systems.

Post by TomKerekes » Fri Oct 31, 2025 12:59 am

M99 (subroutine return) wasn't intended to be called without a subroutine call. The previous looping behavior was unintentional and only worked in certain cases. It now fails if the file has an UTF-8 BOM at the beginning of the file. We will fix this in the next Version. In the mean time M47 can be used to loop an entire file.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply