|
@@ -26,6 +26,7 @@ void InitFlash();
|
|
|
#define TIMER1_S1 60
|
|
|
#define TIMER1_S2 500000
|
|
|
#define TIMER1_PER 30000000
|
|
|
+#define MAINLOOPDIV 5 // 主中断40K 40K/MAINLOOPDIV 主循环
|
|
|
// Prototype statements for functions found within this file.
|
|
|
interrupt void MainISR(void);//主中断
|
|
|
interrupt void xint1_isr(void);//FO中断
|
|
@@ -49,9 +50,9 @@ void EnableDog(void);
|
|
|
|
|
|
// Used for running BackGround in flash, and ISR in RAM
|
|
|
//速度计算相关
|
|
|
-
|
|
|
+int16 t4 = 0;
|
|
|
int16 Xint2Cnt = 0;
|
|
|
-
|
|
|
+int16 hallCmtnTrig = 0;
|
|
|
extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
|
|
|
|
|
|
int16 SerialCommsTimer;
|
|
@@ -139,12 +140,24 @@ IMPULSE impl1 = IMPULSE_DEFAULTS;
|
|
|
// Create an instance of DATALOG Module 950:3A 但是只有外界扭矩低于36N才能启动起来
|
|
|
Uint16 pwm_limit_table1[5] = {950,1300,1650,1930,2320};//0 50 100 150 200
|
|
|
//// 0, 100, 200, 300, 400 500 600 700 800 900 1000
|
|
|
-Uint16 pwm_limit_table2[]= {950,1650,2320,3050,3750,4460,5200,5929,6623,7358,8050,
|
|
|
+/*Uint16 pwm_limit_table2[]= {950,1650,2320,3050,3750,4460,5200,5929,6623,7358,8050,
|
|
|
8744,9400,10165,10848,11600,12290,12953,13684,14310,15050,
|
|
|
15760,16434,17054,17750,18493,19134,19830,20507,21260,21926,
|
|
|
- 22602,23232,23935,24609,25240};
|
|
|
-
|
|
|
-int32 pwmlimitrate = 32768;
|
|
|
+ 22602,23232,23935,24609,25240};*/
|
|
|
+Uint16 pwm_limit_table2[]= {950,1650,2320,2985,3668,4348,5024,5705,6383,7058,7739,
|
|
|
+ 8449,9122,9800,10480,11163,11826,12499,13181,13860,14515,
|
|
|
+ 15192,15872,16530,17203,17861,18537,19183,19848,20525,21161,
|
|
|
+ 21838,22488,23216,23850,24507,24507};
|
|
|
+ //// 0, 100, 200, 300, 400 500 600 700 800 900 1000
|
|
|
+Uint16 pwm_limit_table_neg[]= {950,1616,2272,2961,3641,4305,4983,5659,6326,6994,7670,
|
|
|
+ 8348,9022,9700,10361,11502,11714,12379,13066,13727,14426,//1100-2000
|
|
|
+ 15085,15610,16325,17005,17660,18338,19000,19693,20390,21048,
|
|
|
+ 21706,22428,23104,22823,24434,24434};
|
|
|
+/*Uint16 pwm_limit_table_neg[]= {950,1650,2320,3050,3750,4476,5200,5830,6520,7200,7808,
|
|
|
+ 8500,9206,9800,10510,11200,11930,12579,13229,13892,14540,
|
|
|
+ 15209,15920,16616,17291,18000,18775,19424,20507,21260,21926,
|
|
|
+ 22602,23232,23935,24609,25240};*/
|
|
|
+int32 pwmlimitrate = 24507;
|
|
|
|
|
|
Uint32 IsrTicker=0;
|
|
|
void main(void)
|
|
@@ -236,7 +249,7 @@ void main(void)
|
|
|
PWMDAC_INIT_MACRO(7,pwmdac1) ; // PWM 7A,7B
|
|
|
|
|
|
// Initialize Hall module
|
|
|
- hall1.DebounceAmount = 0;
|
|
|
+ hall1.DebounceAmount = 1;
|
|
|
hall1.Revolutions = 1;
|
|
|
HALL3_INIT_MACRO(&hall1);//读hall值 初始化HallGpioBuffer,HallGpioAccepted
|
|
|
|
|
@@ -283,8 +296,8 @@ void main(void)
|
|
|
// IDLE loop. Just sit and loop forever:
|
|
|
for(;;) //infinite loop
|
|
|
{
|
|
|
- if(IsrTicker%2 ==0)
|
|
|
- {
|
|
|
+ if(IsrTicker%MAINLOOPDIV ==0)
|
|
|
+ {//8Khz
|
|
|
mainLoop();
|
|
|
}
|
|
|
}
|
|
@@ -310,8 +323,59 @@ interrupt void MainISR(void)
|
|
|
// Verifying the ISR
|
|
|
IsrTicker++;
|
|
|
IsrTicker = (IsrTicker>20000)? 0 : IsrTicker;
|
|
|
-
|
|
|
- //IsrTime = MastIsrTimePercent();
|
|
|
+ VirtualTimer++;
|
|
|
+ VirtualTimer &= 0x00007FFF;
|
|
|
+ HALL3_READ_MACRO(&hall1);
|
|
|
+ ClosedFlag = TRUE;
|
|
|
+ if (ClosedFlag==TRUE)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (hall1.CmtnTrigHall==0x7FFF)
|
|
|
+ {
|
|
|
+
|
|
|
+ hallCmtnTrig = 1;
|
|
|
+ speed_direction(hall1.HallGpioAccepted,PreviousState);
|
|
|
+
|
|
|
+ if (hall1.HallGpioAccepted==5)
|
|
|
+ {
|
|
|
+ pwm1.CmtnPointerIn = 5;
|
|
|
+ }
|
|
|
+ else if (hall1.HallGpioAccepted==1)
|
|
|
+ { pwm1.CmtnPointerIn = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (hall1.HallGpioAccepted==3)
|
|
|
+ { pwm1.CmtnPointerIn = 1;
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (hall1.HallGpioAccepted==2)
|
|
|
+ { pwm1.CmtnPointerIn = 2;
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (hall1.HallGpioAccepted==6)
|
|
|
+ { pwm1.CmtnPointerIn = 3;
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (hall1.HallGpioAccepted==4)
|
|
|
+ {
|
|
|
+ pwm1.CmtnPointerIn = 4;
|
|
|
+ }
|
|
|
+ PreviousState = hall1.HallGpioAccepted;
|
|
|
+
|
|
|
+
|
|
|
+ } // delay
|
|
|
+ } // ClosedFlag==TRUE
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if(Enable_ALLOW == 0 && pwm1.DutyFunc==0){
|
|
|
+ bldcpwm_close();
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+ BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
+
|
|
|
+ }
|
|
|
// Acknowledge interrupt to recieve more interrupts from PIE group 1
|
|
|
CpuTimer0.RegsAddr->TCR.bit.TIF=1;
|
|
|
PieCtrlRegs.PIEACK.bit.ACK1 = 1;
|
|
@@ -374,32 +438,37 @@ void HVDMC_Protection(void)
|
|
|
interrupt void xint2_isr(void)
|
|
|
{
|
|
|
static int32 last_XintTime = TIMER1_PER;
|
|
|
-
|
|
|
-/* if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
|
|
|
+ static Uint32 last_hall = 0;
|
|
|
+ Uint32 temp = 0;
|
|
|
+ if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
|
|
|
// 速度为0
|
|
|
XintTime = (int32)TIMER1_PER;
|
|
|
CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清中断标志位
|
|
|
}else{
|
|
|
XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
|
|
|
- }*/
|
|
|
+ }
|
|
|
XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
|
|
|
+ temp = (GpioDataRegs.GPADAT.all>>24)&0x00000007; /* read all three GPIOs at once*/
|
|
|
|
|
|
- if(XintTime<30000){
|
|
|
+ if(_IQabs(XintTime)<30000 || temp == last_hall ){
|
|
|
//filter 防抖
|
|
|
XintTime = last_XintTime;
|
|
|
- }
|
|
|
- last_XintTime = XintTime;
|
|
|
+ PieCtrlRegs.PIEACK.bit.ACK1 =1;
|
|
|
|
|
|
+ return ;
|
|
|
+ }
|
|
|
if(Direction == -1){
|
|
|
XintTime = -XintTime;
|
|
|
}
|
|
|
Xint2Cnt = 1;
|
|
|
+ last_XintTime = XintTime;
|
|
|
+ last_hall = temp;
|
|
|
+
|
|
|
CpuTimer1.RegsAddr->TCR.bit.TRB = 1; //重新装载
|
|
|
CpuTimer1.RegsAddr->TCR.bit.TSS = 0;//定时器restart
|
|
|
|
|
|
PieCtrlRegs.PIEACK.bit.ACK1 =1;
|
|
|
|
|
|
-
|
|
|
}
|
|
|
|
|
|
interrupt void xint1_isr(void)
|
|
@@ -472,17 +541,118 @@ void ClosePwm(void)
|
|
|
EDIS;
|
|
|
}
|
|
|
void speed_direction(Uint16 hall,Uint16 hall_last){
|
|
|
-
|
|
|
+//防抖
|
|
|
+ static int16 i = 0;
|
|
|
+ static int16 j=0;
|
|
|
|
|
|
if(hall_last == 2 && hall == 6 || hall_last == 6 && hall == 4 ||hall_last == 4 && hall == 5 ||hall_last == 5 && hall == 1||hall_last == 1 && hall == 3||hall_last == 3 && hall == 2)
|
|
|
{
|
|
|
- Direction = 1;
|
|
|
+ i++;
|
|
|
+ j--;
|
|
|
+ if(j<0){
|
|
|
+ j=0;
|
|
|
+ }
|
|
|
+ if(i>5){
|
|
|
+ i = 6;
|
|
|
+ Direction = 1;
|
|
|
+ }
|
|
|
}
|
|
|
else if(hall_last == 4 && hall == 6 || hall_last == 6 && hall == 2 ||hall_last == 2 && hall == 3 ||hall_last == 3 && hall == 1||hall_last == 1 && hall == 5|| hall_last == 5 && hall == 4)
|
|
|
{
|
|
|
- Direction = -1;
|
|
|
+ i--;
|
|
|
+ if(i<0){
|
|
|
+ i=0;
|
|
|
+ }
|
|
|
+ j++;
|
|
|
+ if(j>5){
|
|
|
+ j= 6;
|
|
|
+ Direction = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+void speed_cal_filter(void){
|
|
|
+ int32 RotarRPMNew = 0;
|
|
|
+ int32 RotarrpmOut = 0;
|
|
|
+ static int32 last_speed = 0;
|
|
|
+ static int32 last_RotarRPMNew = 0;
|
|
|
+ Uint16 Halltemp = (Uint16)((GpioDataRegs.GPADAT.all>>24)&0x00000007); /* read all three GPIOs at once*/
|
|
|
+ static Uint16 Hall_1 = 0;
|
|
|
+ //static Uint16 Hall_2 = 0;
|
|
|
+ static int32 Hall_2_cnt = 0;
|
|
|
+ int16 directiontemp = 0;
|
|
|
+
|
|
|
+ if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
|
|
|
+ // 速度为0
|
|
|
+ XintTime = TIMER1_PER;
|
|
|
+ Xint2Cnt = 1;
|
|
|
+ RotarRPMNew =0;
|
|
|
+ last_speed = 0;
|
|
|
+ CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+ if(Halltemp == hall1.HallGpioAccepted){
|
|
|
+ Hall_2_cnt++;
|
|
|
+ if(Hall_2_cnt>MAINLOOPFREQ/2){//0.5s
|
|
|
+ Hall_2_cnt = MAINLOOPFREQ/2+1;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Hall_2_cnt = 0;
|
|
|
+ }
|
|
|
+ if(XintTime == TIMER1_PER || Hall_2_cnt>=5000){
|
|
|
+ RotarRPMNew = 0;
|
|
|
+ last_speed = 0;
|
|
|
+ }else{
|
|
|
+ RotarRPMNew =(int32)360000000/XintTime;
|
|
|
+ }
|
|
|
}
|
|
|
+ test2 = RotarRPMNew;
|
|
|
+ if(Xint2Cnt == 1){
|
|
|
+ //防止突然反向
|
|
|
+ Xint2Cnt = 0;
|
|
|
+ if(Hall_1 == 2 && Halltemp == 6 || Hall_1 == 6 && Halltemp == 4 ||Hall_1 == 4 && Halltemp == 5 ||Hall_1 == 5 && Halltemp == 1||Hall_1 == 1 && Halltemp == 3||Hall_1 == 3 && Halltemp == 2)
|
|
|
+ {
|
|
|
+ directiontemp = 1;
|
|
|
+ }
|
|
|
+ else if(Hall_1 == 4 && Halltemp == 6 || Hall_1 == 6 && Halltemp == 2 ||Hall_1 == 2 && Halltemp == 3 ||Hall_1 == 3 && Halltemp == 1||Hall_1 == 1 && Halltemp == 5|| Hall_1 == 5 && Halltemp == 4)
|
|
|
+ {
|
|
|
+
|
|
|
+ directiontemp = -1;
|
|
|
+ }
|
|
|
+ if((Direction ==1 && directiontemp == -1 )||(Direction == -1 && directiontemp == 1 )){
|
|
|
+ //说明在振动
|
|
|
+ RotarRPMNew = 0;
|
|
|
+ last_speed = 0;
|
|
|
+ }
|
|
|
+ Hall_1 = Halltemp;
|
|
|
+ test4 = Halltemp;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if(_IQabs(RotarRPMNew - last_RotarRPMNew)>1500 &&(_IQabs(RotarRPMNew)<300 ||_IQabs(last_RotarRPMNew)<300) ){
|
|
|
+ //在0转速附近有大的阶跃
|
|
|
+ test3 = RotarRPMNew;
|
|
|
+ RotarRPMNew = 0;
|
|
|
+ last_speed = 0;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ /* if(RotarRPMNew - last_RotarRPMNew>500){
|
|
|
+ RotarRPMNew = last_RotarRPMNew+4;
|
|
|
+ }else if(RotarRPMNew - last_RotarRPMNew<-500){
|
|
|
+ RotarRPMNew = last_RotarRPMNew-4;
|
|
|
+ }*/
|
|
|
+ last_RotarRPMNew = RotarRPMNew;
|
|
|
+ RotarrpmOut = lowpassfilter(last_speed,RotarRPMNew,_IQ12(0.1),_IQ12(0.125));
|
|
|
+ last_speed = RotarrpmOut;
|
|
|
+
|
|
|
+ speed1.Speed = _IQdiv(RotarrpmOut,speed1.BaseRpm);
|
|
|
+ speed1.SpeedRpm = RotarrpmOut;
|
|
|
+ // }
|
|
|
}
|
|
|
void speed_cal(void){
|
|
|
|
|
@@ -497,7 +667,11 @@ void speed_cal(void){
|
|
|
XintTime = TIMER1_PER;
|
|
|
Xint2Cnt = 1;
|
|
|
CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
|
|
|
-
|
|
|
+ int count = 0;
|
|
|
+ for(count = 0;count<8;count++){
|
|
|
+ RotarrpmArr[count] = 0;
|
|
|
+ }
|
|
|
+ RotarrpmSum = 0;
|
|
|
RotarRPMNew =0;
|
|
|
}else{
|
|
|
RotarRPMNew =(int32)360000000/XintTime;
|
|
@@ -523,16 +697,24 @@ void pwmlimit_speed(void){
|
|
|
pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*50)*(pwm_limit_table1[index+1] - pwm_limit_table1[index])/50+pwm_limit_table1[index];
|
|
|
}else if(_IQabs(speed1.SpeedRpm)< 3500){
|
|
|
Uint16 index = _IQabs(speed1.SpeedRpm)/100;
|
|
|
- pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table2[index+1] - pwm_limit_table2[index])/100+pwm_limit_table2[index];
|
|
|
+ if(speed1.SpeedRpm>0){
|
|
|
+ pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table2[index+1] - pwm_limit_table2[index])/100+pwm_limit_table2[index];
|
|
|
+ }else{
|
|
|
+ pwmlimit = (Uint32)(_IQabs(speed1.SpeedRpm)-index*100)*(pwm_limit_table_neg[index+1] - pwm_limit_table_neg[index])/100+pwm_limit_table_neg[index];
|
|
|
+ }
|
|
|
|
|
|
}else{
|
|
|
- pwmlimit = 25240;//最大值
|
|
|
+ if(speed1.SpeedRpm>0){
|
|
|
+ pwmlimit = pwm_limit_table2[36];//最大值
|
|
|
+ }else{
|
|
|
+ pwmlimit = pwm_limit_table_neg[36];//最大值
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
void taskfree(void){
|
|
|
|
|
|
|
|
|
- if(SerialCommsTimer>4000){
|
|
|
+ if(SerialCommsTimer>(MAINLOOPFREQ/4)){
|
|
|
GpioDataRegs.GPATOGGLE.bit.GPIO22 = 1;
|
|
|
|
|
|
SerialCommsTimer = 0;
|
|
@@ -607,176 +789,13 @@ void mainLoop(void){
|
|
|
}
|
|
|
|
|
|
ServiceDog();
|
|
|
- speed_cal();
|
|
|
+
|
|
|
+ //speed_cal();
|
|
|
+ speed_cal_filter();
|
|
|
pwmlimit_speed();
|
|
|
taskfree();
|
|
|
FaultTreat();
|
|
|
|
|
|
- #if (BUILDLEVEL==LEVEL1)
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the RMP3 module and call the RAMP Control 3 macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- rmp3.DesiredInput = CmtnPeriodTarget;
|
|
|
- rmp3.Ramp3Delay = RampDelay;
|
|
|
- RC3_MACRO(&rmp3);
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the IMPULSE module and call the Impulse macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- impl1.Period = rmp3.Out;
|
|
|
- IMPULSE_MACRO(&impl1);
|
|
|
- HALL3_READ_MACRO(&hall1);
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the MOD6 module and call the Mod 6 counter macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- mod1.TrigInput = impl1.Out;
|
|
|
- MOD6CNT_MACRO(&mod1);
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the PWM_DRV module and call the PWM signal generation
|
|
|
- // update macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- pwm1.CmtnPointerIn = test2;//(int16)mod1.Counter;
|
|
|
- if(EnableFlag == 0){
|
|
|
- pwm1.DutyFuncIn = 0;
|
|
|
- }else{
|
|
|
- pwm1.DutyFuncIn = test;
|
|
|
- }
|
|
|
-
|
|
|
- BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
-
|
|
|
-
|
|
|
- #endif // (BUILDLEVEL==LEVEL1)
|
|
|
- // =============================== LEVEL 3 ======================================
|
|
|
- // Level 3 describes the closed-loop operation of sensored trapezoidal
|
|
|
- // drive of BLDC motor using Hall sensor.
|
|
|
- // ==============================================================================
|
|
|
-
|
|
|
- #if (BUILDLEVEL==LEVEL3)
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- BemfA = _IQ12toIQ(AdcResult.ADCRESULT1);
|
|
|
- BemfB = _IQ12toIQ(AdcResult.ADCRESULT2);
|
|
|
- BemfC = _IQ12toIQ(AdcResult.ADCRESULT3);
|
|
|
- DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT4);//-_IQ(0.5); //1.65V offset added on HVDMC board.
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the RMP3 module and call the Ramp control 3 macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
-
|
|
|
-
|
|
|
- HALL3_READ_MACRO(&hall1);
|
|
|
-
|
|
|
- ClosedFlag=TRUE;
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the RMP2 module and call the Ramp control 2 macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- rmp2.DesiredInput = (int32)DFuncDesired;
|
|
|
- RC2_MACRO(&rmp2);
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the PWM_DRV module and call the PWM signal generation
|
|
|
- // update macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- if (ClosedFlag==TRUE) {
|
|
|
- if (hall1.CmtnTrigHall==0x7FFF) {
|
|
|
-
|
|
|
- PreviousState = pwm1.CmtnPointer;
|
|
|
-
|
|
|
- // Comment the following if-else-if statements in case of
|
|
|
- // inverted Hall logics for commutation states.
|
|
|
- /* if (hall1.HallGpioAccepted==5)
|
|
|
- { pwm1.CmtnPointer = 0;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==1)
|
|
|
- { pwm1.CmtnPointer = 1;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==3)
|
|
|
- { pwm1.CmtnPointer = 2;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==2)
|
|
|
- { pwm1.CmtnPointer = 3;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==6)
|
|
|
- { pwm1.CmtnPointer = 4;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==4)
|
|
|
- {
|
|
|
- pwm1.CmtnPointer = 5;
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // Comment the following if-else-if statements in case of
|
|
|
- // non-inverted Hall logics for commutation states.
|
|
|
- if (hall1.HallGpioAccepted==2)
|
|
|
- pwm1.CmtnPointer = 0;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==6)
|
|
|
- pwm1.CmtnPointer = 1;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==4)
|
|
|
- pwm1.CmtnPointer = 2;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==5)
|
|
|
- pwm1.CmtnPointer = 3;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==1)
|
|
|
- pwm1.CmtnPointer = 4;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==3)
|
|
|
- pwm1.CmtnPointer = 5;
|
|
|
-
|
|
|
-
|
|
|
- } //hall1.CmtnTrigHall == 0x7FFF
|
|
|
- } // ClosedFlag==TRUE
|
|
|
- else{
|
|
|
- pwm1.CmtnPointer = (int16)mod1.Counter;
|
|
|
- }
|
|
|
- pwm1.DutyFunc = DfuncTesting;
|
|
|
- BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the SPEED_PR module and call the speed calculation macro.
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
|
|
|
- {
|
|
|
- speed1.TimeStamp = VirtualTimer;
|
|
|
- SPEED_PR_MACRO(&speed1);
|
|
|
- test = (test ==0 )? 1 : 0;
|
|
|
- }
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the PWMDAC module
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- pwmdac1.MfuncC1 = (int32)hall1.HallGpioAccepted<<20;
|
|
|
- pwmdac1.MfuncC2 = BemfA;
|
|
|
- PWMDAC_MACRO(6,pwmdac1) // PWMDAC 6A, 6B
|
|
|
-
|
|
|
- pwmdac1.MfuncC1 = DCbus_current;
|
|
|
- pwmdac1.MfuncC2 = (int32)mod1.Counter<<20;
|
|
|
- PWMDAC_MACRO(7,pwmdac1) // PWMDAC 7A, 7B
|
|
|
-
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the DATALOG module
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- DlogCh2 = (int16)mod1.Counter;
|
|
|
- DlogCh1 = (int16)hall1.HallGpioAccepted;
|
|
|
- DlogCh3 = (int16)_IQtoIQ15(BemfA);
|
|
|
- DlogCh4 = (int16)_IQtoIQ15(BemfB);
|
|
|
-
|
|
|
-
|
|
|
- #endif // (BUILDLEVEL==LEVEL3)
|
|
|
|
|
|
// =============================== LEVEL 4 ======================================
|
|
|
// Level 4 verifies the closed current loop and current PI controller.
|
|
@@ -786,113 +805,22 @@ void mainLoop(void){
|
|
|
static Uint16 DC_Current_cnt = 0;
|
|
|
//static int32 DC_Current_sum = 0;
|
|
|
static int32 DC_Current_sum_filter = 0;
|
|
|
-
|
|
|
+ int32 dt = _IQ12(0.1);
|
|
|
+ int32 cutoff = _IQ12(1);
|
|
|
DC_current_real = (Uint32)AdcResult.ADCRESULT5*33;//Q12 I真实 = adc * 3.3 / 4096 /0.1
|
|
|
- DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,_IQ12(1),_IQ12(0.05));
|
|
|
+ DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,cutoff,dt);
|
|
|
last_DC_current = DC_current_filer;
|
|
|
- carveData1();
|
|
|
- CanCurve();
|
|
|
-
|
|
|
- UserCANprocess();
|
|
|
-
|
|
|
DC_Current_sum_filter += DC_current_filer*pwm1.DutyFunc>>15;
|
|
|
- // DC_Current_sum += DC_current_real*pwm1.DutyFunc>>15;
|
|
|
DC_Current_cnt++;//由于stall最多16384/20K秒 所以最多0.819715秒计算一次平均值
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- #if (BUILDLEVEL==LEVEL4)
|
|
|
- BemfA = AdcResult.ADCRESULT1;
|
|
|
- BemfB = AdcResult.ADCRESULT2;
|
|
|
- BemfC = AdcResult.ADCRESULT3;
|
|
|
-
|
|
|
- HALL3_READ_MACRO(&hall1);
|
|
|
-
|
|
|
- if (hall1.Revolutions>=0)
|
|
|
- ClosedFlag=TRUE;
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // 电流闭环
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- pid1_spd.Ref = _IQ(0.3);
|
|
|
- pid1_spd.Fbk = _IQabs(speed1.Speed);
|
|
|
- // tempIdc=pid1_idc.Fbk;
|
|
|
- pid1_idc.Umax = pwmlimit<<9;
|
|
|
- pid1_idc.Umin = (-pwmlimit)<<9;
|
|
|
- pid1_idc.Ref = CurrentSet;//给定电流 反转给-
|
|
|
- if(speed1.SpeedRpm<0){
|
|
|
- pid1_idc.Fbk = -_IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
|
|
|
- }else{
|
|
|
- pid1_idc.Fbk = _IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
|
|
|
- }
|
|
|
- // if(pid1_idc.Fbk<0) pid1_idc.Fbk=tempIdc; // Eliminate negative values
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- step++;
|
|
|
- if(step == 1){
|
|
|
- PI_MACRO(&pid1_idc);
|
|
|
- }else if(step == 2){
|
|
|
- PI_MACRO(&pid1_spd);
|
|
|
- step = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if(EnableFlag == 0){
|
|
|
- SetPI(&pid1_spd);
|
|
|
- resetPI(&pid1_idc);
|
|
|
- rmp2.DesiredInput = 0;
|
|
|
- RC2_MACRO(&rmp2);
|
|
|
- pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out); // controlled speed duty-cycle
|
|
|
-
|
|
|
- }else{
|
|
|
- pwm1.DutyFuncIn = (int16)_IQtoIQ15(_IQmpy(pid1_idc.Out,pid1_spd.Out)); // controlled speed duty-cycle
|
|
|
- rmp2.Out = pwm1.DutyFuncIn;
|
|
|
- // pwm1.DutyFuncIn = (int16)(_IQtoIQ15(pid1_idc.Out));
|
|
|
+ if(hallCmtnTrig==1){
|
|
|
+ hallCmtnTrig = 0;
|
|
|
+ DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
|
|
|
+ DC_Current_sum_filter =0;
|
|
|
+ DC_Current_cnt = 0;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- if (ClosedFlag==TRUE)
|
|
|
- {
|
|
|
- if (hall1.CmtnTrigHall==0x7FFF)
|
|
|
- {
|
|
|
- DC_current_avr = DC_Current_sum/DC_Current_cnt;
|
|
|
- DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
|
|
|
- DC_Current_sum = 0;
|
|
|
- DC_Current_sum_filter =0;
|
|
|
- DC_Current_cnt = 0;
|
|
|
- //计算速度
|
|
|
- speed1.TimeStamp = VirtualTimer;
|
|
|
- SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
|
|
|
- PreviousState = hall1.HallGpioAccepted;
|
|
|
- if (hall1.HallGpioAccepted==2)
|
|
|
- pwm1.CmtnPointerIn = 0;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==6)
|
|
|
- pwm1.CmtnPointerIn = 1;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==4)
|
|
|
- pwm1.CmtnPointerIn = 2;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==5)
|
|
|
- pwm1.CmtnPointerIn = 3;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==1)
|
|
|
- pwm1.CmtnPointerIn = 4;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==3)
|
|
|
- { pwm1.CmtnPointerIn = 5;
|
|
|
-
|
|
|
- }//hall1.HallGpioAccepted==3
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
-
|
|
|
- #endif // (BUILDLEVEL==LEVEL4)
|
|
|
-
|
|
|
-
|
|
|
- // =============================== LEVEL 5 ======================================
|
|
|
- // Level 5 verifies the closed speed loop and speed PI controller.
|
|
|
- // ==============================================================================
|
|
|
+ carveData1();
|
|
|
+ CanCurve();
|
|
|
+ UserCANprocess();
|
|
|
|
|
|
#if (BUILDLEVEL==LEVEL5)
|
|
|
|
|
@@ -902,13 +830,9 @@ void mainLoop(void){
|
|
|
BemfA = AdcResult.ADCRESULT1;
|
|
|
BemfB = AdcResult.ADCRESULT2;
|
|
|
BemfC = AdcResult.ADCRESULT3;
|
|
|
- //DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT5); //采样值<<12
|
|
|
|
|
|
- rc1.TargetValue = SpeedRef;
|
|
|
- RC_MACRO(&rc1);
|
|
|
|
|
|
- HALL3_READ_MACRO(&hall1);
|
|
|
- pid1_spd.Ref = rc1.SetpointValue;
|
|
|
+ pid1_spd.Ref = SpeedRef;//rc1.SetpointValue;
|
|
|
pid1_spd.Fbk = speed1.Speed;
|
|
|
if(EnableFlag == 0 || FaultFlag.all !=0){
|
|
|
Enable_ALLOW = FALSE;
|
|
@@ -928,100 +852,18 @@ void mainLoop(void){
|
|
|
|
|
|
PI_MACRO(&pid1_spd);
|
|
|
int16 pwmtmp =(int16)_IQtoIQ15(pid1_spd.Out);
|
|
|
- if((int16)_IQtoIQ15(pid1_spd.Out)>pwmlimit){
|
|
|
- pwmtmp = pwmlimit; // controlled speed duty-cycle
|
|
|
- }else if((int16)_IQtoIQ15(pid1_spd.Out)<-pwmlimit){
|
|
|
- pwmtmp = -pwmlimit; // controlled speed duty-cycle
|
|
|
- }else{
|
|
|
- pwmtmp = (int16)_IQtoIQ15(pid1_spd.Out); // controlled speed duty-cycle
|
|
|
- }
|
|
|
|
|
|
+ //pwmtmp = test;//上位机开环控制
|
|
|
pwm1.DutyFuncIn = ((int32)pwmtmp*LimitMotCtrTemp(600000))>>15;//1分钟降一次
|
|
|
- //pwm1.DutyFuncIn = test;
|
|
|
- rmp2.Out = pwm1.DutyFuncIn;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- ClosedFlag = TRUE;
|
|
|
- if (ClosedFlag==TRUE)
|
|
|
- {
|
|
|
-
|
|
|
- if (hall1.CmtnTrigHall==0x7FFF)
|
|
|
- {
|
|
|
-
|
|
|
- // DC_current_avr = DC_Current_sum/DC_Current_cnt;
|
|
|
- DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
|
|
|
- // DC_Current_sum = 0;
|
|
|
- DC_Current_sum_filter =0;
|
|
|
- DC_Current_cnt = 0;
|
|
|
- speed_direction(hall1.HallGpioAccepted,PreviousState);
|
|
|
-
|
|
|
- if (hall1.HallGpioAccepted==5)
|
|
|
- { pwm1.CmtnPointerIn = 5;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==1)
|
|
|
- { pwm1.CmtnPointerIn = 0;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==3)
|
|
|
- { pwm1.CmtnPointerIn = 1;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==2)
|
|
|
- { pwm1.CmtnPointerIn = 2;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==6)
|
|
|
- { pwm1.CmtnPointerIn = 3;
|
|
|
-
|
|
|
- }
|
|
|
- else if (hall1.HallGpioAccepted==4)
|
|
|
- {
|
|
|
- pwm1.CmtnPointerIn = 4;
|
|
|
- }
|
|
|
- PreviousState = hall1.HallGpioAccepted;
|
|
|
- /*
|
|
|
- if (hall1.HallGpioAccepted==2)
|
|
|
- pwm1.CmtnPointerIn = 0;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==6)
|
|
|
- pwm1.CmtnPointerIn = 1;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==4)
|
|
|
- pwm1.CmtnPointerIn = 2;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==5)
|
|
|
- pwm1.CmtnPointerIn = 3;
|
|
|
-
|
|
|
- else if (hall1.HallGpioAccepted==1)
|
|
|
- pwm1.CmtnPointerIn = 4;
|
|
|
|
|
|
- else if (hall1.HallGpioAccepted==3)
|
|
|
- { pwm1.CmtnPointerIn = 5;
|
|
|
-
|
|
|
- }//hall1.HallGpioAccepted==3
|
|
|
- */
|
|
|
- //}//direction == Normal
|
|
|
-
|
|
|
- } // delay
|
|
|
- } // ClosedFlag==TRUE
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if(Enable_ALLOW == 0 && pwm1.DutyFunc==0){
|
|
|
- bldcpwm_close();
|
|
|
-
|
|
|
- }else{
|
|
|
- BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // BLDCPWM_MACRO(1,2,3,&pwm1);
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
- // Connect inputs of the DATALOG module
|
|
|
- // ------------------------------------------------------------------------------
|
|
|
+ if(pwm1.DutyFuncIn>pwmlimit){
|
|
|
+ pwm1.DutyFuncIn = pwmlimit; // controlled speed duty-cycle
|
|
|
+ }else if(pwm1.DutyFuncIn<-pwmlimit){
|
|
|
+ pwm1.DutyFuncIn = -pwmlimit; // controlled speed duty-cycle
|
|
|
+ }
|
|
|
|
|
|
+ rmp2.Out = pwm1.DutyFuncIn;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
#endif // (BUILDLEVEL==LEVEL5)
|
|
@@ -1029,9 +871,9 @@ void mainLoop(void){
|
|
|
//IsrTime = MastIsrTimePercent();
|
|
|
T2 = CpuTimer2.RegsAddr->TIM.all;
|
|
|
if(T1 < T2){
|
|
|
- IsrTime = (Uint32)(T1 +CpuTimer2.RegsAddr->PRD.all -T2)* 100 /6000;
|
|
|
+ IsrTime = (Uint32)(T1 +CpuTimer2.RegsAddr->PRD.all -T2)* 100 /7500;
|
|
|
}else{
|
|
|
- IsrTime = (Uint32)(T1-T2)* 100 /6000;
|
|
|
+ IsrTime = (Uint32)(T1-T2)* 100 /7500;
|
|
|
}
|
|
|
|
|
|
}
|