# simulated spindle encoder (for spindle-synced moves)
loadrt sim_spindle names=sim_spindle
setp sim_spindle.scale 0.01666667
loadrt limit2 names=limit_speed
loadrt lowpass names=spindle_mass
loadrt near names=near_speed
loadrt scale names=rpm_rps

setp rpm_rps.gain .0167

# this limit doesn't make any sense to me:
setp limit_speed.maxv 5000.0 # rpm/second

# encoder reset control
# hook up motion controller's sync output
net spindle-index-enable spindle.0.index-enable <=> sim_spindle.index-enable

# report our revolution count to the motion controller
net spindle-pos sim_spindle.position-fb => spindle.0.revs

# simulate spindle mass
setp spindle_mass.gain .07

# spindle speed control
net spindle-speed-cmd  spindle.0.speed-out => limit_speed.in
net spindle-speed-limited limit_speed.out => sim_spindle.velocity-cmd spindle_mass.in

# for spindle velocity estimate
net spindle-rpm-filtered     spindle_mass.out near_speed.in2
net spindle-rpm-filtered     rpm_rps.in
net spindle-rps-filtered     rpm_rps.out spindle.0.speed-in 

# at-speed detection
setp near_speed.scale 1.1
setp near_speed.difference 10

net spindle-speed-cmd => near_speed.in1
net spindle-at-speed near_speed.out spindle.0.at-speed

addf limit_speed servo-thread
addf spindle_mass servo-thread
addf rpm_rps servo-thread
addf near_speed servo-thread
addf sim_spindle servo-thread

