Firmware/FFBoard/UserExtensions/Src/TMC4671.cpp
/**
 * Moves the rotor and estimates polarity and direction of the encoder
 * Polarity is found by measuring the n pulse.
 * If polarity was found to be reversed during the test direction will be reversed again to account for that
 */
Moves the rotor and estimates polarity and direction of the encoder
Polarity is found by measuring the n pulse.
If polarity was found to be reversed during the test direction will be reversed again to account for that
Moves the rotor and estimates polarity and direction of the encoder Polarity is found by measuring the n pulse . If polarity was found to be reversed during the test direction will be reversed again to account for that
void TMC4671::estimateABNparams(){
	blinkClipLed(100, 0);
	int32_t pos = getPos();
	setPos(0);
	PhiE lastphie = getPhiEtype();
	MotionMode lastmode = getMotionMode();
	updateReg(0x25, 0,0x1000,12); 
	setPhiE_ext(0);
	setPhiEtype(PhiE::ext);
	setFluxTorque(0, 0);
	setMotionMode(MotionMode::torque,true);
	for(int16_t flux = 0; flux <= bangInitPower; flux+=10){
		setFluxTorque(flux, 0);
		Delay(5);
	}
	int16_t phiE_abn = readReg(0x2A)>>16;
	int16_t phiE_abn_old = 0;
	int16_t rcount=0,c = 0; 
	uint16_t highcount = 0; 
	for(int16_t p = 0;p<0x0fff;p+=0x2f){
		setPhiE_ext(p);
		Delay(10);
		c++;
		phiE_abn_old = phiE_abn;
		phiE_abn = readReg(0x2A)>>16;
		if(phiE_abn < phiE_abn_old){
			rcount++;
		}
		if((readReg(0x76) & 0x04) >> 2){
			highcount++;
		}
	}
	setPos(pos+getPos());
	setFluxTorque(0, 0);
	setPhiEtype(lastphie);
	setMotionMode(lastmode,true);
	bool npol = highcount > c/2;
	abnconf.rdir = rcount > c/2;
	abnconf.apol = npol;
	abnconf.bpol = npol;
	abnconf.npol = npol;
	blinkClipLed(0, 0);
}
void TMC4671 :: estimateABNparams ( ) { blinkClipLed ( 100 , 0 ) ; int32_t pos = getPos ( ) ; setPos ( 0 ) ; PhiE lastphie = getPhiEtype ( ) ; MotionMode lastmode = getMotionMode ( ) ; updateReg ( 0x25 , 0 , 0x1000 , 12 ) ; setPhiE_ext ( 0 ) ; setPhiEtype ( PhiE :: ext ) ; setFluxTorque ( 0 , 0 ) ; setMotionMode ( MotionMode :: torque , true ) ; for ( int16_t flux = 0 ; flux <= bangInitPower ; flux += 10 ) { setFluxTorque ( flux , 0 ) ; Delay ( 5 ) ; } int16_t phiE_abn = readReg ( 0x2A ) >> 16 ; int16_t phiE_abn_old = 0 ; int16_t rcount = 0 , c = 0 ; uint16_t highcount = 0 ; for ( int16_t p = 0 ; p < 0x0fff ; p += 0x2f ) { setPhiE_ext ( p ) ; Delay ( 10 ) ; c ++ ; phiE_abn_old = phiE_abn ; phiE_abn = readReg ( 0x2A ) >> 16 ; if ( phiE_abn < phiE_abn_old ) { rcount ++ ; } if ( ( readReg ( 0x76 ) & 0x04 ) >> 2 ) { highcount ++ ; } } setPos ( pos + getPos ( ) ) ; setFluxTorque ( 0 , 0 ) ; setPhiEtype ( lastphie ) ; setMotionMode ( lastmode , true ) ; bool npol = highcount > c / 2 ; abnconf . rdir = rcount > c / 2 ; abnconf . apol = npol ; abnconf . bpol = npol ; abnconf . npol = npol ; blinkClipLed ( 0 , 0 ) ; }
Moves the rotor and estimates polarity and direction of the encoder
 Polarity is found by measuring the n pulse.
Moves the rotor and estimates polarity and direction of the encoder Polarity is found by measuring the n pulse .
// Set dir normal // Count how often direction was in reverse // Count high state of n pulse for polarity estimation // Rotate a bit // Count how often the new position was lower than the previous indicating a reversed encoder or motor direction //	if(npol != abnconf.npol) // Invert dir if polarity was reversed TODO correct? likely wrong at the moment //		abnconf.rdir = !abnconf.rdir;