problem with reading temperatur and altitude

Hi there i’m facing i probleme to read temperature and altitude in my program. i have a bmp085. i use a library from i2cdev jrowberg.

my programe look like that

void loop()
{
//

//
	while (micros() - loop_timer < 4000);// i did some functions every 4 milliseconds
	loop_timer = micros();

//
}

me i want to get altitude and temperature with no delay function and every 4milliseconds.

the simple code of jrowberg is

    // request temperature
    barometer.setControl(BMP085_MODE_TEMPERATURE);
    
    // wait appropriate time for conversion (4.5ms delay)
    lastMicros = micros();
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    // read calibrated temperature value in degrees Celsius
    temperature = barometer.getTemperatureC();

    // request pressure (3x oversampling mode, high detail, 23.5ms delay)
    barometer.setControl(BMP085_MODE_PRESSURE_3);
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    pressure = barometer.getPressure();

    altitude = barometer.getAltitude(pressure);

my question is how can i get alltitude and temperature in my loop that run on 4 milliseconds. or how can i delayed the request of temperature and altitude without using delay and that run on 4 milliseconds loop time.

i did lot of tries but i didn’t succed, please help

Thanks

You can't. Read the data sheet.

I read the datasheet and i get altitude and temperature a simple programe with delays. but in my program i can't read those two data. i have not a good programming skill so i wwant some people help me. that all.

Hi,
I think you need to see this bit of the data sheet regarding conversion times.


Unless you are in ultra low power mode.
Tom… :slight_smile:
What is the application that needs such short sample times?

BST-BMP085-DS000-05.pdf (638 KB)

I build a quadcopter, and now i’m working on altitude hold. so i have to sample my PID every 4ms. now i want to do an algorithm that get pressur and temperature. i should do in my program a request to get altitude after this request i should wait 25.3 ms then i can get the pressur then i did a request for temperatur then i should wait 4.5 ms then i get temperature. i have to do this whitout delay function. and every 4ms.

my program should look like this

void loop()
{
//resquets(temperature)
i wai 4.5 ms
get temperature
seconde loop
request pressur
i wait 25.5 ms
get pressur.
and so on

//
while (micros() - loop_timer < 4000);// i did some functions every 4 milliseconds
loop_timer = micros();

//
}

hi again, i add this in my loop to get altitude now i want to add some lines for temperature but i have no idea how to swith between altitude and temperature.
i want to get altitude first after i get temperature can anyone help me please

unsigned long previousMillis = 0;
int signal_alt = 0;
int i = 0;
void loop()
{


	if (signal_alt == 0)
	{
		barometer.setControl(BMP085_MODE_PRESSURE_3);//request for pressur
		signal_alt = 1;
	}

	unsigned long currentMillis = millis();
	if (currentMillis - previousMillis >= 25.5)
	{
		previousMillis = currentMillis;
		pressure = barometer.getPressure();//after waiting 25.5 ms i get the pressur
		altitude = barometer.getAltitude(pressure);
		signal_alt = 0;
	}



///
	while (micros() - loop_timer < 4000);
	loop_timer = micros();
///




}
[code]

i would like someone who can help me to add request for temperature that requeir 4.5ms
Thanks

jone31:

while (micros() - loop_timer < 4000);// i did some functions every 4 milliseconds

loop_timer = micros();

i suspect you will find that your WHILE should be IF

Have a look at Several Things at a Time

…R

no i should wait 4 ms. i have to work with while (micros() - loop_timer < 4000), i block the programe until 4ms lapsed

jone31:
my program should look like this

void loop() 

{
    request temperature
    wait 4.5 ms
    get temperature

request pressure
    wait 25.5 ms
    get pressure
}

Since you don't want to delay/wait, what you really want is:

void loop() 
{
    if no timer is running (both are zero)
        request temperature
        start the temperature timer (set it to micros())

    if the temperature timer is running and has reached 4.5ms
        get temperature
        stop the temperature timer (set it to 0)
        request pressure
        start the pressure timer (set it to micros())

    if the pressure timer is running and has reached 25.5ms
        get pressure
        stop the pressure timer (set it to zero)
}

The sensor cannot measure temperature and pressure every 4 milliseconds, so the PID algorithm will not work properly. Change the PID loop time.

jone31:
no i should wait 4 ms. i have to work with while (micros() - loop_timer < 4000), i block the programe until 4ms lapsed

If you really do want to block the program just use delay() or delayMicroseconds().

I had assumed you did not want to block.

…R

First make sure that you are getting T and P values to your arduino and look at the delays within the measurement subroutines.

Presumably you want 250 updates per second for sending your killer robot flying platform on homing-missile-avoidance-loops, split-S near ground level and stuff like that. The BMP085 is, unless set to impaired accuracy, much slower. Nevertheless, you can write an extrapolation routine to provide your flight control subsystems with an estimate of what the altitude should have got to 4, 8, 12, 16, 20, 24 ms after the BMP085 told it a real one. As you'd need three or four consequetive altitude points to get 2nd and 3rd derivatives, the really fancy spline extrapolated h(t) is only good for manouvers in which the control surfaces were still during the forecasting interval and for at least 0.1 seconds before it, as occurs in a loop-the-loop near ground level.

I've never tried testing other inflences on a BMP085 pressure reading but I bet it would detect ground-effect overpressure and tell you that you are some way under when you are in fact twelve inches off the floor. Therefore you might want some other independent height sensor for takeoff height cal, landing, and any really low flying manouvers. I read that ultrasonics pulse timers might get used for that. The expensive way, with a full 3D GPS would be no good for air support of a strike against skynet as the satellites belong to you-know-who.

Back to your question about delays. Do the excercises in examples and go through your bmp085 subroutines to change any blocking delays in there.

Thanks for all you answer some flight controller use temperatur and pressur to do altitude hold. and they works on 3ms loop time.

i read some documentation and they told that we get temperature and pressur to do the altitude hold.

Mr johnwasser i don't understand what do you mean by timer ? could you please explaine more or write me some lines of code thanks. you are great as always.

jone31:
Mr johnwasser i don’t understand what do you mean by timer ? could you please explaine more or write me some lines of code thanks. you are great as always.

Only because I wanted to use the code myself. This code is untested. It might not even compile. Good luck with it.

float CurrentTemperature = 0;  // Degrees C
float CurrentPressure = 0; // Pascals
float CurrentAltitude = 0;  // Meters
void UpdateAltitude() {
  static unsigned long temperatureTimer = 0;
  static unsigned long pressureTimer = 0;
  // If neither timer is running...
  if (temperatureTimer == 0 && pressureTimer == 0) {
    // Request a temperature reading
    BMP085.setControl(BMP085_MODE_TEMPERATURE);
    // Start the temperature timer
    temperatureTimer = micros();
  }
  // If the temperature timer is running and has reached 4.5 ms
  if (temperatureTimer != 0 &&
      micros() - temperatureTimer >= 4500UL) {
    // Fetch the temperature reading
    CurrentTemperature = BMP085.getTemperatureC();
    // Stop the temperature timer
    temperatureTimer = 0;
    // Reqest pressure/altitude reading in high detail mode
    barometer.setControl(BMP085_MODE_PRESSURE_3);
    // Start the pressure timer
    pressureTimer = micros();
  }
  // If the pressure timer is running and has reached 25.5 ms
  if (pressureTimer != 0 &&
      micros() - pressureTimer >= 25500UL) {
    // Fetch the pressure and calculate altitude
    CurrentPressure = BMP085.getPressure();
    CurrentAltitude = BMP085.getAltitude(CurrentPressure);
    // Stop the pressure timer
    pressureTimer = 0;
  }
}

God bless you Mr johnwasser it's works like a charm.
Thanks

jone31:
God bless you Mr johnwasser it's works like a charm.
Thanks

Do you now understand what I meant by "Start the timer (set it to micros())" and "Stop the timer (set it to zero)" and "If neither timer is running"? You can use that trick any time you want to delay an action or create a timed sequence of actions. For longer intervals you could use millis() instead of micros().