I am making a line following robot using PID. I have QTR-8RC sensor. I just order it online and received yesterday. When I attached QTR with my robot, It was not emitting LED, not even power on LED. I want to confirm that is there any other method to turn on these LED’s or there is a hardware damage with them.
Robot is emitting violet light(IR perhaps). I was able to see them when I turned on my mobile camera. I placed my code below. My robot is not working well. it’s working for a while and suddenly starting moving circular. I have a doubt that it is not calibrating sensors well. Can any one help me to solve this problem.
// Code...............
#include<QTRSensors.h>
#define Kp 0.1 // experiment to determine this, start by something small that just makes your bot follow the line at a slow speed
#define Kd 5 // experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd)
#define ki 0 // This can be used if needed.
#define TIMEOUT 1000 // waits for 2500 us for sensor outputs to go low
int num_sensors = 8;
int rightBaseSpeed = 200;
int leftBaseSpeed = 200;
int rightMaxSpeed = 254;
int leftMaxSpeed = 254;
//*****************************************
#include<AFMotor.h>
AF_DCMotor leftMotor(3); // left motor M1a
AF_DCMotor rightMotor(2); // Right motor M2a
int integral = 0;
int derivative = 0;
QTRSensorsRC qtrrc((unsigned char[]) {2,13,14,15,16,17,18,19 }, num_sensors , 2500, QTR_NO_EMITTER_PIN);
void setup()
{
Serial.begin(9600);
for(int i = 0 ; i < 33 ; i++)
{
if(i < 9 || i >= 25)
turnRight();
else
turnLeft();
qtrrc.calibrate();
delay(10); //20
}
wait();
delay(TIMEOUT);
}
int lastError = 0;
void loop()
{
unsigned int sensors[8];
int position = qtrrc.readLine(sensors); // get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position.
int error = position - 3500 ;
//Serial.println(position);
integral = integral + error;
derivative = error - lastError;
int motorSpeed = Kp * error + Kd * derivative + ki * integral ;
lastError = error;
int rightMotorSpeed = rightBaseSpeed + motorSpeed;
int leftMotorSpeed = leftBaseSpeed - motorSpeed;
if (rightMotorSpeed > rightMaxSpeed ) rightMotorSpeed = rightMaxSpeed; // prevent the motor from going beyond max speed
if (leftMotorSpeed > leftMaxSpeed ) leftMotorSpeed = leftMaxSpeed; // prevent the motor from going beyond max speed
if (rightMotorSpeed < 0) rightMotorSpeed = 0; // keep the motor speed positive
if (leftMotorSpeed < 0) leftMotorSpeed = 0; // keep the motor speed positive
{
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
leftMotor.setSpeed(rightMotorSpeed);
rightMotor.setSpeed(leftMotorSpeed);
}
}
void turnLeft(){
leftMotor.run(BACKWARD);
rightMotor.run(FORWARD);
leftMotor.setSpeed(180); //180
rightMotor.setSpeed(180); //180
}
void turnRight(){
leftMotor.run(FORWARD);
rightMotor.run(BACKWARD);
leftMotor.setSpeed(180); //180
rightMotor.setSpeed(180); //180
}
void wait(){
leftMotor.run(RELEASE);
rightMotor.run(RELEASE);
}