Pages: [1]   Go Down
Author Topic: Modelling Temperature Sensor Delay  (Read 941 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this?
Code:
setup()
{
  TempPrev = getTemp(); // function that wraps the reading of the temperature
}
loop()
{
  TempRead = getTemp();
  Temperature = TempRead + TempRead - TempPrev;
  TempPrev = TempRead;
  ...
}
Logged

Rob Tillaart

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

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3031
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3031
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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'.
« Last Edit: October 21, 2012, 09:16:15 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How about this?

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley
Logged

Cumming, GA
Offline Offline
Edison Member
*
Karma: 20
Posts: 1655
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3031
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: