// Here we use 'real encoder with direct PWM'. Further the axis are scaled
// in this configuration, the PID gives 'torque' to iqCmd.
// the phasePos is received from tne encoder on the motor shaft.
// The idCmd is set to 0
// the PID regulates the position by setting torque, if the motor is not at the desired position
// Compared to 'real encoder with direct microstepping', following main elements have to be reconfigured:
// SlipGain=0 (instead 0.25) ,PhasePosSf= calculated value (instead of 0)
// look also at PwmSf,PhaseMode,PhaseCtrl,

// -> PhasePosSf is calculated as follows:  (2048*pole_cycle)/(256*enc_step) = 8*pole_cycle/enc_step

// e.g.   Motor[x].pPhaseEnc -> PowerBrick[.].Chan[.].PhaseCapt.a
//        1 rev = 8192 phase_step = 4 pole_cycle = 512000 PhaseCapt =256*2000 (256=scaling of encTable, 2000=enc_step/rev)
//        PhasePosSf 8*4/2000=0.016


// x einraster == -> x-N and x-S poles =2*x poles -> 1 rev = x*2048 ustep=phase_step
// changing the polarity from S-N-S (one pole cycle) are 2048 phase_step. phase_step is also called ustep

//Mot 1: Stage Y Parker MX80L D11 25mm               one pole cycle = 13mm = 2048 phase_step
//Enc 1: Stage Y Parker MX80L D11 inc_enc 20nm       one pole cycle = 13mm = 650000 enc_step (20nm/enc_step)

//Mot 2: Stage X Parker MX80L D11 25mm               one pole cycle = 13mm = 2048 phase_step
//Enc 2: Stage X Parker MX80L D11 inc_enc 20nm       one pole cycle = 13mm = 650000 enc_step (20nm/enc_step)

//Mot 3: Rotation stage LS Mecapion MDM-DC06DNC0H    32 poles = 1 rev = 16*2048=32768 phase_step
//Enc 3: Rotation stage LS Mecapion                  1 rev = 1048576 enc_steps

//Mot 4: Stage X Stada Stepper                       670mA 200 poles 1 rev = 100*2048 phase_step (2 stepper motor)
//Enc 4: Renishaw absolute BiSS

//Mot 5: Stage Y Stada Stepper                       670mA 200 poles 1 rev = 100*2048 phase_step (2 stepper motor)
//Enc 5: Renishaw absolute BiSS

//Enc 6: Interferometer Y

//Enc 7: Interferometer X

//Stage Y Parker MX80L
//--------------------
//Motor[1].pPhaseEnc -> PowerBrick[0].Chan[1].PhaseCapt.a
// 1 el_step = 13mm = 2048 phase_step = 166400000 PhaseCapt =256*650000 (256=scaling of encTable)
// -> PhasePosSf=(2048*el_cycle)/(256*enc_step) = 8*el_cycle/enc_step =2048*1/(256*650000)=8*1/650000=1./81250=1.23077e-05
//2048 phase_step =166400000 PhaseCapt -> PhasePosSf =  2048/166400000= 2048./(256*650000)
//use um as motor unit
!encoder_sim(enc=1,tbl=9,mot=9,posSf=13000./2048)
!encoder_inc(enc=1,tbl=1,mot=1,posSf=13000./650000)
!motor_servo(mot=1,ctrl='ServoCtrl',Kp=25,Kvfb=400,Ki=0.02,Kvff=350,Kaff=5000,MaxInt=1000)
!motor(mot=1,dirCur=0,contCur=800,peakCur=2400,timeAtPeak=1,IiGain=5,IpfGain=8,IpbGain=8,JogSpeed=10.,numPhase=3,invDir=True,servo=None,PhasePosSf=1./81250,PhaseFindingDac=100,PhaseFindingTime=50,SlipGain=0,AdvGain=0,PwmSf=10000,FatalFeLimit=200,WarnFeLimit=100,InPosBand=2,homing='enc-index')




//Stage X Parker MX80L (top stage, mounted on Y stage)
//----------------------------------------------------
//Motor[2].pPhaseEnc -> PowerBrick[0].Chan[1].PhaseCapt.a
// 1 el_step = 13mm = 2048 phase_step = 166400000 PhaseCapt =256*650000 (256=scaling of encTable)
// -> PhasePosSf=(2048*el_cycle)/(256*enc_step) = 8*el_cycle/enc_step =2048*1/(256*650000)=8*1/650000=1./81250=1.23077e-05
//2048 phase_step =166400000 PhaseCapt -> PhasePosSf =  2048/166400000= 2048./(256*650000)
//PhaseFreq=20000,PhasePerServo=1 -> Kvfb=220*4 Ki/=4,Kvff*=4,Kaff*=4*4

!encoder_sim(enc=2,tbl=10,mot=10,posSf=13000./2048)
!encoder_inc(enc=2,tbl=2,mot=2,posSf=13000./650000)
!motor_servo(mot=2,ctrl='ServoCtrl',Kp=22,Kvfb=350,Ki=0.02,Kvff=240,Kaff=1500,MaxInt=1000)
!motor(mot=2,dirCur=0,contCur=800,peakCur=2400,timeAtPeak=1,IiGain=5,IpfGain=8,IpbGain=8,JogSpeed=10.,numPhase=3,invDir=True,servo=None,PhasePosSf=1./81250,PhaseFindingDac=100,PhaseFindingTime=50,SlipGain=0,AdvGain=0,PwmSf=10000,FatalFeLimit=200,WarnFeLimit=100,InPosBand=2,homing='enc-index')


//rot stage
//---------
//use 360'000 for 360 deg as motor unit
//1 rev = 16*2048=32768 phase_step = 1048576 enc_steps
//PhasePosSf= 8*el_cycle/enc_step =8*16/1048576=1./8192
//Motor[1].pPhaseEnc=EncTable[1].pEnc=Acc84B[0].Chan[0].SerialEncDataA.a
// -> PhasePosSf is calculated as follows:  (2048*pole_cycle)/(256*enc_step) = 8*pole_cycle/enc_step
//PhasePosSf = (2048*pole_cycle)/(SerialEncDataA)=8*16/1048576=1/32

!encoder_sim(enc=3,tbl=11,mot=11,posSf=360000./32768)
!encoder_biss(enc=3,tbl=3,mot=3,numBits=20,posSf=360000./1048576)
Motor[3].pPhaseEnc=Acc84B[0].Chan[2].SerialEncDataA.a
!motor_servo(mot=3,ctrl='ServoCtrl',Kp=0.8,Kvfb=20,Ki=0.001,Kvff=40,Kaff=0,MaxInt=1000)
!motor(mot=3,dirCur=0,contCur=1000,peakCur=2000,timeAtPeak=1,IiGain=1.5,IpfGain=0,IpbGain=3,JogSpeed=180.,numPhase=3,invDir=True,servo=None,PhasePosSf=1./8192,PhaseFindingDac=100,PhaseFindingTime=50,SlipGain=0,AdvGain=0,PwmSf=10000,FatalFeLimit=3000,WarnFeLimit=1000,InPosBand=10, HomeOffset=228987)


//Stada stage
//-----------
//512000 ustep == 50000 encCnt == 2500um
//posSf = userUnits/encoder_steps
//servoSf=motor_u_steps/userUnits

!encoder_biss(enc=4,numBits=32,posSf=1./20)
!encoder_biss(enc=5,numBits=32,posSf=1./20)
!motor(mot=4,current=200,JogSpeed=0.5,invDir=1,servoSf=204.8,InPosBand=1,HomeOffset=39278)
!motor(mot=5,current=200,JogSpeed=0.5,invDir=1,servoSf=204.8,InPosBand=1,HomeOffset=39736)


//Interferometer 1,2
//------------------
!encoder_inc(enc=6,tbl=6,mot=6 ,posSf=.01) //1 incr=10nm -> posSf=.01 to scale to um
!encoder_inc(enc=7,tbl=7,mot=7 ,posSf=-.01) //1 incr=10nm,inverse direction -> posSf=.01 to scale to um

#3,4,5hmz

//holding current
!holding_current(m4=[0,240],m5=[0,240])


