GlobalVent / wiki

central place organize jamvent
GNU General Public License v3.0
3 stars 0 forks source link

Conversion of output byte values #160

Closed carlyjb closed 4 years ago

carlyjb commented 4 years ago

For I2C comms, there are existing abstract classes

carlyjb commented 4 years ago

Start by assuming consistent temp

ralphBellofattoSTX commented 4 years ago

actually.

assume 20c and above... so only one branch on the conversion...

ajo27 commented 4 years ago

Overly detailed notes from dev/debugging:

-Using example values from datasheets -Assuming T > 20C to ease reversing conditional logic for 2nd-order compensation Equation: (-7/2^37)dT^2 + (C6 >> 23)dT + (2000-temp_calc) = 0 a,b,c constants and solve using quadratic formula in code, checking against min/max dT values specified in datasheet (MS5803 only, MS5603 all 2nd order coeffs are 0 when T > 20) to determine which of the 2 solutions is valid

-

temperature_reported: 20.15 temp_calc: 2015 a = -0.000000000050932 b = 0.003464102745056 c = -15.000000000000000 dT = 4330.40 dT: 4330 Raw temperature: 8387050

This raw temp doesn’t quite match the value in the datasheet (for these other given values) of 8387300 -- running it through forward starting with 8387300 to confirm this is similar to what was seen with the MS5603 code:

(Check done in MATLAB): RAW_TEMP = 8387050; RAW_TEMP_DS = 8387300; dT = RAW_TEMP - (32745)2^8; dT_DS = RAW_TEMP_DS - (32745)2^8; TEMP = 2000 + dT29059/2^23 TEMP_DS = 2000 + dT_DS29059/2^23

TEMP = 2015 ---> 2014.9996 TEMP_DS = 2015.8659

Note these are first order values for temp, need to calculate T2 to get 2nd order (see notes on validating pressure values below for 2nd-order comparison)

NOTE: Both are close to 2015 but our reversal algorithm when given 2015 as its input actually gives a temperature that will be truncated down by ~0.01 deg when run back through the conversion on the other side

Now the temperature numbers (OFF, SENS and raw temp) look right for both versions (MS5803 and MS5607), but raw pressure is 0?

rawValue = ( 32768 (int64_t)pressure_calc + _OFF ) ( 2097152 / _SENS ); → This returns 0 bc 2097152/_SENS is truncated to 0

Casting to doubles for intermediate calcs solves this underflow error

ajo27 commented 4 years ago

Final test numbers for MS5803 version --

(1) Run datasheet values of T = 20.15 C and P = 1000.5 mbar through reversal algorithm to get both raw values. T

(2) Do normal forward calculations using these raw values, and the datasheet raw values to see how the final T and P values differ

(1) For inputs : temp_reported = 20.15; pres_reported = 1000.5;

Reversal algorithm gives: Raw temperature: 8387050 Raw pressure: 4311531

(2) Sanity check following same method as above to confirm that the datasheet examples are only showing different due to rounding errors (can copy paste whole block into MATLAB)

RAW_TEMP_DS = 8387300;
RAW_TEMP = 8387050;

dT_DS = RAW_TEMP_DS - (32745)*2^8;
dT = RAW_TEMP - (32745)*2^8;

TEMP_DS = 2000 + dT_DS*29059/2^23
TEMP = 2000 + dT*29059/2^23

OFF_DS = 42845*2^16+(29457*dT_DS)/2^7
OFF = 42845*2^16+(29457*dT)/2^7

OFF_DS = OFF_DS - 1*(TEMP_DS - 2000)^2/2^4
OFF = OFF - 1*(TEMP - 2000)^2/2^4

SENS_DS = 46546*2^15+(29751*dT_DS)/2^8
SENS = 46546*2^15+(29751*dT)/2^8

TEMP_DS_2 = TEMP_DS - 7*(dT_DS^2/2^37)
TEMP_2 = TEMP - 7*(dT^2/2^37)

RAW_PRES_DS = 4311550
RAW_PRES = 4311531

PRES_DS = (RAW_PRES_DS*SENS_DS/2^21 - OFF_DS)/2^15
PRES = (RAW_PRES*SENS/2^21 - OFF)/2^15

Pres_reported (datasheet values) -- 10005.4884 = 1000.5489 mbar
Pres_reported (from test) -- 10004.999 = 1000.4999 mbar

2nd order Temp (datasheet unclear, the value reported in that table doesn't specify and may actual just be the first order temp?) - 20.15864
2nd order Temp (from test) - 20.149986 

Takeaway -- There are some accumulated precision/underflow errors, hopefully this won't affect any of the simulator/control testing

ralphBellofattoSTX commented 4 years ago

is there test code for this checked in somewhere?

ajo27 commented 4 years ago

Right now the code is written as a Photon script with the functions defined inline, I think that should be enough to commit and then you can rip the function defs out and put them in the relevant files that you already have set up

ralphBellofattoSTX commented 4 years ago

that would work.

ajo27 commented 4 years ago

MSxxxxReverseConversion.zip

Broke it out as 2 separate files (one per sensor) so relevant code can be ripped from each. Only the MS5803 needs the quadratic equation solved.

A few notes: -I was unsure on whether the output should be the unsigned long or the actual 3-byte buffer obtained from that unsigned long, the code to output the buffer is in place but commented out (just needs return value and return type of function to be changed as well if switching back to this).

-Note the differences/errors between running the datasheet numbers as-is and going from the example calculated temp/pressure --> reverse conversion --> Back through forward calculations. Just something to be aware of that these aren't quite 1:1 due to rounding/underflow etc (https://github.com/GlobalVent/wiki/issues/160#issuecomment-652204167)

-The datasheet mentions that dT, OFF and SENS should all be bounded by the min/max defined there. This is not in place anywhere in the code except for the block in the MS5803 reverse conversion function when we need to check which quadratic root is a valid value. ---Is this something to put in place in the actual library functions reading from these sensors? Right now there are no checks there against these values