-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_jan07a.ino
executable file
·311 lines (260 loc) · 76.8 KB
/
sketch_jan07a.ino
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <avr/wdt.h>
#define TIMEOUT WDTO_8S
//前轮引脚
int f1=23;
int f2=25;
int fc=8;
//左轮引脚
int l1=27;
int l2=29;
int lc=10;
//右轮引脚
int r1=35;
int r2=37;
int rc=11;
double spd=140;
unsigned int state = 0;
unsigned int f_wheel; //记录U型测速模块的次数
unsigned int l_wheel;
unsigned int r_wheel;
double speedf;
double speedl;
double speedr;
double kp=1.5, ki=0.00, kd=1.2;
unsigned long SampleTime2=20;
unsigned long preTime;
int pwm_f,pwm_l,pwm_r;
unsigned long pid_pre;
double lastErr_f = 0;
double lastErr_l = 0;
double lastErr_r = 0;
double errSum_f = 0;
double errSum_l = 0;
double errSum_r = 0;
double Setpoint_f=0;
double Setpoint_l=0;
double Setpoint_r=0;
int Led = 5;
int lastTime2;
double fv;
double lv;
double rv;
double Vx;
double Vy;
double W,w;
int i = 0;
String words;
int count = 0;
void fcount()
{
f_wheel++;
}
void lcount()
{
l_wheel++;
}
void rcount()
{
r_wheel++;
}
void setup ()
{
Serial.begin(9600);
attachInterrupt(1,rcount, FALLING);
attachInterrupt(0,lcount, FALLING);
attachInterrupt(4,fcount, FALLING);
pinMode(f1,OUTPUT);
pinMode(f2,OUTPUT);
pinMode(fc,OUTPUT);
pinMode(l1,OUTPUT);
pinMode(l2,OUTPUT);
pinMode(lc,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(rc,OUTPUT);
pinMode(Led,OUTPUT);
digitalWrite(Led,HIGH);
InitPid();
wdt_enable(TIMEOUT);
Serial.println("Stlt Working.");
}
void loop()
{
if(Serial.available()>0)
{
while(Serial.available()>0)
{
int inChar = Serial.read();
delay (2);
words += (char)inChar;
if (inChar == '&' && i == 0)
{
Vx = words.toInt();
words = "";
i++;
}
else if (inChar == '&' && i == 1)
{
Vy = words.toInt();
words = "";
i++;
}
else if (inChar == '&' && i == 2)
{
W = words.toInt();
words = "";
i=0;
}
}
Serial.print(Vx);
Serial.print(" ");
Serial.print(Vy);
Serial.print(" ");
Serial.print(W);
Serial.println();
lastErr_f=0;
lastErr_l=0;
lastErr_r=0;
Calculate();
}
Move();
wdt_reset();
}
void Calculate()
{
w = W / 100;
fv = -1 * Vy + 40 * w;
lv = -0.866 * Vx + 0.5 * Vy + 40 * w;
rv = 0.866 * Vx + 0.5 * Vy + 40 * w;
Serial.print(fv);
Serial.print(" ");
Serial.print(lv);
Serial.print(" ");
Serial.print(rv);
Serial.println();
Setpoint_f = abs(fv);
Setpoint_l = abs(lv);
Setpoint_r = abs(rv);
}
void Move()
{
go(fv,lv,rv);
if(fv==0 && lv==0 && rv==0)
{
state = 0;//stopping
digitalWrite(f2,LOW);
digitalWrite(f1,LOW);
digitalWrite(l1,LOW);
digitalWrite(l2,LOW);
digitalWrite(r2,LOW);
digitalWrite(r1,LOW);
}
else
{
state = 1;
}
if(state == 1)
{
testPID();
}
}
void go(int d_f,int d_l,int d_r)
{
if(d_f>0)
{
digitalWrite(r2,LOW);
digitalWrite(r1,HIGH);
}
else
{
digitalWrite(r2,HIGH);
digitalWrite(r1,LOW);
}
if(d_l>0)
{
digitalWrite(l1,HIGH);
digitalWrite(l2,LOW);
}
else
{
digitalWrite(l1,LOW);
digitalWrite(l2,HIGH);
}
if(d_r>0)
{
digitalWrite(f2,HIGH);
digitalWrite(f1,LOW);
}
else
{
digitalWrite(f2,LOW);
digitalWrite(f1,HIGH);
}
}
void InitPid()
{
f_wheel=0;
l_wheel=0;
r_wheel=0;
pid_pre = millis();
}
//===============PID=====================
void testPID()
{
unsigned long pid_now =millis();
int pid_time =(pid_now-pid_pre);
if(pid_time>=SampleTime2)
{
pid_pre = pid_now;
double f_count = (double)f_wheel;
double l_count = (double)l_wheel;
double r_count = (double)r_wheel;
speedf = f_count/pid_time*40.8854;
speedl = l_count/pid_time*40.8854;
speedr = r_count/pid_time*40.8854;
f_wheel = 0;
l_wheel = 0;
r_wheel = 0;
double Input_f = speedf;
double Input_l = speedl;
double Input_r = speedr;
double error_f = Setpoint_f - Input_f;
double error_l = Setpoint_l - Input_l;
double error_r = Setpoint_r - Input_r;
errSum_f += error_f;
errSum_l += error_l;
errSum_r += error_r;
double dErr_f=(error_f -lastErr_f);
double dErr_l=(error_l -lastErr_l);
double dErr_r=(error_r -lastErr_r);
double Output_f =kp*error_f + ki *errSum_f +kd *dErr_f;
double Output_l =kp*error_l + ki *errSum_l +kd *dErr_l;
double Output_r =kp*error_r + ki *errSum_r +kd *dErr_r;
if(Output_f> 255) Output_f = 255;
if(Output_l> 255) Output_l = 255;
if(Output_r> 255) Output_r = 255;
if(Output_f< -255) Output_f = -255;
if(Output_l< -255) Output_l = -255;
if(Output_r< -255) Output_r = -255;
/*计算1.33的speeds次方*/
pwm_f = pwm_f + (int)Output_f;
pwm_l = pwm_l + (int)Output_l;
pwm_r = pwm_r + (int)Output_r;
if(pwm_f> 255) pwm_f = 255;
if(pwm_l> 255) pwm_l = 255;
if(pwm_r> 255) pwm_r = 255;
if(pwm_f< 0) pwm_f = 0;
if(pwm_l< 0) pwm_l = 0;
if(pwm_r< 0) pwm_r = 0;
lastErr_f = error_f;
lastErr_l = error_l;
lastErr_r = error_r;
pwm_output(pwm_f,pwm_l,pwm_r);
}
}
void pwm_output(int pwm_f,int pwm_l,int pwm_r)
{
analogWrite(fc,pwm_f);
analogWrite(lc,pwm_l);
analogWrite(rc,pwm_r);
}