1,105,288 Community Members

PID Code

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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!!

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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
Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

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:

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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)

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

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.

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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.

Question Answered as of 2 Years Ago by Narue
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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

It's visual basic. And I know (and prefer) quite a few modern languages, but my boss insists on vb6.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: