PID CONTROL
Feedback loop system.
MATHEMATICAL FORM:
BLOCK DIAGRAM
1. Compare the desired position to
the current position
2. The result is stored as the "Error"
3. The PID system decides how much
to steer
4. The motors steer the robot and the
sensors check the position again
5. Repeat
POSITION IN LFR
• Perfectly on the line corresponds to a
position of 1000.
• The further to the left our cart is, the
closer position is to 0
The further to the right our cart is, the
closer position is to 2000.
• Position is never greater than 2000 or
less than 0!
•
SET POINT
• It is the position the robot is stable in.
• in our case in dead center of the line.
• This is the position the control algorithm strives to achieve.
• We use two new quantities in our algorithm,
• An average of the sensor readings and sum of the sensor readings.
4 STEPS OF PID FOR LFR
• Sensor read and average values.
• PID calculate
• Calculate set point
• Drive motor
USING THE SENSOR TO FIND THE
LINE
We can have each sensor report how much light is
reflected and get a picture of which side the line is
on.
If we then take a weighted average of the sensor
values, we get a single number representing our
position.
In this case we know we know the sensor is to the
left of the center position, 1000.
So we can expect the values to average out to a bit
lower, around 750.
SETTING POINT
• It is simple as taking average of all sensor values.
• Now we’ll build the complete PID algorithm.
• We start with calculation of the sensor sum and average similar to code
To take an average of a bunch of values, we use this formula
We're going to take a weighted average, which is similar, only each value is multiplied by a "weight"
SETTING POINT
sensors_average = 0;
sensors_sum = 0;
for (int i = 0; i < 5; i++){
sensors[i] = analogRead(i);
sensors_average += sensors[i] * i * 1000; //Calculating the weighted
mean of the sensor readings.
sensors_sum += int(sensors[i]); //Calculating sum of sensor readings.
}
void pid_calc(){
position = int(sensors_average / sensors_sum);
proportional = position – set_point; // Replace set_point by your set
point
integral = integral + proportional;
derivative = proportional - last_proportional;
last_proportional = proportional;
error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}
CALCULATE TURN
• Formula for calculation of error value is the functional definition of PID control.
• you have to define the values of Kp, Ki and Kd in the code somewhere.
• After calculating the value of error,we need to tell the motor to move such that the
error is minimized.
CALCULATE TURN
void calc_turn(){ //Restricting the error value between +256.
if (error_value< -256){ error_value = -256; } if (error_value> 256){
error_value = 256;
}
// If error_value is less than zero calculate right turn speed values
if (error_value< 0){
right_speed = max_speed + error_value;
left_speed = max_speed;
}
// Iferror_value is greater than zero calculate left turn values
else{
right_speed = max_speed;
left_speed = max_speed - error_value;
}
}
SETTING MOTOR DRIVING
• According to your execute a left turn if you reduce the speed of your left motor and
a right turn if you reduce the speed of the right motor
• We use a value max_speed that has to be defined by you right in the beginning to
control the peed of the motor.
• The maximum value of this is 256, which corresponds to the maximum output of the
8 bit DAC converter on the Arduino.
• So let’s define a function which does exactly
that. ‘motor_right’ and ‘motor_left’ are the pin numbers at which your motors are
connected via the motor driver
SETTING MOTOR DRIVING
• void motor_drive(intright_speed, intleft_speed){
• // Drive motors according to the calculated values for a turn
• analogWrite(motor_right, right_speed);
• analogWrite(motor_left, left_speed);
• delay(50); // Optional
• }
SUMMING-UP
SUMMING-UP
• Say your left motor moves faster than your right motor, add this before
the analogWrite().
• The ‘20’ is a random number, and you should set it depending on your motors.
• Just set the values and do a test run if its still not approximately same add subtract
accordingly till you have a more or less syncdmotors(i.e they run at approx. the
same speed).
• Put the functions together, and use this statement in the loop section.
SUMMING-UP
• void loop(){
• sensors_read(); //Reads sensor values and computes sensor sum and weighted
average
• pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
• calc_turn(); //Computes the error to be corrected
• motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
• }
SUMMING-UP
• Set all three constants to zero. Run the robot and see how it handles.
• Vary the values of Kp, Ki and Kd in that order, one at a time and test your robot.
• Do step 2 over and over again to get your bot perfectly tuned.

More Related Content

PPTX
National pension scheme(nps)
PDF
Digital Signal Processing Tutorial:Chapt 1 signal and systems
PDF
lec 2 Robotics time & motion
PPTX
Pid control for line follwoers
PPTX
Pid control for line follwoers
PPTX
Presentation on control systems for line follower robot
PPTX
Autonomous Line Followers With Arduino and PID.pptx
PDF
Arduino Programming for Basic Robotics - University of Moratuwa
National pension scheme(nps)
Digital Signal Processing Tutorial:Chapt 1 signal and systems
lec 2 Robotics time & motion
Pid control for line follwoers
Pid control for line follwoers
Presentation on control systems for line follower robot
Autonomous Line Followers With Arduino and PID.pptx
Arduino Programming for Basic Robotics - University of Moratuwa

Similar to Pid lfr (20)

PDF
Robotics Report final.compressed (1)
PPTX
Line follower(theory + coding + videos)
PDF
report_komal
PDF
import RPi-GPIO as GPIO import time #from AlphaBot import AlphaBot imp.pdf
DOCX
Path Following Robot
PDF
Design and Implementation of a Self-Balancing Two-Wheeled Robot Driven by a F...
PDF
A Proportional-Integral-Derivative Control Scheme of Mobile Robotic platforms...
TXT
Final pid2
PDF
2013 arduino pid lab 0
DOC
Project report
PDF
Day 2 slides UNO summer 2010 robotics workshop
PPT
motor_2.ppt
PDF
Real-time PID control of an inverted pendulum
PPTX
Line Following Robot Presentation
PDF
PID Controller based DC Motor Speed Control
PDF
Arduino Workshop Day 2 - Advance Arduino & DIY
PDF
Serial Arm Control in Real Time Presentation
PPT
Arduinomotorcontrolusingservoultrasonic
PDF
project_NathanWendt
PDF
IRJET- Design & Development of Two-Wheeled Self Balancing Robot
Robotics Report final.compressed (1)
Line follower(theory + coding + videos)
report_komal
import RPi-GPIO as GPIO import time #from AlphaBot import AlphaBot imp.pdf
Path Following Robot
Design and Implementation of a Self-Balancing Two-Wheeled Robot Driven by a F...
A Proportional-Integral-Derivative Control Scheme of Mobile Robotic platforms...
Final pid2
2013 arduino pid lab 0
Project report
Day 2 slides UNO summer 2010 robotics workshop
motor_2.ppt
Real-time PID control of an inverted pendulum
Line Following Robot Presentation
PID Controller based DC Motor Speed Control
Arduino Workshop Day 2 - Advance Arduino & DIY
Serial Arm Control in Real Time Presentation
Arduinomotorcontrolusingservoultrasonic
project_NathanWendt
IRJET- Design & Development of Two-Wheeled Self Balancing Robot
Ad

Recently uploaded (20)

PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
CyberSecurity Mobile and Wireless Devices
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Design Guidelines and solutions for Plastics parts
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Module 8- Technological and Communication Skills.pptx
PPTX
introduction to high performance computing
PPTX
Software Engineering and software moduleing
PPTX
communication and presentation skills 01
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
CyberSecurity Mobile and Wireless Devices
III.4.1.2_The_Space_Environment.p pdffdf
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Exploratory_Data_Analysis_Fundamentals.pdf
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Design Guidelines and solutions for Plastics parts
Abrasive, erosive and cavitation wear.pdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
distributed database system" (DDBS) is often used to refer to both the distri...
Module 8- Technological and Communication Skills.pptx
introduction to high performance computing
Software Engineering and software moduleing
communication and presentation skills 01
Ad

Pid lfr

  • 3. BLOCK DIAGRAM 1. Compare the desired position to the current position 2. The result is stored as the "Error" 3. The PID system decides how much to steer 4. The motors steer the robot and the sensors check the position again 5. Repeat
  • 4. POSITION IN LFR • Perfectly on the line corresponds to a position of 1000. • The further to the left our cart is, the closer position is to 0 The further to the right our cart is, the closer position is to 2000. • Position is never greater than 2000 or less than 0! •
  • 5. SET POINT • It is the position the robot is stable in. • in our case in dead center of the line. • This is the position the control algorithm strives to achieve. • We use two new quantities in our algorithm, • An average of the sensor readings and sum of the sensor readings.
  • 6. 4 STEPS OF PID FOR LFR • Sensor read and average values. • PID calculate • Calculate set point • Drive motor
  • 7. USING THE SENSOR TO FIND THE LINE We can have each sensor report how much light is reflected and get a picture of which side the line is on. If we then take a weighted average of the sensor values, we get a single number representing our position. In this case we know we know the sensor is to the left of the center position, 1000. So we can expect the values to average out to a bit lower, around 750.
  • 8. SETTING POINT • It is simple as taking average of all sensor values. • Now we’ll build the complete PID algorithm. • We start with calculation of the sensor sum and average similar to code To take an average of a bunch of values, we use this formula We're going to take a weighted average, which is similar, only each value is multiplied by a "weight"
  • 9. SETTING POINT sensors_average = 0; sensors_sum = 0; for (int i = 0; i < 5; i++){ sensors[i] = analogRead(i); sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor readings. sensors_sum += int(sensors[i]); //Calculating sum of sensor readings. } void pid_calc(){ position = int(sensors_average / sensors_sum); proportional = position – set_point; // Replace set_point by your set point integral = integral + proportional; derivative = proportional - last_proportional; last_proportional = proportional; error_value = int(proportional * Kp + integral * Ki + derivative * Kd); }
  • 10. CALCULATE TURN • Formula for calculation of error value is the functional definition of PID control. • you have to define the values of Kp, Ki and Kd in the code somewhere. • After calculating the value of error,we need to tell the motor to move such that the error is minimized.
  • 11. CALCULATE TURN void calc_turn(){ //Restricting the error value between +256. if (error_value< -256){ error_value = -256; } if (error_value> 256){ error_value = 256; } // If error_value is less than zero calculate right turn speed values if (error_value< 0){ right_speed = max_speed + error_value; left_speed = max_speed; } // Iferror_value is greater than zero calculate left turn values else{ right_speed = max_speed; left_speed = max_speed - error_value; } }
  • 12. SETTING MOTOR DRIVING • According to your execute a left turn if you reduce the speed of your left motor and a right turn if you reduce the speed of the right motor • We use a value max_speed that has to be defined by you right in the beginning to control the peed of the motor. • The maximum value of this is 256, which corresponds to the maximum output of the 8 bit DAC converter on the Arduino. • So let’s define a function which does exactly that. ‘motor_right’ and ‘motor_left’ are the pin numbers at which your motors are connected via the motor driver
  • 13. SETTING MOTOR DRIVING • void motor_drive(intright_speed, intleft_speed){ • // Drive motors according to the calculated values for a turn • analogWrite(motor_right, right_speed); • analogWrite(motor_left, left_speed); • delay(50); // Optional • }
  • 15. SUMMING-UP • Say your left motor moves faster than your right motor, add this before the analogWrite(). • The ‘20’ is a random number, and you should set it depending on your motors. • Just set the values and do a test run if its still not approximately same add subtract accordingly till you have a more or less syncdmotors(i.e they run at approx. the same speed). • Put the functions together, and use this statement in the loop section.
  • 16. SUMMING-UP • void loop(){ • sensors_read(); //Reads sensor values and computes sensor sum and weighted average • pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd • calc_turn(); //Computes the error to be corrected • motor_drive(right_speed, left_speed); //Sends PWM signals to the motors • }
  • 17. SUMMING-UP • Set all three constants to zero. Run the robot and see how it handles. • Vary the values of Kp, Ki and Kd in that order, one at a time and test your robot. • Do step 2 over and over again to get your bot perfectly tuned.