-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLineFollower_BLE.ino
309 lines (261 loc) · 5.83 KB
/
LineFollower_BLE.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
#include "LineFollower.h"
#include "BLE_Control.h"
#include <QTRSensors.h>
#include <Preferences.h>
#include <AsyncTCP.h>
#include <Button.h>
#define IN1 20
#define IN2 19
#define IN3 17
#define IN4 18
#define RED 14
#define GREEN 15
#define BLUE 16
const int TARGET_VALUE = 7500;
float baseSpeed = 100;
int maxSpeed = 254;
bool running = false;
float kp = 0.02;
float ki = 0;
float kd = 0.45;
float p;
float i;
float d;
int correction;
float lastError;
bool readBlack = true;
bool flipMotors = false;
Button button(12);
const int PWM_FREQ = 500; // Recall that Arduino Uno is ~490 Hz. Official ESP32 example uses 5,000Hz
const int PWM_RESOLUTION = 8;
Preferences prefs;
QTRSensors qtr;
const uint8_t sensorCount = 16;
uint16_t sensorValues[sensorCount];
bool calibrating = false;
void setup() {
button.begin();
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
initLedc();
// stop all motors
drive(0, 0);
Serial.begin(9600);
//while(!Serial);
Serial.println("Serial initialized");
readCoefficients();
setupBLE();
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){22, 21, 24, 23, 10, 11, 8, 9, 6, 7, 1, 5, 0, 2, 3, 4}, sensorCount);
if (button.pressed()) {
calibrating = true;
while (!button.released());
} else {
light(RED);
}
if (calibrating) {
while (!button.pressed());
calibrateSensors();
} else {
readCalibration();
// go to loop (wait for start from button or bluetooth)
}
/*
while (!button.pressed());
if (calibrating) {
calibrateSensors();
} else {
readCalibration();
start();
}*/
}
void loop() {
correction = PIDController(qtr.readLineBlack(sensorValues));
if (running) {
drive(constrain(baseSpeed + correction, -254, 254), constrain(baseSpeed - correction, -254, 254));
} else {
drive(0, 0);
}
if (button.pressed()) {
if (running) {
stop();
} else {
start();
}
}
}
void start() {
if (calibrating) {
return;
}
running = true;
light(GREEN);
}
void stop() {
if (calibrating) {
return;
}
running = false;
light(RED);
}
void calibrateSensors() {
light(BLUE);
for (uint16_t i = 0; i < 400; i++){
qtr.calibrate();
}
// save to prefs
if (qtr.calibrationOn.initialized) {
prefs.begin("prefs", false);
for (int i = 0; i < sensorCount; i++) {
String maxKey = "max";
String minKey = "min";
maxKey += i;
minKey += i;
prefs.putUShort(maxKey.c_str(), qtr.calibrationOn.maximum[i]);
prefs.putUShort(minKey.c_str(), qtr.calibrationOn.minimum[i]);
}
prefs.end();
}
light(RED);
calibrating = false;
}
int PIDController(int value) {
p = TARGET_VALUE - value;
i += p;
d = p - lastError;
lastError = p;
return kp * p + ki * i + kd * d;
}
void initLedc() {
ledcSetup(1, PWM_FREQ, PWM_RESOLUTION);
ledcSetup(2, PWM_FREQ, PWM_RESOLUTION);
ledcSetup(3, PWM_FREQ, PWM_RESOLUTION);
ledcSetup(4, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(IN1,1);
ledcAttachPin(IN2,2);
ledcAttachPin(IN3,3);
ledcAttachPin(IN4,4);
}
void readCoefficients() {
prefs.begin("prefs", true); // true = read only
if (prefs.isKey("bs")) {
baseSpeed = prefs.getInt("bs");
}
if (prefs.isKey("ms")) {
maxSpeed = prefs.getInt("ms");
}
if (prefs.isKey("kp")) {
kp = prefs.getFloat("kp");
}
if (prefs.isKey("ki")) {
ki = prefs.getFloat("ki");
}
if (prefs.isKey("kd")) {
kd = prefs.getFloat("kd");
}
if (prefs.isKey("rb")) {
readBlack = prefs.getBool("rb");
}
if (prefs.isKey("f")) {
flipMotors = prefs.getBool("f");
}
prefs.end();
}
void readCalibration() {
// dummy calibrate to create arrays
qtr.calibrate();
if (!qtr.calibrationOn.initialized) {
return;
}
prefs.begin("prefs", true);
for (int i = 0; i < sensorCount; i++) {
String maxKey = "max";
String minKey = "min";
maxKey += i;
minKey += i;
if (prefs.isKey(maxKey.c_str())) {
qtr.calibrationOn.maximum[i] = prefs.getUShort(maxKey.c_str());
}
if (prefs.isKey(minKey.c_str())) {
qtr.calibrationOn.minimum[i] = prefs.getUShort(minKey.c_str());
}
}
prefs.end();
}
void writeCoefficients() {
prefs.begin("prefs", false); // false = read/write
prefs.putInt("bs", baseSpeed);
prefs.putInt("ms", maxSpeed);
prefs.putFloat("kp", kp);
prefs.putFloat("ki", ki);
prefs.putFloat("kd", kd);
prefs.putBool("rb", readBlack);
prefs.putBool("f", flipMotors);
prefs.end();
}
void drive(int leftMotor, int rightMotor) {
if (flipMotors) {
//driveInverted(leftMotor, rightMotor);
//return;
}
if (leftMotor < 0) {
// backward
ledcWrite(1, abs(leftMotor));
ledcWrite(2, 0);
} else {
// forward
ledcWrite(1, 0);
ledcWrite(2, leftMotor);
}
if (rightMotor < 0) {
// backward
ledcWrite(3, abs(rightMotor));
ledcWrite(4, 0);
} else {
// forward
ledcWrite(3, 0);
ledcWrite(4, rightMotor);
}
}
void driveInverted(int leftMotor, int rightMotor) {
if (leftMotor < 0) {
// backward
ledcWrite(2, abs(leftMotor));
ledcWrite(1, 0);
} else {
// forward
ledcWrite(2, 0);
ledcWrite(1, leftMotor);
}
if (rightMotor < 0) {
// backward
ledcWrite(4, abs(rightMotor));
ledcWrite(3, 0);
} else {
// forward
ledcWrite(4, 0);
ledcWrite(3, rightMotor);
}
}
void light(int pin) {
// RED, GREEN, BLUE: LOW = ON! HIGH = OFF!
if (pin == RED) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
} else if (pin == GREEN) {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
} else if (pin == BLUE) {
digitalWrite(BLUE, LOW);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
} else if (pin == 0) {
// all off
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}
}