# smooth a motor movement

i have a stepper motor (controlled by an arduino) that moves some gears and a pulley. the problem is that the pulley jerks a lot: the movement begin and stop too much suddenly. to make the movement more fluid i think i can use a kind of ramp: the first movement are slow, when the motors has reached the velocity it keep going on, then smoothly slow down.

i have tried to use a sine wave in this way:

// generate ramp
float t = 0;
float deltat = PI/(200+1)
for(int i=0; i<200; i++) {
ramp[i] = 1000 - sin(t)*900;
t += deltat;
}


now i have a ramp of 200 elements that goes from 1000 (very slow) to 100 (very fast) following a sinus wave. now i move the motor with this code:

for(int i=0; i<200; i++) {
int delay_velocity = ramp[i];

digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delay_velocity);

digitalWrite(STEP_PIN, LOW);
delayMicroseconds(delay_velocity);
}


this works quite fine: the pulley does not jerk so much and i have control on how many steps to do.

but this work around has also problems:

• i can't make ramps very long (due to overflow in atmega) - say an array no more longer than 400 elements more or less (it depends on how long is the arduino sketch)
• i have control on how many steps but i have no idea on how many time it takes to get it completed (i think i can calculate it with some summation or integral but it is not so practical)

so i'm looking for some other way to smooth my movement. i think i can have a different curve instead of a sinewave. something more like this?

till now i don't have considered to generate the ramp in a computer and then pass it to arduino but could be a possibility.

• need help!can i get the full working code where you'll have a ramping step dir output on two pins, im interested in the first code that you posted. i can't figure out the rest because of my poor programming skills. or just the ramping parts with all the declarations complete, so that i can get an understanding. i am driving a dugong dc drive with an arduino uno. – user16088 Nov 13 '12 at 7:03
• i'm talking about stepper motor.. never used a dc motor – nkint Nov 13 '12 at 12:47

What you're looking for is called a Trapezoidal velocity profile.

There is no need to use sin waves. Smooth motion can be achieved by simply linearly ramping up the velocity to its maximum, holding it there, then letting it ramp down again. Many CNC milling machines and robots use this type of profile.

If you want super smooth motion, you can go for an S-Curve profile.

The maths for this is considerably harder, but the motion is beautiful. In this video they show how the two motion profiles affect a glass of water.

• wow, yeah, it is exactly what i was looking for. do you have also some links for implementation? – nkint Aug 23 '12 at 8:04
• I really,really doubt that video is showing trapezoidal velocity vs s-curve velocity. It looks like no acceleration at all vs linear acceleration. – Connor Wolf Nov 13 '12 at 9:43
• The thing I think they mean is that a linear acceleration profile produces a s-curved displacement profile. – Connor Wolf Nov 13 '12 at 9:44
• @FakeName - Yeah, now that I watch it again, it's hard to see any acceleration going on. It might be that the acceleration phase is so short that it's impossible to detect in the video. In which case it's not really a fair test either. – Rocketmagnet Nov 13 '12 at 10:32
• @Rocketmagnet - Oh, I certainly see acceleration, but I don't see ramped acceleration. It looks like straight-up linear acceleration to me. – Connor Wolf Nov 14 '12 at 2:40

Instead of putting your ramp values in an array, just calculate them on the fly, if you have sufficient horsepower in your Arduino:

function ramp (int i)
{
return 1000 - sin(t * i) * 900;
}


You can tweak the equation as needed.

• no this compromise the movement of the motor is not a suitable solution – nkint Aug 23 '12 at 7:25