-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSA08_PID.ino
181 lines (146 loc) · 3.75 KB
/
LSA08_PID.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
//Lsc08 Auto Bot code
const float Kp = 5; // Kp value that you have to change
//const float Kd = 2; // Kd value that you have to change
const float Ki = 0.01;
const int setPoint = 35; // Middle point of sensor array
const int baseSpeed = 70; // Base speed for your motors
const int maxSpeed = 90; // Maximum speed for your motors
const byte rx = 0; // Defining pin 0 as Rx
const byte tx = 1; // Defining pin 1 as Tx
const byte serialEn = 2; // Connect UART output enable of LSA08 to pin 2
const byte jp = 3; // Connect JPULSE of LSA08 to pin 4
int cnt=0;
int flag=0;
int inA1=22;
int inA2=23;
int inB1=26;
int inB2=27;
int dirA1=24;
int dirA2=25;
int dirB1=28;
int dirB2=29;
int pwmA=7;
int pwmB=8;
void setup() {
pinMode(serialEn,OUTPUT);
pinMode(jp,INPUT);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
for(int i=22;i<=29;i++) {
pinMode(i,OUTPUT);
}
digitalWrite(dirA1, HIGH);
digitalWrite(dirA2, HIGH);
digitalWrite(dirB1, HIGH);
digitalWrite(dirB2, HIGH);
digitalWrite(serialEn,HIGH);
Serial.begin(9600);
}
void stopp();
void forward(int);
float lastError = 0;
void loop() {
digitalWrite(serialEn,LOW);
while(Serial.available() <= 0);
int positionVal = Serial.read();
digitalWrite(serialEn,HIGH);
const byte jpulse= digitalRead(jp);
if(jpulse == LOW && cnt==0 && flag==0)
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, LOW);
digitalWrite(inB2, HIGH);
forward(positionVal);
Serial.println("First");
// delay(100);
}
else if(jpulse == HIGH && cnt==0 && flag==0)
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, LOW);
digitalWrite(inB2, HIGH);
forward(positionVal);
cnt=1;
Serial.println("Second");
delay(100);
//delay(1000);
}
else if(jpulse == HIGH && cnt ==1 && flag==0)
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, LOW);
digitalWrite(inB2, HIGH);
forward(positionVal);
Serial.println("Third");
// delay(100);
}
else if(jpulse == LOW && cnt ==1)
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, LOW);
digitalWrite(inB2, HIGH);
forward(positionVal);
cnt=2;
Serial.println("Fourth");
// delay(100);
}
else if(jpulse==HIGH && cnt ==2)
{
flag=1;
stopp();
Serial.println("Fifth");
//delay(100);
// Serial.println("stopped888888");
}
//Serial.println(flag);
else if(jpulse==LOW && cnt ==2 && flag==1)
{
stopp();
Serial.println("Sixth");
// delay(100);
// Serial.println("stopped");
}
else if(positionVal == 255) {
stopp();
Serial.println("Seventh");
//delay(100);
}
else {
forward(positionVal);
Serial.println("Eighth");
//delay(100);
}
}
void stopp(){
digitalWrite(inA1, HIGH);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, HIGH);
digitalWrite(inB2, HIGH);
//analogWrite(pwm1, 0);
//analogWrite(pwm2, 0);
}
void forward(int positionVal){
float error = positionVal - setPoint;
// float der = error - lastError;
float integral = integral + error;
float motorSpeed = Kp * error + Ki * integral;
lastError = error;
float pwmA1 = baseSpeed - motorSpeed;
float pwmB1 = baseSpeed +motorSpeed;
if(pwmA1 > maxSpeed)
pwmA1 = maxSpeed;
if(pwmB1 > maxSpeed)
pwmB1 = maxSpeed;
if(pwmA1 < 0)
pwmA1 = 0;
if(pwmB1 < 0)
pwmB1 = 0;
analogWrite(pwmA,pwmA1);
analogWrite(pwmB,pwmB1);
//Serial.println(pwmA1);
delay(10);
}