1、ADC电流偏置读取
void Offset_CurrentReading(void)
{
static uint16_t ADC_PhaseU_Curr[100];
static uint16_t ADC_PhaseV_Curr[100];
uint16_t temp;
uint32_t sum_U=0;
uint32_t sum_V=0;
uint8_t i,k;
for(i=0;i<100;i++){
while(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)==RESET);
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
ADC_PhaseU_Curr[i] = ADC_DualConvertedValueTab[0];
ADC_PhaseV_Curr[i] = ADC_DualConvertedValueTab[1];
}
for(k=0;k<100;k++){
for(i=k+1;i<100;i++)
if(ADC_PhaseU_Curr[i]>ADC_PhaseU_Curr[k]){
temp=ADC_PhaseU_Curr[i];
ADC_PhaseU_Curr[i]=ADC_PhaseU_Curr[k];
ADC_PhaseU_Curr[k]=temp;
}
}
sum_U=0;
for(k=25;k<75;k++){
sum_U=sum_U+ADC_PhaseU_Curr[k];
}
ADCSampPare.OffsetPhaseU_Curr =sum_U/50;
printf("U: %d \n", ADCSampPare.OffsetPhaseU_Curr);
for(k=0;k<100;k++){
for(i=k+1;i<100;i++)
if(ADC_PhaseV_Curr[i]>ADC_PhaseV_Curr[k]){
temp=ADC_PhaseV_Curr[i];
ADC_PhaseV_Curr[i]=ADC_PhaseV_Curr[k];
ADC_PhaseV_Curr[k]=temp;
}
}
sum_V=0;
for(k=25;k<75;k++){
sum_V=sum_V+ADC_PhaseV_Curr[k];
}
ADCSampPare.OffsetPhaseV_Curr =sum_V/50;
printf("V: %d \n", ADCSampPare.OffsetPhaseV_Curr);
}
2、转速测量
为以后速度环做准备
angeel_ch[sp_flag]=Anglee;
if ((TIM2->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down){
//encoder timer down-counting
if(angeel_ch[sp_flag]>angeel_ch[(sp_flag+1)%11])
interval=angeel_ch[sp_flag]-angeel_ch[(sp_flag+1)%11]-4000;
else interval=angeel_ch[sp_flag]-angeel_ch[(sp_flag+1)%11];
}
else{
if(angeel_ch[sp_flag]<angeel_ch[(sp_flag+1)%11])</angeel_ch[(sp_flag+1)%11])
interval=angeel_ch[sp_flag]-angeel_ch[(sp_flag+1)%11]+4000;
else interval=angeel_ch[sp_flag]-angeel_ch[(sp_flag+1)%11];
}
if((interval<200)&&(interval>-200)){
speedms=interval*15;
FirstOrder_LPF_Cacl(speedms,speedms_F,0.1);
pi_spd.Fdb=speedms_F;
}
sp_flag++;
if (sp_flag>=11)sp_flag=0;
3、q轴单轴调试
Pid参数:
#define Id_Kp 0.0
#define Id_Ki 0.0
#define Id_Kc 0
#define Iq_Kp 0.1
#define Iq_Ki 0.5
#define Iq_Kc 0
pi_iq.Ref= 50;
pi_iq.Fdb=I_dq.I2;
PID_CALC(pi_iq);
FirstOrder_LPF_Cacl(pi_iq.Out,pi_iq.OutF,0.05);
Volt_dq.V2= pi_iq.OutF;
Volt_dq.V1= 0;
结合之前的开环调试,整体流程大致这样。
4、PID限幅与计算
#define UP16LIMIT(var,max,min) {(var) = (var)>(max)?(max):(var) ;
#define PID_CALC(v) \
v.Err = v.Ref - v.Fdb; \
v.Up= _IQmpy(v.Kp,v.Err); \
v.Ui= v.Ui + _IQmpy(v.Ki,v.Up); \
UP16LIMIT(v.Ui,v.OutMax,v.OutMin);\
v.Ud = v.Kd * (v.Up - v.Up1);\
v.Out = v.Up + v.Ui + v.Ud;\
UP16LIMIT(v.Out,v.OutMax,v.OutMin);\
v.Up1 = v.Up;
5、主程序调用
int main(void)
{
NVIC_Configuration();
StateContr.Control_Mode = 1;
PID_init();
Key_GPIO();
Hall_Gpio();
Encoder_Init();
USARTx_Config();
Adc_Conf();
DRV8313_GPIO();
Tim1_PWM_Init();
ROTATOR_Init();
printf("hello \n");
Offset_CurrentReading();
while(1){
Key_Scan();
}
}
|