Shop OBEX P1 Docs P2 Docs Learn Events
PID help please — Parallax Forums

PID help please

Zap-oZap-o Posts: 452
edited 2009-11-19 20:34 in Propeller 1
I find that when I run Andy's code the derivative is always 0. I thought the derivative would only be negative when the slope was horizontal?

Basically I am running the code the same as Andy has his.

[code]

PUB Calc_Pid(in): Out
' Calculate Δt
T := Cnt
Dt := T - Tp
Dt := Math.fDiv(Math.FFloat(dt), Math.FFloat(clkfreq))
' Calculate error
E := Math.FSub(TargetTemp,In)

' Calculate proportional
P := Math.FMul(E, kp)

'calculate intergral
Et := Math.FDiv(Math.FAdd(E, Ep), 2.0)
Da := Math.FMul(Et, Dt)
A := Math.FAdd(A, Da)

IF Math.Fcmp(a, maxArea) == 1 'if a is > maxarea
A := maxArea
IF Math.Fcmp(a, minArea) == -1
A := minArea

I := Math.FMul(ki, a)

'calculate Derivate
De := Math.FSub(E, Ep) ' ΔE = (now) - E(old)
Dedt := Math.FDiv(De, Dt) ' ΔE/Δt
D := Math.FMul(kd, Dedt) ' D = kd

Comments

  • Nick MuellerNick Mueller Posts: 815
    edited 2009-11-19 17:30
    > A overview is that I input a floating point number and I should get a output of a number.

    Not having worked with this 'object', but have you set kp, ki and kd?
    kp = 1 should work, the rest can be zero for a start.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Zap-oZap-o Posts: 452
    edited 2009-11-19 17:39
    Nick I have assigned all the variables needed. I think that I am sampling to fast but, I am not sure.
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-11-19 18:33
    > I think that I am sampling to fast but, I am not sure.

    I kind of doubt that you could sample too fast. At least you should get a proportional value.
    I would patch in some prints to see what happens inside of the PID and in big despair add a wait for one second.

    Maybe that helps.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Zap-oZap-o Posts: 452
    edited 2009-11-19 19:21
    I apologize for not being clear people. I am getting the Proportional and integral values as I would expect. Its just the derivative that stays at 0.0 until 2 seconds pass (random really) then quickly goes back to 0. This is in effect makes the derivative useless for my project. As far as I can tell.
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-11-19 19:42
    > Its just the derivative that stays at 0.0 until 2 seconds pass (random really) then quickly goes back to 0.
    > This is in effect makes the derivative useless for my project. As far as I can tell.

    Ah, well. But you never look at the derivate (nor P nor I) itself, you just look at the output. The derivate is added to the output factored by kd. And now, how's you output looking? Does that make the bump?


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Zap-oZap-o Posts: 452
    edited 2009-11-19 20:25
    The output sucks if I just run it with the Kd. It wont run at all. Just wanted to see and compare how the P vs. I vs. D runs with the variables independent. Again I think I am sampling too much or have a code problem.
    Otherwise it runs as to be expected.
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-11-19 20:34
    > The output sucks if I just run it with the Kd.

    That's no wonder!
    Frankly speaking, a PID with only the D-part and P & I set to zero is nonsense. Maybe it can serve as a pink noise generator. smile.gif)

    There are several ways to tune a PID, and one simple:
    Set P=1, I & D = 0.
    Adjust P until the output starts to oszillate. Set P to about 70% of that value. If the output shows a bump after a transition, you can dampen it with the I-part. If you need faster response, increase the D-part. Typically, the D-part is very low (say below 10% of the P) and it only increases the tendency for oszillations. The faster the system is respnding, the lower the I-part has to be.

    There are much more clever ways to adjust a PID.



    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
Sign In or Register to comment.