Skip to content

Instantly share code, notes, and snippets.

@Piezoid
Created August 28, 2022 23:09
Show Gist options
  • Save Piezoid/dcc1887c9d92efbb0965826e7e08ff03 to your computer and use it in GitHub Desktop.
Save Piezoid/dcc1887c9d92efbb0965826e7e08ff03 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ecc8c4e9-9a1d-46de-b3d4-69dbe149c7de",
"metadata": {},
"outputs": [],
"source": [
"import sympy as sp\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b48bf4dd-15a5-4e26-bad0-c3ee837b5d62",
"metadata": {},
"outputs": [],
"source": [
"v0, v1, v2 = sp.symbols('v_0, v_1, v_2', positive=True) # Entry, cruising, and exit velocities\n",
"t, ta, tc, td = sp.symbols('t t_a, t_c t_d', positive=True)\n",
"a, k, d = sp.symbols('a k d', positive=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6278725f-bf42-4fa0-ae87-617654fbb970",
"metadata": {},
"outputs": [],
"source": [
"# No cruising, models (accel and decel durations) -> (distance travelend, end speed)\n",
"va = v0 + sp.integrate(a*t, (t, 0, t))\n",
"vd = va.subs(t, ta) + sp.integrate(-a*t, (t, 0, t))\n",
"\n",
"d_tot = sp.simplify(sp.integrate(va*t, (t, 0, ta)) + sp.integrate(vd*t, (t, 0, td)))\n",
"v_end = sp.simplify(vd.subs(t, td))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8a7e1bbf-8dc5-443a-b8a0-103c4f85725d",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle t_{a} = \\frac{\\sqrt{- 2 v_{0} + \\sqrt{4 a d + 2 v_{0}^{2} + 2 v_{2}^{2}}}}{\\sqrt{a}}$"
],
"text/plain": [
"Eq(t_a, sqrt(-2*v_0 + sqrt(4*a*d + 2*v_0**2 + 2*v_2**2))/sqrt(a))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle t_{d} = \\frac{\\sqrt{- 2 v_{2} + \\sqrt{2} \\sqrt{2 a d + v_{0}^{2} + v_{2}^{2}}}}{\\sqrt{a}}$"
],
"text/plain": [
"Eq(t_d, sqrt(-2*v_2 + sqrt(2)*sqrt(2*a*d + v_0**2 + v_2**2))/sqrt(a))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle v_{1} = \\frac{\\sqrt{4 a d + 2 v_{0}^{2} + 2 v_{2}^{2}}}{2}$"
],
"text/plain": [
"Eq(v_1, sqrt(4*a*d + 2*v_0**2 + 2*v_2**2)/2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Solve for accel and decel durations, then peak velocity (v1)\n",
"tas, tds = sp.solve([d_tot - d, v_end - v2], [ta, td])[0]\n",
"v1s = sp.simplify(va.subs(t, tas))\n",
"\n",
"display(sp.Eq(ta, tas))\n",
"display(sp.Eq(td, tds))\n",
"display(sp.Eq(v1, v1s))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "fae9eddd-7666-48c2-9918-a82cf4686b19",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle d_{c} = d \\left(1 - k\\right)$"
],
"text/plain": [
"Eq(d_c, d*(1 - k))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle t_{a} = \\frac{\\sqrt{- 2 v_{0} + \\sqrt{4 a d k + 2 v_{0}^{2} + 2 v_{2}^{2}}}}{\\sqrt{a}}$"
],
"text/plain": [
"Eq(t_a, sqrt(-2*v_0 + sqrt(4*a*d*k + 2*v_0**2 + 2*v_2**2))/sqrt(a))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle t_{d} = \\frac{\\sqrt{- 2 v_{2} + \\sqrt{4 a d k + 2 v_{0}^{2} + 2 v_{2}^{2}}}}{\\sqrt{a}}$"
],
"text/plain": [
"Eq(t_d, sqrt(-2*v_2 + sqrt(4*a*d*k + 2*v_0**2 + 2*v_2**2))/sqrt(a))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle t_{c} = \\frac{\\sqrt{2} d \\left(1 - k\\right)}{\\sqrt{2 a d k + v_{0}^{2} + v_{2}^{2}}}$"
],
"text/plain": [
"Eq(t_c, sqrt(2)*d*(1 - k)/sqrt(2*a*d*k + v_0**2 + v_2**2))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Solves for accel and decel durations when the peak velocity is reduced by using a virtual acceleration = k * a (0 < k <= 1)\n",
"v1sk = v1s.subs(a, k * a)\n",
"tas, tds = sp.solve([va.subs(t, ta) - v1sk, vd.subs(t, td) - v2], [ta, td])[0]\n",
"# Cruised distance and time:\n",
"d_cruise = sp.simplify(d - d_tot.subs({ta: tas, td: tds}))\n",
"t_cruise = sp.simplify(d_cruise / v1sk)\n",
"\n",
"display(sp.Eq(sp.Symbol('d_c'), d_cruise))\n",
"display(sp.Eq(ta, tas))\n",
"display(sp.Eq(td, tds))\n",
"display(sp.Eq(tc, t_cruise))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment