Hello ! I recently started on a project related to line follower robot. I have searched various sources available on the internet and found instructable's http://www.instructables.com/id/Arduino-based-line-follower-using-Pololu-QTR-8RC-l/ to be much helpful. I tested it out and the line follower works perfectly. Curious enough i wanted to incorporate an obstacle avoider fuction to the robot as well incase there is some obstacle on the line. For this i bought an ULTRASONIC sensor. I checked the sensor separately and tested it for a specific distance which will turn on an LED. Worked beautifully as well so i merged the obstacle avoider code with the Line follower code.I altered the code so that at a specific distance the robot will stop. NOW HERE COMES MY PROBLEM !!!
When i start the robot although the robot stops at a specific distance however the line following is not that GREAT. It follows the line barely. Now i am no CODE GURU but by my understanding the obstacle avoider fuction is causing a delay in the line follower function.
I have searched the internet for some clues but havent yet rectified the problem. I HOPE THE GOOD PEOPLE OF THIS FORUM WOULD HELP ME SOLVE THIS.
AGAIN i am not a programming expert but have a pretty basic self taught knowledge so if i have made a STUPID mistake it was not intentional. ![]()
Following is my code which produces the delay in line following.
#include <QTRSensors.h>
#include <AFMotor.h>
#include <Ultrasonic.h>#define TRIGGER_PIN 46 // Pin defining at pin 46 and pin 44
#define ECHO_PIN 44AF_DCMotor motor1(1, MOTOR12_8KHZ ); // PIN 11 - create motor #1 pwm
AF_DCMotor motor2(2, MOTOR12_8KHZ ); // PIN 3 - create motor #2 pwm
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);const int dangerThresh = 10; // danger distance 10 cm
// Change the values below to suit your robot's motors, weight, wheel type, etc.
#define KP .2
#define KD 5
#define M1_DEFAULT_SPEED 90
#define M2_DEFAULT_SPEED 90
#define M1_MAX_SPEED 110
#define M2_MAX_SPEED 110
#define MIDDLE_SENSOR 3
#define NUM_SENSORS 5 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2
#define DEBUG 0 // set to 1 if serial debug output neededQTRSensorsRC qtrrc((unsigned char[]) { A8, A9, A10, A11, A12, A13, A14, A15} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{delay(1000);
manual_calibration();
set_motors(0,0);
}int lastError = 0;
int last_proportional = 0;
int integral = 0;void loop()
{
unsigned int sensors[5];
int position = qtrrc.readLine(sensors);
int error = position - 2000;int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);}
void set_motors(int motor1speed, int motor2speed)
{
float cmMsec;// ALOGRATHM FOR conversion of time to distance
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);if (cmMsec>dangerThresh)
{
if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
motor1.setSpeed(motor1speed); // set motor speed
motor2.setSpeed(motor2speed); // set motor speed
motor1.run(FORWARD);
motor2.run(FORWARD);
}
else
{
if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
motor1.setSpeed(motor1speed); // set motor speed
motor2.setSpeed(motor2speed); // set motor speed
motor1.run(RELEASE);
motor2.run(RELEASE);
}}
void manual_calibration() {
int i;
for (i = 0; i < 250; i++) // the calibration will take a few seconds
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}if (DEBUG) { // if true, generate sensor dats via serial output
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn*);*
- Serial.print(' ');*
- }*
- Serial.println();*
- for (int i = 0; i < NUM_SENSORS; i++)*
- {*
_ Serial.print(qtrrc.calibratedMaximumOn*);_
_ Serial.print(' ');_
_ }_
_ Serial.println();_
_ Serial.println();_
_ }_
_}_
_[/quote]*_