Notifications
Clear all

Line Follower Over Reacting  

  RSS

Ozcraig
(@ozcraig)
Active Member
Joined: 5 months ago
Posts: 7
2020-06-12 12:51 am  

Hi everyone

First timer here, and I must say I enjoy learning from the many great ideas on this site. I am working on a line follower vehicle, similar to the Elegoo sketch from this site. I have altered the code slightly as my motors have never operated at the same speed, so I have coded a speed difference to try to establish driving in a straight line to start with. That seems to work reasonably well. I have also decided to decelerate one motor to achieve turns, instead of increasing speed, so that we don't get the speed wobbles. That seems to work reasonably well too. Hardware is Arduino Uno and the common yellow Chinese motor/gearbox, and two Keyes IR sensor/receiver modules.

An issue that I am still having is that it seems to over react to some turns and runs off track. I have changed the delay time , which is the time during which one motor decelerates to make a slight turn, but can't find a happy medium with a short or long time. Of course, being a motor and gearbox, there is a time lag in responding to speed changes. I'm wondering if anyone has dealt with this issue and come up with a good fix. I'm also wondering if the use of "delay" to make the turns is the best method, but am unsure what may be better. My code is attached. I look forward to any advice or comments! Cheers!

 

/*Let's try reducing one motor speed for the turns
   instead of increasing. That should reduce accelerations
   and reduce speed wobbles.
*/

int LvSpeed = 90;        /* For some reason this L motor 
speed is always faster than R motor so we slow L motor 
to achieve straight travel*/
int RvSpeed = 120; //normal run speed
int Rmotorturn_speed = 90;    // turn speed of R motor
int turn_delay = 7;   //7ms speed adjustment to make slight turn
int Lmotorturn_speed = 65;/*reduced L motor turn speed
  to straighten up drive*/

//L298N Connection
const int motorA1      = 4;  //IN1
const int motorA2      = 2; //IN2
const int motorAspeed  = 3; //ENA PWM SPEED CONTROL
const int motorB1      = 7;  //IN3
const int motorB2      = 6;  //IN4
const int motorBspeed  = 5;  //ENB PWM SPEED CONTROL

//Sensor Connection
const int left_sensor_pin = A0;
const int right_sensor_pin = A1;

int left_sensor_state;
int right_sensor_state;

void setup() {
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);

  Serial.begin(9600);

  delay(3000);

}

void loop() {


  left_sensor_state = analogRead(left_sensor_pin);
  right_sensor_state = analogRead(right_sensor_pin);

  if (right_sensor_state > 500 && left_sensor_state < 500)
  {
    Serial.println("turning right");
    /*slow the right motor B because the R sensor is reading
      HIGH so it is sensing the line and needs to turn right
      away from the line*/
    digitalWrite (motorA1, HIGH);
    digitalWrite(motorA2, LOW);
    digitalWrite (motorB1, HIGH);
    digitalWrite(motorB2, LOW);

    analogWrite (motorBspeed, Rmotorturn_speed);
    analogWrite (motorAspeed, LvSpeed);
    delay(turn_delay);
    Serial.print (right_sensor_state);


  }
  if (right_sensor_state < 500 && left_sensor_state > 500)


  {
    Serial.println("turning left");
    /*slow the left motor A*/
    digitalWrite (motorA1, HIGH);
    digitalWrite(motorA2, LOW);
    digitalWrite (motorB1, HIGH);
    digitalWrite(motorB2, LOW);

    analogWrite (motorBspeed, RvSpeed);
    analogWrite (motorAspeed, Lmotorturn_speed);

    delay(turn_delay);
    Serial.print (left_sensor_state);
  }

  if (right_sensor_state < 500 && left_sensor_state < 500)
  {
    Serial.println("going forward");

    digitalWrite (motorA2, LOW);
    digitalWrite(motorA1, HIGH);
    digitalWrite (motorB2, LOW);
    digitalWrite(motorB1, HIGH);

    analogWrite (motorAspeed, LvSpeed);
    analogWrite (motorBspeed, RvSpeed);

    //delay(turn_delay);  //delay not needed here

  }

  if (right_sensor_state > 500 && left_sensor_state > 500)
  {
    Serial.println("stop");

    analogWrite (motorAspeed, 0);
    analogWrite (motorBspeed, 0);

  }


}












Quote
Topic Tags
Ruplicator
(@ruplicator)
Estimable Member
Joined: 7 months ago
Posts: 110
2020-06-12 2:02 am  

First let me say I have not done the line following experiment. However, since you are using the photo sensors as feed back I believe you can just rely on them to keep your car on track rather than trying to compensate for motor differences. 

So why don't you remove the motor speed compensation and start out with lowering the speed of both motors until you get the system working. Additionally, remove the delay() commands in the turn code as that with high speed may be the cause of your overshoot. Once you get the system to reliably track the line then you can tweak it to perform better. I hope that helps 😊 


ReplyQuote
Ozcraig
(@ozcraig)
Active Member
Joined: 5 months ago
Posts: 7
2020-06-12 7:23 am  

Thanks for your comments Ruplicator. I have spent a few hours following your recommendations. I have found that my two cheap Chinese motors just do not want to run at the same speed and I am continually going in circles unless I code in a speed differential, even without trying to follow lines. I have used this same pair on a different project and did same on that one.I have some new motors ordered so it will be interesting to see if they are any better.

However, I took your advice and removed the speed compensation, to no avail. I lowered the speed of both motors as low as I can without the slower one stalling and refusing to move, and removed the delays, to no avail. It is my understanding that the delays are there to run the motors at the speeds relevant to the left or right turn as required, so a longer delay should result in holding the turning speeds for longer, and a short or no delay should result in very little or no turning action. Is that how you read the sketch?

After faffing about for a few hours I have found that a delay of 50ms seems to work with my setup, with both motors as slow as I can make them. I also found that fine tuning the IR sensors to close to the edge of the black track reduces oscillations. With this combination I am able to follow a straight line. Since the car is currently parked over my other more fancy black track line I'll give that one a shot over the weekend.

Once again, thanks for your advice. 😀 


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 7 months ago
Posts: 110
2020-06-12 8:27 pm  
Posted by: @ozcraig

It is my understanding that the delays are there to run the motors at the speeds relevant to the left or right turn as required, so a longer delay should result in holding the turning speeds for longer, and a short or no delay should result in very little or no turning action. Is that how you read the sketch?

Yes and no. First of all remember that once you set both motors on and set a specific speed for each motor that same speed configuration will continue until it is changed delay or not. Any time the sensor (left or right) is indicating that it is not seeing the line you are slowing the motor on the opposite side. Without the delays the loop will continue to test the sensor as quickly as the MCU can process it. If MCU finds the sensor not seeing the line it will slow the motor it's opposite. If on the next loop the sensor still doesn't see the sensor then MCU will continue to slow the appropriate motor. Therefore , as soon as the sensor can see the line the MCU will then speed up the motor that was slowed and continue straight. 

With the delays installed, there will be obviously a delay before the MCU can make any corrective action.  The delay is tuning the circuit and you will most likely be able to tune it for a specific track but if you move the car to a different track with different degrees of turns, it most likely won't work again.

Make sure the correct motor is slowed when a given sensor looses the track i.e., if the left sensor can't see the track the right motor should be running slower.

If you car leaves the track in a turn you may not be slowing the inside motor enough. If the turn in your track is sharper than the difference in motor speed then the car will leave the track.

If your car is leaving the track in the a strait section then the sensors may not be reading the track correctly.  Once neither sensor sees the track then the MCU doesn't know what to do.

If the car wobbles from side to side in bigger swings before it leave the track, that says there is something wrong with the freed back loop. In other words the MCU can't control the motors fast enough before the car looses sight of the track.

I hope that helps.

This post was modified 4 months ago by Ruplicator

ReplyQuote