Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* BMP581 streams already-compensated values, so there are no per-device trim
* coefficients to parse or store (unlike BMP180/BMP280/BMP390). This CalibDetails
* is therefore a pass-through scaler:
* <li> pressure = raw / 64 -> Pa
* <li> temperature = raw / 65536 -> deg C
* <li> pressure = raw / 64 -> Pa (unsigned 24-bit)
* <li> temperature = raw / 65536 -> deg C (<b>signed</b> 24-bit, two's complement)
* (see BST-BMP581-DS004).
* <p>
* The temperature channel arrives as an unsigned 24-bit field, so it has to be
* sign-extended here: without it a reading of -0.5 deg C decodes as 255.5
* (DEV-1102).
*
* @author Shimmer
*/
Expand All @@ -26,10 +30,23 @@ public CalibDetailsBmp581(String mMacIdFromUart) {
public double[] calibratePressureSensorData(double UP, double UT) {
double[] caldata = new double[2];
caldata[0] = UP / 64.0; // Pa (downstream /1000 -> kPa, same as BMP390 legacy path)
caldata[1] = UT / 65536.0; // deg C
caldata[1] = signExtend24(UT) / 65536.0; // deg C
return caldata;
}

/**
* Reads a 24-bit field as two's complement: the BMP581 temperature register
* is signed, but both of the driver's parse paths deliver it as UINT24.
* Idempotent, so a value that is already signed passes through unchanged.
*
* @param raw the 24-bit field, as parsed
* @return the signed value, -2^23 to 2^23-1
*/
public static double signExtend24(double raw) {
long bits = ((long) raw) & 0xFFFFFFL;
return (bits & 0x800000L) != 0 ? bits - 0x1000000L : bits;
}

@Override
public byte[] generateCalParamByteArray() {
// No coefficients on BMP581.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ public ObjectCluster processDataCustom(SensorDetails sensorDetails, byte[] rawDa
}
if (channelDetails.mObjectClusterName.equals(ObjectClusterSensorName.TEMPERATURE_BMP581)) {
double raw = ((FormatCluster) ObjectCluster.returnFormatCluster(objectCluster.getCollectionOfFormatClusters(ObjectClusterSensorName.TEMPERATURE_BMP581), channelDetails.mChannelFormatDerivedFromShimmerDataPacket.toString())).mData;
// BMP581 temperature is pre-compensated: raw/65536 = deg C
double calTemp = raw / 65536.0;
// BMP581 temperature is pre-compensated and signed: s24(raw)/65536 = deg C
double calTemp = CalibDetailsBmp581.signExtend24(raw) / 65536.0;
objectCluster.addCalData(channelDetails, calTemp, objectCluster.getIndexKeeper() - 1);
objectCluster.incrementIndexKeeper();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.shimmerresearch.sensors.bmpX80;

import static org.junit.Assert.*;

import org.junit.Test;

/**
* The BMP581's compensated temperature is a signed 24-bit value over 65536, and
* the driver receives it as UINT24. Before DEV-1102 it was used unsigned, so a
* fridge test that took two Shimmer3Rs below freezing exported 254.33 to 256.00
* deg C for every sample under 0 deg C.
*
* @author Mark Nolan
*/
public class API_00013_Bmp581TemperatureSignTest {

private static final double LSB_C = 1.0 / 65536.0;

private static double calTemp(long raw) {
return new CalibDetailsBmp581("test").calibratePressureSensorData(0, raw)[1];
}

@Test
public void positiveTemperaturesAreUnchanged() {
assertEquals(0.0, calTemp(0x000000), 0);
assertEquals(1600000 * LSB_C, calTemp(1600000), 0); // 24.41 deg C
assertEquals(0x7FFFFF * LSB_C, calTemp(0x7FFFFF), 0); // top of the range, ~128 deg C
}

@Test
public void subZeroTemperaturesAreNegative() {
assertEquals(-LSB_C, calTemp(0xFFFFFF), 0); // previously 255.99998
// CE2F's coldest reading in the fridge test: -1.667 deg C, previously 254.333
assertEquals(-109226 * LSB_C, calTemp(0xFE5556), 0);
assertEquals(-128.0, calTemp(0x800000), 0); // bottom of the range
}

@Test
public void noSubZeroReadingLandsAboveOneHundredTwentyEightDegrees() {
// The whole upper half of the unsigned range is sub-zero.
for (long raw = 0x800000; raw <= 0xFFFFFF; raw += 0x1111) {
double t = calTemp(raw);
assertTrue("raw 0x" + Long.toHexString(raw) + " gave " + t, t < 0 && t >= -128.0);
}
}

@Test
public void signExtendIsIdempotent() {
assertEquals(-1.0, CalibDetailsBmp581.signExtend24(-1), 0);
assertEquals(-1.0, CalibDetailsBmp581.signExtend24(0xFFFFFF), 0);
assertEquals(0x7FFFFF, CalibDetailsBmp581.signExtend24(0x7FFFFF), 0);
}

@Test
public void pressureStaysUnsigned() {
// 6,400,000 / 64 = 100,000 Pa; the top bit of pressure is magnitude, not sign.
CalibDetailsBmp581 c = new CalibDetailsBmp581("test");
assertEquals(100000.0, c.calibratePressureSensorData(6400000, 0)[0], 0);
assertEquals(0xFFFFFF / 64.0, c.calibratePressureSensorData(0xFFFFFF, 0)[0], 0);
}
}
Loading