as it’s fine up the value at the end after more and more laps… I think it’s ok…
now it depends of how much will it help…
I remade the code:
I store the PID values in eeprom just in case so I don’t have to flash again and again…
when I found all of them… I simply flash it up without the Serial communication part.
now according to my excel of calculation…
with P = 0.5
and D = 2
my diff, varies between 100002.5 and -12.5;
with P = 1.6
and D = 0
my diff varies between: -40 and 40 which give my robot a nice follow as in the video here:
this is the code:
#include <QTRSensors.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
float P, I, D;
const int max_speed = 255;
int s_left, s_right;
int mleft = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int mright = 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()
{
pinMode(mleft, OUTPUT);
pinMode(mright, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
P = EEPROM.read(1);
D = EEPROM.read(2);
I = EEPROM.read(3);
P = P/10;
D = D/10;
I = I/10;
Serial.print("Stored values for PID are:");Serial.println();
Serial.print("kP: ");Serial.print(P);Serial.print(" kD: ");Serial.print(D);Serial.print(" kI: ");Serial.print(I);
Serial.println();
pid_setup();
calib_message();
int i;
for (i = 0; i < 80; i++)
{
if(i < 20 || i >= 60)
go(120,-120);
else
go(-120,120);
qtra.calibrate();
go(0,0);
}
Serial.print("Calibration ok!");
Serial.println();
Serial.print("Press button to START");
Serial.println();
Serial.end();
pinMode(2, INPUT);
while(digitalRead(2)==0);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
analogWrite(mleft, max_speed);
analogWrite(mright, max_speed);
}
void loop()
{
int integral, last_proportional, diff, derivative;
int pos = qtra.readLine(sensors);
pos = pos-2500;
pos = pos/10;
derivative = pos - last_proportional;
integral = integral+pos;
last_proportional = pos;
diff =(int) (pos*P + integral*I + derivative*D);
s_left = max_speed - diff;
s_right = max_speed + diff;
go(s_left, s_right);
}
void pid_setup()
{
Serial.print("kP = ");
while(Serial.available()==0);
P = Serial.parseFloat();
Serial.print(P);
P = P*10;
EEPROM.write(1,P);
Serial.println();
Serial.end();
Serial.begin(9600);
Serial.print("kD = ");
while(Serial.available()==0);
D = Serial.parseFloat();
Serial.print(D);
D = D*10;
EEPROM.write(2, D);
Serial.println();
Serial.end();
Serial.begin(9600);
Serial.print("kI = ");
while(Serial.available()==0);
I = Serial.parseFloat();
Serial.print(I);
I = I*10;
EEPROM.write(3, I);
P = P/10;
D = D/10;
I = I/10;
Serial.println();
Serial.print("kP: ");
Serial.print(P);
Serial.print(" kD: ");
Serial.print(D);
Serial.print(" kI: ");
Serial.print(I);
Serial.println();
}
void calib_message()
{
Serial.print("Calibration starts in... 3... ");
delay(1000);
Serial.print("2... ");
delay(1000);
Serial.print("1... ");
Serial.println();
Serial.print("Calibration started");
Serial.println();
delay(200);
}
void go(int speedLeft, int speedRight)
{
if (speedLeft > 0)
{
digitalWrite(12, LOW);
if (speedLeft > 254)
{
analogWrite(mleft, 254);
}
else
{
analogWrite(mleft, speedLeft);
}
}
else
{
digitalWrite(12, HIGH);
if(speedLeft < -254)
{
analogWrite(mleft, 254);
}
else
{
analogWrite(mleft, -speedLeft);
}
}
if (speedRight > 0)
{
digitalWrite(13, HIGH);
if(speedRight > 254)
{
analogWrite(mright, 254);
}
else
{
analogWrite(mright, speedRight);
}
}
else
{
digitalWrite(13, LOW);
if(speedRight < -254)
{
analogWrite(mright, 254);
}
else
{
analogWrite(mright, -speedRight);
}
}
}
the questions:
should i scale lower the final diff?
should I increase P, and leave D and I = 0?
As I observed… with D = 0, and P = 1.6 my robot wobbles a bit like in the video…
if I increase D… my robot just breaks more and more often hardly following the line…
following this reaction Kp is much more higher than Kd… that gives me a nice following of the line…
although Kp should be lower than Kd… I don’t know what to do…
now I’m stuck with a wobby mice looking robot which doesn’t loose the line… but isn’t smooth…
meanwhile I mean another video was made.
when in the video the robot stops… I write on new values for P, I, D;
here are the values used in chronological order:
P = 1.6, D = 0, I = 0;
P =0.1, D = 4, I = 0;
P = 0.2, D = 4, I = 0;
P = 0.5, D = 4, I =0;