4.9. Combustion engine cruise control#
4.9.1. Question:#
Simulate the step response of the cruise control model equations using a linear state space model in Python.
4.9.2. Mathematical model:#
4.9.3. Solution: Transfer function model: #
4.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 equation (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 a operation point as \(x_{1}^0\), \(\ldots\) , \(x_{n}^0\) and \(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:
4.9.3.1.1. Determine the state space model:#
The state space model is specified as:
Equation \ref{eq:Lin_model} directly specifies the dynamic equation of the state space model (matrices A,B). The other matrices (C,D) are defined based on the required output of the system.
4.9.3.1.2. Simulate the state space model:#
4.9.3.2. Numerical solution:#
First load the necessary libraries into Python:
The control.matlab module contains a number of functions that emulate some of the functionality of MATLAB. The intent of these functions is to provide a simple interface to the python control systems library (python-control) for people who are familiar with the MATLAB Control Systems Toolbox
SymPy is a Python library for symbolic mathematics;
NumPy is a python library used for working with arrays.
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,symbols,solve,sin,pprint
from scipy import signal
import numpy as np
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
Ar=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 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*Ar) ])
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)
pprint(A_sym)
[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)
pprint(B_sym)
⎡ 2 ⎤
⎣1.9 - 0.76⋅(0.0238095238095238⋅v(t) - 1) -9.8⋅cos(θ)⎦
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\) then \(U^0\) can be solved from the previous equation using a symbolic solver
V0=25
theta0=0.01
q = dX.subs(V,V0).subs(theta,theta0) #substitute V and theta with V^0=30 and theta^0=0.01
sol=solve(q,u) #find the U0 value that fits equation q=0
U0= float(sol[u]) # Working point for the input
print(float(U0))
0.28611767673099464
We now substitute these linearization values in our symbolic matrices A and B
A=A_sym.subs(V,V0).subs(theta,theta0).subs(u,U0)
print(A)
B=B_sym.subs(V,V0).subs(theta,theta0).subs(u,U0)
print(B)
Matrix([[-0.0207687977512875]])
Matrix([[1.77548752834467, -9.79951000408332]])
Create a state space model from A and B with the velocity V as an output
C = [1]
D = [0,0]
sys = signal.StateSpace(A,B,C,D)
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=np.add(np.zeros((1000,2)),[0.1,0]) #input signals u and theta
X0=[0] #Initial values for V
T, yout, xout = signal.lsim(sys, 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 var') # 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='SS: 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+V0 [m/s]') # label the y-axis
ax1.grid() # plot a grid
fig.tight_layout()
plt.show() # show the plot
Compare the linearized sate space model with the ODE model:
from scipy.integrate import solve_ivp
import numpy as np
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]
def ODEFUN(t, y, n, r, beta, omega_n, m , Cr, rho, Cd, A, Tm ):
u=U0+0.1#
theta=0.01# step of 0.01 radians = 0.6 degrees
omega= n/r*y[1]
dx = y[1] # v is second element of y(t)
dv = 1/m*(n*u/r*Tm*(1-beta*(omega/omega_n-1)**2) - m*g*np.sin(theta) - m*g*Cr*np.sign(y[1]) - 0.5*rho*Cd*A*y[1]**2)
return [dx,dv]
y0=[0,25]
TSPAN = [0,200]
sol = solve_ivp(ODEFUN, TSPAN, y0, args=( n, r, beta, omega_n, m , Cr, rho, Cd, A, Tm ), dense_output=True)
t = np.linspace(0, 100, 1000)
v = sol.sol(t)[1,:] #Select the second number in the output vector = v (velocity)
import matplotlib.pyplot as plt # Load the pyplot object from the matplotlib library
fig, axes = plt.subplots()
axes.plot(t, v.T, 'b', label='ODE: velocity of the var') # plot the time signal and create a label for the legend
axes.legend(loc='best') # position the legend at a minimal overlapping position
axes.set(xlabel='t[s]', ylabel='Velocity v [m/s]') # label the y-axis
axes.grid() # plot a grid
axes.plot(t, yout+V0, 'r', label='SS: Real velocity of the car') # plot the time signal and create a label for the legend
axes.legend(loc='best') # position the legend at a minimal overlapping position
axes.set(xlabel='t[s]', ylabel='Velocity V+V0 [m/s]') # label the y-axis
axes.grid() # plot a grid
fig.tight_layout()
plt.show() # show the plot