1

I want to make something move with smooth motion. Think about an elevator, it doesn't go full speed and dead stop when it reaches the desired floor. It goes as fast as it can and incrementally slows down until it reaches the desired floor.

I need a loop that inputs...

int steps = 10;
int target = 100;

So the function will take ten steps to reach the target value of 100.

Ideally this function should act very much like a PID loop.

In essence I'm hoping there is an easier way to acomplish this than creating a PID loop.

CTOverton
  • 616
  • 2
  • 9
  • 20
  • It should soft start too? In that case I'll recomend you somthing like the S-curve https://en.wikipedia.org/wiki/S_Curve – Rama Dec 05 '16 at 18:51

2 Answers2

2

You can always do something simpler than PID by adjusting your speed based on how close you are (a P loop, if you will). Of course, with a fixed number of steps, you'll have to just go the rest of the way on the last one, but if it's small enough it shouldn't be a big deal.

This amounts to just going a fixed percentage of the distance left each step, and the percentage depends on how fast you want to slow down. In your example, you might do something like 40%:

{0., 40., 64., 78.4, 87.04, 92.224, 95.3344, 97.2006, 98.3204, 98.9922}
{0, 40, 64, 78, 87, 92, 95, 97, 98, 100} after rounding

The code for starting at 0 and going to a target value N would then be

#include <math.h>
...
int steps[NUM_STEPS];
for (int i = 0; i < NUM_STEPS - 1; i++) {
    steps[i] = N - N * pow(.6, i);
}
steps[NUM_STEPS - 1] = N;
Iluvatar
  • 1,537
  • 12
  • 13
  • That is exactly what I'm looking for, I don't know why my brain isn't favoring me today but how would you write that in an equation? The math is just not coming to me :/ – CTOverton Dec 05 '16 at 19:01
  • Basically just `N-N*frac^i` where `frac` is the increase rate. – Iluvatar Dec 05 '16 at 19:12
1

What I needed exactly courtesy of Iluvatar...

#include <math.h> 
using namespace std;

int main()
{
    void smooth(int, int, float);

    smooth(10, 100, 0.6);

    return 0;
}

void smooth(int steps, int target, float increment) {
    for (int i = 0; i < steps - 1; i++) {
        int num = target - target * pow(increment, i);
        cout << num << endl;
    }
    cout << target << endl;
}
CTOverton
  • 616
  • 2
  • 9
  • 20