I read in the PDF that a delay was needed between readings, that was why I wasn’t sure if there was a delay required.
I am currently having issues finding a sufficient P value. I set it really low and it sways along the straight line but can’t make the corners, I increase it just a little bit (less than one) and then it jerks back and forth for about a foot and just shoots off the line so I assume the correct P value is somewhere in between but I’ve tried everything in between and it either can’t make the turn or jerks back and forth. Can this be caused by something other than my code? The tires have traction and aren’t slipping so that isn’t it.
My position is -1500 to 1500 so I assumed the P term would be somewhere around 0.17 since 1500*0.17~255 meaning when the farthest sensor sees the edge it will be to the point where the motor gets shut off. That said, I started my P at .01, made it up to .2 and tried everything in between.
I just thought of something, could it help if I set the motor setpoint to say 200, that way the outside motor could go faster than it normally would on the straight portion? Right now my code won’t allow the motor speed to exceed the setpoint so it’s basically only slowing one motor or the other.
#include <PololuQTRSensors.h>
#define NUM_SENSORS 4 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN QTR_NO_EMITTER_PIN
// sensors 0 through 3 are connected to analog inputs 0 through 5, respectively
PololuQTRSensorsAnalog qtra((unsigned char[]) {
0, 1, 2, 3}
,
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
const float Kp = 0.2;
const float Kd = 0;
const int M1 = 255;
const int M2 = 255;
int lastError = 0;
int m1Pin = 9;
int m2Pin = 10;
int triggerPin = 8;
int buzzerPin = 7;
int redCount = 0;
int sensorValue;
void setup()
{
delay(500);
int i;
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // turn on LED to indicate we are in calibration mode
for (i = 0; i < 400; i++) // make the calibration take about 10 seconds
{
qtra.calibrate(); // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
}
digitalWrite(13, LOW); // turn off LED to indicate we are through with calibration
// print the calibration minimum values measured when emitters were on
Serial.begin(9600);
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
// print the calibration maximum values measured when emitters were on
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
// Wait for user to push button
pinMode(triggerPin, INPUT);
pinMode(buzzerPin, OUTPUT);
while(!digitalRead(triggerPin)){
}
}
void loop()
{
// read calibrated sensor values and obtain a measure of the line position
// from 0 to 3000, 1500 is center due to the use of four sensors
unsigned int position = qtra.readLine(sensorValues);
// Calculate error based on center
int error = position - 1500;
// Calculate motor speed to correct the direction
int turn = Kp * error + Kd * (error - lastError);
int m1Speed = M1 + turn;
int m2Speed = M2 - turn;
// Ensure the motors do not go below 0 or above their set max
if (m1Speed < 0)
m1Speed = 0;
if (m2Speed < 0)
m2Speed = 0;
if (m1Speed > M1)
m1Speed = M1;
if (m2Speed > M2)
m2Speed = M2;
// Set Motor Speeds
analogWrite(m1Pin, m1Speed);
analogWrite(m2Pin, m2Speed);
lastError = error;
// CODE FOR CHECKING COLOR SENSOR
sensorValue = analogRead(A4);
if (sensorValue > 60)
redCount++;
else
redCount = 0;
if (redCount > 3)
digitalWrite(buzzerPin, HIGH);
else
digitalWrite(buzzerPin, LOW);
}
Thanks for clearing up that I term description, that made it very easy to understand