ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

Proposal for better MS5611 Baro pressure calculation

Open Crashpilot1000 opened this issue 13 years ago • 9 comments

Hi! I ported the ms baro pressure calculation from my other project to your project. I haven't tried it on arducopter but i think it should be an improvement as well. The code is a heavily modified part of mwii and baseflight, i don't see copyright problems. All modifications were done by me and so it's free :) .... Check it out: // Calculate Temperature and compensated Pressure in real units (Celsius degrees_100, mbar_100). void AP_Baro_MS5611::_calculate() { int32_t temp, off2 = 0, sens2 = 0, delt, pressure; int64_t dT, off, sens; float dTf; dTf = (float)D2 - ((float)C5 * 256.0f); dT = dTf; off = (int64_t)((uint32_t)C2 << 16) + (dTf / 128 * (float)C4); sens = (int64_t)((uint32_t)C1 << 15) + (dTf / 256 * (float)C3); temp = 2000 + ((dT * C6) >> 23); // temperature is not so important not so precise if (temp < 2000) // temperature lower than 20degC { delt = temp - 2000; delt = 5 * delt * delt; off2 = (delt >> 1); sens2 = (delt >> 2); if (temp < -1500) // temperature lower than -15degC { delt = temp + 1500; delt = delt * delt; off2 += ( 7 * delt); sens2 += ((11 * delt) >> 1); } } off -= off2; sens -= sens2; Press = (float)(((((int64_t)D1 * sens ) >> 21) - off) >> 15); }

Cheers Kraut Rob

Crashpilot1000 avatar Jun 07 '13 17:06 Crashpilot1000

This is better [CODE] void AP_Baro_MS5611::_calculate() { int32_t temp, off2 = 0, sens2 = 0; int64_t off, sens; float dTf, delt; dTf = (float)D2 - ((float)C5 * 256.0f); temp = 2000 + (((int64_t)dTf * C6) >> 23); off = (int64_t)((uint32_t)C2 << 16) + (dTf / 128.0f * (float)C4); sens = (int64_t)((uint32_t)C1 << 15) + (dTf / 256.0f * (float)C3);

if (temp < 2000)                                             // temperature lower than 20degC
{
    delt  = (float)(temp - 2000);
    delt  = delt * delt;
    off2  = (int32_t)(delt * 2.5f);
    sens2 = (int32_t)(delt * 1.25f);
    if (temp < -1500)                                        // temperature lower than -15degC
    {
        delt  = (float)(temp + 1500);
        delt  = delt * delt;
        off2  = (int32_t)((float)off2  + (delt * 7.0f));
        sens2 = (int32_t)((float)sens2 + (delt * 5.5f));
    }
}
off     -= off2;
sens    -= sens2;
Press = (float)(((((int64_t)D1 * sens ) >> 21) - off) >> 15);

} [/CODE]

Crashpilot1000 avatar Jun 07 '13 20:06 Crashpilot1000

Hi, it would be nice to know what you have implemented? Which formulas were used, why did you choose these over a more or less detailed model etc. Did you try out the code, and was that for a specific purpose (very high alt., extreme temperatures?) ?

dongfang avatar Jun 08 '13 22:06 dongfang

Hi, my apm is currently not installed and i am working on STM Based naze32 / Baseflight /multiwii currently i am using this code on my version: static float ms5611_calculate(void) { int32_t temp; int64_t off, sens; float dTf, delt; float off2 = 0, sens2 = 0;

dTf  = (float)ms5611_ut - ((float)ms5611_c[5] * 256.0f);
temp = 2000 + (((int64_t)dTf * ms5611_c[6]) >> 23);          // temperature is not so important not so precise
off  = (int64_t)((uint32_t)ms5611_c[2] << 16) + (dTf / 128.0f * (float)ms5611_c[4]);
sens = (int64_t)((uint32_t)ms5611_c[1] << 15) + (dTf / 256.0f * (float)ms5611_c[3]);

if (temp < 2000)                                             // temperature lower than 20degC
{
    delt  = (float)(temp - 2000);
    delt  = delt * delt;
    off2  = delt * 2.50f;
    sens2 = delt * 1.25f;
    if (temp < -1500)                                        // temperature lower than -15degC
    {
        delt  = (float)(temp + 1500);
        delt  = delt * delt;
        off2  = off2  + delt * 7.0f;
        sens2 = sens2 + delt * 5.5f;
    }
}
off     = off  - (int64_t)off2;
sens    = sens - (int64_t)sens2;
return (float)(((((int64_t)ms5611_up * sens ) >> 21) - off) >> 15);

}

It is based on: This: http://code.google.com/p/multiwii/source/browse/trunk/MultiWii/Sensors.ino#668

and that: http://code.google.com/p/afrodevices/source/browse/trunk/baseflight/src/drv_ms5611.c?spec=svn298&r=298#153

And of course the Datasheet here: http://www.meas-spec.com/downloads/MS5611-01BA03.pdf

Crashpilot1000 avatar Jun 08 '13 22:06 Crashpilot1000

Crashpilot, As DongFang says can you be more clear about what the improvement is? Is it faster? is it more accurate? If it's more accurate whats the difference?

rmackay9 avatar Jun 10 '13 06:06 rmackay9

Crashpilot, the fact is that Arducopter and Ardupilot uses a 8bit processor. So making use of 64bit integers is (too) slow. The mod you have is good for the PX4 or VRBRAIN that uses 32bit processor.

Emile

2013/6/10 Randy Mackay [email protected]

Crashpilot, As DongFang says can you be more clear about what the improvement is? Is it faster? is it more accurate? If it's more accurate whats the difference?

— Reply to this email directly or view it on GitHubhttps://github.com/diydrones/ardupilot/issues/345#issuecomment-19182383 .

emilecastelnuovo avatar Jun 10 '13 08:06 emilecastelnuovo

It is OK with wide integers. They are being used all over the place. The only thing that really hurts is division. But again, what is the idea behind the implementation and what advantages will it bring? I am interested because I want to help somebody doing very high altitude stuff with APM - does the implementation approximate better there?

dongfang avatar Jun 10 '13 08:06 dongfang

Hi! Somehow my last comment didn't make it. First of all i think the 64Bit stuff is no real issue because just + - and Bitshifts are done. Probably 32 Bit variables are done on 8Bit cpu the same way. I don't know the speed of that calculation on arduino but doing everything with "float" is not a good thing as well. On the other hand the code is more to by the book (ref manual) so i think it's better in precision/range, at least it can handle temperatures below -15 degree ;) . Some people in russia will find it useful when they "arduplane" their combustion engine powered planes up in the air. Perhaps other people can use it with weatherbaloons. I have seen rc explorer se doing high altitude dives with his foamyplane carried way up by a baloon. These are situations where a -15 deg compensation might come in handy. Cheers Kraut Rob

Crashpilot1000 avatar Jun 10 '13 16:06 Crashpilot1000

Hi! Just a little update: I tested out the crc calculation of the Ms5611 Baro calibration data based on the AN520 document (http://www.servoflo.com/pressure-sensors/227-measurement-specialties-a-intersema/781-an520.html). Conclusion: It really isn't worth the effort, because i did more then 10 tests and never had a faulty checksum. So i think it can be left omitted. I think the crc code in the AN520 document can turn falsely true if the whole buffer is zero. So i altered the crc code to catch that situation. I post it here just for info, it seems really to be of no practical use:

static bool ms5611_crc_ok(void) { uint16_t crc = 0, zerocheck = 0; uint8_t i, k, crc_save;

crc_save = (uint8_t)(ms5611_c[7] & 0xF);                     // Save last 4 bit
ms5611_c[7] = 0xFF00 & ms5611_c[7];                          // Last byte must be cleared for crc calculation
for (i = 0; i < 16; i++)
{
    zerocheck = zerocheck | ms5611_c[i>>1];                  // To check for empty buffer
    if (i%2 == 1) crc ^= (ms5611_c[i>>1]) & 0x00FF;
    else crc ^= ms5611_c[i>>1] >> 8;
    for (k = 8; k > 0; k--)
    {
              if (crc & (0x8000)) crc = (crc << 1) ^ 0x3000;
        else crc = crc << 1;
    }
}
crc = (crc >> 12) & 0xF;                                     // crc = CRC code
if (crc_save == (uint8_t)crc && zerocheck != 0) return true;
else return false;

}

Crashpilot1000 avatar Jun 12 '13 15:06 Crashpilot1000

Please delete that whole issue.......

Crashpilot1000 avatar Jun 15 '13 19:06 Crashpilot1000