Hello guys,
I need to make a line follower for a competition...
I use:
Arduino
Ardumoto shield
30:1 reduction motors
QTRA sensors...
now following 3pi I got this code:
#include <QTRSensors.h>
#include <SoftwareSerial.h>
int s_left, s_right;
int i, pos, max_speed, last_prop;
int integral;
int speed_left = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int speed_right = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
QTRSensorsAnalog qtra((unsigned char[]) {
0, 1, 2, 3, 4, 5}
, 6);
unsigned int sensors[6];
void setup()
{
s_left=0;
s_right=0;
Serial.begin(9600);
pinMode(speed_left, OUTPUT);
pinMode(speed_right, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
analogWrite(speed_left, 0); //PWD DOWN
analogWrite(speed_right, 0); //PWM DOWN
for (i = 0; i < 125; i++) //5 second callibrate
{
qtra.calibrate();
}
Serial.print("PUSH!");
Serial.println();
while(digitalRead(2)==0);
digitalWrite(12, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(13, HIGH);
analogWrite(speed_left, s_left);
analogWrite(speed_right, s_right);
}
void loop()
{
pos = qtra.readLine(sensors);
int proportional = ((int)pos)-2000;
int derivative = proportional - last_prop;
integral += proportional;
last_prop = proportional;
int pid = proportional *(1/4) + integral/10000 + derivative*4;
const int max_speed = 100;
if (pid>max_speed)
pid = max_speed;
if (pid<-max_speed)
pid= -max_speed;
if(pid<0)
{
s_right = max_speed;
s_left = (max_speed+pid);
}
else
{
s_right=((pid - max_speed)*-1); //multiply by -1, not giving negative values to motors
s_left=max_speed;
}
analogWrite(speed_left, s_left);
analogWrite(speed_right, s_right);
}
void break_robot()
{
analogWrite(speed_left, 0);
analogWrite(speed_right, 0);
}
now the problem is that no matter what, my program won't run as I want... no matter what values I give, the returned PID will be always over 125... I mean values like 4574 or 354 or things like this...basically... it just makes the correction with static values
0 and 100
100 and 0
0 and 0
or 100 and 100
can you please help me out with optimizing the code?