{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## A simplified Cruise Control Model\n", "\n", "### Question: \n", "\n", "Simulate the step response of the cruise control model equations using a transfer function model in Python.\n", "\n", "### Mathematical model:\n", "\n", " [Simplified cruise control model for a car.](Simplified_Cruise_control_model.ipynb)\n", "\n", "\n", "### Solution: Transfer function simulation: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numerical solution:\n", "\n", "First load the necessary libraries into Python:\n", "\n", "- 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.\n", "- **NumPy** is a python library used for working with arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Detailed solution: " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import psutil\n", "L = psutil.Process().parent().cmdline()\n", "if any(\"jupyter-notebook\" in string for string in L):\n", " %matplotlib notebook\n", "elif any(\"jupyter-labhub\" in string for string in L):\n", " %matplotlib widget\n", "else: \n", " %matplotlib inline\n", "\n", "from scipy import signal\n", "import numpy as np " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first order ODE model for the simplified cruise control model specified as [previously](Simplified_Cruise_control_model.ipynb#Solution),\n", "\n", "\\begin{eqnarray}\n", " \\dot x &=& v \\\\\n", " \\dot v&=& - \\frac{b}{m} v + \\frac{u}{m}\n", "\\end{eqnarray}\n", "\n", "can be transformed to the Laplace domain (**assuming that x(0)=0 and v(0)=0!**): \n", "\n", "\\begin{eqnarray}\n", "s X(s) &=& V(s) \\\\\n", "s V(s) &=& - \\frac{b}{m} V(s) + \\frac{U(s)}{m}.\n", "\\end{eqnarray}\n", "\n", "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:\n", "\n", "1. Position versus input force:\\\n", "\\begin{align}\n", "H_1(s)= \\frac{X(s)}{U(s)} = \\frac{\\frac{1}{m}}{s^2+\\frac{b}{m}s},\n", "\\end{align}\n", "2. Velocity versus input force:\\\n", "\\begin{align}\n", "H_2(s)= \\frac{V(s)}{U(s)} = \\frac{\\frac{1}{m}}{s+\\frac{b}{m}},\n", "\\end{align}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We assume the parameter values for m and b are 1000 kg and 1000 N/(m/s):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "m=1000 #kg,\n", "b=1000 #N/(m/s)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The command [H = TransferFunction(num,den)](https://python-control.readthedocs.io/en/0.8.3/generated/control.TransferFunction.html) 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. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " H1= TransferFunctionContinuous(\n", "array([0.001]),\n", "array([1., 1., 0.]),\n", "dt: None\n", ") \n", " H2= TransferFunctionContinuous(\n", "array([0.001]),\n", "array([1., 1.]),\n", "dt: None\n", ")\n" ] } ], "source": [ "H1 = signal.TransferFunction([1/m],[1,b/m,0]) #Position output transfer function\n", "H2 = signal.TransferFunction([1/m],[1,b/m]) #Velocity output transfer function \n", "\n", "print('\\n','H1=',H1,'\\n','H2=',H2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a time vector (t) with 1000 discrete time steps between 0 and 10 and a step input force of 6000 [N] " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "T = np.linspace(0, 10, 1000)\n", "u = 6000*np.ones(1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lsim.html?highlight=lsim#scipy.signal.lsim) function by the [SciPy library](https://docs.scipy.org/doc/scipy/reference/index.html) : \n", "\n", ">T, yout, xout = signal.lsim(system, U, T, X0=None, interp=True)\n", "\n", "
\n", "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.
\n", "