Go Down

Topic: Line Follower Not Working (Read 156 times) previous topic - next topic

tx3ha

Hey,
I am making a line follower for a competition and i am using following components
micro gear motor(3000rpm)
arduino nano
pololu qtr 18rc
l298

The robot is doing callibration(Motor Working) but its not working after it(motor is not rotating).I have tried almost everything but no luck.I am not able to troubleshoot the code.
Thanks in advance
Code: [Select]

#include <QTRSensors.h>

#define E1 10  // Enable Pin for motor 1
#define E2 11  // Enable Pin for motor 2
 
#define I1 A1  // Control pin 1 for motor 1
#define I2 A2  // Control pin 2 for motor 1
#define I3 A3  // Control pin 1 for motor 2
#define I4 A4  // Control pin 2 for motor 2
#define Kp 55
#define Kd 0
#define rightMaxSpeed 250
#define leftMaxSpeed 250
#define rightBaseSpeed 175
#define leftBaseSpeed 175
#define NUM_SENSORS 8
#define TIMEOUT 2500

#define p 12
#define x 140

QTRSensorsRC qtrrc((unsigned char[]) {2, 3, 4, 5, 6, 7, 8, 9} ,NUM_SENSORS, TIMEOUT);

unsigned int sensorValues[NUM_SENSORS];

void setup()
{

  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
  delay(4000);
  int i;
  for (int i=0;i<p;i++)
  {if (i>=p/4 && i<3*p/4)
    {analogWrite(E1, x); // Run in half speed
    analogWrite(E2, x); // Run in full speed
 
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, LOW);
    digitalWrite(I4, HIGH);}
  else
    {analogWrite(E1, x); // Run in half speed
    analogWrite(E2, x); // Run in full speed
 
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);}
    qtrrc.calibrate();
    delay(60);
    digitalWrite(I1, LOW);
    digitalWrite(I2, LOW);
    digitalWrite(I3, LOW);
    digitalWrite(I4, LOW);
    delay(300);}
    analogWrite(E1, 255); // Run in half speed
    analogWrite(E2, 255); // Run in full speed
 
    digitalWrite(I1, LOW);
    digitalWrite(I2, LOW);
    digitalWrite(I3, LOW);
    digitalWrite(I4, LOW);
  delay(2000);
  Serial.begin(9600);
}

int lastError = 0;

void loop()
{
  unsigned int sensors[8];
  int position = qtrrc.readLine(sensors);
  int error = position - 2500;
  int motorSpeed = Kp*error+Kd*(error-lastError);
  lastError = error;
  int rightMotorSpeed = rightBaseSpeed+motorSpeed;
  int leftMotorSpeed = leftBaseSpeed-motorSpeed;
  

  if(rightMotorSpeed>rightMaxSpeed) rightMotorSpeed = rightMaxSpeed;
  if(leftMotorSpeed>leftMaxSpeed) leftMotorSpeed = leftMaxSpeed;
  if(rightMotorSpeed < 0) rightMotorSpeed = 0;
  if(leftMotorSpeed < 0) leftMotorSpeed = 0;
 
    analogWrite(E2,190 );//rightMotorSpeed
    analogWrite(E1,190 );//leftMotorSpeed
 
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);

}

PaulS

Code: [Select]
  int i;
  for (int i=0;i<p;i++)
  {if (i>=p/4 && i<3*p/4)

When the for loop is running, there are two variables named i in scope. One hides the other. Why? There is no excuse for that.

Code: [Select]
    {analogWrite(E1, x); // Run in half speed
    analogWrite(E2, x); // Run in full speed

x is a piss-poor name for the speed.

Code: [Select]
    {analogWrite(E1, x); // Run in half speed
    analogWrite(E2, x); // Run in full speed
 
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, LOW);
    digitalWrite(I4, HIGH);}

Stand on the throttle, then put the car in gear. I hope you don't drive your real car that way.

Quote
I am not able to troubleshoot the code.
Seriously? You are unable to put Serial.print() statements in the code, to find out values of the variables being used to determine the speed?

Code: [Select]
    analogWrite(E2,190 );//rightMotorSpeed
    analogWrite(E1,190 );//leftMotorSpeed

What was the point in locating the line and calculating data to keep the line in sight, when you then ignore the line and just mash the throttle?

EVERY { goes on a line BY ITSELF.
EVERY } goes on a line BY ITSELF.
Programmers that know what they are doing keep the code properly indented. Those that can't do that, but want to appear as though they know what they are doing use Tools + Auto Format. Those that don't even want to appear to know what they are doing post piss-poorly indented code.

Think about which category you want to be in.
The art of getting good answers lies in asking good questions.

Delta_G

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

ballscrewbob

tx3ha

DO NOT CROSS POST especially 3 times  GRR.

Please read this before you make any more comments.

Further transgressions may lead to your loss of forum privileges.

Bob.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up