Spindle Interface II

Moderators: TomKerekes, dynomotion

Post Reply
jackgiz
Posts: 22
Joined: Wed Mar 08, 2023 3:37 am

Spindle Interface II

Post by jackgiz » Fri Sep 08, 2023 5:10 pm

Hi Tom,
You helped me get my spindle up and running a few months ago. Everything worked fairly well except that as the spindle would run the speed would slowly decrease. It looked like something was slowly heating up in my C6 interface board that was causing the problem. I contacted the CNC4PC customer support about the issue and they suggested I upgrade to a C41S board which has a much improved circuit. Only problem is I can't get it to work. Here are the spec's of the new board:

INPUT SPECIFICATIONS
On-state voltage range 2 to 5V DC
Minimum on-state input current 10 mA
Recommended PWM signal frequency (200Hz)
Signal Type Active High

I am running the spindle off of Channel 6, pin 5 on RJ5. How do I know what frequency that channel is outputing? How do I adjust it? I have attached my init file for reference.

Thanks,
Jack
Attachments
Init File 0327.c
(10.35 KiB) Downloaded 20 times

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

Re: Spindle Interface II

Post by TomKerekes » Fri Sep 08, 2023 5:55 pm

Hi Jack,

The C41S expects a PWM (Pulse Width Modification) where the faction of on time (duty cycle) determines the output more or less regardless of the constant frequency. Where the C6 expects a pulse frequency to determine the output regardless of the duty cycle.

The C6 is somewhat simpler to use because a Step/Dir output can be used to control its output in the same manners as stepping motor.

PWM outputs require a couple lines of C to control the PWM. So a loop would be required to send the Axis Output to the PWM. And An axis would need to be configured so the output would be proportional to the commanded speed (Velocity Feed Forward only).

If you want to use the C41S, as a first step connect it to a PWM output (Bit26) and run the program below in some unused Thread. This should cause the C41S to output 5V. Then change the 128 to 0 - 255 to see if the output responds appropriately.

HTH

Code: Select all

#include "KMotionDef.h"

main()
{
	SetBitDirection(26,1);  // define PWM #2 bit as an output JP6 Pin5
	FPGA(IO_PWMS_PRESCALE) = 255;  	// divide clock by 255 (254 Hz)
	FPGA(IO_PWMS) = 128;  			// duty cycle 128 = 50%
	FPGA(IO_PWMS+1) = 1;  			// Enable
}
Last edited by TomKerekes on Fri Sep 08, 2023 5:58 pm, edited 1 time in total.
Reason: Forgot to post C Program
Regards,

Tom Kerekes
Dynomotion, Inc.

jackgiz
Posts: 22
Joined: Wed Mar 08, 2023 3:37 am

Re: Spindle Interface II

Post by jackgiz » Sun Sep 10, 2023 4:06 pm

Hi Tom,
So far so good. I need to play with the adjustment pot a little but basically at 128 I got 5 volts and at 255 I got a little over 10 volts. All this time I thought the C6 was using PWM.

I tried to take the next step and modify the canned spindle programs to drive the spindle but didn't get very far. I set bit 27 (direction) and 26 (speed) in the init file and then used the attached file to drive the spindle CCW. This new C41 board does not require a CCW and CW bit. It's either CCW or it's not so I'm only using bit 27 for direction. Using the console it appears that I'm passing the STATEVAR just fine but for some reason I only get zero's for the SPEEDVAR even when I try to force a test speed 15000 it still prints as zero's on the console. What am I doing wrong?

Also, can you help me understand the difference between KMVAR and SPEEDVAR?

Jack
Attachments
SpindleOnCCWJogDir2.c
(1.33 KiB) Downloaded 27 times

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

Re: Spindle Interface II

Post by TomKerekes » Sun Sep 10, 2023 5:15 pm

Hi Jack,
So far so good. I need to play with the adjustment pot a little but basically at 128 I got 5 volts and at 255 I got a little over 10 volts.
good

Several bugs: Printing the integer SPEEDVAR as a float %f instead of %d. Validate will point out these types of errors:

Checking C:\Users\tk\Downloads\SpindleOnCCWJogDir2.c ...
C:\Users\tk\Downloads\SpindleOnCCWJogDir2.c, line 19 : warning invalidPrintfArgType_float
%f in format string (no. 1) requires 'double' but the argument type is 'signed int'.
printf("Jogging Spindle %f SPEEDVAR/sec\n", SPEEDVAR);
^


Brackets in wrong place. Should be: UserData[SPEEDVAR]

CheckDone takes an axis number not a bit number.

But the main thing you should decide first is how you want to control things. Basically whether to use a KFLOP Axis or not. This would allow you to use Axis Jog commands like you had before. It will also allow using the Axis Acceleration Parameter to gradually ramp up and down the voltage/speed. I would recommend this as it also provides a level of abstraction where the axis is commanded to do things (ie Jog) regardless of the actual type of hardware involved.

Otherwise just writing to the PWM will change the voltage instantly and how it changes speed will be determined entirely by the Spindle Controller.

If using an axis there will need to be a forever loop to output the Axis Output to the PWM. See the Setup Gcode 4 axis PWM signMag.c example

Also, can you help me understand the difference between KMVAR and SPEEDVAR?
KMVAR defines which persist variable that KMotionCNC should use to pass the speed to KFLOP. Its value must match the Variable KMotionCNC's S Action is configured to use.

SPEEDVAR defines which persist variable where the last specified speed is saved.

Normally they would be the same. But its possible that the KMVAR is used by other actions and lost so better to explicitly save it somewhere.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

jackgiz
Posts: 22
Joined: Wed Mar 08, 2023 3:37 am

Re: Spindle Interface II

Post by jackgiz » Mon Sep 25, 2023 9:27 pm

Hi Tom,

I took your advice and am working on using Channel 6 for controlling the spindle. I've started modifying the init file as step 1 but have a question and issue.

First, I'm not sure I have the OutputChan0 and 1 set correctly (lines 361 and 362.

Second, the program validates correctly but won't compile. It keeps asking for a ; on line 416.
I set up a forever loop and then added an If statement to handle an ESTOP but there is a problem with the If.

Attached is my init file.

Thanks for all of your help,

Jack
Attachments
Init File 0925.c
(11.2 KiB) Downloaded 21 times

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

Re: Spindle Interface II

Post by TomKerekes » Mon Sep 25, 2023 10:14 pm

Ji Jack,
First, I'm not sure I have the OutputChan0 and 1 set correctly (lines 361 and 362.
With 'NO_OUTPUT_MODE' nothing is output by the Axis so those aren't used. Your C Code will do the outputting.

Second, the program validates correctly but won't compile. It keeps asking for a ; on line 416.
I set up a forever loop and then added an If statement to handle an ESTOP but there is a problem with the If.
C Programs are case sensitive. You have 'If' instead of ''if'. So the Compiler interprets If as a function call which should have a semicolon afterward.

There are also 2 extraneous close brackets at the very end.


The call to:

Code: Select all

void OutputSignMag(int ch, int pwm, int dir_bit);
Should have the first parameter as the Axis Channel used. So in your case it should be 6.

2nd parameter is the PWM to be used. I think you are using PWM0 ?

3rd parameter is your direction bit. If you don't have a direction bit then some unused bit should be used. Or a Virtual Bit. or an LED (46).

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply