4.7. A simplified Cruise Control Model#

4.7.1. Question:#

Simulate the step response of the cruise control model equations using a state space model in Python.

4.7.2. Mathematical model:#

Simplified cruise control model for a car.

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

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 differential equation decribing the car motion \(\dot v + \frac{b}{m} v = \frac{u}{m}\) can be transformed to an ODE form:

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

we can convert the previous equation to a state space form with state \(\textbf X= \left [ \begin{array}{c} x \\ v \end{array}\right]\) and input \(\textbf U= [u]\) :

(4.41)#\[\begin{align} \dot{\textbf X} &= \left [ \begin{array}{cc} 0 &1 \\ 0 & - \frac{b}{m} \end{array} \right ] \textbf X + \left [ \begin{array}{c} 0 \\ \frac{1}{m} \end{array}\right] \textbf U,\\ &= A \textbf X + B \textbf U. \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).

Create a state space model from A and B with state \(\mathbf{X}= \left [ \begin{array}{c} x \\ v \end{array}\right] \) and output \(\mathbf{Y}= C \mathbf{X} + D \mathbf{U}= \left [ \begin{array}{c} x \\ v \end{array}\right] \)

A =np.matrix([[0,1],[0,-b/m]])
B =np.matrix([[0],[1/m]])
C = np.matrix([[1,0],[0,1]])
D = np.matrix([[0],[0]])
sys = signal.StateSpace(A,B,C,D)

TFs=signal.ss2tf(A,B,C,D)
print(TFs[0])
[[ 0.00000000e+00 -1.11022302e-16  1.00000000e-03]
 [ 0.00000000e+00  1.00000000e-03  0.00000000e+00]]

We create a time vector with 1000 discrete time steps between 0 and 10

T = np.linspace(0, 10, 1000)

For initial conditions, we assume the initial car position x(0) =0 and the intial velocity is v(0) = 0. The input force is a step function of 6000 N. Then the vector of initial conditions (y0) and the input value U are equal to

y0=[0,0]  #Initial position of 0 [m] and initial velocity of 0 [m/s]
U = 6000*np.ones(1000)  #Step input force [N]
T,yout,xout = signal.lsim(sys, U, T, y0)

Finally we plot the resulted simulated velocities (Vout) 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[:,0], '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(T, yout[:,1], '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/c19675a378eff44b741b6ae4ef20b01738715f111483aa838edea6ae2425414c.png

4.7.4. → Check out this animated example#