Sunday 15 March 2020

Virtual PID Line Follower

When you are making a PID line follower there are a lot of problems. How many sensors to use? What should be coefficients? What should be the distance between sensors for your problem? One cannot afford to buy so many sensors and changing distance between sensors is a very time-consuming process.
For best performance, you have to tune Kp, Ki, and Kd. Tuning these parameters of a PID line follower is a very hard task. You have to keep changing value and burn the code again and again to the microcontroller. With the help of this app, you can learn easily how to tune these values.


PID Algorithm:

Initialize I=0,error=0,last_error=0
1. Read all sensors output s0,s1,s3...,sn which can be either 0 or 1. 


2. Calculate current Position using.
 if (s0+s1+s2+...+sn>0) then current_position=(s0*b0+s1*b1+s2*b2+..+sn*bn)/(s0+s1+s2+...+sn)

else current_position=s0*b0+ s1*b1+ s2*b2+...+ sn*bn
Where b0, b1, b2,..., bn are sensor coefficients. These sensor cofficients are decided by us. For example, for 5 sensors cofficients can be 
b0=10, b1=20,b2=30,b3=40,b4=50 ,For these cofficients our set point shoud be 30 which is the middle sensor cofficient value.

3. Find error using,  
error = set_position- current_position
set_position contains the value where the error should be zero.

4. Set I=I+error

5. pid_error = kp*error + ki*I + kd*(error-last_error)

6. Remember current error in last_error variable. last_error = error
7. Set motor speeds:  
if (pid_error<0) then left_motor=max_speed, right_motor=max_speed+pid_error

else left_motor=max_speed-pid_error, right_motor=max_speed

8. Limit output speed. if(left_motor>max_speed) then left_motor= max_speed
if(left_motor< -max_speed) then left_motor= -max_speed 
if(right_motor> max_speed) then right_motor= max_speed 
if(right_motor< -max_speed) then right_motor= -max_speed

No comments:

Post a Comment