Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Help with line follower using QTR8-RC


#1

hey everyone,
i am making a line follower using pololu QTR8RC and 30:1 metal gear motors, as im not a expert in coding i need help! please go through my code and let me know is my code is fine or not?

here is the code:

#include <QTRSensors.h>
#define NUM_SENSORS   8     // number of sensors used
#define TIMEOUT       2500  // waits for 2500 microseconds for sensor outputs to go low
#define EMITTER_PIN   0   // emitter is controlled by digital pin 2

// sensors 0 through 7 are connected to digital pins 3 through 12, respectively
QTRSensorsRC qtrrc((unsigned char[]) {9, 8, 7, 6, 5, 4, 3, 2},
  NUM_SENSORS, TIMEOUT, EMITTER_PIN);
  unsigned int sensorValues[NUM_SENSORS];
 unsigned int position;
int error_value;
int last_proportional;
int proportional;
int integral;
int derivative;
int set_point;
int right_speed;
int left_speed;
int max_speed; 

void setup()
{ 
           
         
          pinMode(11, OUTPUT);
           pinMode(10, OUTPUT); 
             
  for (int i = 0; i < 100; i++) 
  {
           qtrrc.calibrate(); 
  }
     
           
}

void sensors_read(){         
           position = qtrrc.readLine(sensorValues);
                   }

void pid_calc(){ 
          proportional =  position - 3400 ; 
          integral = integral + (proportional);
          derivative = (proportional - last_proportional);
          last_proportional = proportional;
          error_value = int(proportional*1.2+integral*0+derivative*2);

               }

void calc_turn(){ 
if (error_value< -100){
          error_value = -100;
                      }
if (error_value> 100){
          error_value = 100;
                     }
if (error_value< 0){
          right_speed = 100+ error_value;
          left_speed = 100;
                   }

else{
          right_speed = 100;
          left_speed = 100- error_value;
    }
}
void motor_drive(int right_speed, int left_speed)
{ 
            analogWrite(10, right_speed );
            analogWrite(11, left_speed);
            
}
void loop(){
            sensors_read();
            pid_calc();
            calc_turn();
            motor_drive(right_speed, left_speed);
}

PID based Line Following using Pololu QTR-8RC Sensors
#2

Hi.

I moved your post to its own thread, since it did not seem particularly related to the previous one. Have you tried your code? If your robot is not doing what you want, could you post some details about your robot and what it is doing wrong?

-Claire