title: Calculate time for triangular and / or trapezoidal motion profile with initial velocity
body: <p>I want to calculate the time it takes to reach <em>target-pos</em> from <em>initial-pos</em>, taken in account some initial velocities.</p>
  
  <ul>
  <li>i: Initial Position: <strong>0.0</strong> m </li>
  <li>t: Target Position: <strong>10.0</strong> m </li>
  <li>a: Acc: <strong>0.3</strong> m/s<sup>2</sup> </li>
  <li>d: Dec: <strong>0.3</strong> m/s<sup>2</sup> </li>
  <li>v: Max Velocity: <strong>0.5</strong> m/s </li>
  <li>u: Initial Velocities: [<strong>0.0</strong> m/s, <strong>-0.5</strong> m/s, <strong>5.0</strong> m/s]</li>
  </ul>
  
  <p>First I calculate the distance needed to travel to reach the max velocity. </p>
  
  <pre><code>v_dist = ((v² * (a + d)) / (a * d)) / 2.0 
  v_dist = ((0.25 * 0.6) / 0.09) / 2.0 
  v_dist = ((0.15) / (0.09)) / 2.0 
  v_dist = 0.833333 
  </code></pre>
  
  <p>If we do not reach the maximum velocity, in other words, if we do not travel the distance we need to travel to reach the maximum velocity, it will always be a <strong>Triangular Motion Profile</strong></p>
  
  <p>For a <strong>Triangular Motion Profile</strong> I use the following formula:</p>
  
  <pre><code>t = sqrt(2.0 * abs(t-i) * ((a+d)/(a*d))) 
  Which results in: 
  t = sqrt(2.0 * abs(10.0) * ((0.3+0.3)/(0.3*0.3))) 
  t = sqrt(20 * (0.6/(0.09))) 
  t = sqrt(20 * (6.6667)) 
  t = sqrt(133.333) 
  t = 11.547 
  </code></pre>
  
  <p>Unfortunately this formula doen not respect the initial velocity, and I can not figure out where to insert this. I also experience difficulties in wrapping my head around the <code>((a+d)/(a*d))</code> part.</p>
  
  <p>How can I adjust the formula so it does take an initial velocity in account, even if the current direction of motion is in the opposite of where the target position is?</p>
  
  <p>For a <strong>Trapezoidal Profile</strong> I use the following formula: </p>
  
  <pre><code>t = (abs(t-i) - ((a * (v/a)²) /2) + ((d * (v/d)²) /2)) / v + (v/a) + (v/d)
  </code></pre>
  
  <p>For this formula I have the same problem as for the triangular formula. I do not understand where I put the u (initial velocity) so it is processed correctly.</p>