-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForest.ino
172 lines (166 loc) · 2.99 KB
/
Forest.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
#include <stdlib.h>
#include <Wire.h>
#define m1 10
#define m2 6
#define m3 8
#define m1pwm 11
#define m2pwm 7
#define m3pwm 9
int a, error, f, integrate, pid, pwm1, pwm2, pwm3, d, i = 0, set_pwm = 100,Fpwm=100;
int dist[5] = {0, 146, 138, 170, 70,100};
int c[5] = {0, 2, -18, 0, -20,25};
float kp[5] = {0 , 5, 3, 4, 2,5 };
int flag = 0;
void setup()
{
delay(5000);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m1pwm, OUTPUT);
pinMode(m2pwm, OUTPUT);
pinMode(m3pwm, OUTPUT);
Wire.begin();
Serial.begin (2000000);
Serial3.begin(115200);
}
void loop()
{ Serial.print(i);
Serial.print("\t");
Serial.print(a);
Serial.print("\t");
Serial.print(d);
Serial.print("\t");
Serial.print(f);
Serial.print("\t");
Serial.print(dist[i]);
Serial.print("\t");
Serial.print(c[i]);
Serial.print("\t");
// Serial.print(flag);
//Serial.print("\t");
Serial.print(pwm1);
Serial.print("\t");
Serial.print(pwm2);
Serial.print("\t");
Serial.println(pwm3);
distance();
angle();
rotation();
calc();
pwm();
}
int rotation ()
{
if ( d == dist[i] )
{
//Serial.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
i++;
if ((( i % 2) == 0) &&i<5)
a = 2 ;
else if (i<4);
a = 1;
}
}
void distance()
{
Wire.requestFrom(8, 1);
while (Wire.available())
{
d = Wire.read();
}
}
void angle()
{ //Serial.print("In sending\t");
if (Serial3.available())
{
int b = Serial3.read();
f = map(b, 0, 255, 0, 359);
if (f > 180)
{
f = f - 360;
}
}
}
void calc()
{
error = f - c[i];
pid = (kp[i] * error);
if (a == 2) // when using wheel 1 and 3
{
/*if ( flag == 0 )
{
int pid1 = (4 * error);
pwm1 = 150 - pid1;
pwm3 = 150 + pid1;
pwm2 = 0;
if (((error < 2 ) || (error == 2)) && ((error > -2) || (error == -2)))
{
flag = 1;
}
}*/
pwm2 = set_pwm - pid; // should move anticlockwise or else 0
pwm3 = set_pwm + pid; // should move clockwise or else 0
pwm1 = pid;
if ( error > 0 )
{
digitalWrite(m1, LOW);
}
else if ( error < 0)
{
digitalWrite(m1, HIGH);
}
else if (error == 0)
{
pwm1 = 0;
}
digitalWrite(m2, HIGH);
digitalWrite(m3, LOW);
}
else if (a == 1) // when using wheel 1 and 2
{
pwm2 = set_pwm + pid;
pwm1 = set_pwm - pid;
pwm3 = 0;
digitalWrite(m2, LOW);
digitalWrite(m1 , HIGH);
}
if (i > 4)//robot is adjusted till the encoder is adjacent to the line
{pwm1 = set_pwm - pid; // should move anticlockwise or else 0
pwm3 = set_pwm + pid;
pwm2=0;
digitalWrite(m1,1);
digitalWrite(m3,0);
}
}
void pwm()
{
if (pwm1 > 254)
{
pwm1 = 254;
}
else if (pwm1 < 0 )
{
pwm1 = 0 ;
}
if (pwm2 > 254)
{
pwm2 = 254;
}
else if (pwm2 < 0)
{
pwm2 = 0;
}
if (pwm3 > 254)
{
pwm3 = 254;
}
else if (pwm3 < 0)
{
pwm3 = 0;
}
analogWrite(m1pwm, pwm1);
analogWrite(m2pwm, pwm2);
analogWrite(m3pwm, pwm3);
}
}