# HAL config file for MAX NC test machine
#######################################################
# first load all the HAL modules we're going to need
#######################################################
# kinematics
loadrt [KINS]KINEMATICS
# main motion controller module
loadrt [EMCMOT]EMCMOT base_period_nsec=[EMCMOT]BASE_PERIOD servo_period_nsec=[EMCMOT]SERVO_PERIOD num_joints=[KINS]JOINTS
# using steppers....
loadrt stepgen step_type=0,0,0
# I/O thru the parport
loadrt hal_parport cfg="0x0378"
# counting the spindle counter in software
loadrt counter num_chan=1
# misc blocks needed to do fun things
loadrt ddt count=6
# spindle speed pwm
loadrt pwmgen output_type=0
# spindle speed feedback
loadrt lowpass count=1
loadrt mult2 count=1
loadrt scale count=1

#######################################################
# add realtime functions to the threads
# first the high speed thread
#######################################################
# first we read parport inputs
addf parport.0.read		base-thread
# update counter
addf counter.update-counters    base-thread
# generate step and dir signals
addf stepgen.make-pulses	base-thread
# generate spindle pwm
addf pwmgen.make-pulses        base-thread
# write to outputs
addf parport.0.write		base-thread



# now the servo thread
########################################################
# capture axis and spindle positions
addf stepgen.capture-position	servo-thread
addf counter.capture-position	servo-thread
# process motion commands coming from user space
addf motion-command-handler	servo-thread
# run main motion controller
addf motion-controller		servo-thread
# differentiators to make vel and acc signals
addf ddt.0			servo-thread
addf ddt.1			servo-thread
addf ddt.2			servo-thread
addf ddt.3			servo-thread
addf ddt.4			servo-thread
addf ddt.5			servo-thread
# other misc blocks
addf scale.0			servo-thread
addf pwmgen.update              servo-thread
# update output info, axis and spindle
addf stepgen.update-freq	servo-thread
addf lowpass.0			servo-thread
addf mult2.0			servo-thread


#######################################################
# interconnections
#
# first, the basic stepper machine connections
#######################################################

# connect position commands from motion module to step generator
net Xpos-cmd <= joint.0.motor-pos-cmd
net Xpos-cmd => stepgen.0.position-cmd
net Ypos-cmd <= joint.1.motor-pos-cmd
net Ypos-cmd => stepgen.1.position-cmd
net Zpos-cmd <= joint.2.motor-pos-cmd
net Zpos-cmd => stepgen.2.position-cmd

# connect position feedback from step generators
# to motion module
net Xpos-fb <= stepgen.0.position-fb
net Xpos-fb => joint.0.motor-pos-fb
net Ypos-fb <= stepgen.1.position-fb
net Ypos-fb => joint.1.motor-pos-fb
net Zpos-fb <= stepgen.2.position-fb
net Zpos-fb => joint.2.motor-pos-fb

# connect enable signals for step generators
net Xen <= joint.0.amp-enable-out
net Xen => stepgen.0.enable
net Yen <= joint.1.amp-enable-out
net Yen => stepgen.1.enable
net Zen <= joint.2.amp-enable-out
net Zen => stepgen.2.enable

# connect signals to step pulse generator outputs
net Xstep <= stepgen.0.step
net Xdir <= stepgen.0.dir
net Ystep <= stepgen.1.step
net Ydir <= stepgen.1.dir
net Zstep <= stepgen.2.step
net Zdir <= stepgen.2.dir

#############################################################################
# Spindle Speed
# The spindle goes forward only
net spindle-forward <= spindle.0.forward
net spindle-forward => pwmgen.0.enable

# PID is not used to give closed-loop spindle speed control, because at the
# highest speeds the software-based counter cannot keep up (note: with 'counter',
# it can)
# Instead, this offset and scale have been determined empirically
# -- if these numbers are accurate, then top speed at this gearing is about
# 1200 rpm
#
# gain = 1/1128 ~ .00089
# offset = .07
# PWM    Measured RPM
# 25%    200
# 50%    482
# 60%    595
setp scale.0.gain .00089
setp scale.0.offset .07

net spindle-speed-cmd <= spindle.0.speed-out
net spindle-speed-cmd => scale.0.in
net spindle-speed-pwm <= scale.0.out
net spindle-speed-pwm => pwmgen.0.value

# Spindle speed feedback -- spindle-speed-fb-rpm can be metered or scoped
# but beware that at the highest speeds the software-based counter cannot
# keep up, and the speed is estimated incorrectly

# The gain of the lowpass filter is a trade-off: Higher numbers track spindle
# speed better when the speed changes.  Lower numbers give a more steady
# estimate of average spindle speed when the speed is nominally constant
setp lowpass.0.gain 0.01
net spindle-speed-fb-raw <= counter.0.velocity
net spindle-speed-fb-raw => lowpass.0.in
net spindle-speed-fb-filtered <= lowpass.0.out

setp mult2.0.in0 60
net spindle-speed-fb-filtered mult2.0.in1
net spindle-speed-fb-rpm mult2.0.out
#############################################################################

# create a signal for the estop loopback
net estop-loop iocontrol.0.user-enable-out iocontrol.0.emc-enable-in

# create signals for tool loading loopback
net tool-prep-loop iocontrol.0.tool-prepare iocontrol.0.tool-prepared
net tool-change-loop iocontrol.0.tool-change iocontrol.0.tool-changed

# create a signal for "spindle on"
net spindle_on <= spindle.0.on

# connect physical pins to the signals
net Xstep => parport.0.pin-03-out
net Xdir => parport.0.pin-02-out
net Ystep => parport.0.pin-05-out
net Ydir => parport.0.pin-04-out
net Zstep => parport.0.pin-07-out
net Zdir => parport.0.pin-06-out
# amp enable (active lo)
#linksp Xen		=> parport.0.pin-01-out
#setp parport.0.pin-01-out-invert 1

# spindle enable
net spindle_on => parport.0.pin-01-out
setp parport.0.pin-01-out-invert 1

net pwm pwmgen.0.pwm	=> parport.0.pin-14-out

#######################################################
# set scaling and other parameters of the basic machine

# set stepgen module scaling - get values from ini file
setp stepgen.0.position-scale [JOINT_0]SCALE
setp stepgen.1.position-scale [JOINT_1]SCALE
setp stepgen.2.position-scale [JOINT_2]SCALE

# set stepgen module accel limits - get values from ini file
# jmk said to set these to ini value + 5% to avoid stepgen bug
setp stepgen.0.maxaccel [JOINT_0]STEPGEN_MAXACCEL
setp stepgen.1.maxaccel [JOINT_1]STEPGEN_MAXACCEL
setp stepgen.2.maxaccel [JOINT_2]STEPGEN_MAXACCEL

# end of basic machine

net homeswitches <= parport.0.pin-10-in
net homeswitches => joint.0.home-sw-in
net homeswitches => joint.2.home-sw-in

#######################################################
# Beginning of threading-related stuff
#######################################################

# spindle counter
# connect encoder signals to counter counter
# B is not used because it is the counter module, not encoder
net spindle-phase-A => counter.0.phase-A
net spindle-phase-Z => counter.0.phase-Z

net spindle-phase-A <= parport.0.pin-11-in
net spindle-phase-B <= parport.0.pin-12-in
net spindle-phase-Z <= parport.0.pin-13-in

# (that way thread pitches can be straightforward,
#  a 20 tpi thread would multiply the counter output
#  by 1/20, etc)
setp counter.0.position-scale 1024

# hook up motion controller's spindle indexing pin
net spindle-index-enable <= spindle.0.index-enable
net spindle-index-enable <=> counter.0.index-enable

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


#######################################################
# make vel and accel sigs for testing
#######################################################

# send the position commands thru differentiators to
# generate velocity and accel signals (for testing)

# define the signals, and hook them up
net Xpos-cmd => ddt.0.in
net Xvel <= ddt.0.out
net Xvel => ddt.1.in
net Xacc <= ddt.1.out

net Ypos-cmd => ddt.2.in
net Yvel <= ddt.2.out
net Yvel => ddt.3.in
net Yacc <= ddt.3.out

net Zpos-cmd => ddt.4.in
net Zvel <= ddt.4.out
net Zvel => ddt.5.in
net Zacc <= ddt.5.out
