diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 18bfc999c..7a7a47e10 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -1616,27 +1616,8 @@ else if(responseCommand==BMP280_CALIBRATION_COEFFICIENTS_RESPONSE){ printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); } } else if(responseCommand==PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE){ - byte[] pressureResoRes = null; - byte[] filteredByteArray = null; - if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP390)){ - pressureResoRes = new byte[23]; //21 bytes + 2 - pressureResoRes = readBytes(23, responseCommand); - printLogDataForDebugging("BMP390 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - }else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP280)){ - pressureResoRes = new byte[26]; //24 bytes + 2 - pressureResoRes = readBytes(26, responseCommand); - printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - }else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP180)){ - pressureResoRes = new byte[24]; //22 bytes + 2 - pressureResoRes = readBytes(24, responseCommand); - printLogDataForDebugging("BMP180 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - } - - if(pressureResoRes!=null){ - filteredByteArray = Arrays.copyOfRange(pressureResoRes, 2, pressureResoRes.length); - retrievePressureCalibrationParametersFromPacket(filteredByteArray,CALIB_READ_SOURCE.LEGACY_BT_COMMAND); - } - } + processPressureCalibCoefficientsResponse(); + } else if(responseCommand==EXG_REGS_RESPONSE){ delayForBtResponse(300); // Wait to ensure the packet has been fully received byte[] bufferAns = readBytes(11, responseCommand); @@ -3341,14 +3322,95 @@ public void readPressureCalibrationCoefficients() { } } } else if (getHardwareVersion() == HW_ID.SHIMMER_3R) { - // BMP581 self-compensates and has no coefficients; its firmware NACKs - // GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND, so don't send it. - if (!isSupportedBmp581()) { + // LogAndStream_Shimmer3R v1.01.007+ answers with the fitted sensor's ID + // ([A6][01][03] for a BMP581), which confirms or overrides the + // SR-number rule. v1.01.006 is the one version that NACKs it on a + // BMP581, and without NACK handling an unanswered command times out + // and drops the connection - so skip it there and let the SR-number + // rule stand. The rule's firmware guard (>= v1.01.006) makes "rule + // says BMP581 and firmware < v1.01.007" exactly v1.01.006. + boolean isBmp581PerSrNumber = isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails()); + boolean isFwWithInBandPressureSensorId = getShimmerVerObject().compareVersions(FW_ID.LOGANDSTREAM, 1, 1, 7); + if (!isBmp581PerSrNumber || isFwWithInBandPressureSensorId) { writeInstruction(GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND); } } } + /** + * Parses a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, laid out as + * [0xA6][len = 1+n][sensorId][n coefficient bytes] (log-and-stream-common + * Comms/shimmer_bt_uart.c). The response byte has already been read. + *
+ * The length byte, not the sensor the driver assumed, decides how much is + * read, so the stream stays in step whatever the Shimmer sends. The length + * is then checked against the sensor ID, and a mismatch is rejected rather + * than applied. On a Shimmer3R the sensor ID is authoritative: it overrides + * the SR-number rule in either direction (see + * {@link #setPressureSensorIdInBand(int)}). On a Shimmer3 the coefficients + * are applied only if they are for the sensor class in use. + */ + protected void processPressureCalibCoefficientsResponse() { + byte[] length = readBytes(1, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE); + if(length==null){ + return; + } + int lengthToRead = length[0]&0xFF; + if(lengthToRead==0){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: zero length, no sensor ID"); + return; + } + byte[] payload = readBytes(lengthToRead, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE); + if(payload==null){ + return; + } + + int sensorId = payload[0]&0xFF; + byte[] coefficients = Arrays.copyOfRange(payload, 1, payload.length); + String bytesReceived = UtilShimmer.bytesToHexStringWithSpacesFormatted(ArrayUtils.addAll(length, payload)); + + int expectedLength = getPressureCalibCoefficientByteLength(sensorId); + if(expectedLength<0 || coefficients.length!=expectedLength){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: sensor ID " + sensorId + " with " + coefficients.length + + " coefficient bytes, expected " + expectedLength + ":\t" + bytesReceived); + return; + } + + SENSORS sensorReported = null; + switch(sensorId){ + case PRESSURE_SENSOR_ID.BMP180: + sensorReported = SENSORS.BMP180; + break; + case PRESSURE_SENSOR_ID.BMP280: + sensorReported = SENSORS.BMP280; + break; + case PRESSURE_SENSOR_ID.BMP390: + sensorReported = SENSORS.BMP390; + break; + case PRESSURE_SENSOR_ID.BMP581: + sensorReported = SENSORS.BMP581; + break; + } + printLogDataForDebugging(sensorReported + " CALIB Received:\t" + bytesReceived); + + if(getHardwareVersion()==HW_ID.SHIMMER_3R){ + if(sensorId!=PRESSURE_SENSOR_ID.BMP390 && sensorId!=PRESSURE_SENSOR_ID.BMP581){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: a Shimmer3R carries a BMP390 or BMP581, not a " + sensorReported); + return; + } + setPressureSensorIdInBand(sensorId); + } + else if(!mSensorBMPX80.mSensorType.equals(sensorReported)){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: coefficients are for a " + sensorReported + " but the driver is using a " + mSensorBMPX80.mSensorType); + return; + } + + // A BMP581 self-compensates, so it has no coefficients to apply + if(coefficients.length>0){ + retrievePressureCalibrationParametersFromPacket(coefficients,CALIB_READ_SOURCE.LEGACY_BT_COMMAND); + } + } + public void readBattery(){ writeInstruction(GET_VBATT_COMMAND); } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java index 4056fe9fb..4f153f927 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java @@ -473,7 +473,17 @@ public class BTStream { public static final byte GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND = (byte) 0xA7;//BMP390 public static final byte PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE = (byte) 0xA6;//BMP390 - + + /** The sensor ID byte in a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, which + * is laid out as [0xA6][len = 1+n][sensorId][n coefficient bytes] + * (log-and-stream-common Comms/shimmer_bt_uart.c). */ + public static final class PRESSURE_SENSOR_ID { + public static final int BMP180 = 0; + public static final int BMP280 = 1; + public static final int BMP390 = 2; + public static final int BMP581 = 3; + } + public static final byte SET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x52; public static final byte PRESSURE_OVERSAMPLING_RATIO_RESPONSE = (byte) 0x53; public static final byte GET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x54; @@ -680,7 +690,13 @@ public String toString() { protected SensorMPU9X50 mSensorMpu9x50 = new SensorMPU9150(this); // Shimmer3 - Pressure/Temperature public SensorBMPX80 mSensorBMPX80 = new SensorBMP180(this); - + /** The pressure sensor a Shimmer3R reported in-band in its + * PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE (a {@link PRESSURE_SENSOR_ID}), + * or null if it hasn't. When set it overrides the SR-number rule in + * {@link #isSupportedBmp581()}. Cleared whenever the expansion board details + * change, so each connection starts from the SR-number rule again. */ + protected Integer mPressureSensorIdInBand = null; + // Shimmer3r - Mag private SensorLIS2MDL mSensorLIS2MDL = new SensorLIS2MDL(this); // Shimmer3r - Alt Mag @@ -10822,31 +10838,50 @@ public static boolean isSupportedBmp280(ShimmerVerObject svo, ExpansionBoardDeta * SR number/revision and the firmware version that produces the new output * format. * - * @return true if the connected Shimmer3R board revision and firmware version indicate BMP581 output support + *
+ * If the Shimmer has reported its pressure sensor in-band (the sensor ID in + * its PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, LogAndStream_Shimmer3R + * v1.01.007 onwards), that answer is used and overrides the SR-number rule + * in either direction. Otherwise, e.g. when the firmware NACKs the command + * or is too old to have it, the SR-number rule decides. + * + * @return true if the connected Shimmer3R reported a BMP581, or if its board revision and firmware version indicate BMP581 output support */ public boolean isSupportedBmp581() { + if(mPressureSensorIdInBand!=null && getHardwareVersion()==HW_ID.SHIMMER_3R){ + return mPressureSensorIdInBand==PRESSURE_SENSOR_ID.BMP581; + } return isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails()); } + /** The SR-number rule alone, used when the Shimmer hasn't identified its + * pressure sensor in-band and for SD log files (whose header carries no + * sensor ID). It mirrors the firmware's own fallback, + * ShimBrd_isBmp581PresentPerSrNumber() in log-and-stream-common + * Boards/shimmer_boards.c, plus a firmware-version guard. + * + * @param svo the Shimmer's version details + * @param ebd the Shimmer's expansion board details + * @return true if a Shimmer3R with this board and firmware carries a BMP581 + */ public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDetails ebd) { if(svo==null || ebd==null || svo.getHardwareVersion()!=HW_ID.SHIMMER_3R){ return false; } - // SR48 (GSR+): BMP581 spans two bands - 7.2..7.x AND 8.2 and above. - // 7.0/7.1 and 8.0/8.1 are BMP390, so a single lexicographic ">=" can't - // express it (8.0/8.1 fall between the two BMP581 bands). - boolean sr48Bmp581 = - (ebd.getExpansionBoardId()==HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED - && ebd.getExpansionBoardRev()==7 - && ebd.getExpansionBoardRevSpecial()>=2) // SR48 7.2..7.x - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2); // SR48 8.2 and above - + // Fitted models (">=" applies to both rev fields): SR31-11-2, SR38-4-2, + // SR47-8-2, SR48-7-2, SR48-8-2, SR49-4-2. SR48 (GSR+) has two BMP581 + // bands, 7.2..7.x and 8.2 onwards: 8.0/8.1 went back to the BMP390, so + // the rev-7 band is ">= 7.2 and not >= 8.0". An unprogrammed card (board + // ID 0x00 or 0xFF) matches none of these IDs. boolean boardEligible = - ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2 - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2 - || sr48Bmp581 // SR48 7.2..7.x and 8.2+ - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2 + ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2) // SR38 >= 4.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2 + || (ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 2) + && !ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 0)) // SR48 7.2..7.x + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2) // SR48 >= 8.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2 // Format guard: BMP581 (pre-compensated) output only exists from // LogAndStream_Shimmer3R v1.01.006 onwards. @@ -10855,6 +10890,70 @@ public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDeta return boardEligible && fwEligible; } + /** Records the pressure sensor a Shimmer3R reported in-band and, if the + * sensor class in use is not the one it names, rebuilds the sensor classes + * around the reported sensor. The rebuild is what would have happened had + * the sensor been known when the daughter card ID arrived: the config bytes, + * which the Shimmer sends before this response while connecting, are parsed + * again so the new sensor class picks up its settings (e.g. oversampling). + * + * @param pressureSensorId the sensor ID from the response, a {@link PRESSURE_SENSOR_ID} + */ + public void setPressureSensorIdInBand(int pressureSensorId) { + mPressureSensorIdInBand = pressureSensorId; + + if(isShimmerGen3R()){ + boolean bmp581InUse = mSensorBMPX80.mSensorType==SENSORS.BMP581; + if(bmp581InUse!=isSupportedBmp581()){ + consolePrintLn("Pressure sensor reported in-band (ID " + pressureSensorId + ") differs from the SR-number rule, switching to " + (isSupportedBmp581()? "BMP581":"BMP390")); + sensorAndConfigMapsCreate(); + if(mShimmerUsingConfigFromInfoMem && ConfigByteLayout.checkConfigBytesValid(mConfigBytes)){ + configBytesParse(mConfigBytes, COMMUNICATION_TYPE.BLUETOOTH); + } + } + } + } + + /** + * @return the pressure sensor the Shimmer reported in-band (a {@link PRESSURE_SENSOR_ID}), or null if it hasn't + */ + public Integer getPressureSensorIdInBand() { + return mPressureSensorIdInBand; + } + + /** + * @param pressureSensorId a {@link PRESSURE_SENSOR_ID} + * @return the number of coefficient bytes that follow this sensor ID in a + * PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, or -1 if the ID is unknown + */ + public static int getPressureCalibCoefficientByteLength(int pressureSensorId) { + switch(pressureSensorId){ + case PRESSURE_SENSOR_ID.BMP180: + return 22; + case PRESSURE_SENSOR_ID.BMP280: + return 24; + case PRESSURE_SENSOR_ID.BMP390: + return 21; + case PRESSURE_SENSOR_ID.BMP581: + return 0; // self-compensating, no coefficients + default: + return -1; + } + } + + @Override + public void setExpansionBoardDetails(ExpansionBoardDetails eBD){ + super.setExpansionBoardDetails(eBD); + // A different board may carry a different pressure sensor + mPressureSensorIdInBand = null; + } + + @Override + public void clearExpansionBoardDetails(){ + super.clearExpansionBoardDetails(); + mPressureSensorIdInBand = null; + } + /** Returns true if the Shimmer is using new sensors. These sensors are: *
+ * No hardware: the response handler reads from an in-memory byte queue. It lives
+ * in this package because the handler and the instruction stack are protected
+ * members of ShimmerBluetooth.
+ *
+ * @author Mark Nolan
+ */
+public class API_00021_Bmp581InBandSensorIdTest {
+
+ /** Coefficients of a real BMP390 (ByteCommunicationSimulatorS3R.getPressureResoTest()) */
+ private static final byte[] BMP390_COEFFICIENTS = {
+ (byte) 0xE7, (byte) 0x6B, (byte) 0xF0, (byte) 0x4A, (byte) 0xF9,
+ (byte) 0xAB, (byte) 0x1C, (byte) 0x9B, (byte) 0x15, (byte) 0x06,
+ (byte) 0x01, (byte) 0xD2, (byte) 0x49, (byte) 0x18, (byte) 0x5F,
+ (byte) 0x03, (byte) 0xFA, (byte) 0x3A, (byte) 0x0F, (byte) 0x07,
+ (byte) 0xF5
+ };
+
+ /** Bytes following the 0xA6 response byte */
+ private static byte[] response(int sensorId, byte[] coefficients) {
+ byte[] bytes = new byte[2 + coefficients.length];
+ bytes[0] = (byte) (1 + coefficients.length);
+ bytes[1] = (byte) sensorId;
+ System.arraycopy(coefficients, 0, bytes, 2, coefficients.length);
+ return bytes;
+ }
+
+ private static class QueueRadio implements ByteCommunication {
+ final Deque