-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimization working
More file actions
274 lines (251 loc) · 6.4 KB
/
optimization working
File metadata and controls
274 lines (251 loc) · 6.4 KB
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
#define led 13
#define dryrunswitch 12
#define actualrunswitch 11
#define AIN1 6
#define BIN1 9
#define AIN2 5
#define BIN2 8
#define PWM1 3
#define PWM2 10
#define stby 7
#define basespeed 90
#define Kp 30// to be determined
#define Kd 40
#define l 1
#define s 2
#define r 3
#define b 4
#define finish 5
int y =0;
void motor1(bool dir1, bool dir2, uint8_t pwm);
void motor2(bool dir1, bool dir2, uint8_t pwm);
void motor1(bool dir1, bool dir2, uint8_t pwm)
{
digitalWrite(AIN1, dir1);
digitalWrite(AIN2, dir2);
analogWrite(PWM1, pwm);
}
void motor2(bool dir1, bool dir2, uint8_t pwm)
{
digitalWrite(BIN1, dir1);
digitalWrite(BIN2, dir2);
analogWrite(PWM2, pwm);
}
int index = 0;
int path[100];
bool linestate[5];
float error, error_prev;
int steeringval;
int leftmotspeed;
int rightmotspeed;
float numerator;
float denominator;
void turnleft()
{
motor1(HIGH, LOW, basespeed);
motor2(HIGH, LOW, basespeed);
while ((linestate[4] == 1) || (linestate[0] == 1))
{
Serial.print("Moving forward");
readir();
}
int start_time = millis();
while (millis() - start_time < 10){}
motor1(HIGH, HIGH,basespeed);
motor2(HIGH, LOW, basespeed);
Serial.print("stopped moving forward and turning left");
Serial.print("");
while (1)
{
readir();
Serial.print("trying to come out of the line");
if (linestate[2] == 0)
{
break;
}
}
while (1)
{
readir();
Serial.print("turning left into the line");
if ( linestate[2] == 1)
{
break;
}
}
Serial.print("reached line");
motor2(HIGH, HIGH, 0);
}
void turnright()
{
motor1(HIGH, LOW, basespeed);
motor2(HIGH, HIGH, 0);
Serial.println("turning right");
while (1)
{
readir();
Serial.println("trying to go out of the right line");
if (linestate[2] == 0)
{
break;
}
}
while (1) //
{
readir();
Serial.println("finding the right line");
if (linestate[1] && linestate[2])
{
break;
}
}
motor1(HIGH, HIGH, 0);
}
void turnback()
{
int start_time1 = millis();
while(millis() - start_time1 < 400){}
Serial.println("turning back");
motor1(HIGH, LOW, 120);
motor2(LOW, HIGH, 120);
while (1)
{
readir();
if (linestate[2] == 0)
{
break;
}
}
while (1)
{
readir();
if (linestate[2] == 1)
{
break;
}
}
motor1(HIGH, HIGH, 0);
motor2(HIGH, HIGH, 0);
}
void readir()
{
for (int i = 0; i < 5; i++)
{
linestate[i] = digitalRead(18 - i);
// Serial.print(linestate[i]);
// Serial.print("\t");
}
Serial.println("");
}
void linefollow()
{
Serial.print("Following line");
error_prev = error;
numerator = ((linestate[0] * (-2)) + (linestate[1] * (-1)) + (linestate[3]) + (linestate[4] * 2));
denominator = linestate[0] + linestate[1] + linestate[2] + linestate[3] + linestate[4];
error = (numerator) / (denominator);
// Serial.print("Numerator: ");
// Serial.print(numerator);
// Serial.print("\t");
// Serial.print("Dinominator: ");
// Serial.print(denominator);
// Serial.print("\n");
// Serial.print("Error: ");
// Serial.print(error);
// Serial.print("\n");
steeringval = Kp * error + Kd * (error - error_prev);
leftmotspeed = (basespeed) + steeringval;
rightmotspeed = (basespeed)-steeringval;
motor1(1, 0, (leftmotspeed < 0 ? 0 : leftmotspeed));
// Serial.print(leftmotspeed);
// Serial.print("\t");
motor2(1, 0, (rightmotspeed < 0 ? 0 : rightmotspeed));
// Serial.println(rightmotspeed);
}
void setup()
{
Serial.begin(9600);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(actualrunswitch, INPUT);
pinMode(dryrunswitch, INPUT);
pinMode(led, OUTPUT);
digitalWrite(stby, HIGH);
}
void loop()
{ while(dryrunswitch==LOW)
{
;
}
while (1)
{
readir();
linefollow();
if ((linestate[0] == 1) && (linestate[1] == 1) && (linestate[2] == 1) && (linestate[3] == 0) && (linestate[4] == 0))
{
if ((linestate[0] == 1) && (linestate[1] == 1) && ((linestate[2] == 1) || (linestate[3] == 0)) && (linestate[4] == 0))
{
turnleft();
}
}
else if ((linestate[0] == 1) && (linestate[1] == 1) && (linestate[2] == 1) && (linestate[3] == 1) && (linestate[4] == 1))
{
Serial.println("either finish or left");
int start_time2 = millis();
while(millis() - start_time2 < 200){
Serial.println("moving forward !!!");
}
readir();
if((linestate[0] == 1) && (linestate[1] == 1) && (linestate[2] == 1) && (linestate[3] == 1) && (linestate[4] == 1)){
Serial.println("end detected");
motor1(HIGH, HIGH, 0);
motor2(HIGH, HIGH, 0);
digitalWrite(led, HIGH);
y = 1;
while(1){
delay(1);
}
}
else{
Serial.println("left activated through here");
turnleft(); // not added finish
}
}
else if (linestate[0] && linestate[2]){
turnleft();
}
else if ((linestate[0] == 0) && (linestate[1] == 0) && (linestate[2] == 1) && (linestate[3] == 1) && (linestate[4] == 1))
{
motor1(HIGH, LOW, basespeed);
motor2(HIGH, LOW, basespeed);
while ((linestate[4] == 1) && (linestate[0] == 0))
{
Serial.println("moving forward in a r or s junction");
readir();
}
motor1(HIGH, HIGH, 0);
motor2(HIGH, HIGH, 0);
if (linestate[0]+linestate[1]+linestate[2]+linestate[3]+linestate[4])
{
Serial.println("Straight line detected");
linefollow();
}
else
{
turnright();
}
}
else if (!(linestate[0] || linestate[1] || linestate[2] || linestate[3] || linestate[4])){
turnback();
}
}
}