Skip to content

Commit a6ca85c

Browse files
authored
Merge pull request #311 from ShimmerResearch/DEV-1110_bmp581_inband_sensor_id
DEV-1110: read the BMP581 in-band sensor ID and add SR38-4-2 to the SR rule
2 parents 58bf57c + 8a938bd commit a6ca85c

5 files changed

Lines changed: 605 additions & 42 deletions

File tree

‎ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java‎

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,27 +1616,8 @@ else if(responseCommand==BMP280_CALIBRATION_COEFFICIENTS_RESPONSE){
16161616
printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes));
16171617
}
16181618
} else if(responseCommand==PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE){
1619-
byte[] pressureResoRes = null;
1620-
byte[] filteredByteArray = null;
1621-
if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP390)){
1622-
pressureResoRes = new byte[23]; //21 bytes + 2
1623-
pressureResoRes = readBytes(23, responseCommand);
1624-
printLogDataForDebugging("BMP390 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes));
1625-
}else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP280)){
1626-
pressureResoRes = new byte[26]; //24 bytes + 2
1627-
pressureResoRes = readBytes(26, responseCommand);
1628-
printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes));
1629-
}else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP180)){
1630-
pressureResoRes = new byte[24]; //22 bytes + 2
1631-
pressureResoRes = readBytes(24, responseCommand);
1632-
printLogDataForDebugging("BMP180 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes));
1633-
}
1634-
1635-
if(pressureResoRes!=null){
1636-
filteredByteArray = Arrays.copyOfRange(pressureResoRes, 2, pressureResoRes.length);
1637-
retrievePressureCalibrationParametersFromPacket(filteredByteArray,CALIB_READ_SOURCE.LEGACY_BT_COMMAND);
1638-
}
1639-
}
1619+
processPressureCalibCoefficientsResponse();
1620+
}
16401621
else if(responseCommand==EXG_REGS_RESPONSE){
16411622
delayForBtResponse(300); // Wait to ensure the packet has been fully received
16421623
byte[] bufferAns = readBytes(11, responseCommand);
@@ -3341,14 +3322,95 @@ public void readPressureCalibrationCoefficients() {
33413322
}
33423323
}
33433324
} else if (getHardwareVersion() == HW_ID.SHIMMER_3R) {
3344-
// BMP581 self-compensates and has no coefficients; its firmware NACKs
3345-
// GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND, so don't send it.
3346-
if (!isSupportedBmp581()) {
3325+
// LogAndStream_Shimmer3R v1.01.007+ answers with the fitted sensor's ID
3326+
// ([A6][01][03] for a BMP581), which confirms or overrides the
3327+
// SR-number rule. v1.01.006 is the one version that NACKs it on a
3328+
// BMP581, and without NACK handling an unanswered command times out
3329+
// and drops the connection - so skip it there and let the SR-number
3330+
// rule stand. The rule's firmware guard (>= v1.01.006) makes "rule
3331+
// says BMP581 and firmware < v1.01.007" exactly v1.01.006.
3332+
boolean isBmp581PerSrNumber = isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails());
3333+
boolean isFwWithInBandPressureSensorId = getShimmerVerObject().compareVersions(FW_ID.LOGANDSTREAM, 1, 1, 7);
3334+
if (!isBmp581PerSrNumber || isFwWithInBandPressureSensorId) {
33473335
writeInstruction(GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND);
33483336
}
33493337
}
33503338
}
33513339

3340+
/**
3341+
* Parses a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, laid out as
3342+
* [0xA6][len = 1+n][sensorId][n coefficient bytes] (log-and-stream-common
3343+
* Comms/shimmer_bt_uart.c). The response byte has already been read.
3344+
* <p>
3345+
* The length byte, not the sensor the driver assumed, decides how much is
3346+
* read, so the stream stays in step whatever the Shimmer sends. The length
3347+
* is then checked against the sensor ID, and a mismatch is rejected rather
3348+
* than applied. On a Shimmer3R the sensor ID is authoritative: it overrides
3349+
* the SR-number rule in either direction (see
3350+
* {@link #setPressureSensorIdInBand(int)}). On a Shimmer3 the coefficients
3351+
* are applied only if they are for the sensor class in use.
3352+
*/
3353+
protected void processPressureCalibCoefficientsResponse() {
3354+
byte[] length = readBytes(1, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE);
3355+
if(length==null){
3356+
return;
3357+
}
3358+
int lengthToRead = length[0]&0xFF;
3359+
if(lengthToRead==0){
3360+
printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: zero length, no sensor ID");
3361+
return;
3362+
}
3363+
byte[] payload = readBytes(lengthToRead, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE);
3364+
if(payload==null){
3365+
return;
3366+
}
3367+
3368+
int sensorId = payload[0]&0xFF;
3369+
byte[] coefficients = Arrays.copyOfRange(payload, 1, payload.length);
3370+
String bytesReceived = UtilShimmer.bytesToHexStringWithSpacesFormatted(ArrayUtils.addAll(length, payload));
3371+
3372+
int expectedLength = getPressureCalibCoefficientByteLength(sensorId);
3373+
if(expectedLength<0 || coefficients.length!=expectedLength){
3374+
printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: sensor ID " + sensorId + " with " + coefficients.length
3375+
+ " coefficient bytes, expected " + expectedLength + ":\t" + bytesReceived);
3376+
return;
3377+
}
3378+
3379+
SENSORS sensorReported = null;
3380+
switch(sensorId){
3381+
case PRESSURE_SENSOR_ID.BMP180:
3382+
sensorReported = SENSORS.BMP180;
3383+
break;
3384+
case PRESSURE_SENSOR_ID.BMP280:
3385+
sensorReported = SENSORS.BMP280;
3386+
break;
3387+
case PRESSURE_SENSOR_ID.BMP390:
3388+
sensorReported = SENSORS.BMP390;
3389+
break;
3390+
case PRESSURE_SENSOR_ID.BMP581:
3391+
sensorReported = SENSORS.BMP581;
3392+
break;
3393+
}
3394+
printLogDataForDebugging(sensorReported + " CALIB Received:\t" + bytesReceived);
3395+
3396+
if(getHardwareVersion()==HW_ID.SHIMMER_3R){
3397+
if(sensorId!=PRESSURE_SENSOR_ID.BMP390 && sensorId!=PRESSURE_SENSOR_ID.BMP581){
3398+
printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: a Shimmer3R carries a BMP390 or BMP581, not a " + sensorReported);
3399+
return;
3400+
}
3401+
setPressureSensorIdInBand(sensorId);
3402+
}
3403+
else if(!mSensorBMPX80.mSensorType.equals(sensorReported)){
3404+
printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: coefficients are for a " + sensorReported + " but the driver is using a " + mSensorBMPX80.mSensorType);
3405+
return;
3406+
}
3407+
3408+
// A BMP581 self-compensates, so it has no coefficients to apply
3409+
if(coefficients.length>0){
3410+
retrievePressureCalibrationParametersFromPacket(coefficients,CALIB_READ_SOURCE.LEGACY_BT_COMMAND);
3411+
}
3412+
}
3413+
33523414
public void readBattery(){
33533415
writeInstruction(GET_VBATT_COMMAND);
33543416
}

‎ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java‎

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,17 @@ public class BTStream {
473473

474474
public static final byte GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND = (byte) 0xA7;//BMP390
475475
public static final byte PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE = (byte) 0xA6;//BMP390
476-
476+
477+
/** The sensor ID byte in a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, which
478+
* is laid out as [0xA6][len = 1+n][sensorId][n coefficient bytes]
479+
* (log-and-stream-common Comms/shimmer_bt_uart.c). */
480+
public static final class PRESSURE_SENSOR_ID {
481+
public static final int BMP180 = 0;
482+
public static final int BMP280 = 1;
483+
public static final int BMP390 = 2;
484+
public static final int BMP581 = 3;
485+
}
486+
477487
public static final byte SET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x52;
478488
public static final byte PRESSURE_OVERSAMPLING_RATIO_RESPONSE = (byte) 0x53;
479489
public static final byte GET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x54;
@@ -680,7 +690,13 @@ public String toString() {
680690
protected SensorMPU9X50 mSensorMpu9x50 = new SensorMPU9150(this);
681691
// Shimmer3 - Pressure/Temperature
682692
public SensorBMPX80 mSensorBMPX80 = new SensorBMP180(this);
683-
693+
/** The pressure sensor a Shimmer3R reported in-band in its
694+
* PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE (a {@link PRESSURE_SENSOR_ID}),
695+
* or null if it hasn't. When set it overrides the SR-number rule in
696+
* {@link #isSupportedBmp581()}. Cleared whenever the expansion board details
697+
* change, so each connection starts from the SR-number rule again. */
698+
protected Integer mPressureSensorIdInBand = null;
699+
684700
// Shimmer3r - Mag
685701
private SensorLIS2MDL mSensorLIS2MDL = new SensorLIS2MDL(this);
686702
// Shimmer3r - Alt Mag
@@ -10823,31 +10839,50 @@ public static boolean isSupportedBmp280(ShimmerVerObject svo, ExpansionBoardDeta
1082310839
* SR number/revision and the firmware version that produces the new output
1082410840
* format.
1082510841
*
10826-
* @return true if the connected Shimmer3R board revision and firmware version indicate BMP581 output support
10842+
* <p>
10843+
* If the Shimmer has reported its pressure sensor in-band (the sensor ID in
10844+
* its PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, LogAndStream_Shimmer3R
10845+
* v1.01.007 onwards), that answer is used and overrides the SR-number rule
10846+
* in either direction. Otherwise, e.g. when the firmware NACKs the command
10847+
* or is too old to have it, the SR-number rule decides.
10848+
*
10849+
* @return true if the connected Shimmer3R reported a BMP581, or if its board revision and firmware version indicate BMP581 output support
1082710850
*/
1082810851
public boolean isSupportedBmp581() {
10852+
if(mPressureSensorIdInBand!=null && getHardwareVersion()==HW_ID.SHIMMER_3R){
10853+
return mPressureSensorIdInBand==PRESSURE_SENSOR_ID.BMP581;
10854+
}
1082910855
return isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails());
1083010856
}
1083110857

10858+
/** The SR-number rule alone, used when the Shimmer hasn't identified its
10859+
* pressure sensor in-band and for SD log files (whose header carries no
10860+
* sensor ID). It mirrors the firmware's own fallback,
10861+
* ShimBrd_isBmp581PresentPerSrNumber() in log-and-stream-common
10862+
* Boards/shimmer_boards.c, plus a firmware-version guard.
10863+
*
10864+
* @param svo the Shimmer's version details
10865+
* @param ebd the Shimmer's expansion board details
10866+
* @return true if a Shimmer3R with this board and firmware carries a BMP581
10867+
*/
1083210868
public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDetails ebd) {
1083310869
if(svo==null || ebd==null || svo.getHardwareVersion()!=HW_ID.SHIMMER_3R){
1083410870
return false;
1083510871
}
1083610872

10837-
// SR48 (GSR+): BMP581 spans two bands - 7.2..7.x AND 8.2 and above.
10838-
// 7.0/7.1 and 8.0/8.1 are BMP390, so a single lexicographic ">=" can't
10839-
// express it (8.0/8.1 fall between the two BMP581 bands).
10840-
boolean sr48Bmp581 =
10841-
(ebd.getExpansionBoardId()==HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED
10842-
&& ebd.getExpansionBoardRev()==7
10843-
&& ebd.getExpansionBoardRevSpecial()>=2) // SR48 7.2..7.x
10844-
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2); // SR48 8.2 and above
10845-
10873+
// Fitted models (">=" applies to both rev fields): SR31-11-2, SR38-4-2,
10874+
// SR47-8-2, SR48-7-2, SR48-8-2, SR49-4-2. SR48 (GSR+) has two BMP581
10875+
// bands, 7.2..7.x and 8.2 onwards: 8.0/8.1 went back to the BMP390, so
10876+
// the rev-7 band is ">= 7.2 and not >= 8.0". An unprogrammed card (board
10877+
// ID 0x00 or 0xFF) matches none of these IDs.
1084610878
boolean boardEligible =
10847-
ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2
10848-
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2
10849-
|| sr48Bmp581 // SR48 7.2..7.x and 8.2+
10850-
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2
10879+
ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2
10880+
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2) // SR38 >= 4.2
10881+
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2
10882+
|| (ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 2)
10883+
&& !ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 0)) // SR48 7.2..7.x
10884+
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2) // SR48 >= 8.2
10885+
|| ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2
1085110886

1085210887
// Format guard: BMP581 (pre-compensated) output only exists from
1085310888
// LogAndStream_Shimmer3R v1.01.006 onwards.
@@ -10856,6 +10891,70 @@ public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDeta
1085610891
return boardEligible && fwEligible;
1085710892
}
1085810893

10894+
/** Records the pressure sensor a Shimmer3R reported in-band and, if the
10895+
* sensor class in use is not the one it names, rebuilds the sensor classes
10896+
* around the reported sensor. The rebuild is what would have happened had
10897+
* the sensor been known when the daughter card ID arrived: the config bytes,
10898+
* which the Shimmer sends before this response while connecting, are parsed
10899+
* again so the new sensor class picks up its settings (e.g. oversampling).
10900+
*
10901+
* @param pressureSensorId the sensor ID from the response, a {@link PRESSURE_SENSOR_ID}
10902+
*/
10903+
public void setPressureSensorIdInBand(int pressureSensorId) {
10904+
mPressureSensorIdInBand = pressureSensorId;
10905+
10906+
if(isShimmerGen3R()){
10907+
boolean bmp581InUse = mSensorBMPX80.mSensorType==SENSORS.BMP581;
10908+
if(bmp581InUse!=isSupportedBmp581()){
10909+
consolePrintLn("Pressure sensor reported in-band (ID " + pressureSensorId + ") differs from the SR-number rule, switching to " + (isSupportedBmp581()? "BMP581":"BMP390"));
10910+
sensorAndConfigMapsCreate();
10911+
if(mShimmerUsingConfigFromInfoMem && ConfigByteLayout.checkConfigBytesValid(mConfigBytes)){
10912+
configBytesParse(mConfigBytes, COMMUNICATION_TYPE.BLUETOOTH);
10913+
}
10914+
}
10915+
}
10916+
}
10917+
10918+
/**
10919+
* @return the pressure sensor the Shimmer reported in-band (a {@link PRESSURE_SENSOR_ID}), or null if it hasn't
10920+
*/
10921+
public Integer getPressureSensorIdInBand() {
10922+
return mPressureSensorIdInBand;
10923+
}
10924+
10925+
/**
10926+
* @param pressureSensorId a {@link PRESSURE_SENSOR_ID}
10927+
* @return the number of coefficient bytes that follow this sensor ID in a
10928+
* PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, or -1 if the ID is unknown
10929+
*/
10930+
public static int getPressureCalibCoefficientByteLength(int pressureSensorId) {
10931+
switch(pressureSensorId){
10932+
case PRESSURE_SENSOR_ID.BMP180:
10933+
return 22;
10934+
case PRESSURE_SENSOR_ID.BMP280:
10935+
return 24;
10936+
case PRESSURE_SENSOR_ID.BMP390:
10937+
return 21;
10938+
case PRESSURE_SENSOR_ID.BMP581:
10939+
return 0; // self-compensating, no coefficients
10940+
default:
10941+
return -1;
10942+
}
10943+
}
10944+
10945+
@Override
10946+
public void setExpansionBoardDetails(ExpansionBoardDetails eBD){
10947+
super.setExpansionBoardDetails(eBD);
10948+
// A different board may carry a different pressure sensor
10949+
mPressureSensorIdInBand = null;
10950+
}
10951+
10952+
@Override
10953+
public void clearExpansionBoardDetails(){
10954+
super.clearExpansionBoardDetails();
10955+
mPressureSensorIdInBand = null;
10956+
}
10957+
1085910958
/** Returns true if the Shimmer is using new sensors. These sensors are:
1086010959
* <li> Use BMP280 instead of BMP180 as barometer.
1086110960
* <li> Use MPU9250 instead of MPU9150 as IMU.

‎ShimmerDriver/src/main/java/com/shimmerresearch/sensors/bmpX80/SensorBMP581.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ public SensorBMP581(ShimmerDevice shimmerDevice) {
8181
//--------- Constructors for this class end --------------
8282

8383
//--------- Bluetooth commands start --------------
84-
// Oversampling only. There is deliberately NO GET_PRESSURE_CALIBRATION_COEFFICIENTS
85-
// command - the BMP581 firmware NACKs it because the sensor self-compensates.
84+
// Oversampling only. The BMP581 self-compensates, so it has no calibration
85+
// coefficients. GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND (0xA7) is still
86+
// sent: LogAndStream_Shimmer3R v1.01.007+ answers it with just the sensor ID,
87+
// [A6][01][03], which identifies the BMP581 in-band. Only v1.01.006 NACKs it on
88+
// a BMP581, so the driver doesn't send it to that version (see
89+
// ShimmerBluetooth.readPressureCalibrationCoefficients()).
8690
public static final byte SET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x52;
8791
public static final byte PRESSURE_OVERSAMPLING_RATIO_RESPONSE = (byte) 0x53;
8892
public static final byte GET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x54;

0 commit comments

Comments
 (0)