Skip to content

Commit

Permalink
AP_Baro: Add support for MS5837-02BA sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikimram committed Jan 21, 2025
1 parent c372402 commit f4e9fe4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
51 changes: 50 additions & 1 deletion libraries/AP_Baro/AP_Baro_MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ bool AP_Baro_MS56XX::_init()
case BARO_MS5837:
name = "MS5837";
prom_read_ok = _read_prom_5637(prom);
// Check for MS5837 product ID from PROM Word 0 (bits 11:5)
if (prom_read_ok) {
uint8_t product_id = (prom[0] >> 5) & 0x7F; // Extract bits 11-5
switch (product_id) {
case 0b0000000: // MS5837-02BA01
name = "MS5837-02BA01";
_ms56xx_type = BARO_MS5837_02BA;
break;
case 0b0010101: // MS5837-02BA21
name = "MS5837-02BA21";
_ms56xx_type = BARO_MS5837_02BA;
break;
case 0b0011010: // MS5837-30BA26
name = "MS5837-30BA26";
_ms56xx_type = BARO_MS5837;
break;
default:
prom_read_ok = false; // Unsupported sensor type
}
}
break;
case BARO_MS5637:
name = "MS5637";
Expand Down Expand Up @@ -155,6 +175,9 @@ bool AP_Baro_MS56XX::_init()
case BARO_MS5837:
devtype = DEVTYPE_BARO_MS5837;
break;
case BARO_MS5837_02BA:
devtype = DEVTYPE_BARO_MS5837; // Reuse the same dev type for both variants
break;
case BARO_MS5637:
devtype = DEVTYPE_BARO_MS5637;
break;
Expand All @@ -163,7 +186,7 @@ bool AP_Baro_MS56XX::_init()
_dev->set_device_type(devtype);
set_bus_id(_instance, _dev->get_bus_id());

if (_ms56xx_type == BARO_MS5837) {
if (_ms56xx_type == BARO_MS5837 || _ms56xx_type == BARO_MS5837_02BA) {
_frontend.set_type(_instance, AP_Baro::BARO_TYPE_WATER);
}

Expand Down Expand Up @@ -516,5 +539,31 @@ void AP_Baro_MS56XX::_calculate_5837()

_copy_to_frontend(_instance, (float)pressure, temperature);
}
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
void AP_Baro_MS56XX::_calculate_5837_02ba() {
int32_t dT = _D2 - ((int32_t)_cal_reg.c5 << 8);
int32_t TEMP = 2000 + ((dT * _cal_reg.c6) >> 23);

int64_t OFF = ((int64_t)_cal_reg.c2 << 17) + (((int64_t)_cal_reg.c4 * dT) >> 6);
int64_t SENS = ((int64_t)_cal_reg.c1 << 16) + (((int64_t)_cal_reg.c3 * dT) >> 7);

if (TEMP < 2000) {
// Second-order compensation
int32_t T2 = (dT * dT) >> 31;
int64_t TEMP_low = TEMP - 2000;
int64_t OFF2 = 5 * (TEMP_low * TEMP_low) >> 1;
int64_t SENS2 = (TEMP_low * TEMP_low) >> 3;

TEMP -= T2;
OFF -= OFF2;
SENS -= SENS2;
}

int32_t pressure = (((_D1 * SENS) >> 21) - OFF) >> 15;

// Update frontend with calculated values
_copy_to_frontend(_instance, (float)pressure / 10, (float)TEMP / 100);
}


#endif // AP_BARO_MS56XX_ENABLED
14 changes: 13 additions & 1 deletion libraries/AP_Baro/AP_Baro_MS5611.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#define HAL_BARO_MS5837_I2C_ADDR 0x76
#endif

#ifndef HAL_BARO_MS5837_02BA_I2C_ADDR
#define HAL_BARO_MS5837_02BA_I2C_ADDR 0x76
#endif


#ifndef HAL_BARO_MS5637_I2C_ADDR
#define HAL_BARO_MS5637_I2C_ADDR 0x76
#endif
Expand All @@ -37,7 +42,8 @@ class AP_Baro_MS56XX : public AP_Baro_Backend
BARO_MS5611 = 0,
BARO_MS5607 = 1,
BARO_MS5637 = 2,
BARO_MS5837 = 3
BARO_MS5837 = 3,
BARO_MS5837_02BA = 4 // Add new type here
};

static AP_Baro_Backend *probe_5611(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev) {
Expand All @@ -52,6 +58,11 @@ class AP_Baro_MS56XX : public AP_Baro_Backend
static AP_Baro_Backend *probe_5837(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev) {
return probe(baro, std::move(dev), BARO_MS5837);
}
static AP_Baro_Backend *probe_5837_02ba(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev) {
return probe(baro, std::move(dev), BARO_MS5837_02BA);
}



static AP_Baro_Backend *probe(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev, enum MS56XX_TYPE ms56xx_type=BARO_MS5611);

Expand All @@ -73,6 +84,7 @@ class AP_Baro_MS56XX : public AP_Baro_Backend
void _calculate_5607();
void _calculate_5637();
void _calculate_5837();
void _calculate_5837_02ba(); // Calculate Temperature and Pressure for MS5837-02BA sensor
bool _read_prom_5611(uint16_t prom[8]);
bool _read_prom_5637(uint16_t prom[8]);

Expand Down

0 comments on commit f4e9fe4

Please sign in to comment.