Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.electronics.design > #407563 > unrolled thread

PID Without a PhD, Finally

Started byTim Wescott <seemywebsite@myfooter.really>
First post2016-04-14 17:03 -0500
Last post2016-04-26 13:46 -0500
Articles 20 on this page of 98 — 31 participants

Back to article view | Back to sci.electronics.design


Contents

  PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 17:03 -0500
    Re: PID Without a PhD, Finally mrdarrett@gmail.com - 2016-04-14 15:09 -0700
      Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 17:47 -0500
    Re: PID Without a PhD, Finally mrdarrett@gmail.com - 2016-04-14 15:39 -0700
      Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-14 17:46 -0500
      Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-14 19:16 -0400
    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 George Herold <gherold@teachspin.com> - 2016-04-26 18:11 -0700
              Re: PID Without a PhD, Finally rickman <gnuarm@gmail.com> - 2016-04-26 21:22 -0400
            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 Les Cargill <lcargill99@comcast.com> - 2016-04-28 22:02 -0500
    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 mrdarrett@gmail.com - 2016-04-14 16:43 -0700
        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 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 Phil Hobbs <pcdhobbs@gmail.com> - 2016-04-15 00:42 -0700
    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 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 Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-04-15 12:41 -0700
              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 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 George Herold <gherold@teachspin.com> - 2016-04-15 10:18 -0700
      Re: PID Without a PhD, Finally Tim Wescott <seemywebsite@myfooter.really> - 2016-04-15 12:41 -0500
        Re: PID Without a PhD, Finally Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-04-15 12:47 -0700
          Re: PID Without a PhD, Finally "Tim Williams" <tiwill@seventransistorlabs.com> - 2016-04-16 16:17 -0500
        Re: PID Without a PhD, Finally George Herold <gherold@teachspin.com> - 2016-04-15 17:55 -0700
    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 Christian Gollwitzer <auriocus@gmx.de> - 2016-04-16 09:25 +0200
    Re: PID Without a PhD, Finally OG <AIEO@AIOESPAM.COM> - 2016-04-17 10:12 -0700
    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

Page 4 of 5 — ← Prev page 1 2 3 [4] 5  Next page →


#407797

FromJasen Betts <jasen@xnet.co.nz>
Date2016-04-16 09:12 +0000
Message-ID<nesvl0$2vl$1@gonzo.alcatraz>
In reply to#407680
On 2016-04-15, rickman <gnuarm@gmail.com> wrote:
> On 4/15/2016 1:32 PM, Tim Wescott wrote:
>> On Fri, 15 Apr 2016 14:43:03 +0100, Paul wrote:

>
> I was going to make that comment, but then I recalled the term my 
> professor actually used was "difference equations".  "Differential 
> equations" means something else, no?  I couldn't find any sources on 
> this, so I didn't make the comment.  It's been far too long to trust my 
> memory.

AIUI  differential equations is where you have two (or more)
interacting models (eg: population of rabbits, population of foxes) 
and then you've got to simplify to a sigle model. 


-- 
  \_(ツ)_

[toc] | [prev] | [next] | [standalone]


#407801

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2016-04-16 13:35 +0200
Message-ID<dnemauFmjjmU1@mid.dfncis.de>
In reply to#407797
Am 16.04.2016 um 11:12 schrieb Jasen Betts:

> AIUI  differential equations is where you have two (or more)
> interacting models (eg: population of rabbits, population of foxes)
> and then you've got to simplify to a sigle model.

Not really.  Differential equations are equations containing one or more 
derivatives of a given function, and usually the function itself, too. 
I.e. they deal in continuous, non-quantized quantities, and 
differentiable functions.  Their solution, if it can be found, is the 
function itself.  The simplest one is

      d/dt y(t) = k * y(t)

Its general solution is

     y(t) = k1 * exp(k*t)

If there's only one independent variable in play like in this example, 
they are "ordinary" differential equations, or ODEs. If there are 
several, e.g. two or three spatial coordinates, they're "partial" 
differential equations, or PDEs (because they contain "partial" 
derivatives).  There can be systems of DEs, too.  The most famous system 
of PDEs must be that of Maxwell's equations for electro-magnetism.

Those population models you mentioned are usually done as _difference_ 
equations, where all quantities are treated in discrete steps of the 
independent variable, time: something like years, seasons, procreation 
cycles.  The rabbits and foxes case is a system of two coupled ordinary 
difference equations, like this:

     r[next_year] = k1 * r[this_year] - k2 * f[this_year]
     f[next_year] = k3 * f[this_year] + k4 * r[this_year]

[toc] | [prev] | [next] | [standalone]


#407829

Fromrickman <gnuarm@gmail.com>
Date2016-04-16 12:34 -0400
Message-ID<netpd4$1vq$1@dont-email.me>
In reply to#407797
On 4/16/2016 5:12 AM, Jasen Betts wrote:
> On 2016-04-15, rickman <gnuarm@gmail.com> wrote:
>> On 4/15/2016 1:32 PM, Tim Wescott wrote:
>>> On Fri, 15 Apr 2016 14:43:03 +0100, Paul wrote:
>
>>
>> I was going to make that comment, but then I recalled the term my
>> professor actually used was "difference equations".  "Differential
>> equations" means something else, no?  I couldn't find any sources on
>> this, so I didn't make the comment.  It's been far too long to trust my
>> memory.
>
> AIUI  differential equations is where you have two (or more)
> interacting models (eg: population of rabbits, population of foxes)
> and then you've got to simplify to a sigle model.

Differential equations may be used for that, but that is not what a 
differential equation is.  It is one which uses a function and at lease 
one of its derivatives.  These equations only have closed form solutions 
in a small percentage of cases and often require special techniques for 
solving them.

-- 

Rick

[toc] | [prev] | [next] | [standalone]


#407887

FromJasen Betts <jasen@xnet.co.nz>
Date2016-04-17 08:12 +0000
Message-ID<nevgi9$f05$1@gonzo.alcatraz>
In reply to#407829
On 2016-04-16, rickman <gnuarm@gmail.com> wrote:
> On 4/16/2016 5:12 AM, Jasen Betts wrote:
>
>> AIUI  differential equations is where you have two (or more)
>> interacting models (eg: population of rabbits, population of foxes)
>> and then you've got to simplify to a sigle model.
>
> Differential equations may be used for that, but that is not what a 
> differential equation is.  It is one which uses a function and at lease 
> one of its derivatives.  These equations only have closed form solutions 
> in a small percentage of cases and often require special techniques for 
> solving them.

ah. thanks.

-- 
  \_(ツ)_

[toc] | [prev] | [next] | [standalone]


#407732

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-15 23:27 +0300
Message-ID<nerike$gc6$1@dont-email.me>
In reply to#407676
On 15.4.2016 г. 20:32, Tim Wescott wrote:
> On Fri, 15 Apr 2016 14:43:03 +0100, 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
>>
>> OOps just spotted a terminology blunder
>>
>> Page 2
>>    The "PID" in "PID Control" stands for "Proportional, Integral,
>>    Derivative".
>>
>> Page 19
>>
>>     Section Title is "Differential"  Should be "Derivative"
>>
>>  From there on you interchange differential and derivavtive all over the
>> place.
>
> That calls for clarification.  "Derivative" is the correct term if you're
> doing the work with continuous-time hardware (i.e. op-amps, resistors and
> capacitors).  "Differential" is the correct term if you're doing the work
> in sampled time.
>
> So I need to figure out how to work that in.
>

I think derivative is better, it is about the first derivative of the
feedback signal after all. Since it is the result of differentiation
"differential" gets in use but in electronics differential is used a lot
as in "differential signalling", which has little to do with that.
Unless I got it totally wrong this is what causes some confusion
(not much though, anyone who wants to dig into it and understand what
you are teaching will figure it out and if not it will be hopeless
anyway).

Dimiter

[toc] | [prev] | [next] | [standalone]


#407772

FromClifford Heath <no.spam@please.net>
Date2016-04-16 12:32 +1000
Message-ID<5711a467$0$9281$c3e8da3$5d8fb80f@news.astraweb.com>
In reply to#407732
On 16/04/16 06:27, Dimiter_Popoff wrote:
> On 15.4.2016 г. 20:32, Tim Wescott wrote:
>> On Fri, 15 Apr 2016 14:43:03 +0100, 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
>>>
>>> OOps just spotted a terminology blunder
>>>
>>> Page 2
>>>    The "PID" in "PID Control" stands for "Proportional, Integral,
>>>    Derivative".
>>>
>>> Page 19
>>>
>>>     Section Title is "Differential"  Should be "Derivative"
>>>
>>>  From there on you interchange differential and derivavtive all over the
>>> place.
>>
>> That calls for clarification.  "Derivative" is the correct term if you're
>> doing the work with continuous-time hardware (i.e. op-amps, resistors and
>> capacitors).  "Differential" is the correct term if you're doing the work
>> in sampled time.
>>
>> So I need to figure out how to work that in.
>>
>
> I think derivative is better, it is about the first derivative of the
> feedback signal after all. Since it is the result of differentiation
> "differential" gets in use but in electronics differential is used a lot
> as in "differential signalling", which has little to do with that.
> Unless I got it totally wrong this is what causes some confusion
> (not much though, anyone who wants to dig into it and understand what
> you are teaching will figure it out and if not it will be hopeless
> anyway).

I would have thought that derivative is just differential as dt->0.
When dt > 0 you have a difference equation, as in digital control.
If you implement PID in analog and have a true derivative, call it
that.

Clifford Heath.

[toc] | [prev] | [next] | [standalone]


#407655

FromPaul <paul@pcserviceselectronics.co.uk>
Date2016-04-15 14:31 +0100
Message-ID<MPG.317afdaaa5058629897ee@172.16.0.1>
In reply to#407563
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.

2/ Sampling time and loop delay, the closed loop problem that many 
   people overlook is not mentioned in any manner until into the maths.
   Most people forget that digital control systems have a time lag and 
   time between calculations, sampling and worst of all ALL systems
   have a delay between command -> error -> drive -> feedback
   Each part has its own intrinsic delays/inertia and other effects
   that most people never MEASURE.

   Increasing sampling rate or precision does NOT guarantee being able 
   to get to target faster.

   A classic problem I saw was a remotely operated vehicle, with a video 
   link and command back to vehicle. Everybody was annoyed that it could 
   not be driven faster than 30 mph so were looking at using 3D cameras 
   to make it faster. No one had obviously measured delays in system

	Camera frame integration time
	Camera frame transmission time
	Camera frame acquisition time
	frame compression encryption time
	frame radio link transmission time
	frame receive time (missed/corrupt data)
	frame decryption time
	frame network transfer time (multi-computer setup)
	Frame ROTATION by 90 degrees time
	Frame mixing with other graphics
	Frame buffer switching

    Then
	Operator response time
	Controls delay to data time 
	Controls network transfer
	Controls radio link transmission
	Controls receive time (what about missed/corrupt data)
	Controls entered as new commands into drive control loops
	Controls being applied
	Vehicle lag to changes in drive

    So how was 2 video feeds going to speed this up

3/  Limits (especially integrator)

	Limits, limits, limits how often overlooked and bear NO relation 
to 
	reality.

	Examples

       If you have a motor with NO direction control, under what 
	circumstances do you need a NEGATIVE limit ?

	If you have a maximum drive (output level) under what 
	circumstances do you need a larger limit ?

	Real world as in your heater example if you measure ambient
       unless you have some cooling system your minimum IS AMBIENT.

      Light drives can you really have negative light without use of 
      mirrors or other light sources. Ambient light or black is your
      minimum.

	Case I saw was some control software 30 years ago where a PI
	example program had

		Oven
		Heater constant heat drive
		Door that could be open or closed
		2 temperature readings
			Oven
			Ambient

	If you left the door closed the integrator would easily reach
	50,000 degrees C

	If you opened the door it would 'cool' to -500 degrees C and 
	beyond

Just some random ramblings from a nutter to bear in mind


-- 
Paul Carpenter          | paul@pcserviceselectronics.co.uk
<http://www.pcserviceselectronics.co.uk/>    PC Services
<http://www.pcserviceselectronics.co.uk/pi/>  Raspberry Pi Add-ons
<http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font
<http://www.badweb.org.uk/> For those web sites you hate

[toc] | [prev] | [next] | [standalone]


#407659

FromPhil Hobbs <pcdhSpamMeSenseless@electrooptical.net>
Date2016-04-15 10:20 -0400
Message-ID<5710F8B2.9020909@electrooptical.net>
In reply to#407655
On 04/15/2016 09:31 AM, 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.
> 
> 2/ Sampling time and loop delay, the closed loop problem that many 
>    people overlook is not mentioned in any manner until into the maths.
>    Most people forget that digital control systems have a time lag and 
>    time between calculations, sampling and worst of all ALL systems
>    have a delay between command -> error -> drive -> feedback
>    Each part has its own intrinsic delays/inertia and other effects
>    that most people never MEASURE.
> 
>    Increasing sampling rate or precision does NOT guarantee being able 
>    to get to target faster.
> 
>    A classic problem I saw was a remotely operated vehicle, with a video 
>    link and command back to vehicle. Everybody was annoyed that it could 
>    not be driven faster than 30 mph so were looking at using 3D cameras 
>    to make it faster. No one had obviously measured delays in system
> 
> 	Camera frame integration time
> 	Camera frame transmission time
> 	Camera frame acquisition time
> 	frame compression encryption time
> 	frame radio link transmission time
> 	frame receive time (missed/corrupt data)
> 	frame decryption time
> 	frame network transfer time (multi-computer setup)
> 	Frame ROTATION by 90 degrees time
> 	Frame mixing with other graphics
> 	Frame buffer switching
> 
>     Then
> 	Operator response time
> 	Controls delay to data time 
> 	Controls network transfer
> 	Controls radio link transmission
> 	Controls receive time (what about missed/corrupt data)
> 	Controls entered as new commands into drive control loops
> 	Controls being applied
> 	Vehicle lag to changes in drive
> 
>     So how was 2 video feeds going to speed this up

See "A Meeting with Medusa" by Arthur Clarke. (Really a good story.)

Cheers

Phil Hobbs (who's not qualified to comment on Tim's magnum opus.) ;)

-- 
Dr Philip C D Hobbs
Principal Consultant
ElectroOptical Innovations LLC
Optics, Electro-optics, Photonics, Analog Electronics

160 North State Road #203
Briarcliff Manor NY 10510

hobbs at electrooptical dot net
http://electrooptical.net

[toc] | [prev] | [next] | [standalone]


#407662

FromDavid Brown <david.brown@hesbynett.no>
Date2016-04-15 17:25 +0200
Message-ID<ner0vt$87l$1@dont-email.me>
In reply to#407655
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.)

[toc] | [prev] | [next] | [standalone]


#407675

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-15 12:30 -0500
Message-ID<xdGdnT0xP7CouIzKnZ2dnUU7-X-dnZ2d@giganews.com>
In reply to#407662
On Fri, 15 Apr 2016 17:25:58 +0200, David Brown wrote:

> 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.)

Hi David:

Thank you for your pertinent comments.  I think I'll apply most of them.  
The only scary part of your comments are the "if someone uses the code" 
-- I originally meant this code to be for illustrative purposes only, not 
for use in production systems.  My assumption was that people would read 
the article, grok the material, then go off and write code from scratch.

Thus, the first time I got feedback along the lines of "we copied your 
code into our production software and it works GREAT!" my response was

"WHAT!?!  NOOOO!"

So I would be tempted to take your real_t and change it to 
do_not_use_this_for_real_t.

I have a question, since you brought it up and since I'm lazy.  In many 
control systems, 32-bit IEEE floating point is inadequate, because a 24 
bit significand just doesn't cut it.  So the choice of double, with its 
bigger significand, is usually necessary.

Is there a better way to express the "you damned well need 'double' here, 
bub" in code than just using "double", or a comment to that effect?

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#407679

Fromrickman <gnuarm@gmail.com>
Date2016-04-15 13:44 -0400
Message-ID<ner94c$7q7$1@dont-email.me>
In reply to#407675
On 4/15/2016 1:30 PM, Tim Wescott wrote:
>
> Thank you for your pertinent comments.  I think I'll apply most of them.
> The only scary part of your comments are the "if someone uses the code"
> -- I originally meant this code to be for illustrative purposes only, not
> for use in production systems.  My assumption was that people would read
> the article, grok the material, then go off and write code from scratch.
>
> Thus, the first time I got feedback along the lines of "we copied your
> code into our production software and it works GREAT!" my response was
>
> "WHAT!?!  NOOOO!"

That is why papers often use pseudo code rather than real code that can 
be copied.


> So I would be tempted to take your real_t and change it to
> do_not_use_this_for_real_t.
>
> I have a question, since you brought it up and since I'm lazy.  In many
> control systems, 32-bit IEEE floating point is inadequate, because a 24
> bit significand just doesn't cut it.  So the choice of double, with its
> bigger significand, is usually necessary.
>
> Is there a better way to express the "you damned well need 'double' here,
> bub" in code than just using "double", or a comment to that effect?

How about a comment that you shouldn't be doing this unless you actually 
understand what is happening?

-- 

Rick

[toc] | [prev] | [next] | [standalone]


#407686

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-15 12:59 -0500
Message-ID<xdGdnToxP7BztozKnZ2dnUU7-X-dnZ2d@giganews.com>
In reply to#407679
On Fri, 15 Apr 2016 13:44:54 -0400, rickman wrote:

> On 4/15/2016 1:30 PM, Tim Wescott wrote:
>>
>> Thank you for your pertinent comments.  I think I'll apply most of
>> them. The only scary part of your comments are the "if someone uses the
>> code" -- I originally meant this code to be for illustrative purposes
>> only, not for use in production systems.  My assumption was that people
>> would read the article, grok the material, then go off and write code
>> from scratch.
>>
>> Thus, the first time I got feedback along the lines of "we copied your
>> code into our production software and it works GREAT!" my response was
>>
>> "WHAT!?!  NOOOO!"
> 
> That is why papers often use pseudo code rather than real code that can
> be copied.
> 
> 
>> So I would be tempted to take your real_t and change it to
>> do_not_use_this_for_real_t.
>>
>> I have a question, since you brought it up and since I'm lazy.  In many
>> control systems, 32-bit IEEE floating point is inadequate, because a 24
>> bit significand just doesn't cut it.  So the choice of double, with its
>> bigger significand, is usually necessary.
>>
>> Is there a better way to express the "you damned well need 'double'
>> here,
>> bub" in code than just using "double", or a comment to that effect?
> 
> How about a comment that you shouldn't be doing this unless you actually
> understand what is happening?

Because then you would never start.  Possibly 90% of the control loops in 
the world are implemented by people who have no greater understanding of 
control theory than what's presented in that paper.  I'm not trying to 
turn people into control engineers, I'm trying to supply a pragmatic 
solution to a large class of real-world problems.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#407701

FromDavid Brown <david.brown@hesbynett.no>
Date2016-04-15 20:37 +0200
Message-ID<nerc75$lss$1@dont-email.me>
In reply to#407675
On 15/04/16 19:30, Tim Wescott wrote:
<snip>
> Hi David:
> 
> Thank you for your pertinent comments.  I think I'll apply most of them.  
> The only scary part of your comments are the "if someone uses the code" 
> -- I originally meant this code to be for illustrative purposes only, not 
> for use in production systems.  My assumption was that people would read 
> the article, grok the material, then go off and write code from scratch.
> 
> Thus, the first time I got feedback along the lines of "we copied your 
> code into our production software and it works GREAT!" my response was
> 
> "WHAT!?!  NOOOO!"

You might want to give that section a better title than "The Complete
Controller", with the program description "Here is the full text of the
PID controller code" :-)  Perhaps a big warning in red that this is
example code to get started.

> 
> So I would be tempted to take your real_t and change it to 
> do_not_use_this_for_real_t.
> 
> I have a question, since you brought it up and since I'm lazy.  In many 
> control systems, 32-bit IEEE floating point is inadequate, because a 24 
> bit significand just doesn't cut it.  So the choice of double, with its 
> bigger significand, is usually necessary.
> 
> Is there a better way to express the "you damned well need 'double' here, 
> bub" in code than just using "double", or a comment to that effect?
> 

In a good many systems, double is not good enough either - it will be
too slow.  The developer needs to use some sort of scaled integers to
get 32 bits of precision (especially in the integrator), and may even
need to use 64-bit integer arithmetic for intermediary results.

It would be nice if there were some equivalent to <stdint.h> that
defined types like float32_t and float64_t, but there isn't one.  There
are some macros in <float.h> defining aspects of floating point types,
but they are a bit ugly - and since you are always going to have 32-bit
IEEE floats and 64-bit IEEE doubles (unless your compiler cheats by
using 32-bit doubles, as you sometimes see on small micros), there is
nothing there worth checking.  If 32-bit IEEE is not good enough, just
write "double" - and make a note that it has to be a /real/ double, not
a non-standard 32-bit version.

[toc] | [prev] | [next] | [standalone]


#407705

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-15 13:53 -0500
Message-ID<_p6dnZP74PYCpYzKnZ2dnUU7-QPNnZ2d@giganews.com>
In reply to#407701
On Fri, 15 Apr 2016 20:37:34 +0200, David Brown wrote:

> On 15/04/16 19:30, Tim Wescott wrote:
> <snip>
>> Hi David:
>> 
>> Thank you for your pertinent comments.  I think I'll apply most of
>> them. The only scary part of your comments are the "if someone uses the
>> code" -- I originally meant this code to be for illustrative purposes
>> only, not for use in production systems.  My assumption was that people
>> would read the article, grok the material, then go off and write code
>> from scratch.
>> 
>> Thus, the first time I got feedback along the lines of "we copied your
>> code into our production software and it works GREAT!" my response was
>> 
>> "WHAT!?!  NOOOO!"
> 
> You might want to give that section a better title than "The Complete
> Controller", with the program description "Here is the full text of the
> PID controller code" :-)  Perhaps a big warning in red that this is
> example code to get started.
> 
> 
>> So I would be tempted to take your real_t and change it to
>> do_not_use_this_for_real_t.
>> 
>> I have a question, since you brought it up and since I'm lazy.  In many
>> control systems, 32-bit IEEE floating point is inadequate, because a 24
>> bit significand just doesn't cut it.  So the choice of double, with its
>> bigger significand, is usually necessary.
>> 
>> Is there a better way to express the "you damned well need 'double'
>> here,
>> bub" in code than just using "double", or a comment to that effect?
>> 
>> 
> In a good many systems, double is not good enough either - it will be
> too slow.  The developer needs to use some sort of scaled integers to
> get 32 bits of precision (especially in the integrator), and may even
> need to use 64-bit integer arithmetic for intermediary results.
>
> It would be nice if there were some equivalent to <stdint.h> that
> defined types like float32_t and float64_t, but there isn't one.  There
> are some macros in <float.h> defining aspects of floating point types,
> but they are a bit ugly - and since you are always going to have 32-bit
> IEEE floats and 64-bit IEEE doubles (unless your compiler cheats by
> using 32-bit doubles, as you sometimes see on small micros), there is
> nothing there worth checking.  If 32-bit IEEE is not good enough, just
> write "double" - and make a note that it has to be a /real/ double, not
> a non-standard 32-bit version.

I should make sure that the speed problems with double are pointed out -- 
I think I mention it, but I may have been so totally focused on just 
showing the implementation in the simplest way possible that I forget.

32 bit fixed-point is more than adequate for nearly anything, although 
you do sometimes have to take care with the integrator to make sure that 
this holds up with real code.  If you need more, and you're not building 
LIGO-III, then you may well be sampling horrendously faster than you need 
to.

My preference if I'm doing a complicated control system is to use a 
fractional arithmetic type, where the underlying storage is twos 
compliment integers but the functional type has values that range from 
almost -1 to almost +1.  You grab integer data, scale it to a fractional, 
do all the control calculations in fractional, and then convert back to 
integer to apply to a DAC or PWM generator or whatever.  Implementation 
can be in either C or C++, although you can make more readable code with C
++.

I cover various data types (including ints, fractional, and the pitfalls 
of floating point) in my book.  There's enough to fill a chapter.  You do 
need to have a good understanding of what's happening to the data under 
the hood if you're going to do controls or signal processing.

Which reminds me -- at some point I remember seeing that fractional 
arithmetic was a suggested type for C, but I never kept track of whether 
it went someplace useful or is a dead end.

For all that, there are useful control loops to be closed out there that 
can profitably use double-precision floating point on a PIC or an 8051 -- 
just not too many of them.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#407716

FromLasse Langwadt Christensen <langwadt@fonz.dk>
Date2016-04-15 12:41 -0700
Message-ID<5cbbd00a-15a3-4d89-bc54-fef5e7dbffc0@googlegroups.com>
In reply to#407705
Den fredag den 15. april 2016 kl. 20.53.25 UTC+2 skrev Tim Wescott:
> On Fri, 15 Apr 2016 20:37:34 +0200, David Brown wrote:
> 
> > On 15/04/16 19:30, Tim Wescott wrote:
> > <snip>
> >> Hi David:
> >> 
> >> Thank you for your pertinent comments.  I think I'll apply most of
> >> them. The only scary part of your comments are the "if someone uses the
> >> code" -- I originally meant this code to be for illustrative purposes
> >> only, not for use in production systems.  My assumption was that people
> >> would read the article, grok the material, then go off and write code
> >> from scratch.
> >> 
> >> Thus, the first time I got feedback along the lines of "we copied your
> >> code into our production software and it works GREAT!" my response was
> >> 
> >> "WHAT!?!  NOOOO!"
> > 
> > You might want to give that section a better title than "The Complete
> > Controller", with the program description "Here is the full text of the
> > PID controller code" :-)  Perhaps a big warning in red that this is
> > example code to get started.
> > 
> > 
> >> So I would be tempted to take your real_t and change it to
> >> do_not_use_this_for_real_t.
> >> 
> >> I have a question, since you brought it up and since I'm lazy.  In many
> >> control systems, 32-bit IEEE floating point is inadequate, because a 24
> >> bit significand just doesn't cut it.  So the choice of double, with its
> >> bigger significand, is usually necessary.
> >> 
> >> Is there a better way to express the "you damned well need 'double'
> >> here,
> >> bub" in code than just using "double", or a comment to that effect?
> >> 
> >> 
> > In a good many systems, double is not good enough either - it will be
> > too slow.  The developer needs to use some sort of scaled integers to
> > get 32 bits of precision (especially in the integrator), and may even
> > need to use 64-bit integer arithmetic for intermediary results.
> >
> > It would be nice if there were some equivalent to <stdint.h> that
> > defined types like float32_t and float64_t, but there isn't one.  There
> > are some macros in <float.h> defining aspects of floating point types,
> > but they are a bit ugly - and since you are always going to have 32-bit
> > IEEE floats and 64-bit IEEE doubles (unless your compiler cheats by
> > using 32-bit doubles, as you sometimes see on small micros), there is
> > nothing there worth checking.  If 32-bit IEEE is not good enough, just
> > write "double" - and make a note that it has to be a /real/ double, not
> > a non-standard 32-bit version.
> 
> I should make sure that the speed problems with double are pointed out -- 
> I think I mention it, but I may have been so totally focused on just 
> showing the implementation in the simplest way possible that I forget.
> 
> 32 bit fixed-point is more than adequate for nearly anything, although 
> you do sometimes have to take care with the integrator to make sure that 
> this holds up with real code.  If you need more, and you're not building 
> LIGO-III, then you may well be sampling horrendously faster than you need 
> to.
> 
> My preference if I'm doing a complicated control system is to use a 
> fractional arithmetic type, where the underlying storage is twos 
> compliment integers but the functional type has values that range from 
> almost -1 to almost +1.  You grab integer data, scale it to a fractional, 
> do all the control calculations in fractional, and then convert back to 
> integer to apply to a DAC or PWM generator or whatever.  Implementation 
> can be in either C or C++, although you can make more readable code with C
> ++.
> 
> I cover various data types (including ints, fractional, and the pitfalls 
> of floating point) in my book.  There's enough to fill a chapter.  You do 
> need to have a good understanding of what's happening to the data under 
> the hood if you're going to do controls or signal processing.
> 
> Which reminds me -- at some point I remember seeing that fractional 
> arithmetic was a suggested type for C, but I never kept track of whether 
> it went someplace useful or is a dead end.
> 

the ARM CMSIS DSP lib has Q15,Q31,Q63 formats

I'd think most fixed point DSPs uses a 1.xx format and the MUL-ACC handles 
the extra shift need for the muliplication 

-Lasse

[toc] | [prev] | [next] | [standalone]


#407736

FromDimiter_Popoff <dp@tgi-sci.com>
Date2016-04-15 23:40 +0300
Message-ID<nerjeh$je6$1@dont-email.me>
In reply to#407705
On 15.4.2016 г. 21:53, Tim Wescott wrote:
>....
>
> I should make sure that the speed problems with double are pointed out --
> I think I mention it, but I may have been so totally focused on just
> showing the implementation in the simplest way possible that I forget.

I think this is the best approach anyway. Whoever will be
reading your pdf will not be an idiot and will think of such
implications. I went through the thing - for may be 5 minutes - and
never did I stop and think "this should have been done otherwise".

Flooding it with less relevant details will harm it I think.

Then the speed implication is not always true - on one FPU I am
familiar with multiplication costs 2 cycles vs. 1 (dual vs. single).
On another core I am considering at the moment it is 1 cycle in
both cases. Well if the FPU is some 32-bit only cripple and using
64-bit fp will result in some emulation which takes
forever things are much worse of course, but you are teaching PID
with the paper, not the basics of programming.

Dimiter

[toc] | [prev] | [next] | [standalone]


#407743

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-15 16:44 -0500
Message-ID<Jfudnc-Tw4E3_YzKnZ2dnUU7-W-dnZ2d@giganews.com>
In reply to#407736
On Fri, 15 Apr 2016 23:40:59 +0300, Dimiter_Popoff wrote:

> On 15.4.2016 г. 21:53, Tim Wescott wrote:
>>....
>>
>> I should make sure that the speed problems with double are pointed out
>> --
>> I think I mention it, but I may have been so totally focused on just
>> showing the implementation in the simplest way possible that I forget.
> 
> I think this is the best approach anyway. Whoever will be reading your
> pdf will not be an idiot and will think of such implications. I went
> through the thing - for may be 5 minutes - and never did I stop and
> think "this should have been done otherwise".
> 
> Flooding it with less relevant details will harm it I think.
> 
> Then the speed implication is not always true - on one FPU I am familiar
> with multiplication costs 2 cycles vs. 1 (dual vs. single).
> On another core I am considering at the moment it is 1 cycle in both
> cases. Well if the FPU is some 32-bit only cripple and using 64-bit fp
> will result in some emulation which takes forever things are much worse
> of course, but you are teaching PID with the paper, not the basics of
> programming.

What data type to use when is actually not a trivial decision, and it 
pretty much boils down to benchmarking candidates on your target system.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#407826

FromDavid Brown <david.brown@hesbynett.no>
Date2016-04-16 18:16 +0200
Message-ID<netoap$u77$1@dont-email.me>
In reply to#407705
On 15/04/16 20:53, Tim Wescott wrote:

> I cover various data types (including ints, fractional, and the pitfalls
> of floating point) in my book.  There's enough to fill a chapter.  You do
> need to have a good understanding of what's happening to the data under
> the hood if you're going to do controls or signal processing.

Fair enough.  I don't think it would be unreasonable to point out that 
it is covered in your book, or to give a direct link to the book in your 
pdf.  This is part of how you make your living, and people who enjoy and 
benefit from this paper may like to buy the book too.  (I bought it 
myself, many years ago - but unfortunately I lost it in a house move 
before I had read more than the first chapter or so!)

>
> Which reminds me -- at some point I remember seeing that fractional
> arithmetic was a suggested type for C, but I never kept track of whether
> it went someplace useful or is a dead end.

It seems to have died out as an idea.  There were several problems with 
the proposed standard - basically it was too little, too late (people 
already use their own fixed point libraries), too complicated and ugly 
(with all these "_Sat long long _Fract" types), underspecified (there 
was nothing equivalent to <stdint.h>, meaning you could not use them for 
portable programming), and there is little in the way of implementations 
for them.  If there at least were types named "sfract_1_15_t" rather 
than guessing if that might be "short _Fract" or "_Fract", there would 
have been a chance.

gcc has sort-of support for these fixed point types on some targets 
(such as ARM).  It is not complete, and code generation could do with a 
great deal of work.  It's not bad for simple arithmetic on a single 
type, but if you mix types or doing something more complicated, it's 
awful.  And mixed type arithmetic would be one of the main advantages of 
built-in types rather than simple functions.

I think it is more realistic to hope for a standardised C++ template 
class library for fixed point than for it to be an official part of 
future C standards.

>
> For all that, there are useful control loops to be closed out there that
> can profitably use double-precision floating point on a PIC or an 8051 --
> just not too many of them.
>

[toc] | [prev] | [next] | [standalone]


#407704

FromPhil Hobbs <pcdhSpamMeSenseless@electrooptical.net>
Date2016-04-15 14:48 -0400
Message-ID<AZCdnXWzAvX-qozKnZ2dnUU7-UudnZ2d@supernews.com>
In reply to#407675
On 04/15/2016 01:30 PM, Tim Wescott wrote:
> On Fri, 15 Apr 2016 17:25:58 +0200, David Brown wrote:
> 
>> 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.)
> 
> Hi David:
> 
> Thank you for your pertinent comments.  I think I'll apply most of them.  
> The only scary part of your comments are the "if someone uses the code" 
> -- I originally meant this code to be for illustrative purposes only, not 
> for use in production systems.  My assumption was that people would read 
> the article, grok the material, then go off and write code from scratch.
> 
> Thus, the first time I got feedback along the lines of "we copied your 
> code into our production software and it works GREAT!" my response was
> 
> "WHAT!?!  NOOOO!"
> 
> So I would be tempted to take your real_t and change it to 
> do_not_use_this_for_real_t.
> 
> I have a question, since you brought it up and since I'm lazy.  In many 
> control systems, 32-bit IEEE floating point is inadequate, because a 24 
> bit significand just doesn't cut it.  So the choice of double, with its 
> bigger significand, is usually necessary.
> 
> Is there a better way to express the "you damned well need 'double' here, 
> bub" in code than just using "double", or a comment to that effect?
> 

assert(sizeof(real_t) >= 8);

Cheers

Phil Hobbs

-- 
Dr Philip C D Hobbs
Principal Consultant
ElectroOptical Innovations LLC
Optics, Electro-optics, Photonics, Analog Electronics

160 North State Road #203
Briarcliff Manor NY 10510

hobbs at electrooptical dot net
http://electrooptical.net

[toc] | [prev] | [next] | [standalone]


#407706

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-04-15 13:59 -0500
Message-ID<_p6dnZL74PZrpIzKnZ2dnUU7-QOdnZ2d@giganews.com>
In reply to#407704
On Fri, 15 Apr 2016 14:48:03 -0400, Phil Hobbs wrote:

> On 04/15/2016 01:30 PM, Tim Wescott wrote:
>> On Fri, 15 Apr 2016 17:25:58 +0200, David Brown wrote:
>> 
>>> 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.)
>> 
>> Hi David:
>> 
>> Thank you for your pertinent comments.  I think I'll apply most of
>> them. The only scary part of your comments are the "if someone uses the
>> code" -- I originally meant this code to be for illustrative purposes
>> only, not for use in production systems.  My assumption was that people
>> would read the article, grok the material, then go off and write code
>> from scratch.
>> 
>> Thus, the first time I got feedback along the lines of "we copied your
>> code into our production software and it works GREAT!" my response was
>> 
>> "WHAT!?!  NOOOO!"
>> 
>> So I would be tempted to take your real_t and change it to
>> do_not_use_this_for_real_t.
>> 
>> I have a question, since you brought it up and since I'm lazy.  In many
>> control systems, 32-bit IEEE floating point is inadequate, because a 24
>> bit significand just doesn't cut it.  So the choice of double, with its
>> bigger significand, is usually necessary.
>> 
>> Is there a better way to express the "you damned well need 'double'
>> here,
>> bub" in code than just using "double", or a comment to that effect?
>> 
>> 
> assert(sizeof(real_t) >= 8);
> 
> Cheers
> 
> Phil Hobbs

That would give a false alarm on some DSP chips, because sizeof returns 
the number of characters your variable or type fits into, and if the 
processor can't handle data in chunks as small as 8 bits a character is 
bigger (usually 16 or 24, but I believe that it was 32 or 64 in some Cray 
machines).

Fortunately, in a compliant compiler this is captured in CHAR_BIT:

assert(sizeof(real_t) * CHAR_BIT >= 64)

I oversaw the introduction of the TMS320F2812 into a product line where 
we had code that had worked for YEARS, which broke because of the 
implicit assumption that the number of bits in a character was 8.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


Page 4 of 5 — ← Prev page 1 2 3 [4] 5  Next page →

Back to top | Article view | sci.electronics.design


csiph-web