PID library - How to tell when compute executes

Hi all,

I'm using the PID_v1 library. I am averaging my process variable and I need to know when the compute executes to reset my average. How can I tell when this happens?

Looking at the Compute() function, it shows that it returns TRUE or FALSE.

(From http://playground.arduino.cc/Code/PIDLibraryCompute)

Compute()
Description

Contains the pid algorithm. it should be called once every loop(). Most of the time it will just return without doing anything. At a frequency specified by SetSampleTime it will calculate a new Output.
Syntax

Compute()
Parameters
None
Returns
True: when the output is computed
False: when nothing has been done

How do I evaluate this? I've tried passing a boolean - PidAngle.Compute(PidAngleDn)

Doesn't work...

How to use the returned value from a library?

Thanks,

Steve

I've tried passing a boolean - PidAngle.Compute(PidAngleDn)

Don't pass it anything. Test what it returns to determine when the output has been computed.

if ( PidAngle.Compute() == true)
{
    //do stuff if PID was computed
}

Bob,

That's it! Thanks a bunch!

Steve

Well, even though it compiles, it doesn't seem to work, or at least the way I think it should work...

Or could be something wrong with my code?

Here is what I'm doing:

if(PidSpeed.Compute() == false)
{
SpdAvgTot = SpdAvgTot + PidAngleOut;
++SpdAvgCnt;
SpdAvg = SpdAvgTot/SpdAvgCnt;
}
else
{
SpdAvgTot = 0;
SpdAvgCnt = 0;
SpdAvg = 0;
++TempCnt;
}

The first part seems to execute continuously, but the second part never executes. TempCnt stays at 0.

What do you see if you capture PidSpeed.Compute() in a variable and print it ? Is it always false ?

Can you please post all of your program so that we can see the context and how/where variables are declared and initialised ?

The first part seems to execute continuously, but the second part never executes.

Under what circumstances would YOU expect Compute() to fail? Why? Are you doing something to ensure that those circumstances are encountered?

Have you actually looked at the code to see if your assumptions are correct?

The PID library executes at preset intervals using PidSpeed.SetSampleTime(100); <- This will execute at 100ms intervals. I need to know when it actually executes so I can reset the average of the process variable. I may just try to use a moving average but I would still like to know when it actually executes for other reasons.

@UKHeliBob - I haven't tried to print the PidSpeed.Compute() in a variable. I've just added debug counters ++TempCnt; in there and printed those to an LCD but they never increment.

@PaulS - It isn't expected to "fail", it just executes based on it's preset interval.

All of the code is too long to post... The message exceeds the maximum allowed length (9500 characters).

edit - attached code.

Code.ino (16.2 KB)