#
# this is a simple sample HAL config file for the hostmot2 driver
# it is configured with environment variables:
#
# HM2_BOARD is board type, ie "7i43" or "5i20"
#
# HM2_FIRMWARE is the full-path filename of the firmware .BIT file to use
#
# components
loadrt pid num_chan=4
# only the 7i43 needs this, but it doesnt hurt the others
loadrt probe_parport
#
# 0x378,0x778 is the on-board parallel port (cable has radioshack logo)
# 0xdc48,0xdc50 is the parallel port on the pci card (cable has no logo)
#
loadrt hostmot2 debug_idrom=1 debug_module_descriptors=1 debug_pin_descriptors=1 debug_modules=1
loadusr -w /home/seb/emc2/bfload/bin/bfload $(HM2_BOARD)=$(HM2_FIRMWARE)
loadrt hm2_$(HM2_BOARD) # config="enable_raw num_stepgens=2"
#
# set up the watchdog
#
setp hm2_$(HM2_BOARD).0.watchdog.timeout_ns 10000000
#
# set up the encoders, encoder position becomes pid's feedback
#
setp hm2_$(HM2_BOARD).0.encoder.00.scale -1024
setp hm2_$(HM2_BOARD).0.encoder.01.scale -2048
setp hm2_$(HM2_BOARD).0.encoder.02.scale -2048
net motor-00-position hm2_$(HM2_BOARD).0.encoder.00.position => pid.0.feedback
net motor-01-position hm2_$(HM2_BOARD).0.encoder.01.position => pid.1.feedback
#
# let the pid outputs drive the pwm generators
#
setp hm2_$(HM2_BOARD).0.pwmgen.00.scale .0002442002
setp hm2_$(HM2_BOARD).0.pwmgen.01.scale .0002442002
setp hm2_$(HM2_BOARD).0.pwmgen.02.scale .0002442002
net pid-00-command pid.0.output => hm2_$(HM2_BOARD).0.pwmgen.00.value
net pid-01-command pid.1.output => hm2_$(HM2_BOARD).0.pwmgen.01.value
#
# PID config
#
# Tuning on the workbench:
# without a load
# servo thread at 1 ms
# encoder.scale = -1024
# pwmgen.scale = .0002442002 (goes from -1.0 to +1.0
#
#
# Old:
#
# I get oscillation at Pgain = Kc = 14.5, no oscillation at 14.
# Oscillation period is Pc = 0.029 s.
#
# So by Ziegler-Nichols, I should have P=0.6Kc, I=Pc/2, D=Pc/8
#
# This gives me 5 or 6 zero-crossing overshoots on the error signal,
# and it settles with an error up to 0.027 revolutions.
#
# In the steady state, the motor is squeaky because the PWM duty cycle
# never goes to zero. If i turn up the pid.deadband, the stable error gets
# worse.
#
# By twisting the shaft slowly, I can get a stable error of up to 0.067.
#
setp pid.0.Pgain 2.0
setp pid.0.Igain 0.0
setp pid.0.Dgain 0.0
setp pid.0.FF0 0
setp pid.0.FF1 0
setp pid.0.FF2 0
setp pid.0.bias 0
setp pid.0.deadband 0.0005
setp pid.0.maxoutput 0
setp pid.1.Pgain 2.0
setp pid.1.Igain 0.0
setp pid.1.Dgain 0.0
setp pid.1.FF0 0
setp pid.1.FF1 0
setp pid.1.FF2 0
setp pid.1.bias 0
setp pid.1.deadband 0.0005
setp pid.1.maxoutput 0
#
# stepgen
#
setp hm2_$(HM2_BOARD).0.stepgen.00.dirsetup 0.0000002
setp hm2_$(HM2_BOARD).0.stepgen.00.dirhold 0.0000002
setp hm2_$(HM2_BOARD).0.stepgen.00.steplen 0.000040
setp hm2_$(HM2_BOARD).0.stepgen.00.stepspace 0.000040
setp hm2_$(HM2_BOARD).0.stepgen.00.position-scale 1600
setp hm2_$(HM2_BOARD).0.stepgen.01.dirsetup 0.0000002
setp hm2_$(HM2_BOARD).0.stepgen.01.dirhold 0.0000002
setp hm2_$(HM2_BOARD).0.stepgen.01.steplen 0.000040
setp hm2_$(HM2_BOARD).0.stepgen.01.stepspace 0.000040
setp hm2_$(HM2_BOARD).0.stepgen.01.position-scale 1600
#setp hm2_$(HM2_BOARD).0.stepgen.02.dirsetup 0.0000002
#setp hm2_$(HM2_BOARD).0.stepgen.02.dirhold 0.0000002
#
#setp hm2_$(HM2_BOARD).0.stepgen.02.steplen 0.000040
#setp hm2_$(HM2_BOARD).0.stepgen.02.stepspace 0.000040
#
#setp hm2_$(HM2_BOARD).0.stepgen.02.position-scale 1600
#setp hm2_$(HM2_BOARD).0.stepgen.03.dirsetup 0.0000002
#setp hm2_$(HM2_BOARD).0.stepgen.03.dirhold 0.0000002
#
#setp hm2_$(HM2_BOARD).0.stepgen.03.steplen 0.000040
#setp hm2_$(HM2_BOARD).0.stepgen.03.stepspace 0.000040
#
#setp hm2_$(HM2_BOARD).0.stepgen.03.position-scale 1600
#
# stepper 0 and servos 0 and 1 clone servo 2's position
#
net follow-the-leader hm2_$(HM2_BOARD).0.encoder.02.position => pid.0.command pid.1.command hm2_$(HM2_BOARD).0.stepgen.00.position-cmd hm2_$(HM2_BOARD).0.stepgen.01.position-cmd
#
# enable
#
newsig Enable bit
sets Enable FALSE
net Enable => pid.0.enable pid.1.enable hm2_$(HM2_BOARD).0.pwmgen.00.enable hm2_$(HM2_BOARD).0.pwmgen.01.enable
#
# realtime threads & functions
#
loadrt threads name1=main fp1=1 period1=1000000
addf hm2_$(HM2_BOARD).0.pet_watchdog main
addf hm2_$(HM2_BOARD).0.read main
addf pid.0.do-pid-calcs main
addf pid.1.do-pid-calcs main
addf hm2_$(HM2_BOARD).0.write main
#
# ok, all set up
#
start
sets Enable 1