[Math] How to find the final angle of a car after steering

geometry

I'm doing a car simulator.

  • The car makes a turn with maximum steering 30 degrees.
  • With a distance of 4 meters between the two axes of the car, its
    turning radius is $\frac{4}{\tan 30}$ = 6.93 meters.
  • The total steering time (from 0 to 30 degrees or from 30 than 0
    degrees is 2 seconds).
  • If the car moves at 10 m/s, its angular velocity will be $\frac{10}{6.93}$ = 1.443 radians (or 82.68 degrees per second)

My question is: knowing that the car takes 2 seconds to steer from 30 to 0 degrees, and that it is on a steady curve at maximum steering (30 degrees), at a speed of 10 m/s, if the car starts to steer back when it reaches the zero degree of the curve, what will be the final degree of the car when its wheel is fully aligned with the car (0 degrees)?

This might seem simple, but the problem is that as the car decreases the steering, the angular velocity will also shifting and changing everything else.


In the animation below, the car has the same specifications above. The steering is at 30 degrees and when the car arrives from the bottom of the curve (0 degrees), it reverses the steering from 30 to 0 degree in 2 seconds. The final angle of the car is 78.54 degrees.

enter image description here

Best Answer

First, construct a function that describes the steering angle as a function of time. In this case, it seems to be $$\theta(t) = \begin{cases} \frac{15\text{°}}{1\;\text{s}} t, & 0\;\text{s} \le t \lt 2\;\text{s} \\ 30\text{°}, & 2\;\text{s} \le t \lt T + 2\;\text{s} \\ \frac{15\text{°}}{1\;\text{s}} (T + 4\;\text{s} - t), & T + 2\;\text{s} \le t \le T + 4\;\text{s} \\ \end{cases} \tag{1}\label{NA1}$$ where and $T$ is the duration of steering angle being kept at $30\text{°}$. Note that $\text{°}$ and $\text{s}$ are not variables, but denote angular and time units (degrees and seconds), respectively.

Next, construct a function $\dot{\varphi}(t)$ that describes the rate at which the car turns as a function of time, given the steering angle $\theta(t)$ and the car speed $v$.

If the distance between axles is $L = 4\;\text{m}$, the turning radius is $r(\theta)$, $$r(\theta) = \frac{L}{\tan(\theta)}$$ With a turning radius $r(\theta)$, if the car travels at speed $v$ for duration $t$, it traverses $v t$ along the perimeter of the turning circle, and thus turns by $\phi(\theta, v, t)$, $$\phi(\theta, v, t) = \frac{v t}{2 \pi r(\theta)} 360\text{°} = \frac{v t \tan(\theta)}{2 \pi L} 360\text{°}$$ Thus, the instantaneous car turning rate is $$\dot{\varphi}(t) = \frac{d \phi(\theta, v, t)}{d t} = \frac{v \; 360\text{°}}{2 \pi L} \tan\left(\theta(t)\right) \tag{2}\label{NA2}$$

To get the change in car direction $\varphi(t)$, we need to integrate the turning rate over time: $$\varphi(t) = \int_{\tau=0\;\text{s}}^{\tau=t} \dot{\varphi}(\tau) d\tau$$ During the first two seconds, $$\varphi_1 = \int_{\tau=0\;\text{s}}^{\tau=2\;\text{s}} \frac{v \; 360\text{°}}{2 \pi L} \tan\left(\tau \; 15\text{°/s}\right) d\tau = \frac{-2160° v \log\left(\frac{\sqrt{3}}{2}\right)}{L \pi^2} \approx \frac{v}{L} 31.48015\text{°s} \tag{3.1}\label{NA3.1}$$ During the constant steering angle of $30\text{°}$, the car turns $$\varphi_2(T) = \frac{v T \tan(30\text{°})}{2 \pi L} 360\text{°} = \frac{60\text{°} \sqrt{3} v T}{\pi L} \approx \frac{v T}{L} 33.0797\text{°} \tag{3.2}\label{NA3.2}$$ Because of symmetry (reversing the car for the first part is the same as inverting time for driving the third part forwards), the third part turns the same amount, $$\varphi_3 = \varphi_1 \tag{3.3}\label{NA3.3}$$ It can also be calculated via an integral, of course: $$\varphi_3 = \int_{\tau=T+2\;\text{s}}^{\tau=T+4\;\text{s}} \frac{v \; 360\text{°}}{2 \pi L} \tan\left((T+4\;\text{s}-\tau) \; 15\text{°/s}\right) d\tau = \frac{-2160° v \log\left(\frac{\sqrt{3}}{2}\right)}{L \pi^2} \approx \frac{v}{L} 31.48015\text{°s}$$

Combining all three, the total angle the car turns as a function of time $T$ the steering is kept at $30\text{°}$, is $$\varphi(T) = \varphi_1 + \varphi_2(T) + \varphi_3 = \frac{-2 \cdot 2160° v \log\left(\frac{\sqrt{3}}{2}\right)}{L \pi^2} + \frac{60\text{°} \sqrt{3} v T}{\pi L} \approx \frac{v}{L} \left( 62.9603\text{°s} + T \; 33.0797\text{°}\right) \tag{4}\label{NA4}$$

If $v = 10\;\text{m/s}$ and $L = 4\;\text{m}$, then $$\varphi(T) \approx 157.401\text{°} + 82.6993\text{°} \; T$$


In the comment exchange OP points out that it is unclear how $\eqref{NA2}$ yields $\eqref{NA3.1}$. Let's expand on that, hopefully in a way that will help others working on similar problems as well. Although deriving the formulae requires a bit of calculus, I'll show how to use the free, open source SageMath to do the math-fu for you.

First, we need the instantaneous turning rate $\dot{\varphi}(t)$. It turns out that $\eqref{NA2}$ is the correct form for a car with changing steering angle $\theta(t)$, even if the car speed $v(t)$ varies, as long as the axle distance $L$ is constant. We can rewrite $\eqref{NA2}$ as $$\dot{\varphi}(t) = \frac{360\text{°}}{2 \pi L} v(t) \tan\left(\theta(t)\right) \tag{5}\label{NA5}$$

Note that if the car accelerates from standstill at $t = 0$ with constant acceleration $a$, then $v(t) = a t$. If it decelerates (negative $a$), then $v(t) = v_0 + a t$. If the acceleration varies, then $v(t) = \int a(t) d t$. If you know the form of $a(t)$, say $a(t) = 2 t - \frac{1}{5} t^2$ (so that $a(0) = 0$, $a(5) = 5$, and $a(10) = 0$), you can calculate $v(t)$ in Sagemath thus:

t, tau = var('t', 'tau')
a(t) = 2*t - 1/5*t^2
v(t) = integral(a(tau), tau, 0, t)

If you then type just v(t), SageMath responds with the solution,

-1/15*t^3 + t^2

i.e. $v(t) = t^2 - \frac{1}{15} t^3$.

Similarly for the steering angle $\theta(t)$. I call it theta = var('theta') in SageMath, but it's up to you, of course.

Note that most computer algebra systems and programming languages use radians rather than degrees. 360° corresponds to $2\pi$ radians. To convert $x$ degrees to radians, use $x \pi / 180$. To convert $y$ radians to degrees, use $180 y / \pi$. This is the reason for the *pi/180 in tan(), below.

To turn $\eqref{NA5}$ into the change in steering angle, we need to integrate the instantaneous change of car direction $\dot{\varphi}(t)$ over time $t$. Lets assume our functions are valid from time $t = t_0$ to $t = t_1$, and the change in car direction during that time is $\varphi_\Delta$: $$\varphi_\Delta = \int_{t_0}^{t_1} \dot{\varphi}(t) d t \tag{6}\label{NA6}$$ To calculate that, we need calculus, or somebody or something that does the calculus for us. In SageMath, we can do

t, t0, t1, L, V = var('t', 't0', 't1', 'L', 'V')
assume(t0 >= 0, t1 > t0)
v(t) = V
theta(t) = 15*t
d_phi_dt(t) = 360/(2*pi*L)*v(t)*tan(theta(t)*pi/180)
change(t0, t1) = integral( d_phi_dt(t), t, t0, t1)

where the assume line tells SageMath that we're only interested in answers where $0 \le t_0 \lt t_1$. If we then ask SageMath what the change in direction in degrees is from $t = 0$ to $t = 10$, change(0, 10), we get

-2160*V*log(1/2*sqrt(3))/(pi^2*L)

i.e. $\frac{-2160 V}{L \pi^2} \log\left(\frac{\sqrt{3}}{2}\right)$.

If we don't want to use SageMath, and want to do the calculation in our own program, we need to do numerical integration. There are many methods to do this (with varying accuracy and difficulty of implementation), but for many simulations, the simplest of them all, midpoint rule will suffice.

At the core, we have a function $\dot{\varphi}(t)$ of a single variable $t$, and we know both the function, and the range of $t$ we are interested in. The midpoint rule with constant intervals turns the integral into a sum: we examine the function at regular intervals, multiply each by the interval (as if the function consisted of steps, like a staircase), and sum them. It is a surprisingly good approximation, and very quick for a computer to do. Mathematically, if we split time interval $t_0$ to $t_1$ into $N$ parts, then $$\int_{t_0}^{t_1} \dot{\varphi}(t) d t = \frac{t_1 - t_0}{N} \sum_{i = 0}^{N-1} \dot{\varphi}\left(t_0 + \frac{2 i + 1}{2 N} t_1 \right)$$ That is, we do a sum of $N$ terms, each term being $\dot{\varphi}$ at the midpoint of an interval, and then multiply the sum by the width of each interval. The time step, the size of each time interval, is $\frac{t_1 - t_0}{N}$. The larger the $N$, the smaller the time step, and the more accurate the result is (because the "step curve" we actually integrate better matches the actual curve $\dot{\varphi}(t)$).

(If you sample the curve at the beginning of each interval, at $\dot{\varphi}(t_0 + \frac{i}{N} t_1)$, the answer is just as good; it's simply not "midpoint" per se, but the initial value at each interval. The "step curve" is offset by half a time step earlier, that's all.)


Since we know the speed of the car $v$ and the steering angle $\theta$ (relative to the cars current direction, so $\theta = 0$ is forwards, $\theta > 0$ is turning left/counterclockwise, and $\theta < 0$ is turning right/clockwise), we know $\dot{\varphi}(t)$ as well.

In a simulation, you could basically have (pseudocode)

x = x + C1 * speed * cos(direction * pi/180)
y = y + C1 * speed * sin(direction * pi/180)
direction = direction + C2 * speed * tan(steering_angle * pi/180)

where speed and steering_angle (in degrees) can vary from time step to the next.

This is, in fact, numerically integrating both the direction, and the location of the car, using the midpoint algorithm!

You see, the exact formula for the location of the car is $$\begin{cases} \displaystyle x(t) = \int v(t) \cos\left(\theta(t)\right) d t \\ \displaystyle y(t) = \int v(t) \sin\left(\theta(t)\right) d t \\ \end{cases}$$ and we can integrate it numerically (using for example the midpoint algorithm above) just like any other single-variable function.

In fact, the constants C1 and C2 are just

$$\begin{cases} \displaystyle C_1 = T_\Delta \\ \displaystyle C_2 = \frac{360\text{°} T_\Delta}{2 \pi L} \\ \end{cases}$$

where $T_\Delta$ is the duration of each time step, $L$ is the distance between the axles in the car, and $\pi \approx 3.14159265358979323846\dots$ as usual.

A final hint: Most simulations use many more time steps than they display. To get more accurate results, and possibly a more responsive "game" if a human controls the steering and speed/acceleration at each time step, only display/draw the state at regular intervals, not every time step you calculate.

Related Question