mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-ms56xx.git
synced 2025-09-28 22:37:33 +00:00
Added altitude calculation and more compact API
This commit is contained in:
219
src/MS56xx.cpp
219
src/MS56xx.cpp
@@ -1,6 +1,9 @@
|
||||
#include <sta/drivers/MS56xx.hpp>
|
||||
|
||||
#include <sta/debug/assert.hpp>
|
||||
#include <sta/lang.hpp>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace sta
|
||||
@@ -8,7 +11,8 @@ namespace sta
|
||||
MS56xx::MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel level /* = OsrLevel::_1024 */)
|
||||
: device_{device},
|
||||
delay_{delay},
|
||||
osr_{level}
|
||||
osr_{level},
|
||||
intf_{Intf::SPI}
|
||||
{
|
||||
STA_ASSERT(device != nullptr);
|
||||
}
|
||||
@@ -16,7 +20,8 @@ namespace sta
|
||||
MS56xx::MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024)
|
||||
: device_{device},
|
||||
delay_{delay},
|
||||
osr_{osr}
|
||||
osr_{osr},
|
||||
intf_{Intf::I2C}
|
||||
{
|
||||
STA_ASSERT(device != nullptr);
|
||||
}
|
||||
@@ -30,12 +35,6 @@ namespace sta
|
||||
this->readPROM();
|
||||
}
|
||||
|
||||
void MS56xx::pulseCS(uint32_t ms=1) {
|
||||
this->device_->endTransmission();
|
||||
delay_(ms * 1000);
|
||||
this->device_->beginTransmission();
|
||||
}
|
||||
|
||||
void MS56xx::setOsr(OsrLevel osr)
|
||||
{
|
||||
osr_ = osr;
|
||||
@@ -47,161 +46,122 @@ namespace sta
|
||||
delay_(MS56xx::RESET_DELAY);
|
||||
}
|
||||
|
||||
// Helper method to keep code clean
|
||||
void MS56xx::pulseCS(uint32_t ms=1) {
|
||||
this->device_->endTransmission();
|
||||
delay_(ms * 1000);
|
||||
this->device_->beginTransmission();
|
||||
}
|
||||
|
||||
int32_t MS56xx::getPressure(int32_t temperature, Unit unit /* = Unit::hPa */)
|
||||
float MS56xx::getPressure(Unit unit /* = Unit::hPa */)
|
||||
{
|
||||
// Request the ADC to read pressure values.
|
||||
busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
|
||||
// Request the ADC to read temperature values.
|
||||
busCommand(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
|
||||
|
||||
// This might be an incorrect duration.
|
||||
delay_(10 * 1000);
|
||||
// 8.22 ms conversion according to the datasheet.
|
||||
delay_(osrDelay());
|
||||
|
||||
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
|
||||
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
|
||||
|
||||
uint32_t d1_val = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
|
||||
int32_t pressure = calculatePressure(d1_val, reverseTempCalc(temperature), unit);
|
||||
// Difference between actual and reference temperature.
|
||||
uint32_t rawTemperature = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
|
||||
uint32_t dT = rawTemperature - refTemp_ * (0x01<<8);
|
||||
|
||||
// Request the ADC to read pressure values.
|
||||
busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
|
||||
|
||||
// 8.22 ms conversion according to the datasheet.
|
||||
delay_(osrDelay());
|
||||
|
||||
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
|
||||
|
||||
uint32_t rawPressure = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
|
||||
|
||||
// offset and sensitivity at actual temperature.
|
||||
uint64_t offset = offset_ * (0x01<<16) + (tempOffsetCoeff_ * dT) / (0x01<<7);
|
||||
uint64_t sensibility = sensibility_ * (0x01<<15) + (tempSensCoeff_ * dT) / (0x01<<8);
|
||||
|
||||
// The pressure in Pa.
|
||||
float pressure = (rawPressure * (sensibility / 0x01<<21) - offset) / (0x01<<15);
|
||||
pressure = convertPressure(pressure, unit);
|
||||
|
||||
return pressure;
|
||||
}
|
||||
|
||||
int32_t MS56xx::getPressure(Unit unit = Unit::hPa)
|
||||
{
|
||||
int32_t temperature = getTemperature();
|
||||
|
||||
return getPressure(temperature, unit);
|
||||
}
|
||||
|
||||
int32_t MS56xx::getTemperature() {
|
||||
busCommand(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
|
||||
|
||||
// This might be an incorrect duration.
|
||||
delay_(10 * 1000);
|
||||
// 8.22 ms conversion according to the datasheet.
|
||||
delay_(osrDelay());
|
||||
|
||||
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
|
||||
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
|
||||
uint32_t rawTemperature = buffer[0] | buffer[1] << 8 | buffer[2] << 16;
|
||||
|
||||
// Convert the raw values to actual temperature values.
|
||||
uint32_t res = buffer[0] | buffer[1] << 8 | buffer[2] << 16;
|
||||
int32_t temperature = calculateTemperature(res);
|
||||
uint32_t dT = rawTemperature - refTemp_ * (0x01<<8);
|
||||
uint32_t temperature = 20 * dT * tempCoeff_ / (0x01<<23);
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
void MS56xx::setPressureReference(float pressRef, float altRef, Unit unit /* = Unit::hPa */)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
float MS56xx::getAltitudeEstimate(int32_t temperature)
|
||||
{
|
||||
int32_t pressure = getPressure(temperature, Unit::hPa);
|
||||
// Taken from: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf, page 17.
|
||||
sealevel_ = pressRef / (std::pow((1 - altRef / 44330), 5.255));
|
||||
}
|
||||
|
||||
float MS56xx::getAltitudeEstimate()
|
||||
{
|
||||
int32_t temperature = getTemperature();
|
||||
float pressure = getPressure(Unit::hPa);
|
||||
|
||||
return getAltitudeEstimate(temperature);
|
||||
// Taken from: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf, page 16
|
||||
float altitude = 44330 * (1 - std::pow(pressure / sealevel_, 1 / 5.255));
|
||||
}
|
||||
|
||||
int32_t MS56xx::reverseTempCalc(int32_t temp) {
|
||||
return this->tempsens;
|
||||
void MS56xx::readPROM() {
|
||||
uint8_t buffer[2];
|
||||
|
||||
busRead(Operations::READ_PROM, buffer, 2);
|
||||
sensibility_ = uint_8BufferTouint16_t(buffer);
|
||||
|
||||
delay_(1000);
|
||||
|
||||
busRead(Operations::READ_PROM+2, buffer, 2);
|
||||
offset_ = uint_8BufferTouint16_t(buffer);
|
||||
|
||||
delay_(1000);
|
||||
|
||||
busRead(Operations::READ_PROM+4, buffer, 2);
|
||||
tempSensCoeff_ = uint_8BufferTouint16_t(buffer);
|
||||
|
||||
delay_(1000);
|
||||
|
||||
busRead(Operations::READ_PROM+6, buffer, 2);
|
||||
tempOffsetCoeff_ = uint_8BufferTouint16_t(buffer);
|
||||
|
||||
delay_(1000);
|
||||
|
||||
busRead(Operations::READ_PROM+8, buffer, 2);
|
||||
refTemp_ = uint_8BufferTouint16_t(buffer);
|
||||
|
||||
delay_(1000);
|
||||
|
||||
busRead(Operations::READ_PROM+10, buffer, 2);
|
||||
tempCoeff_ = uint_8BufferTouint16_t(buffer);
|
||||
}
|
||||
|
||||
int32_t MS56xx::calculatePressure(uint32_t d1, int32_t dT, Unit unit) {
|
||||
int64_t offset = ( ((uint64_t)this->off) << 17) + ( ( ((uint64_t)this->tco) * dT ) >> 6);
|
||||
int64_t sensitivity = ( ((uint64_t)this->sens) << 16) + ( ( ((uint64_t)this->tcs) * dT ) >> 7);
|
||||
int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15;
|
||||
|
||||
inline float MS56xx::convertPressure(float pressure, Unit unit)
|
||||
{
|
||||
switch (unit)
|
||||
{
|
||||
case Unit::hPa:
|
||||
/* code */
|
||||
break;
|
||||
case Unit::Pa:
|
||||
|
||||
break;
|
||||
case Unit::mbar:
|
||||
break;
|
||||
|
||||
return pressure / 100.0f;
|
||||
case Unit::hPa:
|
||||
return pressure / 100.0f;
|
||||
case Unit::Pa:
|
||||
return pressure;
|
||||
default:
|
||||
STA_UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
|
||||
return pres;
|
||||
}
|
||||
|
||||
int32_t MS56xx::calculateTemperature(uint32_t d2) {
|
||||
int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8);
|
||||
int32_t temp = 2000 + ((dT * ((uint32_t)this->tempsens)) >> 23);
|
||||
|
||||
// Further calculations for low (<20) and very low (<(-15)) could be possible
|
||||
// But I don't know whether they are necessary
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
// Read all constants from the PROM
|
||||
// May be moved to be called in reset() function in future
|
||||
// Request value x -> Read value x; Then request value y etc.
|
||||
// Could be optimized; Not as important since only needed once at start-up
|
||||
void MS56xx::readPROM() {
|
||||
this->device_->beginTransmission();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM);
|
||||
uint8_t sensArr[2];
|
||||
this->device_->receive(sensArr, 2);
|
||||
this->sens = uint_8BufferTouint16_t(sensArr);
|
||||
|
||||
pulseCS();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM+2);
|
||||
uint8_t offArr[2];
|
||||
this->device_->receive(offArr, 2);
|
||||
this->off = uint_8BufferTouint16_t(offArr);
|
||||
|
||||
pulseCS();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM+4);
|
||||
uint8_t tcsArr[2];
|
||||
this->device_->receive(tcsArr, 2);
|
||||
this->sens = uint_8BufferTouint16_t(sensArr);
|
||||
|
||||
pulseCS();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM+6);
|
||||
uint8_t tcoArr[2];
|
||||
this->device_->receive(tcoArr, 2);
|
||||
this->tco = uint_8BufferTouint16_t(tcoArr);
|
||||
|
||||
pulseCS();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM+8);
|
||||
uint8_t t_refArr[2];
|
||||
this->device_->receive(t_refArr, 2);
|
||||
this->t_ref = uint_8BufferTouint16_t(t_refArr);
|
||||
|
||||
pulseCS();
|
||||
|
||||
this->device_->transfer(Operations::READ_PROM+0xA);
|
||||
uint8_t tempsensArr[2];
|
||||
this->device_->receive(tempsensArr, 2);
|
||||
this->tempsens = uint_8BufferTouint16_t(tempsensArr);
|
||||
|
||||
this->device_->endTransmission();
|
||||
}
|
||||
|
||||
// Helper function:
|
||||
// Take first bytes from buffer, swap them and store those in uint16_t
|
||||
// Swap may not be necessary
|
||||
uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) {
|
||||
inline uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) {
|
||||
return (buffer[0] << 8) | buffer[1];
|
||||
}
|
||||
|
||||
@@ -214,6 +174,21 @@ namespace sta
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t MS56xx::osrDelay() {
|
||||
switch (osr_) {
|
||||
case _256:
|
||||
return 1000;
|
||||
case _512:
|
||||
return 2000;
|
||||
case _1024:
|
||||
return 3000;
|
||||
case _2048:
|
||||
return 5000;
|
||||
case _4096:
|
||||
return 10000;
|
||||
}
|
||||
}
|
||||
|
||||
bool MS56xx::busRead(uint8_t reg, uint8_t * buffer, size_t length)
|
||||
{
|
||||
this->device_->beginTransmission();
|
||||
|
Reference in New Issue
Block a user