2 Linefollowing code by Gabriel Fornaeus
4 More information at http://hax0r.se
7 // Header file for musical lulz
12 // For the Pololu Qik motor controller
13 #include <CompactQik2s9v1.h>
14 #include <NewSoftSerial.h>
15 // Setup pins for the motor controller
20 NewSoftSerial mySerial = NewSoftSerial(rxPin,txPin);
21 CompactQik2s9v1 motor = CompactQik2s9v1(&mySerial,rstPin);
23 Servo sensorServo; // Create object to control the front servo with the sensor
25 #include <PololuQTRSensors.h>
26 #define NUM_SENSORS 3 // Number of sensors used(actually five, but we'll skip the outer two)
27 #define TIMEOUT 2500 // Waits for 2500 us for sensor outputs to go low
29 // Sensors 0 through 5 are connected to digital pins 4 through 6, respectively
30 PololuQTRSensorsRC qtrrc((unsigned char[]) { 4 , 5, 6, },
31 NUM_SENSORS, TIMEOUT);
32 unsigned int sensorValues[NUM_SENSORS];
47 // Don't want the servo to move about, even though we'e not using it
48 sensorServo.attach(2);
55 motor.stopBothMotors();
57 digitalWrite(13, HIGH);
61 for (i = 0; i < 80; i++)
63 if (i < 20 || i >= 60)
65 motor.motor0Forward(75);
66 motor.motor1Reverse(75);
70 motor.motor1Forward(75);
71 motor.motor0Reverse(75);
73 qtrrc.calibrate(); // Reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
75 // Stop when the calibration is done
76 motor.stopBothMotors();
77 // Play start noise( add delay later)
78 digitalWrite(13, LOW);
79 tone(12, NOTE_E3, 500);
81 tone(12, NOTE_E3, 500);
83 tone(12, NOTE_E3, 500);
85 tone(12, NOTE_A3, 1000);
87 motor.stopBothMotors();
93 unsigned int position = qtrrc.readLine(sensorValues);
95 // We're far to the right of the line, turn left
98 //Serial.println("right of line");
99 digitalWrite(13, LOW);
100 motor.motor0Forward(127);
101 motor.motor1Reverse(127);
103 // We're sort of centered, drive forward
106 digitalWrite(13, HIGH);
107 motor.motor0Forward(127);
108 motor.motor1Forward(127);
110 // We're to the left, turn right
113 //Serial.println("left of line");
114 digitalWrite(13, LOW);
115 motor.motor0Reverse(127);
116 motor.motor1Forward(127);
125 int lastProportional = 0;
130 // Normally, we will be following a line. The code below is
131 // similar to the 3pi-linefollower-pid example, but the maximum
132 // speed is turned down to 60 for reliability.
134 // Get the position of the line.
135 unsigned int position = qtrrc.readLine(sensorValues);
137 // The "proportional" term should be 0 when we are on the line.
138 int proportional = ((int)position) - 2000;
140 // Compute the derivative (change) and integral (sum) of the
142 int derivative = proportional - lastProportional;
143 integral += proportional;
145 // Remember the last position.
146 lastProportional = proportional;
148 // Compute the difference between the two motor power settings,
149 // m1 - m2. If this is a positive number the robot will turn
150 // to the left. If it is a negative number, the robot will
151 // turn to the right, and the magnitude of the number determines
152 // the sharpness of the turn.
153 int powerDifference = proportional/20 + integral/10000 + derivative*3/2;
155 // Compute the actual motor settings. We never set either motor
156 // to a negative value.
157 const int maximum = 80; // the maximum speed
158 if (powerDifference > maximum)
160 powerDifference = maximum;
162 if (powerDifference < -maximum)
164 powerDifference = -maximum;
166 if (powerDifference < 0)
168 motor.motor0Forward(maximum + powerDifference);
169 motor.motor1Forward(maximum + powerDifference);
173 motor.motor0Forward(maximum - powerDifference);
174 motor.motor1Forward(maximum - powerDifference);
177 // We use the inner three sensors (1, 2, and 3) for
178 // determining whether there is a line straight ahead, and the
179 // sensors 0 and 4 for detecting lines going to the left and
182 if (sensors[1] < 100 && sensors[2] < 100 && sensors[3] < 100)
184 // There is no line visible ahead, and we didn't see any
185 // intersection. Must be a dead end.
188 else if (sensors[0] > 200 || sensors[4] > 200)
190 // Found an intersection.