📄 stm32f10x_encoder.c
字号:
hCurrent_angle_sample_one = hCurrent_angle_sample_two;
hEnc_Timer_Overflow_sample_one = hEnc_Timer_Overflow_sample_two;
}
if ( (ENCODER_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)
{// encoder timer down-counting
wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle -
(hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
}
else
{//encoder timer up-counting
wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle +
(hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
}
// n = a*f
// speed computation as delta angle * 1/(speed sempling time)
temp = (signed long long)(wDelta_angle * SPEED_SAMPLING_FREQ); // SPEED_SAMPLING_TIME = 2ms
temp /= (4*ENCODER_PPR);
temp *= 10; // 0.1 Hz resolution
} //is first measurement, discard it
else
{
bIs_First_Measurement = FALSE;
temp = 0;
hEncoder_Timer_Overflow = 0;
haux = ENCODER_TIMER->CNT;
// Check if Encoder_Timer_Overflow is still zero. In case an overflow IT
// occured it resets overflow counter and wPWM_Counter_Angular_Velocity
if (hEncoder_Timer_Overflow != 0)
{
haux = ENCODER_TIMER->CNT;
hEncoder_Timer_Overflow = 0;
}
}
hPrevious_angle = haux;
return((s16) temp);
}
/*******************************************************************************
* 函数名 : ENC_Get_Mechanical_Speed
* 功能描述 : Export the value of the smoothed motor speed computed in
* ENC_Calc_Average_Speed function
* 输入 : 无
* 输出 : s16
* 返回 : Return motor speed in 0.1 Hz resolution. This routine
will return the average mechanical speed of the motor.
*******************************************************************************/
s16 ENC_Get_Mechanical_Speed(void)
{
return(hRot_Speed);
}
/*******************************************************************************
* 函数名 : ENC_Calc_Average_Speed
* 功能描述 : Compute smoothed motor speed based on last SPEED_BUFFER_SIZE
informations and store it variable
* 输入 : 无
* 输出 : s16
* 返回 : Return rotor speed in 0.1 Hz resolution. This routine
will return the average mechanical speed of the motor.
*******************************************************************************/
void ENC_Calc_Average_Speed(void)
{
s32 wtemp;
u16 hAbstemp;
u32 i;
u8 static bError_counter;
wtemp = ENC_Calc_Rot_Speed();
hAbstemp = ( wtemp < 0 ? - wtemp : wtemp);
/* Checks for speed measurement errors when in RUN State and saturates if
necessary*/
if (State == RUN)
{
if(hAbstemp < MINIMUM_MECHANICAL_SPEED)
{
if (wtemp < 0)
{
wtemp = -(s32)(MINIMUM_MECHANICAL_SPEED);
}
else
{
wtemp = MINIMUM_MECHANICAL_SPEED;
}
bError_counter++;
}
else if (hAbstemp > MAXIMUM_MECHANICAL_SPEED)
{
if (wtemp < 0)
{
wtemp = -(s32)(MAXIMUM_MECHANICAL_SPEED);
}
else
{
wtemp = MAXIMUM_MECHANICAL_SPEED;
}
bError_counter++;
}
else
{
bError_counter = 0;
}
if (bError_counter >= MAXIMUM_ERROR_NUMBER)
{
bError_Speed_Measurement = TRUE;
}
else
{
bError_Speed_Measurement = FALSE;
}
} //上面是马达在运转中 State = RUN
else //其他状态 State
{
bError_Speed_Measurement = FALSE;
bError_counter = 0;
}
/* Compute the average of the read speeds */
hSpeed_Buffer[bSpeed_Buffer_Index] = (s16)wtemp;
bSpeed_Buffer_Index++;
if (bSpeed_Buffer_Index == SPEED_BUFFER_SIZE)
{
bSpeed_Buffer_Index = 0;
}
wtemp=0;
for (i=0;i<SPEED_BUFFER_SIZE;i++)
{
wtemp += hSpeed_Buffer[i];
}
wtemp /= SPEED_BUFFER_SIZE;
hRot_Speed = ((s16)(wtemp)); //输出马达的平均转速。
}
/*******************************************************************************
* 函数名 : ENC_ErrorOnFeedback
* 功能描述 : Check for possible errors on speed measurement when State is
* RUN. After MAXIMUM_ERROR_NUMBER consecutive speed measurement
* errors, the function return TRUE, else FALSE.
* Function return
* 输入 : 无
* 输出 : s16
* 返回 : boolean variable
*******************************************************************************/
bool ENC_ErrorOnFeedback(void)
{
return(bError_Speed_Measurement);
}
/*******************************************************************************
* 函数名 : ENC_Start_Up
* 功能描述 : The purpose of this function is to perform the alignment of
* PMSM torque and flux regulation during the alignment phase.
* 输入 : details the input parameters.
* 输出 : details the output parameters.
* 返回 : details the return value.
*******************************************************************************/
void ENC_Start_Up(void)
{
static u32 wTimebase=0;
if ( (wGlobal_Flags & FIRST_START) == FIRST_START)
{
// First Motor start-up, alignment must be performed
wTimebase++;
if(wTimebase <= T_ALIGNMENT_PWM_STEPS)
{
hFlux_Reference = I_ALIGNMENT * wTimebase / T_ALIGNMENT_PWM_STEPS;
hTorque_Reference = 0; //在这里加大启动扭矩电流。
Stat_Curr_a_b = GET_PHASE_CURRENTS();
Stat_Curr_alfa_beta = Clarke(Stat_Curr_a_b);
Stat_Curr_q_d = Park(Stat_Curr_alfa_beta, ALIGNMENT_ANGLE_S16);
/*loads the Torque Regulator output reference voltage Vqs*/
Stat_Volt_q_d.qV_Component1 = PID_Regulator(hTorque_Reference,
Stat_Curr_q_d.qI_Component1, &PID_Torque_InitStructure);
/*loads the Flux Regulator output reference voltage Vds*/
Stat_Volt_q_d.qV_Component2 = PID_Regulator(hFlux_Reference,
Stat_Curr_q_d.qI_Component2, &PID_Flux_InitStructure);
RevPark_Circle_Limitation();
/*Performs the Reverse Park transformation,
i.e transforms stator voltages Vqs and Vds into Valpha and Vbeta on a
stationary reference frame*/
Stat_Volt_alfa_beta = Rev_Park(Stat_Volt_q_d); //和PARK()函数总是成对使用,所以thea角度不变。
/*Valpha and Vbeta finally drive the power stage*/
CALC_SVPWM(Stat_Volt_alfa_beta);
}
else
{
wTimebase = 0;
ENC_ResetEncoder();
Stat_Volt_q_d.qV_Component1 = Stat_Volt_q_d.qV_Component2 = 0;
hTorque_Reference = PID_TORQUE_REFERENCE;
hFlux_Reference = PID_FLUX_REFERENCE;
wGlobal_Flags &= ~FIRST_START; // alignment done only once
//Clear the speed acquisition of the alignment phase
ENC_Clear_Speed_Buffer();
#ifdef ENCODER
State = RUN;
#endif
}
}
else
{
#ifdef ENCODER
State = RUN;
#endif
}
}
/*******************************************************************************
* 函数名 : TIMx_IRQHandler
* 功能描述 : This function handles TIMx Update interrupt request.
Encoder unit connected to TIMx (x = 2,3 or 4)
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
#if defined(TIMER2_HANDLES_ENCODER)
void TIM2_IRQHandler(void)
{
/* Clear the interrupt pending flag */
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
if (hEncoder_Timer_Overflow != U16_MAX)
{
hEncoder_Timer_Overflow++; //马达每转360度电角度。
}
}
#elif defined(TIMER3_HANDLES_ENCODER)
void TIM3_IRQHandler(void)
{
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
if (Encoder_Timer_Overflow != U16_MAX)
{
Encoder_Timer_Overflow++;
}
}
#elif defined(TIMER4_HANDLES_ENCODER)
void TIM4_IRQHandler(void)
{
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
if (Encoder_Timer_Overflow != U16_MAX)
{
Encoder_Timer_Overflow++;
}
}
#endif // TIMER4_HANDLES_ENCODER
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -