My boss asked me to write a PID class to control a motor today. No big deal, except that the deadline is tommorow evening. My calculus is rusty, and my vb6 is probably worse (which is the language I am to use). Anyone have a good understanding of a PID controller or better yet a working example (in vb6 would be a bonus, but just pseudocode would suffice). Thanks!!

Well, seeing as no one seems to be taking my deadline as seriously as I, this is what I came up with on my own. It's about as simple a PID controller as you can make, and would like to expand a bit on it for things such as tuning etc.

Option Explicit
'desired value
Private mSetPoint As Double

'Constants for proportion, integral, and derivative
Private mKp As Double
Private mKi As Double
Private mKd As Double

'Proportion, last proportion, integral, and differentiation
Private mP As Double
Private mPLast As Double
Private mI As Double
Private mD As Double

Private mInterval As Double 'Time between updates (probably 500msec)

Private mValue As Double

Private mMin As Double
Private mMax As Double

'The actual PID algorithm
Public Sub Update(argActual As Double)
    mP = mSetPoint - argActual
    mI = mI + (mP * mInterval)
    mD = (mP - mPLast) / mInterval
    mPLast = mP
    mValue = (mP * mKp) + (mI * mKi) + (mD * mKd)
    If (mValue < mMin) Then mValue = mMin
    If (mValue > mMax) Then mValue = mMax
End Sub

Well, seeing as no one seems to be taking my deadline as seriously as I

I would think that's obvious. Your deadline means nothing to us, and many of us are perverse enough to respond more slowly to "urgent" questions. Let's also not fail to notice that it's unreasonable to get angry that nobody in the narrow audience of VB6 PID controller writers hasn't replied after a mere two hours. :icon_rolleyes:

I would think that's obvious. Your deadline means nothing to us, and many of us are perverse enough to respond more slowly to "urgent" questions. Let's also not fail to notice that it's unreasonable to get angry that nobody in the narrow audience of VB6 PID controller writers hasn't replied after a mere two hours. :icon_rolleyes:

I forgot to put a smiley to emphasize that I was kidding, but really I was so blunt and egotistical sounding that I thought this would be obvious :twisted:. Apparently sarcasm is not as easy to detect through text as it is in person.... By the way, do YOU have any information regarding PID controllers (sorry for being shameless)

Apparently sarcasm is not as easy to detect through text as it is in person....

A cross we of the dry wit brigade must all bear.

By the way, do YOU have any information regarding PID controllers (sorry for being shameless)

Not that couldn't be gleaned from a similar Google search that I would use to fake sounding knowledgeable. ;) But for actually addressing your problem, faking it won't help.

Hah. Believe me, I tried the Google route. Pretty much ended up with either A)wiki pages with formulas containing unlabeled variables and fuzzy implementations or b)horribly messy, uncommented, and completely hard-coded spaghetti code.

Good news though, I put it installed the PID module (as above with a little modification) on the customers system today and he told me that I am "The foundation of my company". :P So this thread is solved.

Hah. Believe me, I tried the Google route. Pretty much ended up with either A)wiki pages with formulas containing unlabeled variables and fuzzy implementations or b)horribly messy, uncommented, and completely hard-coded spaghetti code.

Good news though, I put it installed the PID module (as above with a little modification) on the customers system today and he told me that I am "The foundation of my company". :P So this thread is solved.

That sounds like Basic, maybe you should learn modern language:
http://code.activestate.com/recipes/577231-discrete-pid-controller/ (I would remove the getters and setters though)
http://cgkit.sourceforge.net/doc2/pidcontroller.html#PIDController

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.