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 @@ -78,6 +78,7 @@
import com.shimmerresearch.sensors.bmpX80.SensorBMP390;
import com.shimmerresearch.sensors.bmpX80.SensorBMP581;
import com.shimmerresearch.sensors.bmpX80.SensorBMPX80;
import com.shimmerresearch.sensors.bmpX80.SdHeaderPressureSensorId;
import com.shimmerresearch.sensors.kionix.SensorKionixAccel;
import com.shimmerresearch.sensors.kionix.SensorKionixKXRB52042;
import com.shimmerresearch.sensors.kionix.SensorKionixKXTC92050;
Expand Down Expand Up @@ -697,6 +698,11 @@ public String toString() {
* {@link #isSupportedBmp581()}. Cleared whenever the expansion board details
* change, so each connection starts from the SR-number rule again. */
protected Integer mPressureSensorIdInBand = null;
/** The pressure sensor an SD log file's header records at offset 224, or
* null if none has been applied. When present it overrides the SR-number
* rule in {@link #isSupportedBmp581()} and {@link #isSupportedBmp280()}.
* Cleared whenever the expansion board details change. */
protected SdHeaderPressureSensorId mPressureSensorIdSdHeader = null;

// Shimmer3r - Mag
private SensorLIS2MDL mSensorLIS2MDL = new SensorLIS2MDL(this);
Expand Down Expand Up @@ -2045,14 +2051,20 @@ else if (isEXGUsingDefaultThreeUnipolarConfiguration()){
uncalibratedData[iUP]=(double)newPacketInt[iUP];
uncalibratedDataUnits[iUT]=CHANNEL_UNITS.NO_UNITS;
uncalibratedDataUnits[iUP]=CHANNEL_UNITS.NO_UNITS;
if (mEnableCalibration){
if (mEnableCalibration && isPressureSensorCalibratable()){
Comment thread
marknolan marked this conversation as resolved.
Resolved
double[] bmp180caldata = mSensorBMPX80.calibratePressureSensorData(UP,UT);
objectCluster.addDataToMap(signalNameBmpX80Pressure,CHANNEL_TYPE.CAL.toString(),CHANNEL_UNITS.KPASCAL,bmp180caldata[0]/1000);
objectCluster.addDataToMap(signalNameBmpX80Temperature,CHANNEL_TYPE.CAL.toString(),CHANNEL_UNITS.DEGREES_CELSIUS,bmp180caldata[1]);
calibratedData[iUT]=bmp180caldata[1];
calibratedData[iUP]=bmp180caldata[0]/1000;
calibratedDataUnits[iUT]=CHANNEL_UNITS.DEGREES_CELSIUS;
calibratedDataUnits[iUP]=CHANNEL_UNITS.KPASCAL;
} else if (mEnableCalibration){
// The SD header names a sensor this parser cannot calibrate, or
// none fitted. The multimap has no CAL entry, so it reads NaN;
// leaving the arrays at 0 would look like a real reading.
calibratedData[iUT]=Double.NaN;
calibratedData[iUP]=Double.NaN;
}
}

Expand Down Expand Up @@ -5535,7 +5547,9 @@ private void createMapOfSensorClasses() {

}
else if(isSupportedNewImuSensors()){
mSensorBMPX80 = new SensorBMP280(this);
// The barometer follows isSupportedBmp280(), which an SD header's
// pressure sensor ID can set apart from the IMU generation
mSensorBMPX80 = isSupportedBmp280()? new SensorBMP280(this):new SensorBMP180(this);
addSensorClass(mSensorBMPX80);

mSensorLSM303 = new SensorLSM303AH(this);
Expand All @@ -5551,7 +5565,7 @@ else if(isSupportedNewImuSensors()){
}

else{
mSensorBMPX80 = new SensorBMP180(this);
mSensorBMPX80 = isSupportedBmp280()? new SensorBMP280(this):new SensorBMP180(this);
addSensorClass(mSensorBMPX80);

mSensorLSM303 = new SensorLSM303DLHC(this);
Expand Down Expand Up @@ -10825,9 +10839,17 @@ public boolean isSupportedErrorLedControl() {
/** Returns true if the Shimmer is using new sensors. These sensors are:
* <li> Use BMP280 instead of BMP180 as barometer.
*
* <p>
* If an SD log file's header names its pressure sensor (see
* {@link #setPressureSensorIdFromSdHeader(int)}), that answer is used
* instead of the board revision.
*
* @return
*/
public boolean isSupportedBmp280() {
if(mPressureSensorIdSdHeader!=null && mPressureSensorIdSdHeader.isPresent() && getHardwareVersion()==HW_ID.SHIMMER_3){
return mPressureSensorIdSdHeader.isSensor(PRESSURE_SENSOR_ID.BMP280);
}
return isSupportedNewImuSensors();
}

Expand All @@ -10849,18 +10871,27 @@ public static boolean isSupportedBmp280(ShimmerVerObject svo, ExpansionBoardDeta
* in either direction. Otherwise, e.g. when the firmware NACKs the command
* or is too old to have it, the SR-number rule decides.
*
* <p>
* An SD log file's header names its pressure sensor from
* LogAndStream_Shimmer3R v1.01.018 (see
* {@link #setPressureSensorIdFromSdHeader(int)}), and that overrides the
* SR-number rule in the same way.
*
* @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;
}
if(mPressureSensorIdSdHeader!=null && mPressureSensorIdSdHeader.isPresent() && getHardwareVersion()==HW_ID.SHIMMER_3R){
return mPressureSensorIdSdHeader.isSensor(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,
* pressure sensor in-band and for SD log files whose header doesn't name
* it. It mirrors the firmware's own fallback,
* ShimBrd_isBmp581PresentPerSrNumber() in log-and-stream-common
* Boards/shimmer_boards.c, plus a firmware-version guard.
*
Expand Down Expand Up @@ -10925,6 +10956,64 @@ public Integer getPressureSensorIdInBand() {
return mPressureSensorIdInBand;
}

/** Applies the pressure sensor an SD log file's header records at offset
* 224. The byte is ignored (the SR-number rule decides, as before) unless
* the firmware is new enough to write it. Call this after the version and
* expansion board details are set, because setting the board clears it,
* and before {@link #sensorAndConfigMapsCreate()}, which builds the
* pressure sensor class from it.
*
* @param headerValue the byte at {@link SdHeaderPressureSensorId#SD_HEADER_INDEX}, 0-255
* @return what the byte means for this file
*/
public SdHeaderPressureSensorId setPressureSensorIdFromSdHeader(int headerValue) {
SdHeaderPressureSensorId sdHeaderId = SdHeaderPressureSensorId.parse(getShimmerVerObject(), headerValue);

mPressureSensorIdSdHeader = sdHeaderId;

boolean isShimmer3R = getHardwareVersion()==HW_ID.SHIMMER_3R;
boolean isNewerSensorPerSrRule = isShimmer3R?
isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails()):isSupportedNewImuSensors();

if(sdHeaderId.isKnown()){
boolean isNewerSensorPerHeader = sdHeaderId.isSensor(isShimmer3R? PRESSURE_SENSOR_ID.BMP581:PRESSURE_SENSOR_ID.BMP280);
if(isNewerSensorPerHeader!=isNewerSensorPerSrRule){
consolePrintLn("Pressure sensor in the SD header, " + sdHeaderId + ", differs from the SR-number rule, using the SD header");
}
if(sdHeaderId.isInferred()){
consolePrintErrLn("Pressure sensor in the SD header, " + sdHeaderId + ", was not confirmed by chip ID");
}
} else if(sdHeaderId.getState()==SdHeaderPressureSensorId.STATE.UNKNOWN){
consolePrintErrLn("Pressure sensor in the SD header, " + sdHeaderId + ", is not one this parser knows, pressure and temperature will be uncalibrated");
}
return sdHeaderId;
}

/**
* @return the pressure sensor ID applied from an SD log file's header, or
* null if none has been applied
*/
public SdHeaderPressureSensorId getPressureSensorIdSdHeader() {
return mPressureSensorIdSdHeader;
}

/**
* @return true if an SD log file's header says the pressure sensor was
* inferred from the SR number rather than confirmed by chip ID
*/
public boolean isPressureSensorInferred() {
return mPressureSensorIdSdHeader!=null && mPressureSensorIdSdHeader.isPresent() && mPressureSensorIdSdHeader.isInferred();
}

/**
* @return false if an SD log file's header names a pressure sensor this
* parser doesn't know, or says none is fitted, in which case the
* pressure and temperature channels are emitted uncalibrated
*/
public boolean isPressureSensorCalibratable() {
return mPressureSensorIdSdHeader==null || !mPressureSensorIdSdHeader.isUncalibrated();
}

/**
* @param pressureSensorId a {@link PRESSURE_SENSOR_ID}
* @return the number of coefficient bytes that follow this sensor ID in a
Expand All @@ -10950,12 +11039,14 @@ public void setExpansionBoardDetails(ExpansionBoardDetails eBD){
super.setExpansionBoardDetails(eBD);
// A different board may carry a different pressure sensor
mPressureSensorIdInBand = null;
mPressureSensorIdSdHeader = null;
}

@Override
public void clearExpansionBoardDetails(){
super.clearExpansionBoardDetails();
mPressureSensorIdInBand = null;
mPressureSensorIdSdHeader = null;
}

/** Returns true if the Shimmer is using new sensors. These sensors are:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.shimmerresearch.sensors.bmpX80;

import java.io.Serializable;

import com.shimmerresearch.driver.ShimmerObject.PRESSURE_SENSOR_ID;
import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID;
import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID;
import com.shimmerresearch.driverUtilities.ShimmerVerObject;
import com.shimmerresearch.driverUtilities.UtilShimmer;

/**
* The pressure sensor LogAndStream firmware records at SD header offset 224
* (SDH_PRESSURE_SENSOR_ID in log-and-stream-common SDCard/shimmer_sd_header.h).
* Offset 224 is the same in the 256-byte Shimmer3 header and the 384-byte
* Shimmer3R header.
*
* <p>
* Bits 0-6 hold a {@link PRESSURE_SENSOR_ID}, the same numbering as the 0xA6
* response. Bit 7 means the firmware inferred the sensor from the SR number
* because the chip-ID check was inconclusive. 0xFE means no pressure sensor is
* fitted (Shimmer3 only). The firmware pre-fills the header with 0xFF, so 0xFF
* means the firmware predates the field.
*
* <p>
* The byte is only trusted from LogAndStream_Shimmer3R v1.01.018 and
* LogAndStream_Shimmer3 v1.01.006. The two version lines overlap, so the gate
* checks the hardware version too.
*/
public class SdHeaderPressureSensorId implements Serializable {

// ShimmerObject holds one, and ShimmerPC.deepClone() serialises the device
private static final long serialVersionUID = 1258143216796546316L;

public static final int SD_HEADER_INDEX = 224;

public static final int ID_MASK = 0x7F;
public static final int INFERRED_FLAG = 0x80;
public static final int NOT_FITTED = 0xFE;
public static final int NOT_RECORDED = 0xFF;

public enum STATE {
/** Firmware below the gate, or 0xFF: the SR-number rule decides, as before. */
ABSENT,
/** A sensor this platform's parser handles; it overrides the SR-number rule. */
KNOWN,
/** Present but not a sensor this parser handles on this platform: emit the channels uncalibrated, no SR-number fallback. */
UNKNOWN,
/** 0xFE, no pressure sensor fitted: any pressure channels in the file are emitted uncalibrated. */
NOT_FITTED
}

private final STATE mState;
private final int mSensorId;
private final boolean mIsInferred;
private final int mRawValue;

private SdHeaderPressureSensorId(STATE state, int sensorId, boolean isInferred, int rawValue) {
mState = state;
mSensorId = sensorId;
mIsInferred = isInferred;
mRawValue = rawValue;
}

/**
* @param svo the version details from the same header
* @return true if this firmware writes a trustworthy offset 224
*/
public static boolean isSupported(ShimmerVerObject svo) {
if(svo==null){
return false;
}
return isVerAtLeast(svo, HW_ID.SHIMMER_3R, 1, 1, 18)
|| isVerAtLeast(svo, HW_ID.SHIMMER_3, 1, 1, 6);
}

// ShimmerVerObject.compareVersions(svo, hw, ...) ignores the hardware
// version, so go to UtilShimmer directly
private static boolean isVerAtLeast(ShimmerVerObject svo, int hardwareVersion, int major, int minor, int internal) {
return UtilShimmer.compareVersions(svo.getHardwareVersion(), svo.getFirmwareIdentifier(),
svo.getFirmwareVersionMajor(), svo.getFirmwareVersionMinor(), svo.getFirmwareVersionInternal(),
hardwareVersion, FW_ID.LOGANDSTREAM, major, minor, internal);
}

/**
* @param svo the version details from the same header
* @param headerValue the byte at {@link #SD_HEADER_INDEX}, 0-255
* @return what the byte means for this file
*/
public static SdHeaderPressureSensorId parse(ShimmerVerObject svo, int headerValue) {
headerValue &= 0xFF;
if(headerValue==NOT_RECORDED || !isSupported(svo)){
return new SdHeaderPressureSensorId(STATE.ABSENT, -1, false, headerValue);
}
if(headerValue==NOT_FITTED){
return new SdHeaderPressureSensorId(STATE.NOT_FITTED, -1, false, headerValue);
}

int sensorId = headerValue & ID_MASK;
boolean isInferred = (headerValue & INFERRED_FLAG)!=0;
STATE state = isSensorHandled(svo.getHardwareVersion(), sensorId)? STATE.KNOWN:STATE.UNKNOWN;
return new SdHeaderPressureSensorId(state, sensorId, isInferred, headerValue);
}

/** The parser has no Shimmer3R path for a BMP180/280 or Shimmer3 path for a
* BMP390/581, so those are as unknown as a future ID would be. */
private static boolean isSensorHandled(int hardwareVersion, int sensorId) {
if(hardwareVersion==HW_ID.SHIMMER_3R){
return sensorId==PRESSURE_SENSOR_ID.BMP390 || sensorId==PRESSURE_SENSOR_ID.BMP581;
}
return sensorId==PRESSURE_SENSOR_ID.BMP180 || sensorId==PRESSURE_SENSOR_ID.BMP280;
}

public STATE getState() {
return mState;
}

/**
* @return true unless the SR-number rule should decide ({@link STATE#ABSENT})
*/
public boolean isPresent() {
return mState!=STATE.ABSENT;
}

/**
* @return true if the byte names a sensor this parser can calibrate
*/
public boolean isKnown() {
return mState==STATE.KNOWN;
}

/**
* @return true if the channels must not be calibrated as any BMP
* ({@link STATE#UNKNOWN} or {@link STATE#NOT_FITTED})
*/
public boolean isUncalibrated() {
return mState==STATE.UNKNOWN || mState==STATE.NOT_FITTED;
}

/**
* @param sensorId a {@link PRESSURE_SENSOR_ID}
* @return true if the byte names this sensor and the parser handles it
*/
public boolean isSensor(int sensorId) {
return mState==STATE.KNOWN && mSensorId==sensorId;
}

/**
* @return the low 7 bits, a {@link PRESSURE_SENSOR_ID} if known, or -1 if
* {@link STATE#ABSENT} or {@link STATE#NOT_FITTED}
*/
public int getSensorId() {
return mSensorId;
}

/**
* @return true if the firmware inferred the sensor from the SR number
* rather than confirming it by chip ID
*/
public boolean isInferred() {
return mIsInferred;
}

public int getRawValue() {
return mRawValue;
}

@Override
public String toString() {
return String.format("0x%02X (%s%s)", mRawValue, mState, mIsInferred? ", inferred from SR number":"");
}
}
Loading
Loading