Introduction: PID Based Line Following Robot With POLOLU QTR 8RC-sensor Array
this is my first write up on instructables ,and today im gonna take you down the road ,and explain about how to bulid a PID based line following robot using QTR-8RC sensor array.
Before going on to the building of the robot ,we need to understand what is called as PID,
Step 1: Working Principle
What is PID??
The term PID stands for proportional,integral,derivative.so simply ,what we are doing with involving PID with line following is,we are giving a command to the robot to follow the line and to detect the turns by calculating the error by considering how far it has moved off the track.
key terms as mentioned in polalu documents
- The proportional value is approximately proportional to your robot’s position with respect to the line. That is, if your robot is precisely centered on the line, we expect a proportional value of exactly 0.
- The integral value records the history of your robot’s motion: it is a sum of all of the values of the proportional term that were recorded since the robot started running.
- The derivative is the rate of change of the proportional value.
In this tutorial, we will talk only about Kp and the Kd terms, however, results can be accomplished using the Ki term as well.the readings that we are getting from the sensor are not only analog readings but also the positional readings of the robot.so basically The sensor provides values from 0 to 2500 ranging from maximum reflectance to minimum reflectance, but, at the same time, also provides information on how far the robot has stranded from the line. )
Now we need to consider the error term , This is the difference of the two values setpoint value and the current value.( The setpoint value is the reading that corresponds to the "perfect" placement of sensors on top of the lines. and The current value is the instantaneous readings of the sensor. For eg : If you are using this array sensor and are making use of 8 sensors, you will receive a positional reading of 3500 if you are spot on, around 0 if you are far too left from the line and around 7000 if you are far too right.). Our goal is to make the error zero. Then only can the robot smoothly follow the line.
Then comes the calculation part,.
1) calculate the error.
Error = Setpoint Value - Current Value = 3500 - position
As im using 8 sensors . the sensor gives a positional reading of 3500 when the robot is perfectly placed. Now that we have calculated our error, the margin by which our robot drifts across the track, it is time for us to scrutinize the error and adjust the motor speeds accordingly
2) determine the adjusted speeds of the motors.
MotorSpeed = Kp * Error + Kd * ( Error - LastError );
LastError = Error;
RightMotorSpeed = RightBaseSpeed + MotorSpeed;
LeftMotorSpeed = LeftBaseSpeed - MotorSpeed;
Logically speaking, an error of 0 means our robot is out to the left, which means that our robot needs to go a bit right, which in turn means, the right motor needs to slow down and the left motor needs to speed up. THIS IS PID!
The MotorSpeed value is determined from the equation itself. RightBaseSpeed and LeftBaseSpeed are the speeds(any value of PWM 0-255) at which the robot runs at when the error is zero.
The code that I have attached also include how to check the positional values of the sensor , so you can open the serial monitor and upload the code and see for yourself with a line how the motors rotate when position varies.
If you run into trouble when implementing your robot,just check whether and see by changing the signs of the equations !!!
And now the trickest part FINDING Kp AND Kd ,I had to spent more than 1 hour to perfectly tune my robot.instead of putting random values I found an easier method to determine this .
- Start with kp and Kd equal to 0,and start with Kp, first try setting Kp to 1 and observe the robot,our goal is to follow the line even if it is wobbly, if the robot overshoots and loses the line reduce the kp value .if the robot cannot navigate a turn and being sluggish increase Kp value.
- Once the robot seems to somewhat follow the line adjust Kd value(Kd value >Kp value)start from 1 and increase the value until you see a smooth drive with lesser wobbling.
- Once the robot start to follow the line ,increase the speed and see whether its able to retain and follow the line.
Keep in mind that speed has a direct impact on PID tuning and you may sometimes need to retune to match the speed of your robot .
Now we can get down to building of our robot.
Step 2: The Build
Arduino atmega 2560 with USB cable –this is the main microcontroller used.
Chassis- for the robot chassis I have used 2 circular acrylic plates that are used for another project which is perfect for this.using nuts and screws I have built a 2 storied chassis ,so that I can attach other modules to the upper plate.or you can use ready made chassis available .
Micrometal gear motors- the robot needed fast rotating motors in order to cope up with PID routine, for that I have used motors rated at 6V 400rpm and suitable grippy wheels.
QTR 8Rc sensor array –this can be used for the line tracking,as mentioned earlier I think you have now got a clear understanding of how to operate the sensor array with PID .the code is very simple and using existing arduino libraries you will be able to build a speedy line follower.
TB6612FNG Motor driver-I wanted to use a motor driver which can handle turns and change directions in a snap, which is capable of effectively braking the motors when the PWM signal went low.
Lipo battery- 11.1V lipo battery is used to provide power to the robot .although I have used a 11.1 V lipo battery, this capacity is more than what is needed for the arduino and the motors .if you can find a light weighted 7.4V lipo battery or 6V Ni-MH battery pack it will be perfect.due to this reason I have to use a buck converter to convert the voltage to 6V .
Buck converter module- https://www.ebay.com/itm/1PCS-DC-DC-LM2596-power-...
In additional to that you need jumper wires,nuts and bolts ,screwdrivers and electrical tapes and also zip ties to make sure that everything is in place.
Step 3: Assembling
attach the motors and a small caster wheel into a plate using nuts and screws and then mount the QTR sensor,motor driver,arduino board and finally battery on to the chassis.
Here is a perfect diagram I found on internet ,which tells you how the connections should be made.
Step 4: Design Your Line Track
Now your project seems to be almost over .as for last stage you need to have a small arena to test your robot.i have used a random line of width 3cm white line on a black background. Make sure you paste everything well.and for the time being avoind 90 degees angle crosses and crosssections ,because it is a complicated case in point of coding.
Step 5: Programme Your Code
1. Download and Install the Arduino
· windows - https://www.arduino.cc/en/Guide/Windows
· Mac OS X - https://www.arduino.cc/en/Guide/MacOSX
· Linux - https://www.arduino.cc/en/Guide/Linux
2. Download and paste QTR 8 RC sensor array file to the Arduino libraries folder.
· Paste files to the path - C:\Arduino\libraries
3. Download and openLINEFOLLOWING.ino
4. Upload the code to the arduino board via a USB cable
Step 6: DONE!!
now you have a line following robot made by yourself.
Hope this tutorial was helpful .dont hesitate to contact me via email@example.com if you have any problem.
see you soon with a another new project.
We have a be nice policy.
Please be positive and constructive.