help with types

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

help with types

Postby mikn » Mon Apr 04, 2016 1:17 pm

I have arduino pro mini and bmp180 barometer.
I read temperature, do calculations according to datasheet but get incorrect values. Tried different ways of calculations (separating values into different variables, changing types) but still no success.

here's part of datasheet:
1datash.gif
1datash.gif (51.64 KiB) Viewed 4995 times


here's macro of i2c reading of raw temperature
2readt.gif
2readt.gif (12.38 KiB) Viewed 4995 times


here are calculations according to datasheet

Code: Select all

x1 = ((ut - ac6) * ac5) >> 15
x2 = (mc << 11) / (x1 + md)
b5 = x1 + x2
t = (b5 + 8) >> 4


variable types are following:
ut, x1,x2,b5,t = long
ac5, ac6 = uint
mc, md = int

results in terminal are

Code: Select all

ut=29358 t=434

434 is incorrect, current temp is 27.5C so it must show 275 or around that.
434 is also changes time to time around 434, 438, etc as I open windows to change inside temp.. so seems like it reads the temperature and not some fixed data.

where I could be wrong? I suppose that the problem may be in calculations and types.
help me to locate the problem, please.
Thank you in advance.
FC 6.1.3.2 (18.02.2016)

User avatar
LeighM
Matrix Staff
Posts: 1962
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 474 times
Been thanked: 674 times
Contact:

Re: help with types

Postby LeighM » Mon Apr 04, 2016 2:09 pm

This is probably due to intermediate (temporary) values being limited to 16 bit.
To force 32 bit you could break down the calculations ...

Code: Select all

x1 = ut - ac6
x1 = x1 * ac5
x1 = x1 >> 15

x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1

...
These users thanked the author LeighM for the post:
mikn (Mon Apr 04, 2016 5:32 pm)
Rating: 5.26%
 

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: help with types

Postby mikn » Mon Apr 04, 2016 3:20 pm

same story, tried this

Code: Select all

x1 = ut - ac6
x1 = x1 * ac5
x1 = x1 >> 15

x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1
b5 = x1 + x2
t = b5
t = t + 8
t = t >> 4

result

Code: Select all

ac5=25369 ac6=20378 mc=-11786 md=2595 ut=29266 t=433


i have manually calculated everything and result is 433 exactly, so I am disappointed if calibration values were read incorrectly. they are 2 bytes each and I don't know where may be mistake there.
Here are macros which read calibration data
3readcal.gif
3readcal.gif (8.97 KiB) Viewed 4964 times

3readuint.gif
3readuint.gif (15.1 KiB) Viewed 4964 times
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: help with types

Postby mikn » Mon Apr 04, 2016 4:50 pm

finally, I've got it working

this code was with error, x1 was incremented and then used with invalid value:
x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1
b5 = x1 + x2


this code worked

Code: Select all

x2 = mc
x2 = x2 << 11
x2 = x2 / (x1 + md)
b5 = x1 + x2


t=273 which is 27.3C :) yahoo!
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: help with types

Postby mikn » Mon Apr 04, 2016 10:06 pm

Here's another problem.
I try to calculate pressure and made calculations simple line by line
But still get incorrect values. Where may be the problem?

Code: Select all

b6 = b5 - 4000
//x1=(b2 * ((b6 * b6) >> 12)) >> 11
x1 = b6 * B6
x1 = x1 >> 12
x1 = x1 * B2
x1 = x1 >> 11

//x2 = (ac2 * b6) >> 11
x2 = ac2 * B6
x2 = x2 >> 11

x3 = x1 + x2

//b3 = (((ac1 * 4 + x3) << OSS) + 2) / 4
b3 = ac1 * 4
b3 = b3 + X3
b3 = b3 << OSS
b3 = b3 + 2
b3 = b3 / 4

x1 = ac3 * b6 >> 13

//x2 = (b1 * ((b6 * b6) >> 12)) >> 16
x2 = b6 * B6
x2 = x2 >> 12
x2 = x2 * B1
x2 = x2 >> 16

//x3 = ((x1 + x2) + 2) >> 2
x3 = x1 + x2 + 2
x3 = x3 >> 2

//b4 = (ac4 * (x3 + 32768)) >> 15
b4 = x3 + 32768
b4 = b4 * AC4
b4 = b4 >> 15

//b7 = ((up - b3) * (50000 >> OSS))
b7 = up
b7 = b7 - B3
p_temp = 50000 >> OSS
b7 = b7 * p_temp

//x1 = (p >> 8) * (p >> 8)
X1 = p
x1 = x1 >> 8
x1 = x1 * X1

//x1 = (x1 * 3038) >> 16
x1 = x1 * 3038
x1 = x1 >> 16

//x2 = (-7357 * p) >> 16
X2 = (-7357) * p
X2 = x2 >> 16

//p = p + ((x1 + x2 + 3791) >> 4)
p_temp = x1 + x2 + 3791
p_temp = p_temp >> 4
p = p + p_temp



corresponding part of datasheet
4datash.gif
4datash.gif (29.33 KiB) Viewed 4924 times


here's the result
MSB=0x19 LSB=0x86 xLSB=0x27 upHEX=0x330C ac1=9111 ac2=-1095 ac3=-14620 ac4=33626 ac5=25285 ac6=23267 b1=6515 b2=40 mb=-32768 mc=-11786 md=2646
up=13068 pf(mm)=309.767272 p(pa)=41299 t(C)=264

thank you in advance
FC 6.1.3.2 (18.02.2016)

kersing
Valued Contributor
Valued Contributor
Posts: 1894
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 541 times
Been thanked: 1058 times
Contact:

Re: help with types

Postby kersing » Mon Apr 04, 2016 10:11 pm

What are the datatypes of the variables?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: help with types

Postby mikn » Mon Apr 04, 2016 10:37 pm

here are they (marked with red + are unsigned)
4vars.gif
4vars.gif (5.91 KiB) Viewed 4915 times
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: help with types

Postby mikn » Tue Apr 05, 2016 1:39 am

damn yes, I have fixed it.
the problem was in readtemperature macro, when I read second byte of data, Last parameter in i2c_Rxbyte was 0 instead of 1.
so next macro ReadPressure was reading completely wrong data and all calculations were wrong.
Whew.
These users thanked the author mikn for the post (total 4):
LeighM (Tue Apr 05, 2016 8:59 am) • kersing (Tue Apr 05, 2016 11:48 am) • medelec35 (Tue Apr 05, 2016 2:48 pm) • Benj (Wed Apr 06, 2016 9:36 am)
Rating: 21.05%
 
FC 6.1.3.2 (18.02.2016)

andreimacoveiciuc
Posts: 1
Joined: Thu May 14, 2015 2:54 pm
Contact:

Re: help with types

Postby andreimacoveiciuc » Fri Feb 03, 2017 8:59 pm

Hello!
Can you share your flowchart please. I create something by miself but it read wrong presure, like -934. I want to compare and see where i do wrong.
Thank you!