-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLineFollower.ino
More file actions
130 lines (107 loc) · 3.45 KB
/
LineFollower.ino
File metadata and controls
130 lines (107 loc) · 3.45 KB
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
/* Assumes you've wired up your line follower according to the available wiring
* diagram. Requires installation of QTRSensor Library as per
* https://www.pololu.com/docs/0J19/2)
*
* Skeleton code for your line follower!
*/
#include <QTRSensors.h> // The sensor library
#define NUM_SENSORS 3 // Reflectance sensor has 3 individual sensors
#define NUM_SAMPLES_PER_SENSOR 4 // Averge 4 analog samples per sensor reading
int leftWheelB = 22;
int leftWheelA = 23;
int rightWheelB = 9;
int rightWheelA = 10;
int onboardLED = 13;
// Object for three QTR-xA sensors on analog inputs 14, 15, and 16.
QTRSensorsAnalog qtra((unsigned char[]) {14, 15, 16},
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR);
unsigned int sensorValues[NUM_SENSORS]; // For storing sensor readings
void setup() {
// Pin configuration
pinMode(leftWheelB, OUTPUT);
pinMode(leftWheelA, OUTPUT);
pinMode(rightWheelB, OUTPUT);
pinMode(rightWheelA, OUTPUT);
pinMode(onboardLED, OUTPUT);
// Show when robot is on
digitalWrite(onboardLED, HIGH);
// Serial debug start
Serial.begin(9600);
// TODO: ADVANCED
// If you want to improve your line follower further, you can use PID control.
// Ask a HURC member about this topic once you get your line follower working!
/* PID variables:
// PID gains
kp = 0;
ki = 0;
kd = 0;
// error accumulators
cumulative_error = 0;
prev_error = 0;
*/
}
void setDriveSpeed(int left, int right) {
if(left > 0) {
analogWrite(leftWheelA, left);
analogWrite(leftWheelB, 0);
}
else if (left < 0) {
analogWrite(leftWheelB, -1*left);
analogWrite(leftWheelA, 0);
}
else {
analogWrite(leftWheelA, 0);
analogWrite(leftWheelB, 0);
}
if(right > 0) {
analogWrite(rightWheelB, right);
analogWrite(rightWheelA, 0);
}
else if (right < 0) {
analogWrite(rightWheelA, -1*right);
analogWrite(rightWheelB, 0);
}
else {
analogWrite(rightWheelA, 0);
analogWrite(rightWheelB, 0);
}
}
void turnRight() {
setDriveSpeed(100,0);
}
void turnLeft() {
setDriveSpeed(0,-100);
}
void loop() {
// Acquire sensor readings
qtra.read(sensorValues);
// Iterate over all sensors and print their reading followed by a tab
for (unsigned char i = 0; i < NUM_SENSORS; i++) {
Serial.print(sensorValues[i]);
Serial.print("\t");
}
Serial.println();
/* TODO: Your code here!
* Right now the robot doesn't take any of the sensor readings into account,
* it just constantly turns right. Your job is to incorporate your sensor
* readings into a decision about how to drive the motors!
*/
turnRight();
delay(10);
// TODO: ADVANCED
// If you want to improve your line follower further, you can use PID control.
// Ask a HURC member about this topic once you get your line follower working!
/* PID control loop
// definition of error changes depending on sensors used
error = (on.light - off.light) - THRESHOLD; // polarity might be change depending on system orientation
// update error integration
cumulative_error += error;
// calculate error derivative
error_derivative = error - prev_error;
control = kp * error + ki * cumulative_error + kd * error_derivative;
drive = control; // need some conversion of control to drive signal (need real values to determine this)
setDriveSpeed(20, drive); // only need to control right wheel's speed if you keep the other constant
// update error buffer
prev_error = error;
*/
}