acceleration = (final velocity - initial velocity) / time
The simple example is gravity. lets say your camera is following a falling object. For simplicity we can say 10ms-<sup>2</sup>.
Starting at rest 0ms-<sup>1</sup>, in 1 second the camera should be moving at 10ms-<sup>2</sup>.
You also have the issue of angular acceleration which is a factor of pi*r. To keep that simple, convert it to linear velocity.
Again, we can simplify velocity so you can build upon it. If we say the motor takes 1cm per step.
1ms-<sup>1</sup> = 100cms-<sup>1</sup> = 1mm ms-<sup>2</sup>
So to accelerate at 10ms-<sup>2</sup> you would need to 1 step in 10ms, two more steps in the next 10ms then 3 steps in the next 10ms after that.
To accelerate at 5ms-<sup>2</sup> you would take the first step in 20ms, two more in the next 20ms.
But that thinks of it from a step perspective which can be a little bit jerky because you can see the velocity changes in steps rather than smoothly especially if you're stepping slowly.
You can think of it from a time perspective so that the velocity changes smoothly
10ms-<sup>2</sup> first step at 10ms, the second step at 16ms, the third step at 20mS the fourth step at 23ms fifth at 25ms and so on.......
Deceleration is the opposite.
Remember how your high school maths teacher said Calculus was important and useful and then demonstrated to you how dull and boring and irrelevant it was.
Well this is where it is useful.
Velocity is an integral of position.
Acceleration is an integral of velocity.
Impulse is an integral of acceleration.
If the position changes with time, this is a velocity
If the velocity changes with time, this is acceleration
If the acceleration changes with time, this is a impulse.
The first two are easy to program onto a joystick.
A pulse on the joystick just steps the motor in that direction.
Holding the joystick means you want to accelerate, so increasing the stepping rate or decreasing the increment timing.
Impulse... this is the tricky one.
Lets assume you hold the joystick to move.....
The motor steps once and checks the joystick position.
If the joystick is still in position, then increase the stepping rate. (linear acceleration) This is normally what you want. But the longer you hold the joystick, the faster the velocity and you might choose to increase the acceleration (impulse).
To decelerate.....
You release the joystick .... but if you stop instantly, the acceleration is very high and so is the impulse because the instant change.
Instead when you release the joystick, the velocity stays the same (no acceleration) so the motor keeps running. This might be what you want or you might want to linear decelerate the motor back to zero when the joystick is idle.
If you want a constant velocity, then reverse joystick decelerates by reducing the stepping rate or increasing the interval.
If you're already decelerating on idle joystick, then reverse joystick decreases the acceleration (impulse).
start:
if forward then (dv=dv+da)
if reverse then (dv=dv-da)
if idle then (dv=dv+0) goto start
if forward or reverse then da=da+1
if idle then da=da-1 goto start
OR
if forward and (forward-previous) then da=da+1
if idle then da=0
if reverse and (forward-previous) then da=da-1
Sorry for my simple pseudo code... but I'm hoping that you can use it to build up the mods to your code.
Bookmarks