3.7. A simplified Cruise Control Model#

3.7.1. Question:#

Simulate the step response of the cruise control model equations using a transfer function model in Python.

3.7.2. Mathematical model:#

Simplified cruise control model for a car.

3.7.3. Solution: Transfer function simulation: #

3.7.4. 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.

3.7.5. Detailed solution:#

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 scipy import signal
import numpy as np 

The first order ODE model for the simplified cruise control model specified as previously,

(3.35)#\[\begin{eqnarray} \dot x &=& v \\ \dot v&=& - \frac{b}{m} v + \frac{u}{m} \end{eqnarray}\]

can be transformed to the Laplace domain (assuming that x(0)=0 and v(0)=0!):

(3.36)#\[\begin{eqnarray} s X(s) &=& V(s) \\ s V(s) &=& - \frac{b}{m} V(s) + \frac{U(s)}{m}. \end{eqnarray}\]

There is one input (U(s)) and two states (X(s) and V(s)) therefore two transfer functions can be derived from the previous equations:

  1. Position versus input force:\

(3.37)#\[\begin{align} H_1(s)= \frac{X(s)}{U(s)} = \frac{\frac{1}{m}}{s^2+\frac{b}{m}s}, \end{align}\]
  1. Velocity versus input force:\

(3.38)#\[\begin{align} H_2(s)= \frac{V(s)}{U(s)} = \frac{\frac{1}{m}}{s+\frac{b}{m}}, \end{align}\]

We assume the parameter values for m and b are 1000 kg and 1000 N/(m/s):

m=1000 #kg,
b=1000 #N/(m/s).

The command H = TransferFunction(num,den) creates a continuous-time transfer function model. The main data members are ‘num’ and ‘den’, which are 2-D lists of arrays containing MIMO numerator and denominator coefficients.

H1 = signal.TransferFunction([1/m],[1,b/m,0]) #Position output transfer function
H2 = signal.TransferFunction([1/m],[1,b/m])  #Velocity output transfer function 

print('\n','H1=',H1,'\n','H2=',H2)
 H1= TransferFunctionContinuous(
array([0.001]),
array([1., 1., 0.]),
dt: None
) 
 H2= TransferFunctionContinuous(
array([0.001]),
array([1., 1.]),
dt: None
)

We create a time vector (t) with 1000 discrete time steps between 0 and 10 and a step input force of 6000 [N]

T = np.linspace(0, 10, 1000)
u = 6000*np.ones(1000)

We want to simulate the response of the H1 and H2 to the specified input (u) assuming zero initial values for both position and velocity. We will use the lsim function by the SciPy library :

T, yout, xout = signal.lsim(system, U, T, X0=None, interp=True)

Parameters:
  • system: an instance of the LTI class or a tuple describing the system.
  • u (array-like, optional) –

    An input array describing the input at each time T (interpolation is assumed between given times). If there are multiple inputs, then each column of the rank-2 array represents an input. If u = 0 or None, a zero input is used.

  • T (array-like) – The time steps at which the input is defined and at which the output is desired. Must be nonnegative, increasing, and equally spaced.
  • x0 (array-like, optional) – Initial condition (default = 0) where the number of states is equal to the number of poles in the transfer function.
  • interp (bool, optional) – Whether to use linear (True, the default) or zero-order-hold (False) interpolation for the input array.
Returns:

  • T (1D ndarray) – Time values of the output.
  • yout (1D ndarray) – Response of the system.
  • xout (ndarray) – Time evolution of the state vector.

y0=[0,0] # not used by lsim in case of TF!!! 
T1,yout1, vout1 = signal.lsim(H1, u, T, y0)
y0=[0] #not used by lsim in case of TF!!! 
T2,yout2, vout2 = signal.lsim(H2, u, T, y0)

Finally we plot the resulted simulated velocities (v) and positions (x) 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(T1, yout1, 'b', label='Car position') # 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='Car position [m]') # label the y-axis 
ax0.grid() # plot a grid 

ax1.plot(T2, yout2, 'b', label='Car speed') # 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='Car spped [m]') # label the y-axis 
ax1.grid() # plot a grid 

fig.tight_layout()
plt.show() # show the plot
../../../_images/200128358d4b63e1afedb91313e07b34543075ebe6042cad4257d57d1fdd2679.png

3.7.6. → Check out this animated example#