Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.dsp, sci.electronics.design, comp.arch.embedded |
| Subject | Re: PID Without a PhD, Finally |
| Date | 2016-04-15 17:25 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ner0vt$87l$1@dont-email.me> (permalink) |
| References | <wpydnbi9wr9Ujo3KnZ2dnUU7-WXNnZ2d@giganews.com> <MPG.317afdaaa5058629897ee@172.16.0.1> |
Cross-posted to 3 groups.
On 15/04/16 15:31, Paul wrote:
> In article <wpydnbi9wr9Ujo3KnZ2dnUU7-WXNnZ2d@giganews.com>,
> seemywebsite@myfooter.really says...
>>
>> Embedded Systems Design (or whatever they call themselves) kept moving
>> this around -- so I've revamped it, updated it, and posted it on the web.
>>
>> Take a gander. Please comment on anything you like/don't like. I'm not
>> sure if the way that I'm setting off the math is a Really Good Idea or a
>> Really Bad Idea -- I'm trying to make it easy for the math-averse to skip
>> over it, without breaking up the flow too much for folks who can read
>> math without breaking stride.
>>
>> http://wescottdesign.com/articles/pid/pidWithoutAPhd.pdf
>
> Well in general good other comments people have stated I would add these
> points -
>
> 1/ Code layout on page 14 of the integral state is UGLY, belongs in
> obfuscated C better laid out out that is easier to read at a glance
> is better for those trying to understand.
>
I would not have said it was obfuscated - but it certainly could be a
little better (as could the code on page 22). So far, I have only
looked at the style of the code - I haven't actually looked at the
details to see what it does, nor have I had time to read the whole
paper. Hopefully I will do so some time - but perhaps by then there
will be a second version from feedback from others in this thread!
As this code is for embedded systems, it should be written in a manner
close to that used in common embedded coding standards such as MISRA.
Here are some points here that I think would make the code clearer, as
well as more compatible with things like MISRA, and more suitable for
flexible use in embedded systems:
1. MISRA does not approve of native types such as "double", because the
precision is implementation defined and can vary between C compilers and
targets. I agree with that regarding integers (so use <stdint.h> types
rather than "int" or "short"), but the use of 32-bit and 64-bit IEEE
formats for "float" and "double" is so close to universal that I think
it is unnecessary. However, many people using code like this will want
to use "float" for speed, while others will want "double" for precision.
I would recommend starting the code with:
typedef double real_t; // Use "float" or "double" as desired
Then use "real_t" throughout, instead of "double".
(It may be interesting to have a small section discussing the merits of
these options - on something like a Cortex-M4F with hardware
single-precision floating point, the speed difference could easily be a
factor of 100. It may even be worth mentioning things like compiler
flags - while most people know about basic optimisation flags, they may
not know the huge difference "-ffast-math" can have on code like this.
You may also want to discuss using integers instead of floating point,
especially considering errors, scaling, rounding, and overflows.)
2. Do not give identifiers names such as "dState" and "iState". There
are, unfortunately, a good many programmers who have totally
misunderstood the point of "Hungarian notation" and will think that
these mean a variable "state" with type "double", and another variable
"state" with type "int".
3. Do not give identifiers names that are almost the same. Do not
abbreviate the important part of the name (the "differential" or
"integrator" part) to a single letter. It's fine to have shorter names
for local variables within a function, but names that are visible over a
wider range (such as the struct fields) should be self-explanatory.
4. If you need a comment to tell you what an identifier is, then the
identifier name is bad. It is the identifier's name that says what it
is - a comment may be used to add extra information such as scaling, or
when it is used.
5. Always use the "one true brace" style. Every "if" and "else" should
have brackets.
6. Do not declare more than one identifier in the same declaration.
7. Even the most conservative and backwards development companies have
heard of C99. Declare your variables when you need them, in the
smallest reasonable scope.
Full marks for good character spacing!
typedef double real_t; // Use "float" or "double" as desired
typedef struct {
real_t lastPosition;
real_t integrator;
real_t integratorLimitMax;
real_t integratorLimitMin;
real_t integralGain;
real_t proportionalGain;
real_t derivativeGain;
} pid_s;
real_t updatePID(pid_s * pid, real_t error, real_t position) {
// The "P" part
real_t pTerm = pid->proportionalGain * error;
// The "I" part
pid->integrator += error;
if (pid->integrator > pid->integratorLimitMax) {
pid->integrator = pid->integratorLimitMax;
} else if (pid->integrator < pid->integratorLimitMin) {
pid->integrator = pid->integratorLimitMin;
}
real_t iTerm = pid->integratorGain * pid->integrator;
// The "D" part
real_t dTerm = pid->derivativeGain * (pid->lastPosition - position);
pid->lastPosition = position;
// Combined PID result
return pTerm + iTerm + dTerm;
}
Note that almost no comments are needed - the functionality is clear
from the names used in the code.
Regarding the typesetting, I recommend using the "listings" package
rather than just a verbatim environment:
<https://www.ctan.org/pkg/listings>
<https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings>
The setup I use for C code is something like this:
\lstset{
language=C,
formfeed=\newpage,
tabsize=4,
basicstyle=\small,
numbers=left,
numberstyle=\tiny,
firstnumber=1,
numberfirstline=true,
stepnumber=5,
numbersep=5pt,
extendedchars=true,
showstringspaces=false,
breakatwhitespace=false,
identifierstyle=\color{Black}\itshape,
commentstyle=\color{Gray}\itshape,
stringstyle=\color{Green}
}
(I vary it a little according to the document in question, but this is
probably fine to get you started.)
Back to comp.dsp | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 17:03 -0500
Re: PID Without a PhD, Finally bitrex <bitrex@de.lete.earthlink.net> - 2016-04-14 19:00 -0400
Re: PID Without a PhD, Finally DJ Delorie <dj@delorie.com> - 2016-04-14 19:09 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 18:25 -0500
Re: PID Without a PhD, Finally DaveC <not@home.cow> - 2016-04-14 22:53 -0700
Re: PID Without a PhD, Finally DJ Delorie <dj@delorie.com> - 2016-04-15 14:31 -0400
Re: PID Without a PhD, Finally Tim Wescott <tim@seemywebsite.com> - 2016-04-15 15:02 -0500
Re: PID Without a PhD, Finally Paul Rubin <no.email@nospam.invalid> - 2016-04-26 00:30 -0700
Re: PID Without a PhD, Finally Andre Lodwig <lodwig@pathme.de> - 2016-04-26 09:45 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-26 12:08 -0500
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-26 21:50 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-26 19:34 -0400
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-26 21:39 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-04-26 17:07 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-26 20:50 -0400
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-26 21:57 -0500
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-26 21:45 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-04-27 10:16 -0700
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-27 13:57 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-27 16:25 -0500
Re: PID Without a PhD, Finally Christian Gollwitzer <auriocus@gmx.de> - 2016-04-27 23:55 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-27 17:12 -0500
Re: PID Without a PhD, Finally spope33@speedymail.org (Steve Pope) - 2016-04-28 01:22 +0000
Re: PID Without a PhD, Finally Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-28 12:02 +0300
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-28 21:59 -0500
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-28 22:02 -0500
Re: PID Without a PhD, Finally M Philbrook <jamie_ka1lpa@charter.net> - 2016-04-30 20:12 -0400
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-02 10:50 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-05-02 13:14 -0500
Re: PID Without a PhD, Finally robert bristow-johnson <rbj@audioimagination.com> - 2016-05-02 16:37 -0700
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-02 17:26 -0700
Re: PID Without a PhD, Finally Paul Rubin <no.email@nospam.invalid> - 2016-05-02 17:57 -0700
Re: PID Without a PhD, Finally Paul <paul@pcserviceselectronics.co.uk> - 2016-05-03 17:16 +0100
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-05-03 17:23 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-03 17:07 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-05-04 11:37 -0500
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-05-02 19:05 -0700
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-05-02 19:07 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-02 17:21 -0700
Re: PID Without a PhD, Finally Paul Rubin <no.email@nospam.invalid> - 2016-05-02 17:53 -0700
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-02 19:48 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-05-02 22:31 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-05-03 10:54 -0400
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-03 08:25 -0700
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-05-02 23:23 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-05-03 11:46 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-14 19:11 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 18:41 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-14 21:50 -0400
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-14 21:57 -0400
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-14 21:58 -0400
Re: PID Without a PhD, Finally Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-15 12:28 +0300
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 11:29 -0400
Re: PID Without a PhD, Finally Tim Wescott <Tim@seemywebsite.com> - 2016-04-15 09:31 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:35 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 14:04 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 13:11 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 14:32 -0400
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 13:51 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:57 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 18:12 -0400
Re: PID Without a PhD, Finally gyansorova@gmail.com - 2016-04-14 16:33 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 19:09 -0500
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-14 21:14 -0500
Re: PID Without a PhD, Finally Tim Wescott <tim@seemywebsite.com> - 2016-04-14 23:09 -0500
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-15 00:37 -0500
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:11 -0500
Re: PID Without a PhD, Finally Clifford Heath <no.spam@please.net> - 2016-04-15 03:37 +1000
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 19:12 -0500
Re: PID Without a PhD, Finally Ralph Barone <ralph@invalid.com> - 2016-04-15 06:05 +0000
Re: PID Without a PhD, Finally quiasmox@yahoo.com - 2016-04-14 20:10 -0500
Re: PID Without a PhD, Finally Tim Wescott <tim@seemywebsite.com> - 2016-04-14 23:06 -0500
Re: PID Without a PhD, Finally quiasmox@yahoo.com - 2016-04-14 23:46 -0500
Re: PID Without a PhD, Finally o pere o <me@somewhere.net> - 2016-04-15 11:23 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:18 -0500
Re: PID Without a PhD, Finally Peter Mairhofer <63832452@gmx.net> - 2016-04-15 03:46 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:19 -0500
Re: PID Without a PhD, Finally Paul <paul@pcserviceselectronics.co.uk> - 2016-04-15 14:43 +0100
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:32 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 13:47 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 13:00 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 14:42 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 14:02 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 16:04 -0400
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-15 13:26 -0700
Re: PID Without a PhD, Finally Tim Wescott <tim@seemywebsite.com> - 2016-04-15 15:32 -0500
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-15 15:06 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 20:13 -0400
Re: PID Without a PhD, Finally Jasen Betts <jasen@xnet.co.nz> - 2016-04-16 09:12 +0000
Re: PID Without a PhD, Finally Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-04-16 13:35 +0200
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-16 12:34 -0400
Re: PID Without a PhD, Finally Jasen Betts <jasen@xnet.co.nz> - 2016-04-17 08:12 +0000
Re: PID Without a PhD, Finally Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-15 23:27 +0300
Re: PID Without a PhD, Finally Clifford Heath <no.spam@please.net> - 2016-04-16 12:32 +1000
Re: PID Without a PhD, Finally Paul <paul@pcserviceselectronics.co.uk> - 2016-04-15 14:31 +0100
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 10:20 -0400
Re: PID Without a PhD, Finally David Brown <david.brown@hesbynett.no> - 2016-04-15 17:25 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:30 -0500
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-15 13:44 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:59 -0500
Re: PID Without a PhD, Finally David Brown <david.brown@hesbynett.no> - 2016-04-15 20:37 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 13:53 -0500
Re: PID Without a PhD, Finally Dimiter_Popoff <dp@tgi-sci.com> - 2016-04-15 23:40 +0300
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 16:44 -0500
Re: PID Without a PhD, Finally David Brown <david.brown@hesbynett.no> - 2016-04-16 18:16 +0200
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 14:48 -0400
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 13:59 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 16:23 -0400
Re: PID Without a PhD, Finally Clifford Heath <no.spam@please.net> - 2016-04-16 12:41 +1000
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-15 19:57 -0700
Re: PID Without a PhD, Finally David Brown <david.brown@hesbynett.no> - 2016-04-16 18:21 +0200
Re: PID Without a PhD, Finally Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-04-15 21:19 +0200
Re: PID Without a PhD, Finally Tim Wescott <tim@seemywebsite.com> - 2016-04-15 15:05 -0500
Re: PID Without a PhD, Finally Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2016-04-15 16:18 -0400
Re: PID Without a PhD, Finally Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2016-04-16 19:52 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 19:38 -0500
Re: PID Without a PhD, Finally John S <Sophi.2@invalid.org> - 2016-04-15 22:02 -0500
Re: PID Without a PhD, Finally robert bristow-johnson <rbj@audioimagination.com> - 2016-04-15 20:36 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-18 11:34 -0500
Re: PID Without a PhD, Finally robert bristow-johnson <rbj@audioimagination.com> - 2016-04-19 22:14 -0700
Re: PID Without a PhD, Finally robert bristow-johnson <rbj@audioimagination.com> - 2016-04-19 22:57 -0700
Re: PID Without a PhD, Finally Christian Gollwitzer <auriocus@gmx.de> - 2016-04-16 09:25 +0200
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-16 15:44 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-17 00:58 -0400
Re: PID Without a PhD, Finally Christian Gollwitzer <auriocus@gmx.de> - 2016-04-17 08:41 +0200
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-18 11:49 -0500
Re: PID Without a PhD, Finally makolber@yahoo.com - 2016-04-18 13:43 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-18 16:24 -0500
Re: PID Without a PhD, Finally lasselangwadtchristensen@gmail.com - 2016-04-18 15:19 -0700
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-18 16:19 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-20 00:34 -0400
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-19 23:07 -0700
Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-20 04:23 -0400
Re: PID Without a PhD, Finally makolber@yahoo.com - 2016-04-20 06:17 -0700
Re: PID Without a PhD, Finally Les Cargill <lcargill99@comcast.com> - 2016-04-18 19:50 -0500
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-18 20:01 -0700
Re: PID Without a PhD, Finally Nobody <nobody@nowhere.invalid> - 2016-04-19 12:37 +0100
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-19 11:45 -0700
Re: PID Without a PhD, Finally Mac Decman <dearman.mark@gmail.com> - 2016-04-21 21:16 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-22 11:47 -0500
Re: PID Without a PhD, Finally Mac Decman <dearman.mark@gmail.com> - 2016-04-22 12:20 -0700
Re: PID Without a PhD, Finally Mac Decman <dearman.mark@gmail.com> - 2016-04-22 13:02 -0700
Re: PID Without a PhD, Finally OG <AIEO@AIOESPAM.COM> - 2016-04-17 10:12 -0700
Re: PID Without a PhD, Finally gyansorova@gmail.com - 2016-04-17 12:48 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-18 11:36 -0500
Re: PID Without a PhD, Finally John Larkin <jjlarkin@highlandtechnology.com> - 2016-04-26 11:12 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-26 13:46 -0500
Re: PID Without a PhD, Finally makolber@yahoo.com - 2016-04-26 11:54 -0700
Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-26 14:38 -0500
Re: PID Without a PhD, Finally herrmannsfeldt@gmail.com - 2016-04-26 14:09 -0700
csiph-web