Dr.
Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design
Control system definition
Is a system, which provides the desired response by controlling the output.
the output is controlled by varying the input
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design
Control system Classifications
• Open and Closed loop control systems .
We are concerned with Closed loop control systems .
Also called feedback control.
• Continuous and Discrete time control systems based on the
type of the signal.
We are concerned with discrete control since we are
dealing with computers and embedded systems.
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Control system Classifications
Controllers design
Open loop control system
In open loop control systems, output is not fed-back to the input. So, the control
action is independent of the desired output.
Limitations
• Do not have a feedback mechanism, so they are very inaccurate and
unreliable
• Output changes due to external disturbances, and are not corrected
automatically
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Control system Classifications
Controllers design
Close loop control system
In closed loop control systems, output is fed back to the input. So, the control
action is dependent on the desired output.
The difference between the input and the
feedback signal , produce error signal
Then the controller produces an
actuating signal which controls the plant.
Advantages over open loop control
• accurate
• Less affected by noise
• Less affected by external and internal disturbances.
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design
Close loop (Feedback) control
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Review control system response Characteristics
desired output
Fig: A typical step response for a 2nd order system (underdamped , Critically
damped, overdamped
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Types of Feedback control
• On/Off control (bang bang)
• PID
Advanced
• LQR (Linear Quadratic Regulator) (optimal control)
• Sliding Mode control
• MPC (Model Predictive Control)
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
On/Off (bang bang) control
• Easiest feedback control
• Turns Actuator on or off when a desired target (setpoint) has been reached.
Limitations
• Regardless of the size of the error, the output of the on-off controller
can only be fully ON or fully OFF, it is not proportional with the error
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
PID (Proportional, Integral , Derivative) Control
Formula
𝑡
𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 + 𝐾𝑖 න 𝑒 𝑇 𝑑𝑇 + 𝐾𝑑 ∙ 𝑒(𝑡)
ሶ
0
➢ 𝑢 𝑡 : the output
• 𝐾𝑝 ∶ the proportional gain
• 𝐾𝑖 ∶ the integral gain
➢ 𝑒 𝑡 : the error , is the difference between the • 𝐾𝑑 ∶ the derivative gain
desired setpoint and the measured (actual) output
𝑡
➢ 0 𝑒 𝑇 𝑑𝑇 : the error integral over time
➢ 𝑒(𝑡):
ሶ the error derivative (rate of change)
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
PID (Proportional, Integral , Derivative) Control
Integral Term Derivative Term
Proportional Term
(Iterm) (Dterm)
(Pterm)
𝑡
𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 + 𝐾𝑖 න 𝑒 𝑇 𝑑𝑇 + 𝐾𝑑 𝑒(𝑡)
ሶ
0
➢ 𝑢 𝑡 : the output
• 𝐾𝑝 ∶ the proportional gain
• 𝐾𝑖 ∶ the integral gain
➢ 𝑒 𝑡 : the error , is the difference between the • 𝐾𝑑 ∶ the derivative gain
desired setpoint and the measured (actual) output
𝑡
➢ 0 𝑒 𝑇 𝑑𝑇 : the error integral over time
➢ 𝑒(𝑡)
ሶ : the error derivative (rate of change)
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
PID (Proportional, Integral , Derivative) Control 𝐹
Proportional Term (P_term) 𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 𝑥𝑎𝑐𝑡 𝑥𝑑𝑒𝑠
𝑒 𝑡 = (𝑥𝑑𝑒𝑠 − 𝑥𝑎𝑐𝑡 )
• Act as virtual spring
𝐾𝑝 very high
Limitations 𝐾𝑝 high
• Leads to instability of the system for a very large gain. 𝐾𝑝 = 1
• Presence of Steady state (offset) error in some cases, such as
system under disturbances
• No damping which leads to increase overshoot
Add Damper (derivative term)
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
PID (Proportional, Integral , Derivative) Control 𝐹
PD control , adding Derivative Term (D_term) 𝑥𝑎𝑐𝑡 𝑥𝑑𝑒𝑠
𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 + 𝐾𝑑 𝑒(𝑡)
ሶ
𝑒 𝑡 = (𝑥𝑑𝑒𝑠 − 𝑥𝑎𝑐𝑡 ) 𝑥
• Act as virtual dumper 𝐾𝑑 very low
𝐾𝑑 low
𝐾𝑑 medium (high)
• Derivative Term brings more stability to the system
𝐾𝑑 very high
• Tuning 𝐾𝑑 can reduce overshooting
Limitations
• Presence of Steady state (offset) error in some cases, such as
system under disturbance
Add Integral term
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Close loop (Feedback) control
Controllers design
PID (Proportional, Integral , Derivative) Control
PID control , adding integral Term (I_term)
𝑡
𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 + 𝐾𝑖 න 𝑒 𝑇 𝑑𝑇 + 𝐾𝑑 𝑒(𝑡)
ሶ 𝑒 𝑡 = (𝑥𝑑𝑒𝑠 − 𝑥𝑎𝑐𝑡 )
0
steady state error
In some cases in presence of disturbances this may lead to steady state errors
• Integral term summing the instantaneous error over time
and gives the accumulated offset (steady state error)
Limitations
• Need to be very carful when dealing with integral term as it may
lead to system instability
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino)
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Discrete system
𝑡
In Continuous time 𝑢 𝑡 = 𝐾𝑝 ∙ 𝑒 𝑡 + 𝐾𝑖 න 𝑒 𝑇 𝑑𝑇 + 𝐾𝑑 𝑒(𝑡)
ሶ 𝑒 = (𝑥𝑑𝑒𝑠 − 𝑥𝑎𝑐𝑡 )
0
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
In Discrete time 𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
𝑒𝑎𝑐𝑡 : is the current error
𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 : is the previous error
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
volatile float er=0;
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
volatile float last_er=0;
TimerFlag = false;
volatile float er_sum=0;
er = des_ang - currentangle;
volatile float er_dot=0;
volatile float ITerm=0; er_dot = (er - last_er )/0.2;
volatile float PTerm=0; er_sum += er * 0.2;
volatile float DTerm=0; last_er = er;
float Kp=0.2; PTerm=Kp*er;
float Kd=0.5;
DTerm=Kd*er_dot;
float Ki=2.2;
ITerm=Ki*er_sum;
//the desired PID output
float des_ang = 90; // desired angle example for position control
Output = PTerm+ DTerm + ITerm ;
volatile float lastangle = 0 // desired angle example for position control }
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
Error derivative ( er_dot) is always noisy
TimerFlag = false; → can use filter to reduce the noise such as
er = des_ang - currentangle; (exponential moving average )
er_dot = (er - last_er )/0.2;
er_sum += er * 0.2;
last_er = er;
PTerm=Kp*er;
DTerm=Kd*er_dot;
ITerm=Ki*er_sum;
//the desired PID output
Output = PTerm+ DTerm + ITerm
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 (𝑒𝑎𝑐𝑡 + 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 ) ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
Error derivative ( er_dot) is always noisy
TimerFlag = false; → can use filter to reduce the noise such as
er = des_ang - currentangle; (exponential moving average )
er_dot = (er - last_er )/0.2;
er_sum += er * 0.2; er_dot_unfiltered = (er - last_er )/0.2;
last_er = er;
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
PTerm=Kp*er;
DTerm=Kd*er_dot;
ITerm=Ki*er_sum;
//the desired PID output
Output = PTerm+ DTerm + ITerm
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
Error derivative ( er_dot) is always noisy
TimerFlag = false; → can use filter to reduce the noise such as
er = des_ang - currentangle; (exponential moving average )
er_dot_unfiltered = (er - last_er )/0.2;
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
last_er = er;
PTerm=Kp*er;
DTerm=Kd*er_dot;
ITerm=Ki*er_sum;
//the desired PID output
Output = PTerm+ DTerm + ITerm
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false;
Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
(exponential moving average )
er_dot_unfiltered = (er - last_er )/0.2;
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2; Reset windup : due to the output being physically impossible
last_er = er; (process saturation)
→ Need to clamp both Iterm and Output to min and max
PTerm=Kp*er;
limits, to avoid the process saturation
DTerm=Kd*er_dot;
ITerm=Ki*er_sum;
//the desired PID output
Output = PTerm+ DTerm + ITerm
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
#define 𝒔𝒊𝒈𝒏(𝒙) (((𝑥) > 0) − ((𝑥) < 0)) // sign function return 1 for +ve and -1 for -ve
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false; Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
er_dot_unfiltered = (er - last_er )/0.2; (exponential moving average )
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
last_er = er;
PTerm=Kp*er; Reset windup : due to the output being physically impossible
DTerm=Kd*er_dot; (process saturation)
ITerm=Ki*er_sum; → Need to clamp both Iterm and Output to min and max
if (Iterm >= 255|| Iterm <= -255){ limits, to avoid the process saturation
Iterm = 255 * sign(ITerm);
}
//the desired PID output
Output = PTerm+ DTerm + Iterm
if (Output >= 255|| Output <= -255){
Output = 255 * sign(Output);
}
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
#define 𝒔𝒊𝒈𝒏(𝒙) (((𝑥) > 0) − ((𝑥) < 0)) // sign function return 1 for +ve and -1 for -ve
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false; Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
er_dot_unfiltered = (er - last_er )/0.2; (exponential moving average )
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
last_er = er;
PTerm=Kp*er; Reset windup : due to the output being physically impossible
DTerm=Kd*er_dot; (process saturation)
ITerm=Ki*er_sum; → Need to clamp both Iterm and Output to min and max
if (Iterm >= 255|| Iterm <= -255){ limits, to avoid the process saturation
Iterm = 255 * sign(ITerm);
}
//the desired PID output
Output = PTerm+ DTerm + Iterm
if (Output >= 255|| Output <= -255){
Output = 255 * sign(Output);
}
}
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
#define 𝒔𝒊𝒈𝒏(𝒙) (((𝑥) > 0) − ((𝑥) < 0)) // sign function return 1 for +ve and -1 for -ve
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false; Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
er_dot_unfiltered = (er - last_er )/0.2; (exponential moving average )
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
last_er = er;
PTerm=Kp*er; Reset windup : due to the output being physically impossible
DTerm=Kd*er_dot; (process saturation)
ITerm=Ki*er_sum; → Need to clamp both Iterm and Output to min and max
if (Iterm >= 255|| Iterm <= -255){ limits, to avoid the process saturation
Iterm = 255 * sign(ITerm);
} Derivative kick : occurs because the value of the error
//the desired PID output
Output = PTerm+ DTerm + Iterm changes suddenly whenever the set point is changed
if (Output >= 255|| Output <= -255){ → it is assumed that the set point is constant, so the
Output = 255 * sign(Output); derivative of it equal zero.
} 𝑑(des_ang − currentangle) 𝑑(− currentangle) −(currentangle−𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒)
} 𝑒ሶ = = 𝑒ሶ =
𝑑𝑡 𝑑𝑡 𝑑𝑡
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
#define 𝒔𝒊𝒈𝒏(𝒙) (((𝑥) > 0) − ((𝑥) < 0)) // sign function return 1 for +ve and -1 for -ve
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false; Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
er_dot_unfiltered = (er - last_er )/0.2; (exponential moving average )
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
er_dot_unfiltered = −(currentangle−𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒)/0.2;
last_er = er;
PTerm=Kp*er; Reset windup : due to the output being physically impossible
𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒 = currentangle;
DTerm=Kd*er_dot; (process saturation)
ITerm=Ki*er_sum; → Need to clamp both Iterm and Output to min and max
if (Iterm >= 255|| Iterm <= -255){ limits, to avoid the process saturation
Iterm = 255 * sign(ITerm);
} Derivative kick : occurs because the value of the error
//the desired PID output
Output = PTerm+ DTerm + Iterm changes suddenly whenever the set point is changed
if (Output >= 255|| Output <= -255){ → it is assumed that the set point is constant, so the
Output = 255 * sign(Output); derivative of it equal zero.
} 𝑑(des_ang − currentangle) 𝑑(− currentangle) −(currentangle−𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒)
} 𝑒ሶ = = 𝑒ሶ =
𝑑𝑡 𝑑𝑡 𝑑𝑡
Dr. Omar Abdelaziz - Computer Controlled Experimentation Course - ME302
Controllers design Close loop (Feedback) control
Implementing PID on embedded system ( Arduino) Code tips and tricks
(𝑒𝑎𝑐𝑡 − 𝑒𝑃𝑟𝑒𝑣𝑖𝑜𝑢𝑠 )
𝑢 = 𝐾𝑝 ∙ 𝑒 + 𝐾𝑖 𝑒 ∙ 𝑑𝑡 + 𝐾𝑑
𝑑𝑡
#define 𝒔𝒊𝒈𝒏(𝒙) (((𝑥) > 0) − ((𝑥) < 0)) // sign function return 1 for +ve and -1 for -ve
if (TimerFlag){ // timer interrupt flag every 0.2s →dt=0.2
TimerFlag = false; Error derivative ( er_dot) is always noisy
er = des_ang - currentangle; → can use filter to reduce the noise such as
er_dot_unfiltered = −(currentangle−𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒)/0.2; (exponential moving average )
𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒 = currentangle;
er_dot= exp_moving_avg(er_dot, er_dot_unfiltered , 0.1);
er_sum += er * 0.2;
PTerm=Kp*er; Reset windup : due to the output being physically impossible
DTerm=Kd*er_dot; (process saturation)
ITerm=Ki*er_sum; → Need to clamp both Iterm and Output to min and max
if (Iterm >= 255|| Iterm <= -255){ limits, to avoid the process saturation
Iterm = 255 * sign(ITerm);
} Derivative kick : occurs because the value of the error
//the desired PID output
Output = PTerm+ DTerm + Iterm changes suddenly whenever the set point is changed
if (Output >= 255|| Output <= -255){ → it is assumed that the set point is constant, so the
Output = 255 * sign(Output); derivative of it equal zero.
} 𝑑(des_ang − currentangle) 𝑑(− currentangle) −(currentangle−𝑙𝑎𝑠𝑡𝑎𝑛𝑔𝑙𝑒)
} 𝑒ሶ = = 𝑒ሶ =
𝑑𝑡 𝑑𝑡 𝑑𝑡