# Include your custom HAL commands here
# This file will not be overwritten when you run stepconf again
loadrt near names=spindle-at-speed
loadrt siggen num_chan=1 #spincle speed control
loadrt and2 count=4      # 0 used in e-stop chain,
                         # 1,2,3 used för connecting spindle speed gain selection
loadrt classicladder_rt
loadrt timedelay count=1

addf spindle-at-speed servo-thread
addf scale.1          servo-thread
addf scale.2          servo-thread
addf scale.3          servo-thread
addf scale.4          servo-thread
addf siggen.0.update  servo-thread
addf and2.0           servo-thread
addf and2.1           servo-thread
addf and2.2           servo-thread
addf and2.3           servo-thread
addf classicladder.0.refresh servo-thread
addf timedelay.0 servo-thread

loadusr -Wn my-mpg python3 serialEventHandler.py --port=/dev/ttyUSB0 -c my-mpg mpg.xml
loadusr classicladder test_ladder.clp --nogui
#loadusr classicladder test_ladder.clp

### spindle at speed monitoring ###
setp spindle-at-speed.scale 0.98
setp spindle-at-speed.difference 10
net spindle-cmd-rpm => spindle-at-speed.in1
#net spindle-rpm-filtered-low-res => spindle-at-speed.in2 # moved to postgui

### temp... until encoder works...
#net spindle-ready <= spindle-at-speed.out => spindle.0.at-speed
net spindle-ready => spindle.0.at-speed
sets spindle-ready 1
###

### spindle cw start/stop ###
net spindle-cw <= spindle.0.forward
net spindle-cw => parport.0.pin-14-out # turn on spindle
### spindle ccw start/stop ###
net spindle-ccw <= spindle.0.reverse
net spindle-ccw => parport.0.pin-17-out # turn on spindle ccw

### spindle speed ctrl ###
# gain and offset is set via M199/O<myspindlegain> when homed 
#setp scale.1.gain 0.473
#setp scale.1.offset 26.5
setp siggen.0.amplitude 1
setp siggen.0.offset 0

net spindle-cmd-rpm-abs => scale.1.in
net scaled-spindle-cmd scale.1.out => siggen.0.frequency 
net spindle-freq siggen.0.clock => parport.0.pin-16-out


### e-stop chain ###
net estop-internal <= iocontrol.0.user-enable-out
#estop-external connected to parport.0.pin-11-in-not 

net estop-internal => and2.0.in0
net estop-external => and2.0.in1
net estop-chain and2.0.out => iocontrol.0.emc-enable-in


### pendant ##########################################

# mpg spindle speed control
net spindle-rpm-cmd-up    classicladder.0.out-08 halui.mdi-command-00 
net spindle-rpm-cmd-down  classicladder.0.out-09 halui.mdi-command-01 

# axis selector connects to x, z axis enable
net jog-axis-sel    my-mpg.axis-selector
net jog-axis-sel    classicladder.0.s32in-00

net jog-x-enable    classicladder.0.out-00
net jog-z-enable    classicladder.0.out-01
net jog-x-enable    halui.axis.x.select
net jog-z-enable    halui.axis.z.select

# jog scale selector/velocity mode
setp scale.2.gain 0.01
setp scale.2.offset 0
net jog-scale-sel      my-mpg.scale-selector
net jog-scale-sel      classicladder.0.s32in-01
net jog-increment-100  classicladder.0.floatout-00     scale.2.in
net jog-increment      halui.axis.selected.increment   scale.2.out

# jogging plus and minus
setp halui.axis.jog-speed 800
net mpg-jog-plus-in     my-mpg.jog-pos-btn    => classicladder.0.in-00
net mpg-jog-minus-in    my-mpg.jog-neg-btn    => classicladder.0.in-01

net mpg-jog-plus-inc   classicladder.0.out-04 => halui.axis.selected.increment-plus
net mpg-jog-minus-inc  classicladder.0.out-06 => halui.axis.selected.increment-minus  
net mpg-jog-plus       classicladder.0.out-05 => halui.axis.selected.plus
net mpg-jog-minus      classicladder.0.out-07 => halui.axis.selected.minus

# connect Func-button
net func-button    my-mpg.func-btn halui.mdi-command-02

# connect E-Stop button
net estop-button       halui.estop.activate   <= my-mpg.estop-btn

# joystic
setp scale.3.gain 0.01
setp scale.3.offset 0
setp scale.4.gain 0.01
setp scale.4.offset 0
net joystick-x my-mpg.joystick-x scale.3.in
net joystick-x-scaled scale.3.out halui.axis.x.analog

net joystick-z my-mpg.joystick-z scale.4.in
net joystick-z-scaled scale.4.out halui.axis.z.analog 

### Misc #################################################

# all-homed used to enable some of the panels and trigger some other stuff
net all-homed motion.is-all-homed

# set the saved spindle speed selection gain
#setp timedelay.0.on-delay 1
net all-homed timedelay.0.in
net all-homed-delayed timedelay.0.out
net all-homed-delayed halui.mdi-command-06

