Go Down

Topic: Need help for PID controlled line follower. (Read 2836 times) previous topic - next topic


Hello Everyone,
I have a  simple 150rpm line follower. But now I need a PID controlled fast line follower for a local line following competition. I have tried to make the algorithm to use with arduino but i am not getting how to get the current position and how to drive the motor to the accurate position. Please help me  :( my competition gonna start 5th March.


Code: [Select]
#include <PololuQTRSensors.h>

#define NUM_SENSORS             6  // number of sensors used
#define NUM_SAMPLES_PER_SENSOR  4  // average 4 analog samples per sensor reading
#define EMITTER_PIN             12  // emitter is controlled by digital pin 2

// sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5},
unsigned int sensorValues[NUM_SENSORS];

int MOTOR1_PIN1 = 5;

int MOTOR1_PIN2 = 6;

int MOTOR2_PIN1 = 11;

int MOTOR2_PIN2 = 10;

int buton = 2;

unsigned int last_proportional = 0;
long integral = 0;

int ok=1;

int center;

void setup() {

  pinMode(MOTOR1_PIN1, OUTPUT);

  pinMode(MOTOR1_PIN2, OUTPUT);

  pinMode(MOTOR2_PIN1, OUTPUT);

  pinMode(MOTOR2_PIN2, OUTPUT);
  pinMode(buton, INPUT);

  int i;
  pinMode(13, OUTPUT);


{digitalWrite(13, HIGH);


digitalWrite(13, HIGH);    // turn on LED to indicate we are in calibration mode
  for (i = 0; i < 400; i++)  // make the calibration take about 10 seconds
    qtra.calibrate();       // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
  digitalWrite(13, LOW);     // turn off LED to indicate we are through with calibration

center = qtra.readLine(sensorValues);


center = qtra.readLine(sensorValues);


void loop() {

unsigned int position = qtra.readLine(sensorValues);

// The "proportional" term should be 0 when we are on the line.
int proportional = ((int)position) - center;

// Compute the derivative (change) and integral (sum) of the
// position.
int derivative = proportional - last_proportional;
integral += proportional;

// Remember the last position.
last_proportional = proportional;

int power_difference = proportional*3.3 + integral/10000 + derivative*7;

// Compute the actual motor settings.  We never set either motor
// to a negative value.
const int max = 180;
if(power_difference > max)
    power_difference = max;
if(power_difference < -max)
    power_difference = -max;

if(power_difference < 0)
  go(max+power_difference, max);
    go(max, max-power_difference);}


void go(int speedLeft, int speedRight) {

  if (speedLeft > 0) {

    analogWrite(MOTOR1_PIN1, speedLeft);

    analogWrite(MOTOR1_PIN2, 0);


  else {

    analogWrite(MOTOR1_PIN1, 0);

    analogWrite(MOTOR1_PIN2, -speedLeft);


  if (speedRight > 0) {

    analogWrite(MOTOR2_PIN1, speedRight);

    analogWrite(MOTOR2_PIN2, 0);


  else {

    analogWrite(MOTOR2_PIN1, 0);

    analogWrite(MOTOR2_PIN2, -speedRight);



Hi i have a problem with my line follower robot. It can t follow the line properly so if you can help me improving pid i will be very grateful. Here is the video which cand explai better the robot's behaviour http://www.youtube.com/watch?v=04i1NqcbhpA&feature=youtu.be
Thank you anticipated!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131