-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineFollower.ino
174 lines (144 loc) · 3.84 KB
/
LineFollower.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
#include <QTRSensors.h>
const int m11Pin = 7;
const int m12Pin = 6;
const int m21Pin = 5;
const int m22Pin = 4;
const int m1Enable = 11;
const int m2Enable = 10;
int m1Speed = 0;
int m2Speed = 0;
float kp = 8;
float ki = 0;
float kd = 20;
int p = 1;
int i = 0;
int d = 0;
int error = 0;
int lastError = 0;
const int maxSpeed = 255;
const int minSpeed = -255;
const int baseSpeed = 200;
QTRSensors qtr;
const int sensorCount = 6;
int sensorValues[sensorCount];
int calibrationTime = 4800;
unsigned long lastCalibration = 0;
int oneCalibrationTime = 500;
bool isCalibrating = 1;
int calibrationIndex = 0;
int noOfCalibrations = 6;
int singleCalibrationTime = 1000;
int calibrationSpeed = 150;
unsigned long errorIndex = 0;
void setup() {
// pinMode setup
pinMode(m11Pin, OUTPUT);
pinMode(m12Pin, OUTPUT);
pinMode(m21Pin, OUTPUT);
pinMode(m22Pin, OUTPUT);
pinMode(m1Enable, OUTPUT);
pinMode(m2Enable, OUTPUT);
qtr.setTypeAnalog();
qtr.setSensorPins((const uint8_t[]){ A0, A1, A2, A3, A4, A5 }, sensorCount);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn on Arduino's LED to indicate we are in calibration mode
Serial.begin(9600);
digitalWrite(LED_BUILTIN, LOW);
}
void calibrate() {
if (lastCalibration == 0) {
lastCalibration = millis();
}
if (millis() - lastCalibration >= oneCalibrationTime) {
lastCalibration = millis();
calibrationIndex++;
//when calibration starts and when it ends it does only a half of a rotation, therefore we split in half the time
if (calibrationIndex == noOfCalibrations - 1) {
oneCalibrationTime = singleCalibrationTime / 2;
} else {
oneCalibrationTime = singleCalibrationTime;
}
}
else {
qtr.calibrate();
if (calibrationIndex % 2 == 1) {
//one time it goes forward and the other backward
setMotorSpeed(-calibrationSpeed, 0);
} else {
setMotorSpeed(calibrationSpeed, 0);
}
}
}
void loop() {
if (isCalibrating) {
calibrate();
if (millis() - lastCalibration > calibrationTime) {
isCalibrating = false;
}
}
else {
computePID();
computeMotorSpeed();
}
}
void computePID() {
errorIndex++;
int error = map(qtr.readLineBlack(sensorValues), 0, 5000, -50, 50);
p = error;
i = i + error;
d = error - lastError;
//we reset the last error every 10 interations in order to accumulate a more significant rate of change
if (errorIndex % 10 == 0) {
lastError = error;
}
}
void computeMotorSpeed() {
int motorSpeed = kp * p + ki * i + kd * d;
m1Speed = baseSpeed;
m2Speed = baseSpeed;
if (error < 0) {
m1Speed += motorSpeed;
} else if (error > 0) {
m2Speed -= motorSpeed;
}
m1Speed = constrain(m1Speed, minSpeed, maxSpeed);
m2Speed = constrain(m2Speed, minSpeed, maxSpeed);
setMotorSpeed(m1Speed, m2Speed);
}
// each arguments takes values between -255 and 255. The negative values represent the motor speed in reverse.
void setMotorSpeed(int motor1Speed, int motor2Speed) {
motor2Speed = -motor2Speed;
if (motor1Speed == 0) {
digitalWrite(m11Pin, LOW);
digitalWrite(m12Pin, LOW);
analogWrite(m1Enable, motor1Speed);
} else {
if (motor1Speed > 0) {
digitalWrite(m11Pin, HIGH);
digitalWrite(m12Pin, LOW);
analogWrite(m1Enable, motor1Speed);
}
if (motor1Speed < 0) {
digitalWrite(m11Pin, LOW);
digitalWrite(m12Pin, HIGH);
analogWrite(m1Enable, -motor1Speed);
}
}
if (motor2Speed == 0) {
digitalWrite(m21Pin, LOW);
digitalWrite(m22Pin, LOW);
analogWrite(m2Enable, motor2Speed);
} else {
if (motor2Speed > 0) {
digitalWrite(m21Pin, HIGH);
digitalWrite(m22Pin, LOW);
analogWrite(m2Enable, motor2Speed);
}
if (motor2Speed < 0) {
digitalWrite(m21Pin, LOW);
digitalWrite(m22Pin, HIGH);
analogWrite(m2Enable, -motor2Speed);
}
}
}