I've been working hard all day to get this to work.

I'm playing around with the math module in python. I eventually want the following code to help me control the speed of a servo motor. The idea is to get the servo speed to accelerate and then decelerate as it reaches its set position--in a parabolic way you might say.

The problem is I'm getting wavy numbers apparently from the algorithm I've set up and it needs to be a nice smooth transition.

from __future__ import division
import math
import time

def Interpolation():
    EffectPercentage = 1
    Step = 1
    nSteps = 32
    MovementStepPercentage = EffectPercentage/nSteps*Step
    print MovementStepPercentage
    while Step << 32:
        Interpolation = (1-math.sin(MovementStepPercentage*180+90))/2*EffectPercentage+MovementStepPercentage*(1-EffectPercentage)
        print "Interpolation =", Interpolation
        Step = Step+1
        print "Step =", Step
        MovementStepPercentage = EffectPercentage/nSteps*Step
        print MovementStepPercentage
        if Step == 32:


I know a thing or two about math, but not enough to get this to work apparently... any pointers?

7 Years
Discussion Span
Last Post by woooee

This is left bit shift

while Step << 32:
while Step < 32:     ## instead?

If you want to increase the speed of a motor, at higher motor speeds the same increase will yield a greater increase in the speed of the motor because of the initial friction which may be part of the reason that there isn't a smooth increase.

Also, depending on the version of Python you are using, you may be getting an integer instead of a floating point.

MovementStepPercentage = EffectPercentage/nSteps*Step  ## an integer
MovementStepPercentage = float(EffectPercentage)/nSteps*Step  ## convert the result to float 
#  which affects this 
Interpolation = (1-math.sin(MovementStepPercentage*180+90))/2*EffectPercentage+MovementStepPercentage*(1-EffectPercentage)
# note that the above line is the same as
# (and not sure if you want it this way or not)
(1-math.sin( (MovementStepPercentage*180) +90))

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.