PID help please
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
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
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
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
> 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
Otherwise it runs as to be expected.
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.
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