Kinematic profile:
$T1$ - increasing acceleration time (max jerk)
$T2$ - constant acceleration time (zero jerk)
$T3$ - decreasing acceleration time (-max jerk)
The goal is to calculate $T1$, $T2$, $T3$. For simplicity, the explaining of calculation $T1$ would be just fine.
I have found the original solution (https://github.com/PX4/Firmware/blob/master/src/lib/FlightTasks/tasks/Utility/VelocitySmoothing.cpp#L70) that works but I don`t understand why.
The main problem for me is to understand why to use a second-order polynomial equation to compute $T1$? I found a similar problem (and explanation(Shortest time to cover certain distance (Jerk involved))) but it doesn`t correlate with the original solution.
Below is pseudo-code of the original solution which I would like to understand.
/* Compute increasing acceleration time ($T1$) */
/* Looks like we want to solve the second-order polynom with respect to t: $\frac{jt^2}{2} + a_0t + v_0 = v$ */
/* Why is $b$ expressed like that? */
$b = \frac{2 a_0 }{j}$
/* Why is $c$ expressed like that? */
$c = \frac{v_0 }{j} + \frac{a_0^2 }{2 j^2} - \frac{v}{j}$
$D = b^2 - 4 c$ /* why is $a$ = 1 ? */
if $D$ < $0$ then solution is not real and $T1 = 0$
else
$T1_{+} = (-b + \sqrt{D}) * 0.5$
$T1_{-} = (-b - \sqrt{D}) * 0.5$
$T3_{+} = \frac{a_0}{j} + T1_{+}$ /* Why do I need to calculate T3? */
$T3_{-} = \frac{a_0}{j} + T1_{-}$ /* Why do I need to calculate T3? */
$T1 = 0$
/* Completely don't understand here */
if $T1_{+} >= 0$ and $T3_{+} >= 0$ then $T1 = T1_{+}$
else if $T1_{-} >= 0$ and $T3_{-} >= 0$ then $T1 = T1_{-}$
if $T1 < dt$ then $T1 = 0$
/*******************************/
Any idea why do I need to solve the second-order polynomial equation in order to evaluate $T1$ is appreciated. Thanks!
You are given four values:
Saying that the rate of change in the acceleration $a$ is $j$ means
$$\frac {da}{dt} = j$$ and assuming $j$ is constant, this solves to $a(t) = jt$ plus some constant, which setting $t = 0$ shows has to be $a_0$. So
$$a(t) = jt + a_0$$
Now acceleration is the rate of change of velocity:
$$\frac{dv}{dt} = a = jt + a_0$$
Integrate that and you get $v(t) = j \frac{t^2}2 + a_0t$ plus another constant, which setting $t = 0$ again shows must be $v_0$. Therefore
$$v(t) = \frac j2 t^2 + a_0t + v_0$$
Now at $t = T_1$, we know that $v(t) = v$, or $$\frac j2 t^2 + a_0t + v_0 = v\\\frac j2 t^2 + a_0t + v_0 - v = 0\\t^2 + \frac{2a_0}j t + \frac {2(v_0 - v)}j = 0$$ Where in the last step, I multiplied the previous equation through by $\frac 2j$. This is why they apply the quadratic formula with $a = 1, b = \frac{2a_0}j, c = \frac {2(v_0 - v)}j$
Now the quadratic formula gives two possible answers. Which one should you use? You need the times $T_1, T_2, T_3$ to all be positive (you can't go back in time and change what happened in the past). But T_2 apparently will be okay (I haven't checked, but I'm guessing that is why it isn't also calculated here) as long as $T_1$ and $T_3$ are positive. So it does a preliminary calculation of $T_1$ and $T_3$ for each of the solutions. It checks if one of them has both $T_1$ and $T_3$ positive. If not, it tries the other solution. If that one also fails, it gives up and returns $0$ (which I'm guessing some calling procedure recognizes as saying "this can't be done").
It also has a minimum allowable time interval $dt$. If even this short interval is more time than is needed to reach the target velocity, it also gives up and returns $0$.