Algorithm to control acceleration until a position is reached

前端 未结 4 1159
[愿得一人]
[愿得一人] 2021-02-02 02:42

I have a point that moves (in one dimension), and I need it to move smoothly. So I think that it\'s velocity has to be a continuous function and I need to control the accelerati

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 03:12

    I'd do something like Alex Deem's answer for trajectory planning, but with limits on force and velocity:

    In pseudocode:

    xtarget:  target position
    vtarget:  target velocity*
    x: object position
    v: object velocity
    dt: timestep
    
    F = Ki * (xtarget-x) + Kp * (vtarget-v);
    F = clipMagnitude(F, Fmax);
    v = v + F * dt;
    v = clipMagnitude(v, vmax);
    x = x + v * dt;
    
    clipMagnitude(y, ymax):
       r = magnitude(y) / ymax
       if (r <= 1)
           return y;
       else
           return y * (1/r);
    

    where Ki and Kp are tuning constants, Fmax and vmax are maximum force and velocity. This should work for 1-D, 2-D, or 3-D situations (magnitude(y) = abs(y) in 1-D, otherwise use vector magnitude).

提交回复
热议问题