I am making a line follower robot for high school, i am using:
-qtr-8rc sensors
-Aduino Uno R3
-LN298N Motor Drive
My electric system is right but the code is failing me, i think i didn't call the functions in the right way in the void loop. Here is the code:
#include <QTRSensors.h>
QTRSensorsRC qtr;
const uint8_t SensorCount = 6;
uint16_t sensorValues[SensorCount];
float Kp = 0.028; //set up the constants value
float Ki = 0;
float Kd = 0;
int P;
int I;
int D;
int lastError = 0;
//Increasing the maxspeed can damage the motors - at a value of 255 the 6V motors will receive 7,4 V
const uint8_t maxspeeda = 200;
const uint8_t maxspeedb = 200;
const uint8_t basespeeda = 180;
const uint8_t basespeedb = 180;
//Set up the drive motor carrier pins
int pin_in1 = 7;
int pin_in2 = 8;
int pin_ena = 6; // PWM speed control
int pin_in3 = 9;
int pin_in4 = 10;
int pin_enb = 5; // PWM speed control
int FULL = 255;
void setup() {
Serial.begin(9600);
//Set up the sensor array pins
QTRSensorsRC((const uint8_t[]){A0, A1, A2, A3, A4, A5}, SensorCount);
pinMode(pin_in1, OUTPUT);
pinMode(pin_in2, OUTPUT);
pinMode(pin_ena, OUTPUT);
pinMode(pin_in3, OUTPUT);
pinMode(pin_in4, OUTPUT);
pinMode(pin_enb, OUTPUT);
}
void loop() {
manual_calibration();
PID_control();
}
void PID_control() {
uint16_t position = qtr.readLine(sensorValues);
int error = 3500 - position;
P = error;
I = I + error;
D = error - lastError;
lastError = error;
int motorspeed = PKp + IKi + D*Kd;
int motorspeeda = basespeeda + motorspeed;
int motorspeedb = basespeedb - motorspeed;
if (motorspeeda > maxspeeda) {
motorspeeda = maxspeeda;
}
if (motorspeedb > maxspeedb) {
motorspeedb = maxspeedb;
}
if (motorspeeda < 0) {
motorspeeda = 0;
}
if (motorspeedb < 0) {
motorspeedb = 0;
}
//Serial.print(motorspeeda);Serial.print(" ");Serial.println(motorspeedb);
}
void manual_calibration(){
int i;
for (i = 0; i < 250; i++)
{
qtr.calibrate(QTR_EMITTERS_ON);
delay(200);
}
}