2021-04-13 18:30:08 +08:00
{
"cells": [
{
"cell_type": "code",
2024-10-23 17:34:26 +08:00
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-23T09:12:50.245778Z",
"start_time": "2024-10-23T09:12:50.236084Z"
}
},
2021-04-13 18:30:08 +08:00
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.integrate import odeint\n",
"from scipy.interpolate import interp1d\n",
"import cvxpy as cp\n",
"\n",
"import matplotlib.pyplot as plt\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
"plt.style.use(\"ggplot\")\n",
"\n",
"import time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-10-23 17:34:26 +08:00
"## V2 Use Dynamics w.r.t Robot Frame 使用相对于机器人坐标系的动力学\n",
2021-04-13 18:30:08 +08:00
"\n",
"explanation here...\n",
"\n",
"benefits:\n",
2024-10-23 17:34:26 +08:00
"* slightly faster mpc convergence time -> more variables are 0, this helps the computation? 能够更快的收敛, 因为更多的变量是0, 这有助于计算? \n",
"* no issues when vehicle heading ~PI 世界坐标系中车辆朝向~PI时没有问题, 不需要归一化角度"
2021-04-13 18:30:08 +08:00
]
},
{
"cell_type": "code",
2024-10-23 17:34:26 +08:00
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-23T09:12:50.267761Z",
"start_time": "2024-10-23T09:12:50.257183Z"
}
},
2021-04-13 18:30:08 +08:00
"outputs": [],
"source": [
"\"\"\"\n",
"Control problem statement.\n",
"\"\"\"\n",
"\n",
2022-08-02 16:33:49 +08:00
"N = 4 # number of state variables\n",
"M = 2 # number of control variables\n",
"T = 20 # Prediction Horizon\n",
"DT = 0.2 # discretization step\n",
"\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
"def get_linear_model(x_bar, u_bar):\n",
2021-04-13 18:30:08 +08:00
" \"\"\"\n",
" Computes the LTI approximated state space model x' = Ax + Bu + C\n",
" \"\"\"\n",
2022-08-02 16:33:49 +08:00
"\n",
" L = 0.3 # vehicle wheelbase\n",
"\n",
2021-04-13 18:30:08 +08:00
" x = x_bar[0]\n",
" y = x_bar[1]\n",
" v = x_bar[2]\n",
" theta = x_bar[3]\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
" a = u_bar[0]\n",
" delta = u_bar[1]\n",
2022-08-02 16:33:49 +08:00
"\n",
" A = np.zeros((N, N))\n",
" A[0, 2] = np.cos(theta)\n",
" A[0, 3] = -v * np.sin(theta)\n",
" A[1, 2] = np.sin(theta)\n",
" A[1, 3] = v * np.cos(theta)\n",
" A[3, 2] = v * np.tan(delta) / L\n",
" A_lin = np.eye(N) + DT * A\n",
"\n",
" B = np.zeros((N, M))\n",
" B[2, 0] = 1\n",
" B[3, 1] = v / (L * np.cos(delta) ** 2)\n",
" B_lin = DT * B\n",
"\n",
" f_xu = np.array(\n",
" [v * np.cos(theta), v * np.sin(theta), a, v * np.tan(delta) / L]\n",
" ).reshape(N, 1)\n",
" C_lin = DT * (\n",
" f_xu - np.dot(A, x_bar.reshape(N, 1)) - np.dot(B, u_bar.reshape(M, 1))\n",
" )\n",
"\n",
" return np.round(A_lin, 4), np.round(B_lin, 4), np.round(C_lin, 4)\n",
"\n",
2021-04-13 18:30:08 +08:00
"\n",
"\"\"\"\n",
"the ODE is used to update the simulation given the mpc results\n",
"I use this insted of using the LTI twice\n",
"\"\"\"\n",
2022-08-02 16:33:49 +08:00
"\n",
"\n",
"def kinematics_model(x, t, u):\n",
2021-04-13 18:30:08 +08:00
" \"\"\"\n",
" Returns the set of ODE of the vehicle model.\n",
" \"\"\"\n",
2022-08-02 16:33:49 +08:00
"\n",
" L = 0.3 # vehicle wheelbase\n",
" dxdt = x[2] * np.cos(x[3])\n",
" dydt = x[2] * np.sin(x[3])\n",
2021-04-13 18:30:08 +08:00
" dvdt = u[0]\n",
2022-08-02 16:33:49 +08:00
" dthetadt = x[2] * np.tan(u[1]) / L\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
" dqdt = [dxdt, dydt, dvdt, dthetadt]\n",
2021-04-13 18:30:08 +08:00
"\n",
" return dqdt\n",
"\n",
2022-08-02 16:33:49 +08:00
"\n",
"def predict(x0, u):\n",
" \"\"\" \"\"\"\n",
"\n",
" x_ = np.zeros((N, T + 1))\n",
"\n",
" x_[:, 0] = x0\n",
"\n",
2021-04-13 18:30:08 +08:00
" # solve ODE\n",
2022-08-02 16:33:49 +08:00
" for t in range(1, T + 1):\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
" tspan = [0, DT]\n",
" x_next = odeint(kinematics_model, x0, tspan, args=(u[:, t - 1],))\n",
2021-04-13 18:30:08 +08:00
"\n",
" x0 = x_next[1]\n",
2022-08-02 16:33:49 +08:00
" x_[:, t] = x_next[1]\n",
"\n",
2021-04-13 18:30:08 +08:00
" return x_\n",
"\n",
"\n",
"\"\"\"\n",
"MODIFIED TO INCLUDE FRAME TRANSFORMATION\n",
"\"\"\"\n",
2022-08-02 16:33:49 +08:00
"\n",
"\n",
"def compute_path_from_wp(start_xp, start_yp, step=0.1):\n",
2021-04-13 18:30:08 +08:00
" \"\"\"\n",
" Computes a reference path given a set of waypoints\n",
" \"\"\"\n",
2022-08-02 16:33:49 +08:00
"\n",
" final_xp = []\n",
" final_yp = []\n",
" delta = step # [m]\n",
"\n",
" for idx in range(len(start_xp) - 1):\n",
" section_len = np.sum(\n",
" np.sqrt(\n",
" np.power(np.diff(start_xp[idx : idx + 2]), 2)\n",
" + np.power(np.diff(start_yp[idx : idx + 2]), 2)\n",
" )\n",
" )\n",
"\n",
" interp_range = np.linspace(0, 1, np.floor(section_len / delta).astype(int))\n",
"\n",
" fx = interp1d(np.linspace(0, 1, 2), start_xp[idx : idx + 2], kind=1)\n",
" fy = interp1d(np.linspace(0, 1, 2), start_yp[idx : idx + 2], kind=1)\n",
"\n",
2021-04-13 18:30:08 +08:00
" # watch out to duplicate points!\n",
2022-08-02 16:33:49 +08:00
" final_xp = np.append(final_xp, fx(interp_range)[1:])\n",
" final_yp = np.append(final_yp, fy(interp_range)[1:])\n",
"\n",
2021-04-13 18:30:08 +08:00
" dx = np.append(0, np.diff(final_xp))\n",
" dy = np.append(0, np.diff(final_yp))\n",
" theta = np.arctan2(dy, dx)\n",
"\n",
2022-08-02 16:33:49 +08:00
" return np.vstack((final_xp, final_yp, theta))\n",
2021-04-13 18:30:08 +08:00
"\n",
"\n",
2022-08-02 16:33:49 +08:00
"def get_nn_idx(state, path):\n",
2021-04-13 18:30:08 +08:00
" \"\"\"\n",
" Computes the index of the waypoint closest to vehicle\n",
" \"\"\"\n",
"\n",
2022-08-02 16:33:49 +08:00
" dx = state[0] - path[0, :]\n",
" dy = state[1] - path[1, :]\n",
" dist = np.hypot(dx, dy)\n",
2021-04-13 18:30:08 +08:00
" nn_idx = np.argmin(dist)\n",
"\n",
" try:\n",
2022-08-02 16:33:49 +08:00
" v = [\n",
" path[0, nn_idx + 1] - path[0, nn_idx],\n",
" path[1, nn_idx + 1] - path[1, nn_idx],\n",
" ]\n",
2021-04-13 18:30:08 +08:00
" v /= np.linalg.norm(v)\n",
"\n",
2022-08-02 16:33:49 +08:00
" d = [path[0, nn_idx] - state[0], path[1, nn_idx] - state[1]]\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
" if np.dot(d, v) > 0:\n",
2021-04-13 18:30:08 +08:00
" target_idx = nn_idx\n",
" else:\n",
2022-08-02 16:33:49 +08:00
" target_idx = nn_idx + 1\n",
2021-04-13 18:30:08 +08:00
"\n",
" except IndexError as e:\n",
" target_idx = nn_idx\n",
"\n",
" return target_idx\n",
"\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
"def normalize_angle(angle):\n",
" \"\"\"\n",
" Normalize an angle to [-pi, pi]\n",
" \"\"\"\n",
" while angle > np.pi:\n",
" angle -= 2.0 * np.pi\n",
"\n",
" while angle < -np.pi:\n",
" angle += 2.0 * np.pi\n",
"\n",
" return angle\n",
"\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
"def get_ref_trajectory(state, path, target_v):\n",
" \"\"\"\n",
" modified reference in robot frame\n",
" \"\"\"\n",
" xref = np.zeros((N, T + 1))\n",
" dref = np.zeros((1, T + 1))\n",
2022-08-02 16:33:49 +08:00
"\n",
" # sp = np.ones((1,T +1))*target_v #speed profile\n",
"\n",
2021-04-13 18:30:08 +08:00
" ncourse = path.shape[1]\n",
"\n",
" ind = get_nn_idx(state, path)\n",
2022-08-02 16:33:49 +08:00
" dx = path[0, ind] - state[0]\n",
" dy = path[1, ind] - state[1]\n",
"\n",
" xref[0, 0] = dx * np.cos(-state[3]) - dy * np.sin(-state[3]) # X\n",
" xref[1, 0] = dy * np.cos(-state[3]) + dx * np.sin(-state[3]) # Y\n",
" xref[2, 0] = target_v # V\n",
" xref[3, 0] = normalize_angle(path[2, ind] - state[3]) # Theta\n",
" dref[0, 0] = 0.0 # steer operational point should be 0\n",
"\n",
" dl = 0.05 # Waypoints spacing [m]\n",
2021-04-13 18:30:08 +08:00
" travel = 0.0\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
" for i in range(T + 1):\n",
2022-08-02 16:33:49 +08:00
" travel += abs(target_v) * DT # current V or target V?\n",
2021-04-13 18:30:08 +08:00
" dind = int(round(travel / dl))\n",
2022-08-02 16:33:49 +08:00
"\n",
2021-04-13 18:30:08 +08:00
" if (ind + dind) < ncourse:\n",
2022-08-02 16:33:49 +08:00
" dx = path[0, ind + dind] - state[0]\n",
" dy = path[1, ind + dind] - state[1]\n",
"\n",
2021-04-13 18:30:08 +08:00
" xref[0, i] = dx * np.cos(-state[3]) - dy * np.sin(-state[3])\n",
" xref[1, i] = dy * np.cos(-state[3]) + dx * np.sin(-state[3])\n",
2022-08-02 16:33:49 +08:00
" xref[2, i] = target_v # sp[ind + dind]\n",
" xref[3, i] = normalize_angle(path[2, ind + dind] - state[3])\n",
2021-04-13 18:30:08 +08:00
" dref[0, i] = 0.0\n",
" else:\n",
2022-08-02 16:33:49 +08:00
" dx = path[0, ncourse - 1] - state[0]\n",
" dy = path[1, ncourse - 1] - state[1]\n",
"\n",
2021-04-13 18:30:08 +08:00
" xref[0, i] = dx * np.cos(-state[3]) - dy * np.sin(-state[3])\n",
" xref[1, i] = dy * np.cos(-state[3]) + dx * np.sin(-state[3])\n",
2022-08-02 16:33:49 +08:00
" xref[2, i] = 0.0 # stop? #sp[ncourse - 1]\n",
" xref[3, i] = normalize_angle(path[2, ncourse - 1] - state[3])\n",
2021-04-13 18:30:08 +08:00
" dref[0, i] = 0.0\n",
"\n",
" return xref, dref"
]
},
{
"cell_type": "code",
2024-10-23 17:34:26 +08:00
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-23T09:13:03.353438Z",
"start_time": "2024-10-23T09:12:50.259738Z"
}
},
2021-04-13 18:30:08 +08:00
"outputs": [
2024-10-23 17:34:26 +08:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:27: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" A[0, 2] = np.cos(theta)\n",
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:28: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" A[0, 3] = -v * np.sin(theta)\n",
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:29: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" A[1, 2] = np.sin(theta)\n",
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:30: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" A[1, 3] = v * np.cos(theta)\n",
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:31: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" A[3, 2] = v * np.tan(delta) / L\n",
"/var/folders/hd/8kg_jtmd6svgg_sc384pbcdm0000gn/T/ipykernel_23553/4187762036.py:36: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)\n",
" B[3, 1] = v / (L * np.cos(delta) ** 2)\n"
]
},
2021-04-13 18:30:08 +08:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-10-23 17:34:26 +08:00
"CVXPY Optimization Time: Avrg: 0.0657s Max: 0.1233s Min: 0.0531s\n"
2021-04-13 18:30:08 +08:00
]
}
],
"source": [
2022-08-02 16:33:49 +08:00
"track = compute_path_from_wp(\n",
" [0, 3, 4, 6, 10, 12, 14, 6, 1, 0], [0, 0, 2, 4, 3, 3, -2, -6, -2, -2], 0.05\n",
")\n",
2021-04-13 18:30:08 +08:00
"\n",
"# track = compute_path_from_wp([0,10,10,0],\n",
"# [0,0,1,1],0.05)\n",
"\n",
2022-08-02 16:33:49 +08:00
"sim_duration = 200 # time steps\n",
"opt_time = []\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
"x_sim = np.zeros((N, sim_duration))\n",
"u_sim = np.zeros((M, sim_duration - 1))\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
"MAX_SPEED = 1.5 # m/s\n",
"MAX_ACC = 1.0 # m/ss\n",
"MAX_D_ACC = 1.0 # m/sss\n",
"MAX_STEER = np.radians(30) # rad\n",
"MAX_D_STEER = np.radians(30) # rad/s\n",
2021-04-13 18:30:08 +08:00
"\n",
2022-08-02 16:33:49 +08:00
"REF_VEL = 1.0 # m/s\n",
2021-04-13 18:30:08 +08:00
"\n",
"# Starting Condition\n",
"x0 = np.zeros(N)\n",
2022-08-02 16:33:49 +08:00
"x0[0] = 0 # x\n",
"x0[1] = -0.25 # y\n",
"x0[2] = 0.0 # v\n",
"x0[3] = np.radians(-0) # yaw\n",
"\n",
"# starting guess\n",
"u_bar = np.zeros((M, T))\n",
"u_bar[0, :] = MAX_ACC / 2 # a\n",
"u_bar[1, :] = 0.0 # delta\n",
"\n",
"for sim_time in range(sim_duration - 1):\n",
"\n",
2021-04-13 18:30:08 +08:00
" iter_start = time.time()\n",
2022-08-02 16:33:49 +08:00
"\n",
" # dynamics starting state w.r.t. robot are always null except vel\n",
" x_bar = np.zeros((N, T + 1))\n",
" x_bar[2, 0] = x_sim[2, sim_time]\n",
"\n",
" # prediction for linearization of costrains\n",
" for t in range(1, T + 1):\n",
" xt = x_bar[:, t - 1].reshape(N, 1)\n",
" ut = u_bar[:, t - 1].reshape(M, 1)\n",
" A, B, C = get_linear_model(xt, ut)\n",
" xt_plus_one = np.squeeze(np.dot(A, xt) + np.dot(B, ut) + C)\n",
" x_bar[:, t] = xt_plus_one\n",
"\n",
" # CVXPY Linear MPC problem statement\n",
" x = cp.Variable((N, T + 1))\n",
2021-04-13 18:30:08 +08:00
" u = cp.Variable((M, T))\n",
" cost = 0\n",
" constr = []\n",
"\n",
" # Cost Matrices\n",
2022-08-02 16:33:49 +08:00
" Q = np.diag([20, 20, 10, 20]) # state error cost\n",
" Qf = np.diag([30, 30, 30, 30]) # state final error cost\n",
" R = np.diag([10, 10]) # input cost\n",
" R_ = np.diag([10, 10]) # input rate of change cost\n",
"\n",
" # Get Reference_traj\n",
" # dont use x0 in this case\n",
" x_ref, d_ref = get_ref_trajectory(x_sim[:, sim_time], track, REF_VEL)\n",
"\n",
" # Prediction Horizon\n",
2021-04-13 18:30:08 +08:00
" for t in range(T):\n",
"\n",
" # Tracking Error\n",
2022-08-02 16:33:49 +08:00
" cost += cp.quad_form(x[:, t] - x_ref[:, t], Q)\n",
2021-04-13 18:30:08 +08:00
"\n",
" # Actuation effort\n",
2022-08-02 16:33:49 +08:00
" cost += cp.quad_form(u[:, t], R)\n",
2021-04-13 18:30:08 +08:00
"\n",
" # Actuation rate of change\n",
" if t < (T - 1):\n",
2022-08-02 16:33:49 +08:00
" cost += cp.quad_form(u[:, t + 1] - u[:, t], R_)\n",
" constr += [\n",
" cp.abs(u[0, t + 1] - u[0, t]) / DT <= MAX_D_ACC\n",
" ] # max acc rate of change\n",
" constr += [\n",
" cp.abs(u[1, t + 1] - u[1, t]) / DT <= MAX_D_STEER\n",
" ] # max steer rate of change\n",
2021-04-13 18:30:08 +08:00
"\n",
" # Kinrmatics Constrains (Linearized model)\n",
2022-08-02 16:33:49 +08:00
" A, B, C = get_linear_model(x_bar[:, t], u_bar[:, t])\n",
" constr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t] + C.flatten()]\n",
"\n",
" # Final Point tracking\n",
2021-04-13 18:30:08 +08:00
" cost += cp.quad_form(x[:, T] - x_ref[:, T], Qf)\n",
"\n",
" # sums problem objectives and concatenates constraints.\n",
2022-08-02 16:33:49 +08:00
" constr += [x[:, 0] == x_bar[:, 0]] # starting condition\n",
" constr += [x[2, :] <= MAX_SPEED] # max speed\n",
" constr += [x[2, :] >= 0.0] # min_speed (not really needed)\n",
" constr += [cp.abs(u[0, :]) <= MAX_ACC] # max acc\n",
" constr += [cp.abs(u[1, :]) <= MAX_STEER] # max steer\n",
"\n",
2021-04-13 18:30:08 +08:00
" # Solve\n",
" prob = cp.Problem(cp.Minimize(cost), constr)\n",
" solution = prob.solve(solver=cp.OSQP, verbose=False)\n",
2022-08-02 16:33:49 +08:00
"\n",
" # retrieved optimized U and assign to u_bar to linearize in next step\n",
" u_bar = np.vstack(\n",
" (np.array(u.value[0, :]).flatten(), (np.array(u.value[1, :]).flatten()))\n",
" )\n",
"\n",
" u_sim[:, sim_time] = u_bar[:, 0]\n",
"\n",
2021-04-13 18:30:08 +08:00
" # Measure elpased time to get results from cvxpy\n",
2022-08-02 16:33:49 +08:00
" opt_time.append(time.time() - iter_start)\n",
"\n",
2021-04-13 18:30:08 +08:00
" # move simulation to t+1\n",
2022-08-02 16:33:49 +08:00
" tspan = [0, DT]\n",
" x_sim[:, sim_time + 1] = odeint(\n",
" kinematics_model, x_sim[:, sim_time], tspan, args=(u_bar[:, 0],)\n",
" )[1]\n",
"\n",
"print(\n",
" \"CVXPY Optimization Time: Avrg: {:.4f}s Max: {:.4f}s Min: {:.4f}s\".format(\n",
" np.mean(opt_time), np.max(opt_time), np.min(opt_time)\n",
" )\n",
")"
2021-04-13 18:30:08 +08:00
]
},
{
"cell_type": "code",
2024-10-23 17:34:26 +08:00
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-23T09:13:27.832869Z",
"start_time": "2024-10-23T09:13:27.612748Z"
}
},
2021-04-13 18:30:08 +08:00
"outputs": [
{
"data": {
2024-10-23 17:34:26 +08:00
"text/plain": "<Figure size 1500x1000 with 5 Axes>",
"image/png": "iVBORw0KGgoAAAANSUhEUgAABdEAAAPeCAYAAADj01PlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd3hUZdrH8e+ZzKRX0iGUBJIQSiCoCOIqdo0V17WByiquroqw2Nv6qmsBNRZ0XV1dEQFFsawliriKBRSsEAQhobc0SA8pkznvH5MMGUhCgCSThN/nurhmzjnPOed+yIiTe+65H8M0TRMREREREREREREREdmPxdMBiIiIiIiIiIiIiIh0Vkqii4iIiIiIiIiIiIg0Q0l0EREREREREREREZFmKIkuIiIiIiIiIiIiItIMJdFFRERERERERERERJqhJLqIiIiIiIiIiIiISDOURBcRERERERERERERaYaS6CIiIiIiIiIiIiIizVASXURERERERERERESkGUqii4iIiIiIiIiIiIg0w+rpADylqKgIu93u6TDaRGRkJAUFBZ4OQ7oovX7kcOk1JIdDrx85XHoNyeHSa0gOh14/cri6y2vIarUSFhbm6TC6tNbkqbrL60Xz6Hy6y1xamsfh/jt1xCbR7XY7tbW1ng7jsBmGATjnY5qmh6ORrkavHzlceg3J4dDrRw6XXkNyuPQaksOh148cLr2GpLED5am6y+tF8+h8ustc2nseauciIiIiIiIiIiIiItIMJdFFRERERERERERERJpxxLZzERERERERERGR7m/16tV88MEHbNy4kaKiIm699VZGjhx5wHNee+01tm3bRlhYGOeddx6nn36625jvv/+e+fPnk5eXR3R0NJdddtkBrysiXZMq0UVEREREREREpNuqrq6mX79+XH311a0an5+fz6OPPkpKSgrTp09n3LhxvPrqq3z//feuMevWrePpp5/mhBNO4PHHH+eEE07gqaeeIjs7u72mISIepEp0ERERERERERHpttLS0khLS2v1+M8++4yIiAgmTpwIQFxcHOvXr+fDDz9k1KhRAHz88cekpqYybtw4AMaNG8fq1av5+OOPmTp1altPQUQ8TEl0ERERERERERGRetnZ2aSmprrtGz58OF9++SV2ux2r1cq6des4++yz3cYMGzaMzMzMFq9dW1tLbW2ta9swDPz8/FzPm+L47H3qvl7ITqsXdi8rXpdfj9F/4KFMzeMa5tjcXLuK7jIP6D5zae95KIkuIiIiIiIiIiJSr7i4mJCQELd9ISEh1NXVUVZWRlhYGMXFxYSGhrqNCQ0Npbi4uMVrv/feeyxYsMC1HR8fz/Tp04mMjGw+HhyU5W7DXr/t+8tSehx/0sFMqdOJiYnxdAhtorvMA7rPXNprHkqii4iIiIiIiIiINLJvNatpmk3u33fMgapgx40bxznnnLPffQoKCrDb7U2eY6Ydh1e/ZPyzV1H23hwq83Kp3rmzVfPobAzDICYmhtzcXNffaVfUXeYB3WcuB5qH1Wpt8cOqA1ESXUREREREREREpF5TFeWlpaV4eXkRGBjY7JiSkpL9Ktj3ZbPZsNlsTR5rNoEZEY0RGYO3pX5ceUmXTnaCc65dfQ7QfeYB3Wcu7TUPS5tfUUREREREREREpItKTExk5cqVbvtWrFhBQkICVquzHjUpKYmsrCy3MStXriQpKand4rIEhzqflJe12z1EpGlKoouIiIiIiIiISLdVVVXFpk2b2LRpEwD5+fls2rSJwsJCAObNm8dzzz3nGn/66adTWFjIa6+9xrZt2/jiiy/44osvOPfcc11j0tPTWbFiBe+//z7bt2/n/fffJysra7/FRtuSJSTU+aSspN3uISJNUzsXERERERERERHpttavX88DDzzg2p49ezYAJ554IjfeeCNFRUWuhDpAVFQUd911F6+99hoLFy4kLCyMP//5z4waNco1Jjk5malTp/Lmm28yf/58YmJimDp1KomJie02D6+GSvSKckxHHYbFq93uJSLulEQXEREREREREZFua/Dgwbz11lvNHr/xxhv32zdo0CCmT5/e4nVHjRrlllhvb652LqYDKiogKLjD7i1ypFM7FxERERERERERkU7OsFrBP8C5UV7q2WBEjjBKoouIiIiIiIiIiHQFgfXV50qii3QoJdFFRERERERERES6gqAQ56MWFxXpUEqii4iIiIiIiIiIdAFGfSW6qUp0kQ6lJLqIiIiIiIiIiEhXoHYuIh6hJLqIiIiIiIiIiEhX4GrnoiS6SEdSEl1ERERERERERKQLMHx8nU9qqjwbiMgRRkl0ERERERERERGRrsBqcz7W1no2DpEjjJLoIiIiIiIiIiIiXYGtPoluVxJdpCMpiS4iIiIiIiIiItIV1Feim0qii3QoJdFFRERERERERES6Alclut2zcYgcYZREFxERERERERER6Qqsauci4glKoouIiIiIiIiIiHQFroVFazwbh8gRRkl0ERERERERERGRrqChnUutKtFFOpKS6CIiIiIiIiIiIl2B2rmIeISS6CIiIiIiIiIiIl2AYdXCoiKeoCS6iIiIiIiIiIhIV6BKdBGPUBJdRERERERERESkK7ApiS7iCVZPByAiIiIiIiIiItLeFi5cyAcffEBxcTFxcXFMnDiRlJSUJsc+//zzfPXVV/vtj4uLIyMjA4DFixfzz3/+c78xc+bMwdvbu22Db2CtT+VpYVGRDqUkuoiIiIiIiIiIdGtLly5l1qxZTJo0ieTkZD7//HMeeeQRnnrqKSIiIvYb/+c//5nx48e7tuvq6rjtttsYNWqU2zg/Pz+eeeYZt33tlkAHtXMR8RC1cxERERERERERkW7to48+4uSTT+aUU05xVaFHRETw2WefNTne39+f0NBQ15/169dTUVHBSSed5DbOMAy3caGhoe07EbVzEfEIVaKLiIiIiIiIiEi3Zbfb2bBhAxdccIHb/tTUVNauXduqa3zxxRcMHTqUyMhIt/1VVVXccMMNOBwO+vXrxyWXXEJ8fHxbhb6/hkp0hwOzrg7Dy6v97iUiLkqii4iIiIiIiIhIt1VaWorD4SAkJMRtf0hICMXFxQc8v6ioiF9//ZWbb77ZbX/Pnj254YYb6NOnD3v27CEzM5P77ruPxx9/nNjY2CavVVtbS22jfuaGYeDn5+d63pyGY4a3z959dXYMa9dK7bnm0cJcu4LuMg/oPnNp73l0rf/SREREREREREREDkFTybXWJNwWL15MQEAAI0eOdNuflJREUlKSazs5OZk77riDTz75hKuvvrrJa7333nssWLDAtR0fH8/06dP3q3BvTkxcHNvqn0eH98ArKKTF8Z1VTEyMp0NoE91lHtB95tJe81ASXUREREREREREuq3g4GAsFst+VeclJSX7VafvyzRNvvzyS/7whz9gPUDVt8VioX///uTm5jY7Zty4cZxzzjmu7YYkfkFBAXa7vdnzDMMgJiaGvIJCMCxgOsjbtg0jtLLFmDqbhnnk5uZimqanwzlk3WUe0H3mcqB5WK3WVn9Y1RQl0UVEREREREREpNuyWq0kJCSwcuVKt2rylStXcswxx7R47urVq8nNzeXkk08+4H1M02Tz5s307t272TE2mw1bw+KgTZzfmntgs0JNDWZtDXTRpKdpml06Ydugu8wDus9c2mseSqKLiIiIiIiIiEi3ds455zBz5kwSEhJISkri888/p7CwkNNOOw2AefPmsXv3bm666Sa387744gsSExPp06fPftd8++23SUxMJDY21tUTfdOmTVxzzTXtOxmrDWpqwF574LEi0iaURBcRERERERERkW7tuOOOo6ysjHfeeYeioiJ69+7NXXfd5WrvUFRURGFhods5lZWVLFu2jIkTJzZ5zYqKCl566SWKi4vx9/cnPj6eBx54gAEDBrTvZKz1lexKoot0GCXRRURERERERESk2zvjjDM444wzmjx244037rfP39+fOXPmNHu9iRMnNptgb1cNSfTa5nuoi0jbsng6ABEREREREREREWklVaKLdDgl0UVERERERERERLoKm5L
2021-04-13 18:30:08 +08:00
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-08-02 16:33:49 +08:00
"# plot trajectory\n",
2021-04-13 18:30:08 +08:00
"grid = plt.GridSpec(4, 5)\n",
"\n",
2022-08-02 16:33:49 +08:00
"plt.figure(figsize=(15, 10))\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.subplot(grid[0:4, 0:4])\n",
2022-08-02 16:33:49 +08:00
"plt.plot(track[0, :], track[1, :], \"b+\")\n",
"plt.plot(x_sim[0, :], x_sim[1, :])\n",
2021-04-13 18:30:08 +08:00
"plt.axis(\"equal\")\n",
2022-08-02 16:33:49 +08:00
"plt.ylabel(\"y\")\n",
"plt.xlabel(\"x\")\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.subplot(grid[0, 4])\n",
2022-08-02 16:33:49 +08:00
"plt.plot(u_sim[0, :])\n",
"plt.ylabel(\"a(t) [m/ss]\")\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.subplot(grid[1, 4])\n",
2022-08-02 16:33:49 +08:00
"plt.plot(x_sim[2, :])\n",
"plt.ylabel(\"v(t) [m/s]\")\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.subplot(grid[2, 4])\n",
2022-08-02 16:33:49 +08:00
"plt.plot(np.degrees(u_sim[1, :]))\n",
"plt.ylabel(\"delta(t) [rad]\")\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.subplot(grid[3, 4])\n",
2022-08-02 16:33:49 +08:00
"plt.plot(x_sim[3, :])\n",
"plt.ylabel(\"theta(t) [rad]\")\n",
2021-04-13 18:30:08 +08:00
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
2024-10-23 17:34:26 +08:00
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
2021-04-13 18:30:08 +08:00
}
],
"metadata": {
"kernelspec": {
2024-10-23 17:34:26 +08:00
"name": "python3",
2021-04-13 18:30:08 +08:00
"language": "python",
2024-10-23 17:34:26 +08:00
"display_name": "Python 3 (ipykernel)"
2021-04-13 18:30:08 +08:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}