Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code for Reading Sensors and Computing Depth

Code Block
languagecpp
   // Read temp and pressure from the sensor
  // TODO: Consider reducing the read frequency to reduce power consumption however, I do not think the sensor uses much power.
  D1 = ms5803_cmd_adc( SENSOR_CMD_ADC_D1 + SENSOR_CMD_ADC_4096);    // read uncompensated pressure
  D2 = ms5803_cmd_adc( SENSOR_CMD_ADC_D2 + SENSOR_CMD_ADC_4096);    // read uncompensated temperature
  // calculate 1st order pressure and temperature correction factors (MS5803 1st order algorithm)
  deltaTemp = D2 - sensorCoefficients[5] * pow( 2, 8 );
  sensorOffset = sensorCoefficients[2] * pow( 2, 16 ) + ( deltaTemp * sensorCoefficients[4] ) / pow( 2, 7 );
  sensitivity = sensorCoefficients[1] * pow( 2, 15 ) + ( deltaTemp * sensorCoefficients[3] ) / pow( 2, 8 );
  // calculate 2nd order pressure and temperature (MS5803 2st order algorithm)
  temperature = ( 2000 + (deltaTemp * sensorCoefficients[6] ) / pow( 2, 23 ) ) / 100;
  pressure = ( ( ( ( D1 * sensitivity ) / pow( 2, 21 ) - sensorOffset) / pow( 2, 15 ) ) / 10 );

  // The pressure at sea level is 1013.25 mbars so if the pressure is lower, then the user is not at sea level and an altitude compensation factor is needed.
  // This needs to be improved to take the lowest reading since power up (not just the first reading above sea level) for situations when the unit does not power up until the user is in the water.
  if ( pressure < 1013.25 && !altitudeCompSet ) { 
    altitudeCompensation = 1013.25 - pressure;
    altitudeCompSet = true;
  }

  // calculate depth
  if ( meters ) depth = ( ( pressure + altitudeCompensation ) - 1013.25 ) / 100.52; // There are 100.52 mbars per meter of depth.
  else depth = ( ( pressure + altitudeCompensation ) - 1013.25 ) / 30.64; // There are 30.64 mbars per foot of depth.

  // If there is a pressure rebound, make sure the depth does not go negative. 
  if ( depth < 0.0 ) depth = 0.0;

  // Convert the temp to ferinheight if the user has selected that unit.
  if ( ferinheight ) temperature = ( ( temperature * 9.0 ) / 5.0 ) + 32.0;

...