-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51car.h
242 lines (196 loc) · 4.73 KB
/
51car.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#ifndef car
#define car
#include<reg52.h>
#include<stdio.h>
#define uint unsigned int //16位,两个字节
#define uchar unsigned char //8位,一个字节
/*uint count_R, count_R_10ms, count_L, count_L_10ms, count_time = 0;//编码器输入脉冲下降沿计数,10ms的计数
int pulse_revolutions = 400; //转一圈脉冲数 ,全速运行每10ms17个脉冲
int count_all_R, count_all_L = 0;
bit direction_R, direction_L;//测得两电机旋转方向*/
int LeftActualSpeed = 0;
int RightActualSpeed = 0;
float Kp=0,Ki=5,Kd=0; //系数
uint Error1=0,Error2=0,Error3=0,Error4=0; //误差
uint LeftCount = 0;
uint RightCount = 0;
uint Left_Speed = 0; //规定速度
uint Right_Speed = 0;
int time=0; //PWM时间循环
int Left_Pwm = 0;//规定占空比
int Right_Pwm = 0;
sbit Left_led = P0^0; //循迹模块
sbit Right_led = P0^2;
sbit IN1 = P1^2; //L289N驱动的定义
sbit IN2 = P1^3;
sbit IN3 = P1^5;
sbit IN4 = P1^4;
sbit ENA = P1^6;
sbit ENB = P1^7;
//A相脉冲下降沿时B脉冲高低电平判断正反转
/*sbit RIGHT_B = P1^1;
sbit LEFT_B = P1^0 ;*/
void delay(int k)//延时k ms
{
uint i;
uchar j;
for(i = 0; i < k; i++)
{
for(j = 0; j < 200; j++) ;
for(j = 0; j < 102; j++) ;
}
}
void Right_moto_go()
{
IN3 = 0;
IN4 = 1;
}
void Left_moto_go()
{
IN1 = 1;
IN2 = 0;
}
void Right_moto_back()
{
IN3 = 1;
IN4 = 0;
}
void Left_moto_back()
{
IN1 = 0;
IN2 = 1;
}
/*void pid()
{
int Leftpwm_output = 0;
int Rightpwm_output = 0;
if (direction_L == 1)
LeftActualSpeed = count_L_10ms ;//10ms测得脉冲*100
else
LeftActualSpeed= -(int)count_L_10ms ;
if (direction_R == 1)
RightActualSpeed = count_R_10ms;//10ms测得脉冲
else
RightActualSpeed = -(int)count_R_10ms;
Error3 = Left_Speed - LeftActualSpeed;
Error4 = Right_Speed - RightActualSpeed;//Kp*(Error3-Error2)||Kp*(Error4-Error2)+
Leftpwm_output = Ki*(Error3)+Kd*(Error3-2*Error2+Error1);
Rightpwm_output = Ki*(Error4)+Kd*(Error4-2*Error2+Error1);
Left_Pwm += Leftpwm_output;
Right_Pwm += Rightpwm_output;
//Left_Pwm = Left_Speed;
//Right_Pwm = Right_Speed;
if((Left_Pwm >= 20)||(Right_Pwm >= 20))
{
Left_Pwm = Right_Pwm = 20;
}
else if((Left_Pwm < -20)||(Right_Pwm < -20))
{
Left_Pwm = Right_Pwm = -20;
}
}*/
void frontRun(int right,int left)
{
Right_Speed = right;
Left_Speed = left;
Right_moto_go();
Left_moto_go();
}
void backRun(int right,int left)
{
Right_Speed = right;
Left_Speed = left;
Right_moto_back();
Left_moto_back();
}
void turnLeft(int right,int left)//左转
{
Left_Speed = left;
Right_Speed = right;
Left_moto_back();
Right_moto_go();
}
void turnRight(int right,int left)//右转
{
Right_Speed = right;
Left_Speed = left;
Right_moto_back();
Left_moto_go();
}
void stop()
{
ENA = 0;
ENB = 0;
}
void pwm_out_left()
{
if(time < Left_Speed)
ENA = 1;
else
ENA = 0;
}
void pwm_out_right()
{
if(time < Right_Speed)
ENB = 1;
else
ENB = 0;
}
//void int0() interrupt 0 //外部中断0处理编码器信号
//{
// LeftCount ++;
// if (LEFT_B == 0)
// {
// direction_L = 1; //正转
// count_all_L++;
// }
// else
// {
// direction_L = 0;
// count_all_L--;
// }
//}
//void int1() interrupt 2 //外部中断1处理编码器信号
//{
// RightCount ++;
// if (RIGHT_B == 1)
// {
// direction_R = 1; //正转
// count_all_R++;
// }
// else
// {
// direction_R = 0;
// count_all_R--;
// }
//}
void timer0() interrupt 1 //定时器0中断处理函数
{
time++;
TR0 = 0; //关闭T0
TH0 = 0XFc;//重新装入定时时间,50hz
TL0 = 0X18;
pwm_out_left();
pwm_out_right();
if (time >= 20) //
{
time = 0;
}
TR0 = 1; //打开T0
}
//void timer1() interrupt 3 //定时器1中断处理函数,测一次速
//{
//
// TR1=0;
// TH1 = 0XD8;
// TL1 = 0XF0;
// TR1=1;
//
// count_L_10ms = LeftCount;
// LeftCount = 0;
// count_R_10ms = RightCount;
// RightCount = 0;
//
//
//}
#endif