-
Notifications
You must be signed in to change notification settings - Fork 1
/
LineFollower.ino
155 lines (123 loc) · 3.65 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
#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;
const int minSpeed = -255;
const int maxSpeed = 255;
const int baseSpeed = 255;
const int calibrationSpeed = 150;
const int sensorCount = 6;
const int blackLineDetectionThresholdValue = 600;
const int otherThanBlackLineDetectionValue = 500;
const int minSensorValue = 0;
const int maxSensorValue = 5000;
const int minErrorValue = -45;
const int maxErrorValue = 45;
const int calibrationSteps = 250;
int sensorValues[sensorCount];
int sensors[sensorCount] = {0, 0, 0, 0, 0, 0};
int m1Speed = 0;
int m2Speed = 0;
int motorSpeed;
int p = 0;
int i = 0;
int d = 0;
int error = 0;
int lastError = 0;
float kp = 30;
float ki = 0.08;
float kd = 150;
QTRSensors qtr;
void 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);
setMotorSpeed(calibrationSpeed, -calibrationSpeed);
for (uint16_t i = 0; i < calibrationSteps; ++i) {
qtr.calibrate();
qtr.read(sensorValues);
// The robot sensor has reached the rightmost position, (only the first sensor is detecting the black line) so the robot has to turn to the left
if (sensorValues[0] > blackLineDetectionThresholdValue && sensorValues[1] < otherThanBlackLineDetectionValue) {
setMotorSpeed(-calibrationSpeed, calibrationSpeed);
}
// The robot sensor has reached the leftmost position, (only the last sensor is detecting the black line) so the robot has to turn to the right
if (sensorValues[sensorCount - 1] > blackLineDetectionThresholdValue && sensorValues[sensorCount - 2] < otherThanBlackLineDetectionValue) {
setMotorSpeed(calibrationSpeed, -calibrationSpeed);
}
}
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
pidControl(kp, ki, kd);
motorPidControl();
setMotorSpeed(m1Speed, m2Speed);
}
void pidControl(float kp, float ki, float kd) {
error = map(qtr.readLineBlack(sensorValues), minSensorValue, maxSensorValue, minErrorValue, maxErrorValue);
p = error;
i = i + error;
d = error - lastError;
lastError = error;
motorSpeed = kp * p + ki * i + kd * d;
}
void motorPidControl() {
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);
}
void setMotorSpeed(int motor1Speed, int motor2Speed) {
motor1Speed = -motor1Speed;
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);
}
}
}