3

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

Rogério Dec
  • 205
  • 4
  • 10
  • At the $instant$ that the wheel is $aligned$ with the car, the car's angle with respect to the direction of travel is zero. Other factors are irrelevant during that $instant$. Am I missing something or is the question about some other instant? – poetasis Oct 20 '18 at 08:39
  • When you say "the final degree", is this angle measured using the center of the initial turn radius? – Jens Oct 20 '18 at 12:09
  • I've edited the original question. I did a simulation with my program and I inserted an animation. – Rogério Dec Oct 20 '18 at 23:14
  • Hello Rogerio, can you share the simulation prgram u used to achieve above simulation?How did u caculate heading of vehicle with steering angle and speed? i see that u obtain 82 degress per sec, does that mean if you travel at speed of 10 with 30 deg steering angle, every sec the car direction will be increased by 82? – 230490 Mar 09 '21 at 08:04
  • Also, i see in the simulation, initially when the car is not at speed 10 and not at steering angle 30, then how do u achieve the headring direction of vehicle, my problem is i need to be able to calculate vehicle heading, given steering angle when the driver is driving the car – 230490 Mar 09 '21 at 08:05
  • Is L wheelbase? distance between front axle and back axle? – 230490 Mar 09 '21 at 08:09

1 Answers1

4

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.

  • As I read the question, the OP only wants to know what happens after the steering angle begins to change from $30^\circ$ to $0^\circ$. – Jens Oct 20 '18 at 12:12
  • 1
    @Jens: I put more weight on the "what will be the final degree of the car when its wheel is fully aligned with the car" part. However, I shall expand on $\eqref{NA3.3}$, showing the explicit integral, just in case. – Nominal Animal Oct 20 '18 at 14:26
  • Thanks for all clarifications! I didn't implement the formulas above because first I did a simulation with my program, but I got another final angle. Why the results are not matching? – Rogério Dec Oct 20 '18 at 23:12
  • @RogérioDec: They don't match, because you gave misleading information. If you calculate your 82.62° without rounding at each step, you'll find it actually is approximately 82.6993° instead. Now, you didn't mention that the car starts from a standstill; you claimed that $v = 10 m/s$. The same method I used above applies to your case, except we don't know all the variables -- specifically, the initial acceleration. – Nominal Animal Oct 21 '18 at 01:23
  • @NominalAnimal, thanks, but the speed IS 10 m/s when it starts to decrease steering (when the car reaches 0 degrees), as I said in the original question. The simulation I inserted has no rounding and it finds the final angle 78.54º – Rogério Dec Oct 21 '18 at 01:45
  • @RogérioDec: Well, let's examine the two cases. Your car starts at an angle of 90º, and your final angle is 78.54º, so by your simulation the car turns by 78.54º - 90º = -11.46º (= 348.54º). My calculations are relative to the car's initial direction. My answer ($\eqref{NA3.1}$ and $\eqref{NA3.3}$) says that during the steering change, if the car travels at 10 m/s, it turns a further 78.70º. In your simulation, that starts at 270º. So, by my calculations, the total change in the car's direction is 270º + 78.70º = 348.7º = -11.30º. My answer differs from your simulation by 0.16º. – Nominal Animal Oct 21 '18 at 02:29
  • @NominalAnimal, thanks for your attention, maybe I'm not expressing myself right. Although the animation shows the car starting at 90º, this was only to illustrate, because, as I said before, only when the car reaches 360º (ie, 0º), it begins to decrease the steering from 30º to 0º for 2 seconds, resulting in the final angle 78.54º. – Rogério Dec Oct 21 '18 at 02:43
  • @RogérioDec: The final angle by itself is irrelevant. Only the change in the direction, in degrees, matters. In your simulation, the car turns by a total of 348.54º. By my calculations, it turns 348.70º. While the answers differ, the difference is tiny. Concerning that 78.54º, $\eqref{NA3.3}$ says it should be 78.700º. I suggest you rerun your simulation with an even smaller timestep. You'll see that the result changes slightly. If you do a few of them, you'll see that at the limit of zero-duration timesteps, our results converge to the same angle. – Nominal Animal Oct 21 '18 at 03:38
  • @NominalAnimal, what is 'd' in the formula at 3.1? – Rogério Dec Oct 22 '18 at 14:46
  • My God, my God, why have you deserted me? – Rogério Dec Oct 23 '18 at 01:42
  • @RogérioDec: $\int_a^b \dots d\tau$ is a definite integral over $\dots$ from $\tau = a$ to $\tau = b$. – Nominal Animal Oct 23 '18 at 02:07
  • I'm not a mathematician, I apologize. I just wanted to know why the second formula in 3.1 -2160 * 10 * (ln (√ (3) / 2)) / (4 * (π ^ 2))) results in 78.700 (which I consider the correct value), but the first formula in 3.1 10 * 360 / (2 * π * 4) * tan (30) results in 82.6993? What do I have to do to get the first formula to result in 78.700? – Rogério Dec Oct 23 '18 at 02:38
  • @RogérioDec: If you graph $\dot{\varphi}(t)$ so that time $t$ is on the horizontal axis, and the rate of change in angle is on the vertical axis, then the integral, $\varphi(t)$, is the area between the curve and the $x$ axis between time 0 and time $t$. – Nominal Animal Oct 23 '18 at 02:46
  • I know it's a habit for mathematicians to answer a question in an 'incognito' way, but could you open up an exception to a poor programmer? This detail is only what is missing so I can continue my project. If you could just tell me WHAT VALUE, WHAT'S MISSING, in the first formula for the result to be 78.700, I'll be eternally grateful. – Rogério Dec Oct 23 '18 at 02:59
  • @RogérioDec: I'm not a mathematician myself, but a computational (materials) physicist. I do programming more than math, math being a just a tool. I'll expand it in a new section to the answer. – Nominal Animal Oct 23 '18 at 03:02
  • @RogérioDec: Is it clearer now? The missing bit is a calculus operation: integrating the instantaneous direction change $\dot{\varphi}$, to get the effected change in direction $\varphi_\Delta$ over the time span you integrate over. – Nominal Animal Oct 23 '18 at 05:07
  • I wish I could give you a lot of points for your answer ... I can only thank you for all your patience, your goodwill, and your great work. Thank you very much! My eternal gratitude. – Rogério Dec Oct 24 '18 at 03:26
  • 1
    @RogérioDec: No worries! Just pay it forward: When you are more confident and have more experience with your chosen field(s), find questions you can answer to help others learn in turn. :) – Nominal Animal Oct 24 '18 at 05:05
  • How to get general steering angle with respect to time, i see that equation 1 is derived keeping the example in question like it reaches 30 deg in 2 sec. Can i have generic equation for that? – 230490 Mar 09 '21 at 08:36
  • To get the vehicle direction, can i directly use this equation?direction = direction + C2 * speed * tan(steering_angle * pi/180) – 230490 Mar 09 '21 at 08:47