Line follower 90º

I’m making a line follower with n20 motors (733 rpm), qtr rc sensor, l298n driver motor, arduino uno R3…
It takes the curves turns and circular turns very nicely bt cannot able to take the 90 degree turn please help me below is my code…

#include <QTRSensors.h>

QTRSensors qtr;

const uint8_t SensorCount = 6;
uint16_t sensorValues[SensorCount];

#define Kp 0.1
#define Kd 1.9
#define MaxSpeed 100
#define BaseSpeed 70
#define speedturn 50

//Motor A
int PWMA = 9;
int IN1 = 8;
int IN2 = 7;
//Motor B
int PWMB = 3;
int IN4 = 4;
int IN3 = 5;

int lastError = 0;

void setup()
{
  qtr.setTypeRC();
  qtr.setSensorPins((const uint8_t[])
  { 19, 18, 17, 16, 15, 14 }, SensorCount);
  for (uint8_t i = 0; i < 250; i++)
  {
    qtr.calibrate();
    delay(20);
    pinMode(PWMA, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(PWMB, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);

  }
}

void loop()
{
  uint16_t position = qtr.readLineBlack(sensorValues);
  if (position > 4700)
  {
    move(1, speedturn, 1);
    move(0, speedturn, 0);
    return;
  }

  if (position < 300)
  {
    move(1, speedturn, 0);
    move(0, speedturn, 1);
    return;
  }

  int error = position - 2500;
  int motorSpeed = Kp *error + Kd *(error - lastError);
  lastError = error;

  int rightMotorSpeed = BaseSpeed + motorSpeed;
  int leftMotorSpeed = BaseSpeed - motorSpeed;

  if (rightMotorSpeed > MaxSpeed) rightMotorSpeed = MaxSpeed;
  if (leftMotorSpeed > MaxSpeed) leftMotorSpeed = MaxSpeed;
  if (rightMotorSpeed < 0) rightMotorSpeed = 0;
  if (leftMotorSpeed < 0) leftMotorSpeed = 0;

  move(1, rightMotorSpeed, 1);
  move(0, leftMotorSpeed, 1);
}

void move(int motor, int speed, int direction)
{
  boolean inPin1 = HIGH, inPin2 = LOW;
  if (direction == 1)
  {
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if (direction == 0)
  {
    inPin1 = LOW;
    inPin2 = HIGH;
  }

  if (motor == 0)
  {
    digitalWrite(IN1, inPin1);
    digitalWrite(IN2, inPin2);
    analogWrite(PWMA, speed);
  }

  if (motor == 1)
  {
    digitalWrite(IN4, inPin1);
    digitalWrite(IN3, inPin2);
    analogWrite(PWMB, speed);
  }
}

This tutorial shows 90 degree turn of the line follower. The robot uses QTR sensor.
https://www.instructables.com/Arduino-Line-Follower/

I am using this code and it is not working well idk why

Hello.

Could you post more details about what happens when you try your program? A video showing the robot’s behavior when it reaches a turn would probably be most helpful. The forum does not allow users to post very large videos, but it does work well with videos linked from hosting sites (like YouTube or Vimeo).

Brandon

What do u think about my video?

Thank you for the video. Your robot seems to be handling the 90° turns fairly well at first, but the straight segments between the turns are very short in proportion to the robot. Is that some design requirement of the line following course, or can you try spacing them out so the turns are farther away from each other (e.g. make it a large square that goes around the whole surface)?

Brandon

I can’t change the path it must be something like that. Do u have other solution? Should I change something on the code?

I still generally recommend testing your robot on a larger course that isolates the turns from each other more so you can get a better sense for what it is doing.

Right now, your robot is pivoting around the inside wheel as soon as it detects a turn, which leaves it askew with the line once the sensors see the line again and the turn is considered complete. With the configuration of your robot, I suspect you will have more reliable results if you have it move forward for a short amount of time before turning (to align it with the turn), then turn in place by driving the inner wheel in reverse at the same speed.

Brandon

how can I move forward for a short time before turning (to line it up with the curve)? Using delay?

Hello.

It would probably be fine to use a simple delay. So, your turning code might look something like this:

  1. Set motor speed forward.
  2. Delay for some small amount of time to line up with the turn (it might take some trial and error to find the right delay and speed combination).
  3. Set motor speeds to 0, and delay for a small amount of time (maybe ~10ms). This is just to avoid instantly switching a motor from driving forward to driving in reverse.
  4. Set motor speed forward for outside motor and set motor speed in reverse for inside motor (both at the same speed).
  5. Delay a small amount of time to make sure your line sensors don’t accidentally see a different part of the course.
  6. Enter a while loop and continually reading the sensors until the position is within your 300-4700 threshold.
  7. Exit the while loop and return to line following.

Brandon