PID Based Line Follower with Junction Counter | Arduino Code By Shees Kamal

PID Line Follower Arduino Code By Shees Kamal

Note: Because of less Time i am not able to explain each and everything regarding Code and Hardware. Sorry
Note: Code is given at the end of this article. 
Note: This code is written by Shees Kamal. So you can expect mistakes. 

Features of this Code?

  1. This code includes PD algorithm for line following.  (Integral is not used because of instability and oscillations occurred from set point.)
  2. This is Junction Based Code.
  3. This code can detect L, T, +, |- and other type of Junctions.
  4. This code is for 5 IR Sensors. ( You can add more sensor easily and increase sensor array size according to it)
  5. This code is for following white line on black background. ( You can easily change 0 1 values according to your sensor detection values to make it work for Black Line).

What you need to change in this code to get your Robot working?

  1. A0 is the Right most sensor and A4 is the Left most Sensor.  So set your wires from A0 to A4( From Right to Left)                                                                                                                          
  2. From 1 to 3 Digital pins of arduino are for Left motor which will connect with IN1, IN2 and ENA pins of L298N  and similarly 4 to 6 Digital pins are for Right Motor which will connect with IN3, IN4 and ENB pins of L298N motor driver shield.                                                            
  3. Set base speed of  motor at which you want to run robot at maximum speed to follow straight line. Base Speed Notation is "iniMotorPower" ( Select base speed in such way that it will stop at junction)(Inertia and momentum are the biggest problem)                                                           
  4. To define path you have to give values associated with specific junctions.                                   "1" denotes GO Straight             "2" denotes GO Right          "3" denotes GO Left                      Like in this code Notation of Junction is "Path". "Path="2233233133223";" Which shows    that it will first take right at first junction then again take right after that take left and so on.               
  5. After that change KP and KD values according to your robot motor movement running on straight line. It will help in taking steady turns.                                                                                    
  6. Change (PIDvalue>95) in motorTurn loop if Statement according to your value set in iniMotorPower. Like i did 160 base speed so if i add 160+95 it will give maximum 255 pwm which we can set in analogWrite command. So this IF, Else will just control your maximum pwm value upto allowable value allowed by arduino.                                                                     
  7. At last change errors according to your robot. Take consideration of errors while changing kp and kd values. Like for example if my kp value is 90 and my base speed is 160 and error is 1.5. Then  Kp*error=90*1.5=135. Which will add with base speed 160 this sum up to 295. It means one of motor speed will become 295 but our if else statement will set it to 255 constraint. The other motor will have value base speed-135=160-135=25. This 25 pwm will be the second motor pwm which will definitely stop the second motor because it's very low voltage.                  
  8.  Change turn speed in goRight and goLeft Loops.                                                                             
  9. At last set time delays in goRight and goLeft loops according to your robot.
This is the general information regarding arduino code to make your robot working. I have missed a lot of information regarding hardware and code. So if you have any ambiguity just ask me in comments. 

BYE -Peace.

More Articles at: PCTECHUB

No comments

Powered by Blogger.