Go Down

Topic: Modelling Temperature Sensor Delay (Read 1 time) previous topic - next topic

TotalNovice

Hi all,
I'm incredibly new to programming and trying to figure out how to compensate for temperature sensor delay in my program.
My problem is that I don't know how to store the previous reading and recall it for use in the equation.

The equation is basically Temperature = TempRead + (TempRead - TempPrev)
where Temperature is a proposed temperature, TempRead is the current reading and TempPrev is the previous reading.

Can anyone help point me in the right direction?

robtillaart

this?
Code: [Select]

setup()
{
  TempPrev = getTemp(); // function that wraps the reading of the temperature
}
loop()
{
  TempRead = getTemp();
  Temperature = TempRead + TempRead - TempPrev;
  TempPrev = TempRead;
  ...
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

michinyon

That code will do what you asked for,   but what you asked for doesn't seem to actually make sense.

What exactly are you trying to calculate with that equation ?

TotalNovice

Robtillaart - thanks so much for your help!

It's part of a larger system, I'm just trying to minimise the delay of the temperature sensor by predicting what the temperature actually is.

michinyon

So you are adding an estimate of the rate of temperature change,  to the latest reading ?  To try to predict what the following reading will be ?   

At the rate at which a microcontroller would be reading the temperature,   that hardly seems necessary.   You may end up simply confusing yourself by amplifying the "noise" which will be present in the temperature readings.

marco_c

#5
Oct 22, 2012, 01:25 am Last Edit: Oct 22, 2012, 04:16 am by marco_c Reason: 1
Temperatures are slow moving inputs compared to the speed of any control devices, so the issue is not generally to predict the temperature changes but to smooth out any of the noise that happens on the input from the sensor. This is usually done by taking an average of a number of values over a set period of time and calling that the 'tempertaure'.
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

dhenry

Quote
The equation is basically Temperature = TempRead + (TempRead - TempPrev)


How about this?

Code: [Select]

unsigned short Temperature(void) {
  static unsigned short TempPrev=0;
  unsigned short TempRead = getTemp(); //read the temperature
  unsigned short tmp = TempRead + (TempRead - TempPrev);
  TempPrev = TempRead;
  return tmp;
}


All if wrap'd in the function call.

TotalNovice

Thanks everyone for your help!

It's for a uni project where they specifically want the temperature change to be predicted in the equation I gave, otherwise I would do what you guys are suggesting  :)

pwillard

This is where things like the PID library can also come in handy.

http://www.arduino.cc/playground/Code/PIDLibrary

michinyon

"It's for a uni project where they specifically want the temperature change to be predicted in the equation"

Temperature = TempRead + ( TempRead - TempPrev )
               
It seems to me that this equation does not 
(a)  estimate the actual temperature
(b)  estimate the temperature delta since the last reading
(c)  estimate the rate of change of the temperature
(d)  make a useful prediction of the temperature.

It doesn't seem to do any of those things.

Just because "they" want something,  doesn't make it right.


dhenry

Quote
Temperature = TempRead + ( TempRead - TempPrev )


it is a crude form of forward error estimation. What it does is to say that the same "error", as defined by TempRead - TempPrev, will persist for the next read, which is generally true and helpful for slow-moving measurements, like temperature.

However, in cases of very high gain (very frequent temperature readings), this approach will yield unstable readings - it is essentially the D in a pid controller.

A different from of it would go like this:

Temperature = TempRead + (( TempRead - TempPrev ) - (TempPrev - TempPrev2)); where TempPrev2 is the temperature two readings ago.

Go Up