{
"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",
"\n",
"[The quarter car model equations](http://localhost:8889/notebooks/2_Quarter_car_suspension/Quarter_car_suspension_model.ipynb)\n",
"\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution: Transfer function model: \n",
"\n",
"#### Generic solution: \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",
"##### Determine the state space model:\n",
"\n",
"The state space model is specified as:\n",
"\n",
"\\begin{align}\n",
"\\dot x = A x + B u \\\\\n",
"y = C x +D u \n",
"\\end{align} \n",
"\n",
"This equation 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. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Detailed solution:\n",
"\n",
"First load the necessary libraries into Python: \n",
"\n",
" - The Python Control Systems Library (python-control) is a Python package that implements basic operations for analysis and design of feedback control systems.\n",
" - NumPy library is a python library used for working with arrays.\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."
]
},
{
"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 "
]
},
{
"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": "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": 2,
"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": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"A = [[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 = [[0],[0],[kw/m1],[0]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a state space model from A and B with the velocity $v_x$ as an output"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"C = np.eye(4)\n",
"D = [[0],[0],[0],[0]]\n",
"sys = signal.StateSpace(A,B,C,D)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the time vector (T) and the input vector (U); "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"T=np.linspace(0,2,100)\n",
"y0=[0,0,0,0] #Initial values for the \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 state space model sys given 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",
"
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", "