-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_sonar_setup_with_previous.ino
314 lines (261 loc) · 6.54 KB
/
encoder_sonar_setup_with_previous.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
312
313
314
#include <math.h>
#define cons 0.01
#define N 48 //Number of ticks per revolution
#define pi 3.1416 // Value of pi
#define input_1 7 //For Motor(2)
#define input_2 6
#define enable_1 5
#define input_3 10 //For Motor(1)
#define input_4 11
#define enable_2 9
#define switchPin 8 //For Switch
#define Channel_A_Motor_1 2
#define Channel_B_Motor_1 3
#define Channel_A_Motor_2 20
#define Channel_B_Motor_2 21
#define Interrupt_Channel_A_Motor_1 0
#define Interrupt_Channel_B_Motor_1 1
#define Interrupt_Channel_A_Motor_2 3
#define Interrupt_Channel_B_Motor_2 2
long int count_1 = 0 , count_2 = 0;
boolean A , B , C , D;
byte state_1 , statep_1 , state_2 , statep_2 ;
volatile int QEM[16] = { 0, -1, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, -1, 0, 1, 0 };
volatile int index_1 , index_2;
float L = 150 , R = 21; //Distance of robot wheel and radius of the wheel
float v = 0.1 , w = (pi/4); //Input linear velocity and angular velocity
float vel_r , vel_l;
int pwm_r , pwm_l;
int right_ticks, left_ticks;
int prev_right_ticks = 0, prev_left_ticks = 0;
float d_left, d_right, d_center, phi;
float theta=0.0, x=0.0, y=0.0;
float theta_dt, x_dt, y_dt;
float m_per_tick = (2*pi*R)/N;
void Achange();
void Bchange();
void Cchange();
void Dchange();
void setup()
{
Serial.begin(57600);
pinMode(input_1 , OUTPUT); //For Motor(1)
pinMode(input_2 , OUTPUT);
pinMode(enable_1 , OUTPUT);
pinMode(input_3 , OUTPUT); //For Motor(2)
pinMode(input_4 , OUTPUT);
pinMode(enable_2 , OUTPUT);
pinMode(switchPin, INPUT); //For Switch
pinMode(Channel_A_Motor_1, INPUT);
pinMode(Channel_B_Motor_1, INPUT);
pinMode(Channel_A_Motor_2, INPUT);
pinMode(Channel_B_Motor_2, INPUT);
attachInterrupt(Interrupt_Channel_A_Motor_1, Achange, CHANGE);
attachInterrupt(Interrupt_Channel_B_Motor_1, Bchange, CHANGE);
attachInterrupt(Interrupt_Channel_A_Motor_2, Cchange, CHANGE);
attachInterrupt(Interrupt_Channel_B_Motor_2, Dchange, CHANGE);
//Read the initial state value of A , B , C and D
A = digitalRead(Channel_A_Motor_1);
B = digitalRead(Channel_B_Motor_1);
C = digitalRead(Channel_A_Motor_2);
D = digitalRead(Channel_B_Motor_2);
//Set initial state value of Motor_1
if( (A==HIGH)&&(B==HIGH) )
{
statep_1 = 0;
}
else if( (A==HIGH)&&(B==LOW) )
{
statep_1 = 1;
}
else if( (A==LOW)&&(B==LOW) )
{
statep_1 = 2;
}
else if( (A==LOW)&&(B==HIGH) )
{
statep_1 = 3;
}
//Set initial state value of Motor_2
if( (C==HIGH)&&(D==HIGH) )
{
statep_2 = 0;
}
else if( (C==HIGH)&&(D==LOW) )
{
statep_2 = 1;
}
else if( (C==LOW)&&(D==LOW) )
{
statep_2 = 2;
}
else if( (C==LOW)&&(D==HIGH) )
{
statep_2 = 3;
}
}
void loop()
{
if(digitalRead(switchPin) == HIGH)
{
right_ticks = count_1;
left_ticks = count_2;
d_right = m_per_tick * (right_ticks - prev_right_ticks);
d_left = m_per_tick * (left_ticks - prev_left_ticks);
prev_right_ticks = right_ticks;
prev_left_ticks = left_ticks;
d_center = (d_right + d_left)/2;
phi = (d_right - d_left)/L;
x_dt = d_center*cos(theta);
y_dt = d_center*sin(theta);
theta_dt = phi;
theta = theta + theta_dt;
Serial.println(theta);
x = x + x_dt;
y = y + y_dt;
vel_r = (2*v+w*L)/(2*R);
vel_l = (2*v-w*L)/(2*R);
pwm_r = 5*15*exp(0.002*abs(vel_r));
pwm_l = 5*15*exp(0.002*abs(vel_l));
Serial.print(pwm_r);
Serial.print(" ");
Serial.println(pwm_l);
if(vel_r >= 0)
{
digitalWrite(input_1 , HIGH);
digitalWrite(input_2 , LOW);
digitalWrite(enable_1 , LOW);
analogWrite(enable_1 , pwm_r);
}
else
{
digitalWrite(input_1 , LOW);
digitalWrite(input_2 , HIGH);
digitalWrite(enable_1 , LOW);
analogWrite(enable_1 , pwm_r);
}
if(vel_l >= 0)
{
digitalWrite(input_3 , HIGH);
digitalWrite(input_4 , LOW);
digitalWrite(enable_2 , LOW);
analogWrite(enable_2 , pwm_l);
}
else
{
digitalWrite(input_3 , LOW);
digitalWrite(input_4 , HIGH);
digitalWrite(enable_2 , LOW);
analogWrite(enable_2 , pwm_l);
}
}
else
{
digitalWrite(input_1 , LOW);
digitalWrite(input_2 , LOW);
digitalWrite(enable_1 , LOW);
digitalWrite(input_3 , LOW);
digitalWrite(input_4 , LOW);
digitalWrite(enable_2 , LOW);
}
}
void Achange()
{
A = digitalRead(Channel_A_Motor_1);
B = digitalRead(Channel_B_Motor_1);
//Determine State Value
if( (A==HIGH)&&(B==HIGH) )
{
state_1 = 0;
}
else if( (A==HIGH)&&(B==LOW) )
{
state_1 = 1;
}
else if( (A==LOW)&&(B==LOW) )
{
state_1 = 2;
}
else if( (A==LOW)&&(B==HIGH) )
{
state_1 = 3;
}
index_1 = 4*state_1 + statep_1;
count_1 = count_1 + QEM[index_1];
statep_1 = state_1;
}
void Bchange()
{
A = digitalRead(Channel_A_Motor_1);
B = digitalRead(Channel_B_Motor_1);
//Determine State Value
if( (A==HIGH)&&(B==HIGH) )
{
state_1 = 0;
}
else if( (A==HIGH)&&(B==LOW) )
{
state_1 = 1;
}
else if( (A==LOW)&&(B==LOW) )
{
state_1 = 2;
}
else if( (A==LOW)&&(B==HIGH) )
{
state_1 = 3;
}
index_1 = 4*state_1 + statep_1;
count_1 = count_1 + QEM[index_1];
statep_1 = state_1;
}
void Cchange()
{
C = digitalRead(Channel_A_Motor_2);
D = digitalRead(Channel_B_Motor_2);
//Determine State Value
if( (C==HIGH)&&(D==HIGH) )
{
state_2 = 0;
}
else if( (C==HIGH)&&(D==LOW) )
{
state_2 = 1;
}
else if( (C==LOW)&&(D==LOW) )
{
state_2 = 2;
}
else if( (C==LOW)&&(D==HIGH) )
{
state_2 = 3;
}
index_2 = 4*state_2 + statep_2;
count_2 = count_2 + QEM[index_2];
statep_2 = state_2;
}
void Dchange()
{
C = digitalRead(Channel_A_Motor_2);
D = digitalRead(Channel_B_Motor_2);
//Determine State Value
if( (C==HIGH)&&(D==HIGH) )
{
state_2 = 0;
}
else if( (C==HIGH)&&(D==LOW) )
{
state_2 = 1;
}
else if( (C==LOW)&&(D==LOW) )
{
state_2 = 2;
}
else if( (C==LOW)&&(D==HIGH) )
{
state_2 = 3;
}
index_2 = 4*state_2 + statep_2;
count_2 = count_2 + QEM[index_2];
statep_2 = state_2;
}