-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedControl.c
64 lines (57 loc) · 1.45 KB
/
SpeedControl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "common.h"
#include "include.h"
#include "SpeedControl.h"
#include "DirectionControl.h"
int32 Right_Speed;
int32 Left_Speed;
void Get_Speed()
{
Left_Speed = tpm_pulse_get(TPM2); //获取左侧tpm速度
Right_Speed = lptmr_pulse_get(); //获取右侧lptmr速度
/*清空计数值*/
tpm_pulse_clean(TPM2);
lptmr_pulse_clean();
}
extern uint8 S_Prop;
extern uint8 S_Inte;
extern uint8 S_Diff;
extern uint8 H_Speed;
extern uint8 L_Speed;
uint16 Speed_P_L;
uint16 Speed_P_R;
uint16 Speed_I_L;
uint16 Speed_I_R;
uint16 Motor_Right_Out;
uint16 Motor_Left_Out;
uint8 Set_Speed=0;
void Correct_Speed(uint8 SetSpeed)
{
uint32 S_Speed_temp;
int32 SpeedError_L;
int32 SpeedError_R;
S_Speed_temp = Set_Speed * 500;
/*计算速度误差*/
SpeedError_L = S_Speed_temp - Left_Speed;
SpeedError_R = S_Speed_temp - Right_Speed;
/*速度PI*/
Speed_P_L = SpeedError_L * S_Prop;
Speed_P_R = SpeedError_R * S_Prop;
//积分累加
Speed_I_L += SpeedError_L * S_Inte;
Speed_I_R += SpeedError_R * S_Inte;
//限幅
if(Speed_I_L > Speed_I_Max)
Speed_I_L = Speed_I_Max;
if(Speed_I_R > Speed_I_Max)
Speed_I_R = Speed_I_Max;
Motor_Right_Out = (Speed_P_R + Speed_I_R);
Motor_Left_Out = (Speed_P_L + Speed_I_L);
}
void Motor_Out()
{
tpm_pwm_duty(TPM0,TPM_CH0,Motor_Right_Out);
tpm_pwm_duty(TPM0,TPM_CH1,0);
tpm_pwm_duty(TPM0,TPM_CH2,Motor_Right_Out);
tpm_pwm_duty(TPM0,TPM_CH3,0);
tpm_pwm_duty(TPM1,TPM_CH0,DirectPID());
}