优化这段代码void Motor_GB_Position(Motor_HandleTypeDef *H_AXISx) { double t=0; int Distance=0; Distance=(int)((H_AXISx->GBAngle - H_AXISx->REL_GB_Angle)/H_AXISx->GB_MAX*H_AXISx->CP_CountMAX); // if(Distance >= ((int)H_AXISx->CP_CountMAX/2)) // Distance=Distance-(int)H_AXISx->CP_CountMAX; // else if(Distance<= ((int)H_AXISx->CP_CountMAX / -2)) // Distance=(int)H_AXISx->CP_CountMAX+Distance; if(H_AXISx->GBDWFLAG==1) { t=(double)H_AXISx->Newspeed/(double)H_AXISx->Accspeed; H_AXISx->DecCount=(uint32_t)(t*t*(double)H_AXISx->Accspeed/2); if(Distance>5) Motor_Run(H_AXISx->Number , CW); else if(Distance<-5) Motor_Run(H_AXISx->Number , CCW); else { H_AXISx->GBDWFLAG=0; return; } if(H_AXISx->DecCount>abs(Distance/2)) { H_AXISx->DecCount=abs(Distance/2); } H_AXISx->againflag=0; H_AXISx->againcount=0; H_AXISx->GBDWFLAG=2; } else if(H_AXISx->GBDWFLAG==2) { if(abs(Distance)<=H_AXISx->DecCount) { H_AXISx->Newspeed=H_AXISx->MINSpeed; H_AXISx->Accspeed+=H_AXISx->Accspeed/10; Motor_Speed_Calculate(H_AXISx); H_AXISx->GBDWFLAG=3; } } else if(H_AXISx->GBDWFLAG==3) { if((abs(Distance)<=H_AXISx->JINGDU)&&(H_AXISx->againcount>=2)) { H_AXISx->againflag=0; H_AXISx->againcount=0; Motor_Run(H_AXISx->Number,STOP); H_AXISx->Nowspeed=0; H_AXISx->GBDWFLAG=0; } else if((Distance>H_AXISx->JINGDU)&&((H_AXISx->againflag==0)||(H_AXISx->againflag==1)))//&&(H_AXISx->Nowspeed<=H_AXISx->MINSpeed)) { H_AXISx->againflag=2; H_AXISx->againcount++; H_AXISx->Newspeed=H_AXISx->MINSpeed; // Motor_Speed_Calculate(H_AXISx); Motor_Run(H_AXISx->Number,CW); } else if((Distance<-(H_AXISx->JINGDU))&&((H_AXISx->againflag==0)||(H_AXISx->againflag==2)))//&&(H_AXISx->Nowspeed<=H_AXISx->MINSpeed)) { H_AXISx->againflag=1; H_AXISx->againcount++; H_AXISx->Newspeed=H_AXISx->MINSpeed; // Motor_Speed_Calculate(H_AXISx); Motor_Run(H_AXISx->Number,CCW); } } }
时间: 2024-04-01 19:36:43 浏览: 79
这段代码中有一些可以进行优化的地方:
1. 可以将 `(int)H_AXISx->CP_CountMAX` 提取出来,避免多次计算。
2. 计算 `H_AXISx->DecCount` 时可以将 `t*t` 的结果存储到一个变量中,避免重复计算。
3. 在第一个 if 语句中,可以将 `Distance` 的绝对值存储到一个变量中,避免多次计算。
4. 可以将 `H_AXISx->againflag==0` 和 `H_AXISx->againflag==1` 的条件合并为一个条件,避免重复代码。
5. 可以将 `Motor_Run(H_AXISx->Number, CW)` 和 `Motor_Run(H_AXISx->Number, CCW)` 提取出来,避免多次重复调用。
以下是优化后的代码:
void Motor_GB_Position(Motor_HandleTypeDef *H_AXISx) {
double t = 0;
int Distance = 0;
int countMax = (int)H_AXISx->CP_CountMAX;
int absDistance = abs((int)((H_AXISx->GBAngle - H_AXISx->REL_GB_Angle) / H_AXISx->GB_MAX * countMax));
int decCount;
if (H_AXISx->GBDWFLAG == 1) {
t = (double)H_AXISx->Newspeed / (double)H_AXISx->Accspeed;
decCount = (int)(t * t * (double)H_AXISx->Accspeed / 2);
if (absDistance > 5) {
int direction = Distance > 0 ? CW : CCW;
Motor_Run(H_AXISx->Number, direction);
} else {
H_AXISx->GBDWFLAG = 0;
return;
}
if (decCount > absDistance / 2) {
decCount = absDistance / 2;
}
H_AXISx->againflag = 0;
H_AXISx->againcount = 0;
H_AXISx->GBDWFLAG = 2;
} else if (H_AXISx->GBDWFLAG == 2) {
if (absDistance <= decCount) {
H_AXISx->Newspeed = H_AXISx->MINSpeed;
H_AXISx->Accspeed += H_AXISx->Accspeed / 10;
Motor_Speed_Calculate(H_AXISx);
H_AXISx->GBDWFLAG = 3;
}
} else if (H_AXISx->GBDWFLAG == 3) {
if ((absDistance <= H_AXISx->JINGDU) && (H_AXISx->againcount >= 2)) {
H_AXISx->againflag = 0;
H_AXISx->againcount = 0;
Motor_Run(H_AXISx->Number, STOP);
H_AXISx->Nowspeed = 0;
H_AXISx->GBDWFLAG = 0;
} else if ((absDistance > H_AXISx->JINGDU) && (H_AXISx->againflag != 2)) {
H_AXISx->againflag = 2;
H_AXISx->againcount++;
H_AXISx->Newspeed = H_AXISx->MINSpeed;
Motor_Run(H_AXISx->Number, CW);
} else if ((absDistance < -(H_AXISx->JINGDU)) && (H_AXISx->againflag != 1)) {
H_AXISx->againflag = 1;
H_AXISx->againcount++;
H_AXISx->Newspeed = H_AXISx->MINSpeed;
Motor_Run(H_AXISx->Number, CCW);
}
}
}
阅读全文