Closed loop spindle
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
18 Sep 2016 18:30 #80574
by jeeybee
Replied by jeeybee on topic Closed loop spindle
I installed the same hal code as above on a lathe, only modifying it to use encoder instead of the switch and the speed control work fine, the use of the index signal for threading also work fine (the starting point at least).
But, when switching feedrate to mm per row it does not add up at all, and also the thread does not make the right pitch,
if I divide it with 60 it will be almost correct but not exactly right.
For example when I set it like below:
M3 s500
G95
G1 g90 x0 f0.01
I get a feedrate of 300mm/min or 5mm/sek
So it seem like the feedrate get 0.01 mm/sek of s500.
Any idea what could be wrong?
But, when switching feedrate to mm per row it does not add up at all, and also the thread does not make the right pitch,
if I divide it with 60 it will be almost correct but not exactly right.
For example when I set it like below:
M3 s500
G95
G1 g90 x0 f0.01
I get a feedrate of 300mm/min or 5mm/sek
So it seem like the feedrate get 0.01 mm/sek of s500.
Any idea what could be wrong?
Please Log in or Create an account to join the conversation.
- andypugh
-
- Offline
- Moderator
-
Less
More
- Posts: 23271
- Thank you received: 4928
18 Sep 2016 19:26 #80575
by andypugh
Replied by andypugh on topic Closed loop spindle
There are a number of things a bit odd with that HAL code.
There is no need to net the encoder-A, B, Z pins. those just exist to let you watch the inits to the hardware encoder counter. HAL does not need them. Note also that that HAL nets two of them to the same signal.
motion.spindle-revs needs to see a signal that increases by one count per rev.
motion.spindle-speed-in needs to see an input in RPM.
So, encoder position goes into motion.spindle-revs unchanged. motion.spindle-speed-in needs encoder velocity * 60 (which is where the scale component comes in)
There is no need to net the encoder-A, B, Z pins. those just exist to let you watch the inits to the hardware encoder counter. HAL does not need them. Note also that that HAL nets two of them to the same signal.
motion.spindle-revs needs to see a signal that increases by one count per rev.
motion.spindle-speed-in needs to see an input in RPM.
So, encoder position goes into motion.spindle-revs unchanged. motion.spindle-speed-in needs encoder velocity * 60 (which is where the scale component comes in)
Please Log in or Create an account to join the conversation.
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
27 Sep 2016 08:08 #80973
by jeeybee
Replied by jeeybee on topic Closed loop spindle
Below is the modified HAL code that use a real encoder:
Here I use the encoder scale and should not Linuxcnc show and run the spindle at the wrong speed if the settings are not correct?
As it is now I just pass it through the scale component but still spindle-fb-rpm show the right speed even though the scale is 1.
#*******************
# SPINDLE S
#*******************
loadrt scale count=2
loadrt lowpass names=lowpass.spindle
loadrt abs names=abs.spindle
addf scale.1 servo-thread #spindle scale
addf lowpass.spindle servo-thread
addf abs.spindle servo-thread
addf pid.3.do-pid-calcs servo-thread
setp pid.3.Pgain [SPINDLE_9]P
setp pid.3.Igain [SPINDLE_9]I
setp pid.3.Dgain [SPINDLE_9]D
setp pid.3.bias [SPINDLE_9]BIAS
setp pid.3.FF0 [SPINDLE_9]FF0
setp pid.3.FF1 [SPINDLE_9]FF1
setp pid.3.FF2 [SPINDLE_9]FF2
setp pid.3.deadband [SPINDLE_9]DEADBAND
setp pid.3.maxoutput [SPINDLE_9]MAX_OUTPUT
setp pid.3.error-previous-target true
setp pid.3.maxerror 1000
net spindle-index-enable <=> pid.3.index-enable
net spindle-enable => pid.3.enable
net spindle-vel-cmd-rpm-abs => pid.3.command
#net spindle-vel-fb-rpm => pid.3.feedback
net spindle-fb-rpm-abs => pid.3.feedback
net spindle-output <= pid.3.output
# ---PWM Generator signals/setup---
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.scale [SPINDLE_9]ENCODER_SCALE
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-scalemax [SPINDLE_9]OUTPUT_SCALE
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-minlim [SPINDLE_9]OUTPUT_MIN_LIMIT
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-maxlim [SPINDLE_9]OUTPUT_MAX_LIMIT
net spindle-enable => hm2_[HOSTMOT2](BOARD).0.7i77.0.1.spinena
net spindle-output => hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4
# ---Encoder feedback signals/setup---
# encoder feedback
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.counter-mode 1
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.filter 1
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-invert 0
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-mask 0
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-mask-invert 0
# connect the HAL encoder inputs to the real encoder.
net spindle_index_in <= hm2_[HOSTMOT2](BOARD).0.encoder.04.input-index
#net spindle_index_in => encoder.0.phase-A
#net spindle-phase-b encoder.0.phase-B
#net spindle_index_in => encoder.0.phase-Z
net spindle-revs <= hm2_[HOSTMOT2](BOARD).0.encoder.04.position
net spindle-vel-fb-rps <= hm2_[HOSTMOT2](BOARD).0.encoder.04.velocity
net spindle-index-enable <=> hm2_[HOSTMOT2](BOARD).0.encoder.04.index-enable
# ---setup spindle control signals---
net spindle-vel-cmd-rps <= motion.spindle-speed-out-rps
net spindle-vel-cmd-rps-abs <= motion.spindle-speed-out-rps-abs
net spindle-vel-cmd-rpm <= motion.spindle-speed-out
net spindle-vel-cmd-rpm-abs <= motion.spindle-speed-out-abs
net spindle-enable <= motion.spindle-on
net spindle-cw <= motion.spindle-forward
net spindle-ccw <= motion.spindle-reverse
net spindle-brake <= motion.spindle-brake
net spindle-revs => motion.spindle-revs
net spindle-at-speed => motion.spindle-at-speed
net spindle-vel-fb-rps => motion.spindle-speed-in
net spindle-index-enable <=> motion.spindle-index-enable
# ---Setup spindle at speed signals---
sets spindle-at-speed true
# Use ACTUAL spindle velocity from spindle encoder
# spindle-velocity bounces around so we filter it with lowpass
# spindle-velocity is signed so we use absolute component to remove sign
# ACTUAL velocity is in RPS not RPM so we scale it.
setp scale.1.gain 1
setp lowpass.spindle.gain 1.000000
net spindle-vel-fb-rps => scale.1.in
net spindle-fb-rpm scale.1.out => abs.spindle.in
net spindle-fb-rpm-abs abs.spindle.out => lowpass.spindle.in
net spindle-fb-rpm-abs-filtered lowpass.spindle.out
# --- SPINDLE-CCW ---
net spindle-ccw hm2_[HOSTMOT2](BOARD).0.7i77.0.0.output-03
# --- SPINDLE-CW ---
net spindle-cw hm2_[HOSTMOT2](BOARD).0.7i77.0.0.output-02
loadusr halmeter -s signal spindle-fb-rpm -g 800 100
Here I use the encoder scale and should not Linuxcnc show and run the spindle at the wrong speed if the settings are not correct?
As it is now I just pass it through the scale component but still spindle-fb-rpm show the right speed even though the scale is 1.
#*******************
# SPINDLE S
#*******************
loadrt scale count=2
loadrt lowpass names=lowpass.spindle
loadrt abs names=abs.spindle
addf scale.1 servo-thread #spindle scale
addf lowpass.spindle servo-thread
addf abs.spindle servo-thread
addf pid.3.do-pid-calcs servo-thread
setp pid.3.Pgain [SPINDLE_9]P
setp pid.3.Igain [SPINDLE_9]I
setp pid.3.Dgain [SPINDLE_9]D
setp pid.3.bias [SPINDLE_9]BIAS
setp pid.3.FF0 [SPINDLE_9]FF0
setp pid.3.FF1 [SPINDLE_9]FF1
setp pid.3.FF2 [SPINDLE_9]FF2
setp pid.3.deadband [SPINDLE_9]DEADBAND
setp pid.3.maxoutput [SPINDLE_9]MAX_OUTPUT
setp pid.3.error-previous-target true
setp pid.3.maxerror 1000
net spindle-index-enable <=> pid.3.index-enable
net spindle-enable => pid.3.enable
net spindle-vel-cmd-rpm-abs => pid.3.command
#net spindle-vel-fb-rpm => pid.3.feedback
net spindle-fb-rpm-abs => pid.3.feedback
net spindle-output <= pid.3.output
# ---PWM Generator signals/setup---
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.scale [SPINDLE_9]ENCODER_SCALE
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-scalemax [SPINDLE_9]OUTPUT_SCALE
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-minlim [SPINDLE_9]OUTPUT_MIN_LIMIT
setp hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4-maxlim [SPINDLE_9]OUTPUT_MAX_LIMIT
net spindle-enable => hm2_[HOSTMOT2](BOARD).0.7i77.0.1.spinena
net spindle-output => hm2_[HOSTMOT2](BOARD).0.7i77.0.1.analogout4
# ---Encoder feedback signals/setup---
# encoder feedback
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.counter-mode 1
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.filter 1
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-invert 0
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-mask 0
setp hm2_[HOSTMOT2](BOARD).0.encoder.04.index-mask-invert 0
# connect the HAL encoder inputs to the real encoder.
net spindle_index_in <= hm2_[HOSTMOT2](BOARD).0.encoder.04.input-index
#net spindle_index_in => encoder.0.phase-A
#net spindle-phase-b encoder.0.phase-B
#net spindle_index_in => encoder.0.phase-Z
net spindle-revs <= hm2_[HOSTMOT2](BOARD).0.encoder.04.position
net spindle-vel-fb-rps <= hm2_[HOSTMOT2](BOARD).0.encoder.04.velocity
net spindle-index-enable <=> hm2_[HOSTMOT2](BOARD).0.encoder.04.index-enable
# ---setup spindle control signals---
net spindle-vel-cmd-rps <= motion.spindle-speed-out-rps
net spindle-vel-cmd-rps-abs <= motion.spindle-speed-out-rps-abs
net spindle-vel-cmd-rpm <= motion.spindle-speed-out
net spindle-vel-cmd-rpm-abs <= motion.spindle-speed-out-abs
net spindle-enable <= motion.spindle-on
net spindle-cw <= motion.spindle-forward
net spindle-ccw <= motion.spindle-reverse
net spindle-brake <= motion.spindle-brake
net spindle-revs => motion.spindle-revs
net spindle-at-speed => motion.spindle-at-speed
net spindle-vel-fb-rps => motion.spindle-speed-in
net spindle-index-enable <=> motion.spindle-index-enable
# ---Setup spindle at speed signals---
sets spindle-at-speed true
# Use ACTUAL spindle velocity from spindle encoder
# spindle-velocity bounces around so we filter it with lowpass
# spindle-velocity is signed so we use absolute component to remove sign
# ACTUAL velocity is in RPS not RPM so we scale it.
setp scale.1.gain 1
setp lowpass.spindle.gain 1.000000
net spindle-vel-fb-rps => scale.1.in
net spindle-fb-rpm scale.1.out => abs.spindle.in
net spindle-fb-rpm-abs abs.spindle.out => lowpass.spindle.in
net spindle-fb-rpm-abs-filtered lowpass.spindle.out
# --- SPINDLE-CCW ---
net spindle-ccw hm2_[HOSTMOT2](BOARD).0.7i77.0.0.output-03
# --- SPINDLE-CW ---
net spindle-cw hm2_[HOSTMOT2](BOARD).0.7i77.0.0.output-02
loadusr halmeter -s signal spindle-fb-rpm -g 800 100
Please Log in or Create an account to join the conversation.
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
27 Sep 2016 08:11 #80974
by jeeybee
Replied by jeeybee on topic Closed loop spindle
This is the INI part
#********************
# Spindle
#********************
[SPINDLE_9]
P = 10
I = 0.3
D = 0
FF0 = 1.0
FF1 = 0.0
FF2 = 0.0
BIAS = 0.0
DEADBAND = 0.0
MAX_OUTPUT = 2000
ENCODER_SCALE = 20
OUTPUT_SCALE = 2000
OUTPUT_MIN_LIMIT = 0.0
OUTPUT_MAX_LIMIT = 2000.0
#********************
# Spindle
#********************
[SPINDLE_9]
P = 10
I = 0.3
D = 0
FF0 = 1.0
FF1 = 0.0
FF2 = 0.0
BIAS = 0.0
DEADBAND = 0.0
MAX_OUTPUT = 2000
ENCODER_SCALE = 20
OUTPUT_SCALE = 2000
OUTPUT_MIN_LIMIT = 0.0
OUTPUT_MAX_LIMIT = 2000.0
Please Log in or Create an account to join the conversation.
- andypugh
-
- Offline
- Moderator
-
Less
More
- Posts: 23271
- Thank you received: 4928
27 Sep 2016 09:14 #80978
by andypugh
I think that you probably need to setp the scale.1.gain to 60 to get RPM,.
The alternative is to use RPS everywhere, there is a pair of pins, motion.spindle-speed-out-rps and motion.spindle-speed-out-abs-rps that you can use to have the spindle speed PID work in RPS all the way though.
Replied by andypugh on topic Closed loop spindle
setp scale.1.gain 1
setp lowpass.spindle.gain 1.000000
net spindle-vel-fb-rps => scale.1.in
net spindle-fb-rpm scale.1.out => abs.spindle.in
I think that you probably need to setp the scale.1.gain to 60 to get RPM,.
The alternative is to use RPS everywhere, there is a pair of pins, motion.spindle-speed-out-rps and motion.spindle-speed-out-abs-rps that you can use to have the spindle speed PID work in RPS all the way though.
Please Log in or Create an account to join the conversation.
- andypugh
-
- Offline
- Moderator
-
Less
More
- Posts: 23271
- Thank you received: 4928
27 Sep 2016 10:53 #80983
by andypugh
Replied by andypugh on topic Closed loop spindle
....And to answer the actual question... (Sorry).
Yes, I would expect the spindle speed to be incorrect with a scale of 1.
(though an FF0 of 1 will give at least an approximation of the right speed until the PID does its thing)
I suggest you have a look with Halmeter at the PID command, feedback, error and output and see if you can figure out what is going awry.
Yes, I would expect the spindle speed to be incorrect with a scale of 1.
(though an FF0 of 1 will give at least an approximation of the right speed until the PID does its thing)
I suggest you have a look with Halmeter at the PID command, feedback, error and output and see if you can figure out what is going awry.
Please Log in or Create an account to join the conversation.
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
27 Sep 2016 11:04 #80984
by jeeybee
Replied by jeeybee on topic Closed loop spindle
Ok thanks I will try some more

Please Log in or Create an account to join the conversation.
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
03 Oct 2016 12:19 #81179
by jeeybee
Tried some more now and When the speed is set to 500 pid command is 500, pid error is about 5, feedback is 500
Spindle revs counted 60 times faster that it should so I added a scale of 0.01699 and now it counts 100 per minut when speed is 100rpm and the spindle feedback also show the right speed, but still when running in g95 x0.2 at 500rpm the speed is about 60 times as fast as it should, the scaling doesnt seem to do anything.
Replied by jeeybee on topic Closed loop spindle
....And to answer the actual question... (Sorry).
Yes, I would expect the spindle speed to be incorrect with a scale of 1.
(though an FF0 of 1 will give at least an approximation of the right speed until the PID does its thing)
I suggest you have a look with Halmeter at the PID command, feedback, error and output and see if you can figure out what is going awry.
Tried some more now and When the speed is set to 500 pid command is 500, pid error is about 5, feedback is 500
Spindle revs counted 60 times faster that it should so I added a scale of 0.01699 and now it counts 100 per minut when speed is 100rpm and the spindle feedback also show the right speed, but still when running in g95 x0.2 at 500rpm the speed is about 60 times as fast as it should, the scaling doesnt seem to do anything.
Please Log in or Create an account to join the conversation.
- andypugh
-
- Offline
- Moderator
-
Less
More
- Posts: 23271
- Thank you received: 4928
03 Oct 2016 12:44 #81183
by andypugh
Have you checked that the signals are consistent where they need to be?
Replied by andypugh on topic Closed loop spindle
This sounds like the PID might be getting command in rpm but feedback in rps.but still when running in g95 x0.2 at 500rpm the speed is about 60 times as fast as it should, the scaling doesnt seem to do anything.
Have you checked that the signals are consistent where they need to be?
Please Log in or Create an account to join the conversation.
- jeeybee
- Offline
- Premium Member
-
Less
More
- Posts: 122
- Thank you received: 0
03 Oct 2016 13:35 #81185
by jeeybee
Replied by jeeybee on topic Closed loop spindle
Hello
I dont know what more to check, the spindle is running at the right speed, motion.spindle-speed-in is correct and
motion.spindle-revs is counting at the right speed, is not that all that matters?
I dont know what more to check, the spindle is running at the right speed, motion.spindle-speed-in is correct and
motion.spindle-revs is counting at the right speed, is not that all that matters?
Please Log in or Create an account to join the conversation.
Time to create page: 0.140 seconds