diff --git a/stm32/HARDWARE/TIMER/timer.c b/stm32/HARDWARE/TIMER/timer.c index 6b2bac9..22f78b9 100644 --- a/stm32/HARDWARE/TIMER/timer.c +++ b/stm32/HARDWARE/TIMER/timer.c @@ -3,6 +3,7 @@ #include "wifi.h" #include "math.h" #include "encoder.h" +#include "motor.h" extern char smog_value_buf[7]; extern char temp_value_buf[7]; @@ -11,6 +12,28 @@ extern char beam_value_buf[7]; extern struct UserInfo userInfo; +float Velocity_KP_Mode2 = 1.1,Velocity_KI_Mode2=7.7; + + +void Set_Pwm(int motor) +{ + if(motor<0) AIN2=1, AIN1=0; + else AIN2=0,AIN1=1; + PWMA=abs(motor); +} + +int Incremental_PI_B_Mode2 (int Encoder,int Target) +{ + static int Bias,Pwm,Last_bias; + Bias=Encoder-Target; //计算偏差 + Pwm+=Velocity_KP_Mode2*(Bias-Last_bias)+Velocity_KI_Mode2*Bias; //增量式PI控制器 + if(Pwm>7200)Pwm=7200; + if(Pwm<-7200)Pwm=-7200; + Last_bias=Bias; //保存上一次偏差 + return Pwm; //增量输出 +} + + void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; @@ -42,9 +65,12 @@ void TIM3_Int_Init(u16 arr,u16 psc) void TIM3_IRQHandler(void) { + int pwm; if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { userInfo.currentSpeed = Read_Encoder(2); + pwm = Incremental_PI_B_Mode2(userInfo.currentSpeed, 10); + Set_Pwm(pwm); TIM_ClearITPendingBit(TIM3, TIM_IT_Update); } } diff --git a/stm32/OBJ/Template.axf b/stm32/OBJ/Template.axf index 923560e..1d24410 100644 Binary files a/stm32/OBJ/Template.axf and b/stm32/OBJ/Template.axf differ diff --git a/stm32/OBJ/Template.build_log.htm b/stm32/OBJ/Template.build_log.htm index ef8ee28..30376c0 100644 --- a/stm32/OBJ/Template.build_log.htm +++ b/stm32/OBJ/Template.build_log.htm @@ -21,18 +21,18 @@ Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V3.0.1.0 Dialog DLL: TCM.DLL V1.35.1.0