3.9. Combustion engine cruise control#
3.9.1. Question:#
Simulate response of the cruise control model equations using a transfer function model in Python.
3.9.2. Mathematical model:#
3.9.3. Solution: Transfer function model: #
3.9.3.1. Generic solution:#
Why do we prefer linear systems?
An important and necessary condition for a linear system is superposition. When a system at rest is subjected to an excitation \(x_1(t)\), it exhibits a response \(y_1(t)\) and if it is subjected to an excitation \(x_2(t)\), it provides a corresponding response \(y_2(t)\). For a linear system, it is fundamental that the excitation \(x_1(t)+x_2(t)\) corresponds with a response \(y_1(t)+y_2(t)\). This important characteristic is called superposition.
Furthermore, it is necessary that a magnitude scale factor is preserved by a linear system. Meaning that an input of \(\beta\) times \(x_1(t)\) results in an response of \(\beta\) times the \(y_1(t)\). This is called the property of homogenity.
How do we linearize a system?
The linearity of many mechanical and electrical systems can be assumed over a reasonable large range of the variables. Almost all physical systems become non-linear as the variables are increased without limitations. One can linearize non-linear elements assuming small signal conditions.
Consider a set of non-linear ordinary differential equations (ODE) where \(\dot x_1(t), \ldots, \dot x_n(t)\) are functions of the state variables \(x_1(t), \ldots, x_n(t)\) and input variables \(u_1(t), \ldots, u_m(t)\):
where \(g(\ldots)\) represent a generalized function.
To perform a linearization we first need to determine an operation point based on the context. For a controlled system this implies the desired control values and for a simulation this implies the assumed working point of the system.
If we assume an operation point for the state \(x_{1}^0\), \(\ldots\) , \(x_{n}^0\) and for the input signal \(u_{1}^0\), \(\ldots\) , \(u_{m}^0\) , then a Taylor series expansion around this operation point can be specified
When we transform the variables to introduce new state variables:
and for the input variables
The linearization point should be achievable therefore when we feed the operation point to our set of differential equations, then a linearization point for the state derivatives can be specified:
When we then substitute the state derivative by
we obtain a set of linear differential equations without an offset
This set of linear differential equations can now be rewritten using a matrix notation:
3.9.3.1.1. Determine the transfer function model:#
Based on \ref{eq:Lin_model} we can now specify a transfer function between a single input (in the example below we choose the first input) and a single output by taking all other input values equal to zero. We can then create a transfer function model based on the previous result (\(\dot x = A x + Bu\)) by eliminating all other states from equation \ref{eq:Lin_model}. Using a matrix notation this elimination is performed by determining the inverse of a matrix:
We can now solve this set of equation using basic matrix operations:
3.9.3.1.2. Simulate the transfer function model:#
3.9.3.2. Numerical solution:#
First load the necessary libraries into Python:
The SciPy library is one of the core packages that make up the SciPy stack. It provides many user-friendly and efficient numerical routines, such as routines for numerical integration, interpolation, optimization, linear algebra, and statistics. The signal processing toolbox currently contains some filtering functions, a limited set of filter design tools, and a few B-spline interpolation algorithms for 1- and 2-D data.
NumPy is a python library used for working with arrays.
SymPy is a Python library for symbolic mathematics;
import psutil
L = psutil.Process().parent().cmdline()
if any("jupyter-notebook" in string for string in L):
%matplotlib notebook
elif any("jupyter-labhub" in string for string in L):
%matplotlib widget
else:
%matplotlib inline
from sympy import Function, Matrix,symbol,symbols,solve,sin,pprint
from scipy import signal
import numpy as np
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[1], line 10
7 else:
8 get_ipython().run_line_magic('matplotlib', 'inline')
---> 10 from sympy import Function, Matrix,symbol,symbols,solve,sin,pprint
11 from scipy import signal
12 import numpy as np
ImportError: cannot import name 'symbol' from 'sympy' (/builds/fet-edu-em/control-systems-and-applications-t4scs2/course-material/modeling-and-simulation-book/venv/lib/python3.8/site-packages/sympy/__init__.py)
The differential equation for the predefined model is
with \(\nu\) the velocity of the car.
Specify the time as a variable
t = symbols('t')
we need to declare all time variable functions ν(t)=V
V = Function('v')(t)
Specify the parameter values:
n=5 #Gear ratio
r=0.5 #Wheel radius [m]
Tm=190 #engine torque constant [Nm]
omega_n=420 #peak torque rate [rad/sec]
m=1000 #mass of the car [kg]
beta = 0.4 #Torque coefficient
Cr=0.01 #roll friction coefficient
rho=1.3 #density of air [kgm^3]
Cd=0.32 #drag coefficient
A=2.4 #Frontal surface car [m^2]
g=9.8 #Gravity constant [N/kg]
Define u and \(\theta\) as two input variables and put them in a symbolic matrix U
u, theta = symbols('u theta', Real=True)
U = Matrix ([[ u], [theta ]])
Setup a vector dX consisting of all the differential equation in the ODE
We will assume that \(\nu\) is positive therefore we will assume \(sign(\nu)=1\).
dX = Matrix ([1/m*(n*u/r*Tm*(1-beta*(n/r*V/omega_n-1)**2)-m*g*sin(theta)-m*g*Cr-1/2*rho*Cd*V**2*A)])
specify a vector X with the time variable functions
X = Matrix ([ V ])
Calculate the jacobian of dX with respect to the time variables defined in X
A_sym = dX.jacobian(X)
print(A_sym)
Matrix([[1.9*u*(0.019047619047619 - 0.000453514739229025*v(t)) - 0.0009984*v(t)]])
Calculate the jacobian of dX with respect to the time variables defined in U
B_sym = dX.jacobian(U)
print(B_sym)
Matrix([[1.9 - 0.76*(0.0238095238095238*v(t) - 1)**2, -9.8*cos(theta)]])
Linearize the differential equation around a stable linearisation point around state values \(V^0,U^0,theta^0\) that fit the model equations
We choice \(V^0 =25\), \(\theta^0=0.01\)
V0=25 # Stable working point for the car speed is 25 m/s
theta0=0.01 # Stable Working point for the steep angle is 0.01 rad
then \(U^0\) can be solved because we know that a stable working point implies that \(\dot v = g(v,u) =0\). This equation can be solved using the symbolic solver included in the Sympy library:
q = dX.subs(V,V0).subs(theta,theta0) #substitute V and theta with V^0=25 and theta^0=0.01 in the equation dX
sol=solve(q,u) #find the U0 value that fits equation q=0
U0= float(sol[u]) # Working point for the input
print(sol[u])
0.286117676730995
We now substitute these linearization values in our symbolic matrices A and B
A=A_sym.subs(V,V0).subs(theta,theta0).subs(u,sol[u])
print(A)
B=B_sym.subs(V,V0).subs(theta,theta0).subs(u,sol[u])
print(B)
Matrix([[-0.0207687977512875]])
Matrix([[1.77548752834467, -9.79951000408332]])
Create a transfer function model based on the previous result (\(\dot x' = A x' + Bu'\)) with for example as input u’(t). Using equation (1) we can derive a transfer function if we assume \(x'_1(0)=0\) using the folowing equation:
We can now solve easily solve this equation by taking \(\Theta(s)=0\):
H = signal.TransferFunction(float(B[0]),[1,-float(A[0])])
print(H)
TransferFunctionContinuous(
array([1.77548753]),
array([1. , 0.0207688]),
dt: None
)
We will generate a solutions in the interval 0 <= t <= 100.
Therefore we will specify a time vector array:
t=np.linspace(0,100,1000)
U=0.1 * np.ones(1000) # step input of 0.1 [m]
X0=[0] #Initial value for the velocity
T, yout, xout = signal.lsim(H, U, t, X0)
Finally we plot the resulted simulated velocities (yout) as a function of time (t)
import matplotlib.pyplot as plt # Load the pyplot object from the matplotlib library
fig, axes = plt.subplots(nrows=1, ncols=2)
ax0, ax1 = axes.flatten()
ax0.plot(T, yout, 'b', label='TF: Linearized velocity of the car') # plot the time signal and create a label for the legend
ax0.legend(loc='best') # position the legend at a minimal overlapping position
ax0.set(xlabel='t[s]', ylabel='Velocity v\' [m/s]') # label the y-axis
ax0.grid() # plot a grid
ax1.plot(T, yout+V0, 'b', label='TF: Real velocity of the car') # plot the time signal and create a label for the legend
ax1.legend(loc='best') # position the legend at a minimal overlapping position
ax1.set(xlabel='t[s]', ylabel='Velocity v [m/s]') # label the y-axis
ax1.grid() # plot a grid
fig.tight_layout()
plt.show() # show the plot