Solutions of Exercise 7.3#
Assume you designed the following control law:
Discretize it using the trapezoidal method with a sampling frequency of \(10\) Hz.
Solution#
Question 1#
First, let’s start by noting that a sampling frequency of \(10\) Hz corresponds to a sampling period of \(T = \frac{1}{10} = 0.1\) seconds.
To discretize the control law, the bilinear formula is applied
Rembember that:
\(\alpha = 0\) corresponds to Forward Euler
\(\alpha = \frac{1}{2}\) corresponds to the Trapezoidal Method
\(\alpha = 1\) corresponds to Backward Euler
In this case, with \(\alpha = \frac{1}{2}\), the bilinear transformation (39) reads
where \(T=0.1\) is the sampling time and \(q^{-1}\) is the time-shift operator, meaning that \(q^{-1} x_k = x_{k-1}\).
To proceed, we apply (40) to our control law. This results in:
If we now multiply both sides by \((21 - 19 q^{-1})\) we get
Because of the meaning of the time-shift operator, this expression is equivalent to
Isolating \(u_k\) we retrieve the discrete-time control law:
Implementation
The discretized control law (41) can be easily implemented with, e.g., a python function invoked that gets called every \(T\) seconds
def control_law(u_prev: float, e_current: float, e_prev: float) -> float:
"""
This function implement the discretized control law and
should be invoked every T = 0.1 seconds.
Arguments:
u_prev (float): The control input at the previous step, i.e. u_{k-1}
e_current (float): The currently-measured error, i.e. e_k
e_prev (float): The previously measured error. i.e. e_{k-1}
Returns:
u_current (float): The control action to be applied to the actuator
"""
return 19.0 / 21.0 * u_prev + 402.0 / 21.0 * e_current - 398.0 / 21.0 e_prev