Author Topic: bug in my code, where? I2C for BMP085 pressure sensor  (Read 4135 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
bug in my code, where? I2C for BMP085 pressure sensor
« on: November 01, 2010, 12:06:19 PM »
I'm using the Axon and WebbotLib with this sensor:
http://www.sparkfun.com/commerce/product_info.php?products_id=9694

See attached code.

The function BMP085_ReadTemp() outputs the correct temperature, that I'm 100% sure. So that means BMP085_Read2() also works.

However, the pressure output is wrong. Plugging in the example values from the datasheet gets me the correct pressure, so that means my equations in BMP085_Convert() are right:
http://www.sparkfun.com/datasheets/Components/General/BST-BMP085-DS000-05.pdf
(see page 13 for code on the left and values on the right)

So that leaves functions BMP085_ReadPres() and BMP085_Read() that could contain the error.

What the monkies am I doing wrong?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #1 on: November 01, 2010, 01:09:27 PM »
Your code shows 'oss = 4' but the table on Pg 10 of the datasheet says it must be between 0 and 3.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #2 on: November 01, 2010, 02:25:36 PM »
Your code shows 'oss = 4' but the table on Pg 10 of the datasheet says it must be between 0 and 3.
haha oops typo, had changed that line so many times I wasn't thinking.

But nope, still doesn't work. No change, actually.

Page 17 shows 0xF4 used for OSS of 3. Is that something I should be using?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #3 on: November 01, 2010, 10:23:44 PM »
Page 17 shows 0xF4 used for OSS of 3. Is that something I should be using?

You already are! That's what this bit of code does..
Code: [Select]
(0x34 + (OSS<<6))
I think your problem is that you are loosing the sign of numbers by casting 16 bit values into 32 bit numbers.
Try out my changed code (attached)
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #4 on: November 02, 2010, 09:25:59 AM »
Hmmmm I loaded this new code and it appears to be working better . . .

On the 7th floor, it says pressure is 101952Pa (-52 above sea level).  It should be +24m above sea level.

On the 1st floor, it says 102223Pa (-74 above sea level). It should be +5m above sea level.

So, it looks like the difference in pressure between 1st and 7th floors is correct, but the absolute values are wrong.

The data sheet says it'll be accurate within 0.5m (worst case scenario), after following their calibration data and equations. Perhaps I should just add another 80m to the final result . . .

Anyone else use this sensor?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #5 on: November 02, 2010, 11:34:19 AM »
What values are you getting back in the calibration code - all those wild variables. Are they similar to the commented out values in the code. ie if any of these are wrong then all the maths will go beserk
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #6 on: November 02, 2010, 02:41:37 PM »
I had it take measurements for four hours straight.

During that time, the pressure increased from 101667 Pa to 102056 Pa (a drift of 32m of altitude).

Looking up the pressure on wunderground.com, it says 102800 Pa, so thats really close. I'll assume that pressure isn't that related to altitude - not nearly as much as it is underwater or as the datasheet makes us think.

There was no drift in temperature (my room is also climate controlled).

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,941
  • Helpful? 98
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #7 on: November 02, 2010, 07:14:03 PM »
That amount of drift could be attributed to changes in the atmospheric or barometric pressure due to changing weather systems. I'll guess that would need a mercury barometer for a calibration standard or a location of know altitude. The air pressure altimeter in aircraft are set to the altitude of the airport during the pre-flight check-list.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #8 on: June 29, 2011, 04:47:22 PM »
Webbot, I'm using the exact code you posted in this thread. For some reason it sometimes works, and sometimes doesn't.

typical output (temperature / pressure):
Code: [Select]
241 100018
241 100026
241 100027
-2642 44421
241 100018
241 100022
241 100018
241 100018
241 100024
241 100027
-2642 44419
-2642 44419
-2642 44414
241 100012
-2642 44416
-2642 44420
241 100024
241 100025
-2642 44416
-2642 44415

For some reason it occasionally jumps from 241 (correct) to -2642 (wrong).

When I print out each individual variable in the code, such as the calibration constants, they all look normal/reasonable. However the value ut is around -32376 when I get the wrong value, making me think the I2C addresses aren't being read properly?

Other relevent code:
Code: [Select]
rprintf("%d %ld",temperature, pressure);
rprintf("ut=%ld",ut);

(using WebbotLib v2.04 . . . is it possible your alpha code fixes this issue?)

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #9 on: June 30, 2011, 04:06:58 AM »
I'm assuming you are outputting the two values after all of your calculations - so its hard to see if the problem is in the raw sensor reading or in your calculations.
In which case can you add in the raw readings ie
Code: [Select]

ut = BMP085_ReadTemp();
up = BMP085_ReadPres();//washington dc should be 101.33kpa
rprintf("\nut=%ld",ut);
rprintf("\nup=%ld",up);
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #10 on: June 30, 2011, 08:36:53 AM »
errrr but my previous post already showed code to output the raw ut value . . . the up value looks fine. :P

The ut value sometimes bounces between ~-32376 and +32376, making me think it's a variable define like a int16_t that should be unsigned . . .

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #11 on: June 30, 2011, 11:57:12 AM »
Yes I can see what the rprintf code line looks like - but I can't see the values that it puts out in your table.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #12 on: June 30, 2011, 12:53:01 PM »
So, all values look fine, except ut.

ut bounces between ~-32376 and +32376 . . . ut goes up with temperature, and when it gets to I think 24.1C, that is when ut jumps from +32376 to -32376. It's as if the minus sign on ut comes and goes.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #13 on: June 30, 2011, 02:17:10 PM »
sigh - yes you keep telling me what you think the problem might be - but unless I can see (in a table) the raw read values as well as the final calculated stuff (which you've previously posted on their own) then who knows where the problem lies! I don't have one of these devices and its all your own code (other than low level WebbotLib stuff to read registers).
So yes your code may be using signed ints instead of unsigned but until you can give me the raw I2C values, followed by me reading the datasheet, then I can't tell you where the bug in your code is.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #14 on: July 01, 2011, 10:28:14 AM »
calibration values (all look correct to me):
AC1 = 7961
AC2 = -1308
AC3 = -14442
AC4 = 33660
AC5 = 24629
AC6 = 24221
B1 = 5498
B2 = 72
MB = -32768
MC = -11075
MD = 2432

from the function BMP085_Read2(), we have:
response[0]=127 response[1]=148

which gives us (these are correct):
ut=32660
up=335528
reading GPS
temp=+23.0C pres=100486Pa


however, when I warm the sensor up (putting my finger on the back of it):
response[0]=129 response[1]=152
ut=-32360
up=333040
temp=-262.C pres=44722Pa    <- obviously wrong


It's blatantly obvious that it's a variable casting bug . . . but where?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #15 on: July 01, 2011, 10:39:42 AM »
Ok, so when you read the two I2C registers to get the temperature then the two bytes are combined to make a 16 bit signed integer. You should check against the datasheet to see if this should an unsigned integer. If so: then you will need to change that function to return an uint16_t instead of an int16_t and you will then need to change your additional calculations to treat is as unsigned as well.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #16 on: July 01, 2011, 11:04:21 AM »
I changed
int16_t BMP085_Read2

to
uint16_t BMP085_Read2

and that fixed it. I had been guessing by changing castes everywhere, but apparently I missed that one . . .

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: bug in my code, where? I2C for BMP085 pressure sensor
« Reply #17 on: July 01, 2011, 11:35:19 AM »
You need to be slightly careful as your BMP085_Read2 function is also called many times by your calibration function. Are you happy that all of the calibration values are also 'unsigned' ?
If not then you may want to create 'BMP085_Read_int16' and 'BMP085_Read_uint16' instead of the existing one.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list