{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## A quarter car suspension\n", "\n", "### Question: \n", "\n", "Simulate the time response of the quarter car suspension model equations descibed below. \n", "\n", "### Mathematical model:\n", "\n", "[The quarter car model equations](http://localhost:8889/notebooks/2_Quarter_car_suspension/Quarter_car_suspension_model.ipynb)" ] }, { "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.\n", "\n", "\n", "\n", "### Solution: Transfer function model: \n", "\n", "#### Write set of ODE's as a state space model: \n", "\n", "We can rewrite the previously describe model as a set of Ordinary Differential Equations (ODE's) by substituting $\\dot x$ with $v_x$ and $\\dot y$ with $v_y$ \n", "\n", "\\begin{eqnarray}\n", "\\dot x&=& v_x \\\\ \n", "\\dot y&=& v_y \\\\\n", "\\dot v_x &=& \\frac{1}{m_1} \\left ( b(v_y-v_x) + k_s(y-x) - k_w(x-r) \\right ) \\\\\n", "\\dot v_y &=& \\frac{1}{m_2} \\left ( - k_s(y-x) -b (v_y - v_x) \\right )\n", "\\end{eqnarray}\n", "\n", "if we define a state vector $X = \\begin{bmatrix} x \\\\ y \\\\ v_y \\\\ v_y \\end{bmatrix}$ and $U = \\begin{bmatrix} r \\end{bmatrix}$ then we can rewrite the previous equation as follows\n", "\n", "\\begin{eqnarray}\n", "\\dot X &=& \\begin{bmatrix} 0&0&1&0 \\\\ 0&0&0&1\\\\ \\frac{-k_s-k_w}{m_1} & \\frac{k_s}{m_1} & \\frac{-b}{m_1} & \\frac{b}{m_1} \\\\ \n", "\\frac{k_s}{m_2} & \\frac{-k_s}{m_2} & \\frac{b}{m_2} & \\frac{-b}{m_2}\n", "\\end{bmatrix} X + \\begin{bmatrix}0\\\\0\\\\ \\frac{k_w}{m_1} \\\\ 0 \\end{bmatrix} U \\\\\n", "&=& A X + B U \\label{eq:Lin_model}\n", "\\end{eqnarray}\n", "\n", "with $A = \\begin{bmatrix} a^1_1& \\dots& a^1_4 \\\\ \\vdots & \\vdots &\\vdots \\\\ a^4_1& \\dots& a^4_4\n", "\\end{bmatrix} = \\begin{bmatrix} 0&0&1&0 \\\\ 0&0&0&1\\\\ \\frac{-k_s-k_w}{m_1} & \\frac{k_s}{m_1} & \\frac{-b}{m_1} & \\frac{b}{m_1} \\\\ \n", "\\frac{k_s}{m_2} & \\frac{-k_s}{m_2} & \\frac{b}{m_2} & \\frac{-b}{m_2}\n", "\\end{bmatrix}$ and $B = \\begin{bmatrix} b^1_1\\\\ \\vdots\\\\ b^4_1 \\end{bmatrix}= \\begin{bmatrix}0\\\\0\\\\ \\frac{k_w}{m_1} \\\\ 0 \\end{bmatrix}$ \n", "\n", "\n", "##### Determine the transfer function models from the state space model:\n", " \n", "Based on the equation above we can now specify a transfer function between a single input (in the example below we choose our only input U=r) and a single output. This is conceptually done by eliminating all other states from previous equation.\n", "\n", "First we convert the set of ordinary differential equations in \\ref{eq:Lin_model} to the Laplace domain resulting in following set of equations: \n", "\n", "\\begin{align}\n", "s X_1(s) &= a^1_1 X_1(s) + \\dots + a^1_4 X_n(s)+ b^1_1 U \\\\\n", " \\vdots &= \\vdots \\\\\n", "s X_4(s) &= a^4_1 X_1(s) + \\dots + a^4_4 X_n(s)+ b^4_1 U \\\\ \n", "\\end{align}\n", "\n", "Secondly we convert this set of equations to a matrix notation, \n", "\\begin{equation} \n", "s \\begin{bmatrix} 1,0,0,0 \\\\ 0,1,0,0 \\\\ 0,0,1,0 \\\\ 0,0,0,1 \\end{bmatrix} \\begin{bmatrix} X_1(s) \\\\ X_2(s) \\\\ X_3(s) \\\\ X_4(s) \\end{bmatrix} = \\begin{bmatrix} a^1_1& \\dots& a^1_4 \\\\ \\vdots & \\vdots &\\vdots \\\\ a^4_1& \\dots& a^4_4\n", "\\end{bmatrix} \\begin{bmatrix} X_1(s) \\\\ X_2(s) \\\\ X_3(s) \\\\ X_4(s) \\end{bmatrix} + \\begin{bmatrix} b^1_1& \\\\ \\vdots\\\\ b^4_1& \\end{bmatrix} U \n", "\\end{equation}\n", "We can rewrite this equation where \n", "\\begin{eqnarray}\n", "I_{4x4}&=& \\begin{bmatrix} 1,0,0,0 \\\\ 0,1,0,0 \\\\ 0,0,1,0 \\\\ 0,0,0,1 \\end{bmatrix} \\ \\ \\ A = \\begin{bmatrix} a^1_1& \\dots& a^1_4 \\\\ \\vdots & \\vdots &\\vdots \\\\ a^4_1& \\dots& a^4_4\n", "\\end{bmatrix}\\\\\n", "B &=& \\begin{bmatrix} b^1_1& \\\\ \\vdots\\\\ b^4_1& \\end{bmatrix} \\ \\ \\rm{and} \\ \\ X(s) = \\begin{bmatrix} X_1(s) \\\\ X_2(s) \\\\ X_3(s) \\\\ X_4(s) \\end{bmatrix}\n", "\\end{eqnarray}\n", "resulting in \n", "\\begin{equation}\n", "\\left [ s I -A \\right ] X(s) = B U(s) \n", "\\end{equation} \n", "\n", "We can now solve this set of equation using basic matrix operations: \n", "\n", "$$ \\begin{bmatrix} X_1(s) \\\\ X_2(s) \\\\ X_3(s) \\\\ X_4(s) \\end{bmatrix} = \\left ( sI - A \\right )^{-1} B U(s) $$\n", "\n", "and because U is single input signal we can divide the left part by U to obtain the different transfer functions \n", "\n", "$$ \\begin{bmatrix} \\frac{X_1(s)}{U(s)} \\\\ \\frac{X_2(s)}{U(s)} \\\\ \\frac{X_3(s)}{U(s)} \\\\ \\frac{X_4(s)}{U(s)} \\end{bmatrix} = \\left ( sI - A \\right )^{-1} B $$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Detailed 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.\n", "- SymPy is a Python library for symbolic mathematics.\n", "- CAS_lib contains custom made functions that are required by this course" ] }, { "cell_type": "code", "execution_count": 1, "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 \n", "from sympy import *\n", "from CAS_lib import minreal " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We assume following parameter values: $m=[m_1,m_2]^T$ with $m_1=20$ kg ,$m_2=375$ kg together with $b=9800$ N/(m/s) and the spring constants $K_w=1e6=1000000$, $K_s=1.3*e5=130000$:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "m1=20 #kg\n", "m2=375 #kg \n", "m = [m1,m2]\n", "b=9800 #N/(m/s)\n", "kw=1e6 #N/m\n", "ks=1.3e5 #N/m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Equation (1) derived above specifies a linear relation $\\dot X = AX+BU$ with $A = \\begin{bmatrix} 0&0&1&0 \\\\ 0&0&0&1\\\\ \\frac{-k_s-k_w}{m_1} & \\frac{k_s}{m_1} & \\frac{-b}{m_1} & \\frac{b}{m_1} \\\\ \n", "\\frac{k_s}{m_2} & \\frac{-k_s}{m_2} & \\frac{b}{m_2} & \\frac{-b}{m_2}\n", "\\end{bmatrix}$ and $B =\\begin{bmatrix}0\\\\0\\\\ \\frac{k_w}{m_1} \\\\ 0 \\end{bmatrix}$ " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "A = Matrix([[0,0,1,0 ], [0,0,0,1],[(-ks-kw)/m1, ks/m1, -b/m1, b/m1],[ks/m2,-ks/m2,b/m2,-b/m2]])\n", "B = Matrix([[0],[0],[kw/m1],[0]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create different transfer function models based on the previous result ($\\dot X = A X + BU$) with as input r(t) \n", "using equation (1) we can derive a transfer function if we assume $x(0) = y(0) =v_x(0) = v_y(0) =0$ using the following set of equations: \n", "\n", "\\begin{align}\n", "s X(s) &= a^1_1 X(s) + a^1_2 Y(s) + a^1_3 V_x(s) + a^1_4 V_y(s) + b^1_1 R(s) \\\\\n", "s Y(s) &= a^2_1 X(s) + a^2_2 Y(s) + a^2_3 V_x(s) + a^2_4 V_y(s) + b^2_1 R(s) \\\\\n", "s V_x(s) &= a^3_1 X(s) + a^3_2 Y(s) + a^3_3 V_x(s) + a^3_4 V_y(s) + b^3_1 R(s) \\\\\n", "s V_y(s) &= a^4_1 X(s) + a^4_2 Y(s) + a^4_3 V_x(s) + a^4_4 V_y(s) + b^4_1 R(s) \\\n", "\\end{align} \n", "\n", "We can now solve this set of equation using matrix operations: \n", "\n", "$$\\begin{bmatrix} \\frac{X(s)}{R(s)} \\\\ \\frac{Y(s)}{R(s)} \\\\ \\frac{V_x(s)}{R(s)} \\\\ \\frac{V_y(s)}{R(s)} \n", "\\end{bmatrix} = \\left ( \\begin{bmatrix} s & 0 & 0 &0 \\\\ 0 & s & 0 &0 \\\\ 0 & 0 & s &0 \\\\ 0&0&0&s \\end{bmatrix} - \\begin{bmatrix} a^1_1 & a^1_2 & a^1_3 &a^1_4 \\\\ a^2_1 & a^2_2 & a^2_3 &a^2_4 \\\\ a^3_1 & a^3_2 & a^3_3& a^3_4 \\\\ a^4_1 & a^4_2 & a^4_3& a^4_4 \\end{bmatrix} \\right )^{-1} \\begin{bmatrix} b^1_1 \\\\ b^2_1 \\\\ b^3_1\\\\ b^4_1 \\end{bmatrix}$$ " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "\n", "s = symbols('s')\n", "TFs =(s*eye(4)-A).inv()*B\n", "\n", "n, d = TFs[0].as_numer_denom() # Take the first element in the list: X(s)/U(s)\n", "num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s \n", "den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s\n", "num = [float(item) for item in num] #convert all elements in the list to floating points\n", "den = [float(item) for item in den] #convert all elements in the list to floating points\n", "H1 = signal.TransferFunction(num,den) #Create the transfer function\n", "H1=minreal(H1) #Minimal realization or pole-zero cancellation\n", "\n", "\n", "n, d = TFs[1].as_numer_denom() # Take the second element in the list: Y(s)/U(s)\n", "num = Poly(n).all_coeffs() #collect the coefficients of the polynomial in s \n", "den = Poly(d).all_coeffs() #collect the coefficients of the polynomial in s\n", "num = [float(item) for item in num] #convert all elements in the list to floating points\n", "den = [float(item) for item in den] #convert all elements in the list to floating points\n", "H2 = signal.TransferFunction(num,den) #Create the transfer function\n", "H2=minreal(H2) #Minimal realization or pole-zero cancellation\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate the response to a step input of 0.2 after 1 second: " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "T=np.linspace(0,2,100)\n", "y0=[0,0,0,0] #Initial values for the states \n", "\n", "a= 0*np.ones(49) # 1 second zero values for input\n", "b= 0.2*np.ones(51) # 1 second 0.2 values for input \n", "U = np.concatenate((a, b), axis=0) # combine both vectors a and b " ] }, { "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", "