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:#

Combustion engine cruise control model equations

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)\):

(4.45)#\[\begin{align} \dot x_1(t) &=& g_1(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ \vdots \\ \dot x_n(t) &=& g_n(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t )) \end{align}\]

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

(4.46)#\[\begin{align} \dot x_1(t) &= g_1(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ &\approx g_1(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) + \left. \frac{\partial g_1}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_1(t)-x_{1}^0) + \dots + \left. \frac{\partial g_1}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_n(t)-x_{n}^0) \\ & \ + \left. \frac{\partial g_1}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} (x_1(t)-u_{1}^0) + \dots + \left. \frac{\partial g_1}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (u_m(t)-u_{m}^0) \\ & \vdots \\ \dot x_n(t) &= g_n(x_1(t), \ldots,x_n(t),u_1(t), \ldots,u_m(t)) \\ &\approx g_n(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) + \left. \frac{\partial g_n}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} (x_1(t)-x_{1}^0) + \dots + \left. \frac{\partial g_n}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_n(t)-x_{n}^0) \\ & \ + \left. \frac{\partial g_n}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (x_1(t)-u_{1}^0) + \dots + \left. \frac{\partial g_n}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u=u_{1}^0, \ldots , u_{m}^0} (u_m(t)-u_{m}^0) \end{align}\]

When we transform the variables to introduce new state variables:

(4.47)#\[\begin{align} x'_1(t) &= x_1(t)-x_{1}^0\\ &\vdots \\ x'_n(t) &= x_n(t)-x_{n}^0 \end{align}\]

and for the input variables

(4.48)#\[\begin{align} u'_1 &= u_1(t)-u_{1}^0\\ &\vdots \\ u'_m &= u_m(t)-u_{m}^0. \end{align}\]

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:

(4.49)#\[\begin{align} \dot x'^0_1 &= g_1(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0) \\ &\vdots \\ \dot x'^0_n &= g_n(x_{1}^0,\ldots,x_{n}^0,u_{1}^0,\ldots,u_{m}^0). \end{align}\]

When we then substitute the state derivative by

(4.50)#\[\begin{align} \dot x'_1(t) &= \dot x_1(t)-\dot x'^0_{1}\\ &\vdots \\ \dot x'_n(t) &= \dot x_n(t)-\dot x'^0_{n} \end{align}\]

we obtain a set of linear differential equations without an offset

(4.51)#\[\begin{align} \dot x'_1(t) &\approx \left. \frac{\partial g_1}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_1(t) + \dots + \left. \frac{\partial g_1}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_n(t) \\ & + \left. \frac{\partial g_1}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_1(t) + \dots + \left. \frac{\partial g_1}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_m(t) \\ &\approx a_1^1 x'_1 + \ldots + a_n^1 x'_{n} + b_1^1 u'_1 + \ldots + b_m^1 u'_{m}\\ & \vdots \\ \dot x'_n(t) &\approx \left. \frac{\partial g_n}{\partial x_1} \right\|_{x=x_{1}^0\ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_1(t) + \dots + \left. \frac{\partial g_n}{\partial x_n} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} x'_n(t) \\ & + \left. \frac{\partial g_n}{\partial u_1} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_1(t) + \dots + \left. \frac{\partial g_n}{\partial u_m} \right\|_{x=x_{1}^0, \ldots,x_{n}^0,u_{1}^0, \ldots , u_{m}^0} u'_m(t) \\ &\approx a_1^n x'_1 + \ldots + a_n^n x'_{n} + b_1^n u'_1 + \ldots + b_m^n u'_{m} \end{align}\]

This set of linear differential equations can now be rewritten using a matrix notation:

(4.52)#\[\begin{align} \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} &= \begin{bmatrix} a_1^1 & \dots& a_n^1 \\ \vdots& \vdots& \vdots \\ a_1^n& \dots& a_n^n \end{bmatrix} \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} + \begin{bmatrix} b_1^1 & \dots& b_m^1 \\ \vdots& \vdots& \vdots \\ b_1^n& \dots& b_m^n \end{bmatrix} \begin{bmatrix} u'_1 \\ \vdots \\ u'_m \end{bmatrix} \\ &= A \begin{bmatrix} \dot x'_1 \\ \vdots \\ \dot x'_n \end{bmatrix} + B \begin{bmatrix} u'_1 \\ \vdots \\ u'_m \end{bmatrix} \label{eq:Lin_model} \tag{1} \end{align}\]

4.9.3.1.1. Determine the state space model:#

The state space model is specified as:

(4.53)#\[\begin{align} \dot x' = A x' + B u' \\ y' = C x' +D u' \end{align}\]

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

(4.54)#\[\begin{equation} \dot \nu = \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{equation}\]

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

(4.55)#\[\begin{align} \frac{dV}{dt} &= \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{align}\]

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

(4.56)#\[\begin{align} \frac{dV}{dt} &= \frac{1}{m} \left ( \frac{n u}{r} T_m \left (1- \beta \left ( \frac{\omega}{\omega_n} -1 \right )^2 \right ) - mg\sin\theta - m g C_r sgn(\nu) -\frac{1}{2} \rho C_d A \nu^2 \right ) \end{align}\]

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
../../../_images/5b97e510387c760190d118866ca4c3612e06f220822f28616da6ca3023684f11.png

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
../../../_images/0cdb01e5163db66730a99e97e89a8211259f1ea4b5bdd2ae882bbec57bad9fc6.png

4.9.4. → Check out this animated example#