-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_LWF_2_GIT.ino
288 lines (210 loc) · 5.89 KB
/
PID_LWF_2_GIT.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
/*
****************CONTROL PID SEGUIDOR DE LINEA******************
AUTOR: ARIEL ARTURO RÌOS SIERRA
CORREO: arturi.marking@gmail.com
FECHA: 25 de marzo del 2018
VERSION: 1.2
***************************************************************
*/
#include <QTRSensors.h>
#define AIN_1 7
#define AIN_2 8
#define BIN_1 10
#define BIN_2 13
#define PWMA 6
#define PWMB 11
#define STBY 9
#define ENCO_A_IZQ 2
#define ENCO_B_IZQ 4
#define ENCO_A_DER 3
#define ENCO_B_DER 5
#define PIN_INTERRUPT_0 2
#define PIN_INTERRUPT_1 3
#define POS_OBJECTIVO 3500
#define BUTTON_STATUS 12
#define COLOR_PISTA 0
#define NUM_SENSOR_IR 8
int estadoBoton = HIGH;
int esperaTiempo = 560;
long tiempo = 0;
int previous = LOW;
bool estadoRobot = false;
const int velocidadBase = 255;
const float kP = 7 * 0.512;
const float kI = 0;
const float kD = 0;
volatile unsigned long leftCount = 0;
volatile unsigned long rightCount = 0;
float rpmIzq = 0;
float rpmDer = 0;
float velocidadIzq = 0;
float velocidadDer = 0;
unsigned long tiempoAnterior = 0;
unsigned int pulsosPorRevolucion = 6;
unsigned int diametroRueda = 20;
int relacionRuedas = 10;
unsigned int posicion_actual = 0;
unsigned int position = 0;
unsigned int posicionAnterior = 0;
unsigned int derivativo = 0;
unsigned int integral = 0;
QTRSensorsAnalog qtra((unsigned char[]) {
0, 1, 2, 3, 4, 5, 6, 7
}, NUM_SENSOR_IR);
unsigned int valoresSensorIr[NUM_SENSOR_IR];
void setup() {
Serial.begin(9600);
pinMode(BUTTON_STATUS, INPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN_1, OUTPUT);
pinMode(AIN_2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN_1, OUTPUT);
pinMode(BIN_2, OUTPUT);
pinMode(STBY, OUTPUT);
pinMode(ENCO_A_IZQ, INPUT);
pinMode(ENCO_B_IZQ, INPUT);
pinMode(ENCO_A_DER, INPUT);
pinMode(ENCO_B_DER, INPUT);
pinMode(PIN_INTERRUPT_0, INPUT_PULLUP);
pinMode(PIN_INTERRUPT_1, INPUT_PULLUP);
for (int i = 0; i < 200; i++) {
qtra.calibrate();
}
attachInterrupt(0, leftEncoderEvento, RISING);
attachInterrupt(1, rightEncoderEvento, RISING);
}
void leftEncoderEvento() {
if (digitalRead(ENCO_A_IZQ) == HIGH) {
if (digitalRead(ENCO_B_IZQ) == LOW) {
leftCount++;
}
} else {
if (digitalRead(ENCO_B_IZQ) == HIGH) {
leftCount++;
}
}
}
void rightEncoderEvento() {
if (digitalRead(ENCO_A_DER) == HIGH) {
if (digitalRead(ENCO_B_DER) == LOW) {
rightCount++;
}
} else {
if (digitalRead(ENCO_B_DER) == HIGH) {
rightCount++;
}
}
}
void loop() {
estadoBoton = digitalRead(BUTTON_STATUS);
delay(40);
if (estadoBoton == HIGH && previous == LOW && millis() - tiempo > esperaTiempo) {
if (estadoRobot)
estadoRobot = false;
else
estadoRobot = true;
tiempo = millis();
}
if (!estadoRobot) {
int motoresEncendidos = digitalRead(STBY);
if (motoresEncendidos == 1) {
digitalWrite(STBY, LOW);
}
}
if (estadoRobot) {
inicioRobot();
}
previous = estadoBoton;
}
void inicioRobot() {
digitalWrite(STBY, HIGH);
float errorPID = calculoPID();
leerVelocidades();
int veloLineal = (rpmIzq + rpmDer) / 2;
float velocidadRPMIzq = veloLineal - errorPID; // 1500 - (-233.90) = 1733.9 // antes era -
float velocidadRPMDer = veloLineal + errorPID; // 1500 + (-233.90) = 1266.1 // antes era +
//Serial.println(errorPID);
if (rpmIzq == 0 && rpmDer == 0) {
primeraArrancada();
}
int potenciaPWMIzq = map(velocidadRPMIzq, 0, 3000, 0, 255); // 147.00
int potenciaPWMDer = map(velocidadRPMDer, 0, 3000, 0, 255); // 107.00
if (potenciaPWMIzq > velocidadBase) {
potenciaPWMIzq = velocidadBase;
}
if (potenciaPWMDer > velocidadBase) {
potenciaPWMDer = velocidadBase;
}
if (position == 3500) {
adelante();
analogWrite(PWMA, potenciaPWMDer);
analogWrite(PWMB, potenciaPWMIzq);
} else if (position < 3500 && position >= 0 ) {
girarIzq();
if (potenciaPWMIzq < 0)
potenciaPWMIzq = -potenciaPWMIzq;
analogWrite(PWMA, potenciaPWMDer);
analogWrite(PWMB, potenciaPWMIzq);
} else {
girarDer();
if (potenciaPWMDer < 0)
potenciaPWMDer = -potenciaPWMDer;
analogWrite(PWMA, potenciaPWMDer);
analogWrite(PWMB, potenciaPWMIzq);
}
}
void leerVelocidades() {
long tiempoActual = millis();
if (tiempoActual - tiempoAnterior >= 1000) {
noInterrupts();
rpmIzq = 60 * leftCount / pulsosPorRevolucion * 1000 / (millis() - tiempoAnterior);
rpmDer = 60 * rightCount / pulsosPorRevolucion * 1000 / (millis() - tiempoAnterior);
leftCount = 0;
rightCount = 0;
tiempoAnterior = millis();
interrupts();
}
}
unsigned int calculoPID() {
position = leerPosicionError(); // 3500
integral = position + posicionAnterior; // 14 . //
derivativo = position - posicionAnterior; // 3500 - 3500
posicionAnterior = position;
return (kP * position + kI * integral + kD * derivativo); // kp = 0.02, ki = 0.00003, kd = 0.04 => -1.2 - 0.00036 - 0 = -1.2
}
unsigned int leerPosicionError() {
qtra.read(valoresSensorIr);
posicion_actual = qtra.readLine(valoresSensorIr, QTR_EMITTERS_ON, COLOR_PISTA);
for (unsigned char i = 0; i < NUM_SENSOR_IR; i++)
{
Serial.print(valoresSensorIr[i]);
Serial.print('\t'); // tab to format the raw data into columns in the Serial monitor
}
int pos = POS_OBJECTIVO - posicion_actual;
Serial.println("| " + String(posicion_actual) + " | " + String(millis()) + " Posicion Error: " + String(pos));
return (pos);
}
void primeraArrancada() {
analogWrite(PWMA, velocidadBase);
analogWrite(PWMB, velocidadBase);
adelante();
}
void adelante() {
digitalWrite(AIN_1, HIGH);
digitalWrite(AIN_2, LOW);
digitalWrite(BIN_1, LOW);
digitalWrite(BIN_2, HIGH);
}
void girarDer() {
digitalWrite(AIN_1, LOW);
digitalWrite(AIN_2, HIGH);
digitalWrite(BIN_1, LOW);
digitalWrite(BIN_2, HIGH);
}
void girarIzq() {
digitalWrite(AIN_1, LOW);
digitalWrite(AIN_2, HIGH);
digitalWrite(BIN_1, LOW);
digitalWrite(BIN_2, HIGH);
}