Shop OBEX P1 Docs P2 Docs Learn Events
PWM Ramp calculation — Parallax Forums

PWM Ramp calculation

James LongJames Long Posts: 1,181
edited 2007-05-01 01:51 in General Discussion
I'll admit....I'm not the math wiz as many are around here....so I thought I would throw in this question..

I would like to take a propeller...and make a pwm circuit. The trick here...I would like the ramp to not be linear.

I would like a curve that started out pretty flat and curve upwards toward the end.

So you math people out there.....what type of formula would give me a curve??

Thanks for all the answers in advance,

James L

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-04-28 03:25
    James,

    This depends of how aggressive you want the curve to be. Picture what you want as some increment "X" per unit of time "S".
    If "X" is the same and "S" is the same, then your ramp will be linear. If "X" gradually increases or decreases per unit of time,
    you now have a slope that is non-linear. An exponential curve would essentially double or halve "X" per unit of time.


    Example: (If X <> 0)

    Exponential increase of X per unit of time
    X = X * 2

    Exponential decrease of X per unit of time
    X = X - (X / 2)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2007-04-28 03:41
    Beau,

    Oh I see.....

    This will be a ramp up only....from 0% duty to 100%

    So I would x would be the change in duty.

    My problem....I need the change in duty to start out small (nearly flat)·and·at the end be·agressive(almost vertical).

    Is there a way to calculate it to do this....or would I have to do a lookup type situation.

    I would like a nice equal curve to the ramp. Like an arc (lower right·quarter of a circle)

    another problem....the time is going to be user selectable(the time will stay the same after selected)....so I must find a way to calculate the ramp.

    Is this a sine table function??

    I'm not good with math.....I always thought I was...but I'm learning I'm not.



    James L
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-04-28 05:38
    James,

    The lower right quadrant of a circle ranges from 270 deg to 360 deg,
    therefore we have 90 steps (360-270 = 90)

    If we call this routine on a regular interval, 'RampSpeedIncrement' can be calculated
    as follows...

    RampSpeedIncrement = {90 Steps} / [noparse][[/noparse] {Total Time} / {Regular Interval} ]

    Edit - another way to look at the same formula that might visually make better sense...

    RampSpeedIncrement = [noparse][[/noparse] {90 Steps} * {Regular Interval} ] / {Total Time}



    Suppose the 'Total Time' is 5 seconds (5000mS)
    Suppose the 'Regular Interval' is 20mS

    RampSpeedIncrement = 90 / [noparse][[/noparse] 5000mS / 20mS ]
    RampSpeedIncrement = 90 / 250
    RampSpeedIncrement = .36

    To deal with decimals multiply this by 1000

    RampSpeedIncrement = 360

    or

    RampSpeedIncrement = [noparse][[/noparse] {90 Steps} * 1000 ] / [noparse][[/noparse] {Total Time} / {Regular Interval} ]

        [b]Example[/b]
    
    
    PUB PWMincrement
        
        RampSpeedIncrement := 360
    
        RampSpeed += RampSpeedIncrement
    
        RampSpeed := RampSpeed <# 90_000
    
        Result := 100 + HRsin(270+(RampSpeed/1000))/10
    
    PUB HRsin(deg)
    ''Human Readable Sine  X1000 notation
        Result := (sin(deg2angle(deg))*1000/65535)    
    
    PUB HRcos(deg)
    ''Human Readable Cosine X1000 notation
        Result := (cos(deg2angle(deg))*1000/65535)    
    
    PUB deg2angle(deg)
    
    '' Convert 0-360 deg to 13 bit
        Result := deg * 1024 / 45
    
    PUB cos(angle)
    
    '' Get cosine of angle (0-8191)
    
        Result := sin(angle + $800)
    
    PUB sin(angle)
    
    '' Get sine of angle (0-8191)
    
        Result := angle << 1 & $FFE
        if angle & $800
           Result := word[noparse][[/noparse]$F000 - Result]
        else
           Result := word[noparse][[/noparse]$E000 + Result]
        if angle & $1000
           -Result    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 4/28/2007 7:14:35 AM GMT
  • James LongJames Long Posts: 1,181
    edited 2007-04-28 14:25
    Beau,

    I have a few·questions.....and hope I'm not being stupid....which I have stupid moments....(seem to be more and more)

    Where is location 0/360 deg·in respect to the sine table.

    Is there a reason you included HRcos in the listing of methods?

    Was it just a default thing to have included?

    Sort of interesting......I was not even close on what I had in my head.......

    But thanks for the math lesson......it's not really your job....so I really appreciate it.

    James L
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-04-28 17:52
    James,

    The Sine table is represented as a 13-bit number ranging from 0 to 8191

    i.e.

    0 Deg = 0
    30 Deg = 682
    45 Deg = 1024
    60 Deg = 1365
    90 Deg = 2048
    180 Deg = 4096
    270 Deg = 6144
    360 Deg = 8192 which overflows and ends up being the same thing as 0

    ...So in order to represent a Deg, you need to convert it to it's 13-bit equivalent.

    Deg * 8192 / 360 would do it , but you can simplify the equation down to Deg * 1024 / 45


    ...Further, the value returned from the Sine table is a 16-bit signed value that ranges from -65535 to 65535
    Since the return value of a Sin or Cos can only be in the range of -1 to 1, we apply the returned 16-bit value
    as the numerator using 65535 as our denominator.

    i.e.

    If you take the Sin of 45 deg or sin(1024), the returned value should be 46340

    ...but you say, wait a minute, my calculator shows that the Sin of 45 deg is .707

    ...It is, your calculator is correct, and so is the value 46340. If you take 46340 as your numerator, and 65535 as your denominator, you get...

    46340 / 65535 = .707 ...The reason I multiplied the result by 1000 was so that I could see past the decimal.




    I included the HRcos for two reasons...
    1) just to be complete, and
    2) if you ever wanted the opposite function. FAST to start --> SLOW to finish


    Changing the line that reads...

    Result := 100 + HRsin(270+(RampSpeed/1000))/10        'SLOW to start --> FAST to finish
    
    



    ...so that it reads...

    Result := 100 - HRcos(270+(RampSpeed/1000))/10        'FAST to start --> SLOW to finish
    
    



    ...would do it.




    "But thanks for the math lesson......it's not really your job....so I really appreciate it."

    No, but I have some background experience in closed loop robotic motor control systems, and I like to exercise my brain with this stuff from time to time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 4/29/2007 1:51:22 AM GMT
  • James LongJames Long Posts: 1,181
    edited 2007-04-28 18:13
    Beau,

    Thanks for being thorough. It prevents other people from asking the same question.

    I'm wondering the basis for the next quote:

    "The lower right quadrant of a circle ranges from 270 deg to 360 deg,
    therefore we have 90 steps (360-270 = 90)"

    Is this standard geometry......or based on standard world conditions.

    I'm a little confused on this part of the use. I figured the coordinates would go from 90·deg to 0 deg. (With zero being at the top center of the circle.) I'm just confused........not that it is not right....It might be because I'm trying to use geometry and it doesn't apply here.

    I guess.....I wanted to know how the coordinates of the equation would be figured out.

    James L
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-04-28 18:33
    James,

    I got this from what you said....

    "I would like a nice equal curve to the ramp. Like an arc (lower right quarter of a circle)"

    ...The lower right quarter of a circle is 270 to 360 Deg.




    0 Deg = East
    90 Deg = North
    180 Deg = West
    270 Deg = South

    www.mathematicshelpcentral.com/misc_pdfs/Unit_Circle.pdf
    www.mathmistakes.info/facts/TrigFacts/learn/uc/uc.html

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 4/28/2007 6:48:40 PM GMT
  • James LongJames Long Posts: 1,181
    edited 2007-04-28 19:28
    Beau,

    That answered my question.

    I don't remember that. I actually don't remember ever learning that.

    I would have never even guessed zero deg to be east. Maybe north or west...

    It was a interesting exersize........I just don't know how I got through Geometry , and Physics.........

    I love physics.......and had great grades.....

    Too many years I guess.

    Hey thanks for completing the learning process......now I see the background to the starting degree.

    James L
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-04-28 20:10
    Hi James,

    Here are a couple other ways to approach this, not at all better than Beau's by any means, just a little different.

    The PWM starts with a frqa value of zero for zero pwm, then after a number of steps it reaches frqa=2^32-1, which is 100%. That is true of either linear or nonlinear.

    If there are to be N equal steps, then each one will be 2^32 divided by N. So for example, with 256 equal steps, the each one will be Fs = 2^24, and

    frqa: (0, Fs, 2*Fs, 3*Fs,... ,255*Fs, 256*Fs-1)

    The last step is special, subtracts 1 to assure the 100% value instead of rolling over to zero.

    nonlinear method 1 ---

    It can start with a lower initial value of Fs and gradually increase the rate by an additive factor of Fd at each step:

    first step Fs
    second step Fs + (Fs+Fd)
    third step Fs + (Fs+Fd) + (Fs+2*Fd)
    n-th step Fs + (Fs+Fd) + (Fs+2*Fd)+(Fs+3*Fd), ... (Fs+(n-1)*Fd)

    Each step is bigger than the one before it, and at the end the terms add up to 2^32. The summation works out like this for N steps:

    2^32 = (N * Fs) + (Fd * N * (N-1) / 2)

    Suppose you want N=256 steps and start with an initial step of Fs=2^16. Solve the above for Fd:

    2^32 = (2^8 * 2^16) + (Fd * 2^8 * 255 / 2)
    ==>
    Fd = 2^17 (I hope I got that right!)

    A more reasonable value might be Fs = 2^20 = 1048576, Fd = 123361 (dropping remainder of 0.88235), where the final rate is about 30 times the initial rate.

    So then the steps are, iteratively, starting with an initial value of frqa=0,
    nextFrqa := 1
    repeat N from 0 to 255
        frqa := nextFrqa -1
        nextfrqa := nextfrqa + Fs + N * Fd 
        wait...   ' timing here for N steps in desired time
    



    I subtracted one from each step because it does not substantially affect the PWM output, and it avoids the possibility that the last step would roll over back to zero.

    nonlinear method 2 ----

    Yet another way to do it would be as an exponential sequence like this
    0
    Fs
    Fs * k
    Fs * k^2
    Fs * k^3
    ...
    Fs * k^n
    


    That concludes with Fs * k^N = 2^32-1, 100% PWM.
    Suppose it starts at Fs = 2^20, and there are to be 256 steps. Then 2^20 * k^256 = 2^32, which gives k=1.033024879022. That "compound interest" can be neatly implemented with the ** operator on the propeller, and the ** multipllier is,

    kprop = 0.033024879022 * 2^32 = 141840775

    So the steps are, iteratively, starting with an initial value of frqa=2^20,
    frqa:=frqa := 1 << 20 
    repeat 255
        wait...   ' timing here for N steps in desired time
        frqa := frqa ** 141840775 + frqa   ' this is 1.033024879022 * frqa
    



    In that, the last step is about 125 times larger than the first step. 2*20 may seem like a large starting number, but remember that is is only 0.024 % PWM.

    edit: corrected the k factor in method 2, from 2^20 * k^256 = 2^32, solve for k, {log(k) = 3*log(2) / 64}

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 4/29/2007 7:55:14 PM GMT
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-04-29 21:34
    Here is a graph of 4 functions.

    attachment.php?attachmentid=46929

    Yellow: cosine
    Red: quadratic (method 1 above)
    dark blue: powers (method 2 above)
    light blue: tangent

    I fiddled with the tangent function in excel and since it goes off to infinity at pi/2, it is possible to scale it for practically any steepness you want. The same is partly true of the power series and the quadratic, which both increase toward infinity. The shape of the curve and the final steepness depends on two free parameters.

    I corrected a mistake in my last post, the k factor in method 2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
    795 x 535 - 13K
  • James LongJames Long Posts: 1,181
    edited 2007-04-30 00:49
    Tracy,

    Thanks for being so thorough.......it helps to see a graphical representation of the differences.

    I'm not sure which method I'm going to use.

    It was primarily research for a unit I'm going to build.



    Thanks all for the help,



    James L
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-04-30 16:51
    Hi James,

    Here is another graph (from Excel), to emphasize that there is a family of curves around each algorithm.

    attachment.php?attachmentid=46943

    This is method 2,
    frqa := frqa ** kprop + frqa

    with six different combininations of kprop and initial frqa. There is one boundary condition (remember boundary conditions from physics!), which is that the value at step 256 will be 2^32, 100% PWM. That constraint ties together the two parameters and defines the family of curves. The initial value is a low value of PWM, but there is no independent control over that.

    The parameters are related by log(k) = (32 log(2) - log(Xo)) / 256
    where Xo is the initial value. The prop could calculate that at run time, say, from the log and antilog tables in the HUB rom.

    If the design calls for independent control of the starting PWM, then there would have to be one more parameter, an offset.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 4/30/2007 5:00:10 PM GMT
    786 x 531 - 17K
  • James LongJames Long Posts: 1,181
    edited 2007-04-30 20:20
    Tracy,

    I actually like the curve from ival 128.

    Looks pretty much like the curve I was looking for.

    The starting point can be higher than zero....so that is useful.

    Wow...I must have hit a sweet spot with you.

    You are so into the pwm thing. I do appreciate the time spent working on it.

    You have given me many more options that I hadn't thought about.

    I do have a question....that· maybe you can answer....

    "frqa·:=·frqa·**·141840775·+·frqa···'·this·is·1.033024879022·*·frqa"

    I'm trying to figure out how you solved for the "141840775". The answer eludes me.

    James L
  • PARPAR Posts: 285
    edited 2007-05-01 00:43
    James Long said...

    Tracy,I do have a question....that· maybe you can answer....

    "frqa·:=·frqa·**·141840775·+·frqa···'·this·is·1.033024879022·*·frqa"

    I'm trying to figure out how you solved for the "141840775". The answer eludes me.


    Tracy describes how the value is derived in the text immediately above where you quote him. PAR

    That concludes with Fs * k^N = 2^32-1, 100% PWM.
    Suppose it starts at Fs = 2^20, and there are to be 256 steps. Then 2^20 * k^256 = 2^32, which gives k=1.033024879022. That "compound interest" can be neatly implemented with the ** operator on the propeller, and the ** multipllier is,

    kprop = 0.033024879022 * 2^32 = 141840775
  • James LongJames Long Posts: 1,181
    edited 2007-05-01 01:26
    Duh.....

    Thanks for show me my oversight. PAR

    Figures.....I'm not paying attention.

    Sorry Tracy.



    James L
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-05-01 01:51
    No problem. The ** operator multiplies the two operands together and then implicitly divides by 2^32.
    141840775 / 2^32 = 0.03302487894
    Which is a good approximation to the target fraction,
    0.033024879022

    To find the factor, reverse the procedure, multiply 2^32 times the target fraction:
    2^32 * 0.033024879022 = 141840775
    & drop the remainder or round off.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.