More updates to the sensor API

This commit is contained in:
dario
2024-03-26 14:10:32 +01:00
parent 4c4f2658e2
commit 26f3a6fcf6
2 changed files with 289 additions and 211 deletions

View File

@@ -1,22 +1,27 @@
#include <sta/drivers/MS56xx.hpp>
#include <sta/debug/assert.hpp>
namespace sta
{
// Forward declaration
uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
MS56xx::MS56xx(SpiDevice * device, DelayMsFunc delay, OsrLevel level)
MS56xx::MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel level /* = OsrLevel::_1024 */)
: device_{device},
delay_{delay},
osr_{level}
{
this->lastPresVal = -1;
this->adcStartTime = -1;
this->presRead = true;
STA_ASSERT(device != nullptr);
}
void MS56xx::init()
MS56xx::MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024)
: device_{device},
delay_{delay},
osr_{osr}
{
STA_ASSERT(device != nullptr);
}
bool MS56xx::init()
{
// Reset device on start-up
this->reset();
@@ -25,145 +30,114 @@ namespace sta
this->readPROM();
}
void MS56xx::setPressureReference(float pressRef, float altRef)
void MS56xx::pulseCS(uint32_t ms=1) {
this->device_->endTransmission();
delay_(ms * 1000);
this->device_->beginTransmission();
}
void MS56xx::setOsr(OsrLevel osr)
{
osr_ = osr;
}
void MS56xx::reset()
{
busCommand(MS56xx::Operations::RESET);
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 */)
{
// Request the ADC to read pressure values.
busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
// This might be an incorrect duration.
delay_(10 * 1000);
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);
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);
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
// Convert the raw values to actual temperature values.
uint32_t res = buffer[0] | buffer[1] << 8 | buffer[2] << 16;
int32_t temperature = calculateTemperature(res);
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);
}
float MS56xx::getAltitudeEstimate()
{
return 0.0; // TODO
}
int32_t temperature = getTemperature();
void MS56xx::requestAdcReadout(uint32_t time) {
// Get current state of calculation
if (time-adcStartTime < delayTimes(this->osr_) || !presRead) {
// Time since last adc command is not enough
return;
}
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
adcStartTime = time;
presRead = false;
}
int32_t MS56xx::getPressure(int32_t temp, uint32_t time) {
if (time-adcStartTime < delayTimes(this->osr_) || presRead) {
// Time since last adc command is not enough
return lastPresVal;
}
uint8_t buffer[3] = { 0 };
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(buffer, 3);
this->device_->endTransmission();
this->presRead = true;
uint32_t d1_val = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
this->requestAdcReadout(time);
return calculatePressure(d1_val, reverseTempCalc(temp));
return getAltitudeEstimate(temperature);
}
int32_t MS56xx::reverseTempCalc(int32_t temp) {
return this->tempsens;
}
// DEPRECATED
int32_t MS56xx::getPressure() {
// Get pure ADC value from the sensor
uint32_t d1 = readPressure();
// Since pressure is temp-dependant, temperature needs to be polled
// Could be too costly timewise, since 8ms is needed for the ADC
// May need future optimisation
uint32_t d2 = readTemp();
int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8);
return calculatePressure(d1, dT);
}
// DON'T USE
uint32_t MS56xx::readPressure() {
// Request pressure value conversion
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
// Could do sth. else and schedule continuation with scheduler in RTOS
// TODO: Find out min
sta::delayMs(10);
// Request readout of ADC value
uint8_t d1Arr[3];
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d1Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
// Shifting may not be necessary, but idk w/o testing
res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16);
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
int32_t MS56xx::calculatePressure(uint32_t d1, int32_t dT) {
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;
switch (unit)
{
case Unit::hPa:
/* code */
break;
case Unit::Pa:
break;
case Unit::mbar:
break;
default:
break;
}
return pres;
}
// NOT RECOMMENDED
// USE TEMP FROM SCA3300 OR STH. ELSE
int32_t MS56xx::getTemperature() {
// Get pure ADC value from the sensor
uint32_t d2 = readTemp();
return calculateTemperature(d2);
}
// OLD; DON'T USE
uint32_t MS56xx::readTemp() {
// Request ADC conversion of temperature
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
// Could do sth. else and schedule continuation with scheduler in RTOS
// TODO: Test out min
sta::delayMs(10);
// Request ADC readout
uint8_t d2Arr[3];
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d2Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
// Shifting may be unnecessary? Don't know really w/o testing
res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16;
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
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);
@@ -174,14 +148,6 @@ namespace sta
return temp;
}
// Reset sequence as described in datasheet
void MS56xx::reset() {
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::RESET);
this->device_->endTransmission();
delayUs(MS56xx::RESET_DELAY);
}
// 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.
@@ -235,7 +201,31 @@ namespace sta
// Helper function:
// Take first bytes from buffer, swap them and store those in uint16_t
// Swap may not be necessary
uint16_t uint_8BufferTouint16_t(uint8_t* buffer) {
uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) {
return (buffer[0] << 8) | buffer[1];
}
bool MS56xx::busCommand(uint8_t command)
{
this->device_->beginTransmission();
this->device_->transfer(command);
this->device_->endTransmission();
return true;
}
bool MS56xx::busRead(uint8_t reg, uint8_t * buffer, size_t length)
{
this->device_->beginTransmission();
this->device_->transfer(reg);
this->device_->receive(buffer, length);
this->device_->endTransmission();
return true;
}
bool MS56xx::busWrite(uint8_t reg, uint8_t * buffer, size_t length)
{
return true;
}
}