ms5611 barometer usage - more altitude hold

Firmware/software/electronics/mechanics
omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

ms5611 barometer usage - more altitude hold

Post by omwdunkley » Thu Jun 06, 2013 10:12 pm

Hey guys!
I wanted to start working on my own altitude hold algorithm. I know some of you have been working on this. But I'd like to go through the process myself!

For now I have extracted the z acceleration minus gravity and integrate this to get vertical speed. This works pretty well.
Next up I would like to fuse this with the barometer signal (converted to meters above ground level).

I have two questions:
What is the fastest way to achieve pressure readings? I can't seem to get more than 50hz. Any more then that and it starts outputting 0s. I've played around with read delays and other stuff (eg reading temperature before or after pressure somehow makes a difference).

I've seen others uses poll the sensor every 10ms / 100hz in other projects. The signal is really really noisy and I could really do with the extra samples to reduce filter lag. How fast can you guys get pressure readings?

I'll upload a graph( pressure, vertical velocity and acceleration) when I'm on my computer at home tomorrow.

Cheers!

tobias
Bitcraze
Posts: 1722
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: ms5611 barometer usage - more altitude hold

Post by tobias » Fri Jun 07, 2013 8:58 am

The ms5611 driver is not optimal in our system and I think it could be improved. Putting a task in the HAL layer that takes care of the readings would be one way to do it.
If I remeber correctly a reading with the 4096 filter takes 8-9ms for either temp or preassure. The temp might not have to be read that often so then it should be possible to get about 100 Hz.

omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

Re: ms5611 barometer usage - more altitude hold

Post by omwdunkley » Sun Jun 09, 2013 10:30 pm

Tobias, thanks for the fast response!

Yup, Ive read that a maximum of 10ms per read is required. That would explain the 50hz for reading temp & pressure together. Unfortunately, skipping reading the temperature 4 out of 5 times (eg reading pressure x4, temp x1, repeat) does not help :( Somehow reading temperature and pressure are coupled. I guess Ill need to dig deeper into the ms5611 code. This sort of low level programming is rather new to me ;)

I would be happy to try and improve the barometer readings - could you possibly give me a pointer or two about how to "put a task in the HAL layer", ie what would this mean programmatically?

Thanks!

memoryleak
Beginner
Posts: 4
Joined: Tue Jun 04, 2013 5:14 am

Re: ms5611 barometer usage - more altitude hold

Post by memoryleak » Mon Jun 10, 2013 4:35 am

Hi,

try to see how the stabilizer task (stabilizer.c) works. There will be read the gyro/accelaration information etc. This will be initiated in the stabilizerInit(void) function (creates a task for stbailizerTask(void *)).
This could be a starting point for you to do it by yourself. If this does not work, I can show you this directly in my code (I modified the ms5611.c for testing purposes).

You can check out phiamo's code as well!

omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

Re: ms5611 barometer usage - more altitude hold

Post by omwdunkley » Mon Jun 10, 2013 7:13 am

Thanks for your response ;)

Thats pretty much what I have been doing so far - adding the read barometer functions calls within stabilizerTasnk(void*).

Code: Select all

static void stabilizerTask(void* param) {
    uint32_t attitudeCounter = 0;
    uint32_t altitudeCounter = 0;
    uint32_t lastWakeTime;

    vTaskSetApplicationTaskTag(0, (void*) TASK_STABILIZER_ID_NBR);

    //Wait for the system to be fully started to start stabilization loop
    systemWaitStart();

    lastWakeTime = xTaskGetTickCount();

    while (1) {
        vTaskDelayUntil(&lastWakeTime, F2T(IMU_UPDATE_FREQ) );

        imu9Read(&gyro, &acc, &mag);

        if (imu6IsCalibrated()) {
            commanderGetRPY(&eulerRollDesired, &eulerPitchDesired, &eulerYawDesired);
            commanderGetRPYType(&rollType, &pitchType, &yawType);

            if (++altitudeCounter >= ALTITUDE_UPDATE_RATE_DIVIDER) {
                //read barometer data using ms5611GetPressure(osr) / ms5611GetTemperature(osr)
                altitudeCounter = 0;
            }
            if (++attitudeCounter >= ATTITUDE_UPDATE_RATE_DIVIDER) {
                //sensor fustion
            }
            // More stuff
      ...} // if imucalib
   ...} // while(1)
...} // stabTask()
If I understand correctly the while loop is set up to work at 500hz. ATtitudeCOunter is set up so the sensorfusion works at 250hz (ie aTtitudeCounter=2). Ive set aLtitudeCounter to 5, so that baroGetData(...) is called at 100hz.

I assume Tobias meant I create my own task for the barometer reading, but wouldnt my (rather ugly) solution above have the same effect?

Are you able to read the barometer data at 100hz?
Cheers!

ak1394
Beginner
Posts: 12
Joined: Fri May 03, 2013 9:53 pm

Re: ms5611 barometer usage - more altitude hold

Post by ak1394 » Mon Jun 10, 2013 3:03 pm

BTW, looking at your code it seems that you reading pressure and temperature in one go?

I briefly played with it as well and I was getting the best results when alternating when reading temp and pressure.

omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

Re: ms5611 barometer usage - more altitude hold

Post by omwdunkley » Mon Jun 10, 2013 3:30 pm

Hey,
I fixed the issue today got roughly 100hz going. The original code is a little confusing. The first time you call get pressure it requests it, and the next time it reads it.

I'll post my code here later tonight once I'm home!

Thanks for the support and interest!

omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

Re: ms5611 barometer usage - more altitude hold

Post by omwdunkley » Fri Jun 14, 2013 10:58 am

Sorry its taken so long to reply, code is a mess etc etc.

Some example measurements: http://goo.gl/N9GUc

Codewise:

Code: Select all

void ms5611GetData(float* pressure, float* temperature, float* asl){

    int32_t tempPressureRaw, tempTemperatureRaw;

    // Dont reader faster than we can
    uint32_t now = xTaskGetTickCount();
    if ((now - lastConv) < CONVERSION_TIME_MS) {
        return;
    }
    lastConv = now;

    if (readState == 0) {
        ++readState;
        // read temp
        tempTemperatureRaw = ms5611GetConversion(MS5611_D2 + MS5611_OSR_DEFAULT);
        tempDeltaT = ms5611CalcDeltaTemp(tempTemperatureRaw);
        *temperature = ms5611CalcTemp(tempDeltaT);
        // cmd to read pressure
        ms5611StartConversion(MS5611_D1 + MS5611_OSR_DEFAULT);
    } else {
        ++readState;
        // read pressure
        tempPressureRaw = ms5611GetConversion(MS5611_D1 + MS5611_OSR_DEFAULT);
        *pressure = ms5611CalcPressure(tempPressureRaw, tempDeltaT);
        *asl = ms5611PressureToAltitude(pressure);
        if (readState == 5){
            // cmd to read temp
            ms5611StartConversion(MS5611_D2 + MS5611_OSR_DEFAULT);
            readState = 0;
        } else {
            // cmd to read pressure
            ms5611StartConversion(MS5611_D1 + MS5611_OSR_DEFAULT);
        }
    }
}

Where I also changed

Code: Select all

#define CONVERSION_TIME_MS 10 // was 100
#define MS5611_OSR_DEFAULT MS5611_OSR_4096
This function could be heavily optimised. Everytime you call it it returns the latest requested value and requests the next value. 1 in 10 times it requests a temperature update instead of a pressure update. For this to work well, you should call it around 100hz.

The resulting values are obviously pretty noisy, I just used a leaky integrator with alpha = 0.92 (results also shown in the link above) to smooth out the values.

Hope this helps someone :)

tobias
Bitcraze
Posts: 1722
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: ms5611 barometer usage - more altitude hold

Post by tobias » Fri Jun 14, 2013 11:51 am

Great! What plotting program are you using? I really like the plots.

omwdunkley
Expert
Posts: 162
Joined: Thu Jun 06, 2013 9:56 pm
Location: Munich

Re: ms5611 barometer usage - more altitude hold

Post by omwdunkley » Fri Jun 14, 2013 11:57 am

What plotting program are you using? I really like the plots.
Google fusion tables to share data.
I mostly use ROS (rqt_plot etc) for monitoring real time data though. Makes recording and playing back data very easy as everything is nicely time stamped.

Post Reply