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
18 changes: 14 additions & 4 deletions ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ public class HighGAccelSensor : IMUSensor , SensorProcessing{
let x = Array(sensorPacket[packetIndexHighGAccelX..<packetIndexHighGAccelX+2])
let y = Array(sensorPacket[packetIndexHighGAccelY..<packetIndexHighGAccelY+2])
let z = Array(sensorPacket[packetIndexHighGAccelZ..<packetIndexHighGAccelZ+2])
let rawDataX = Double(ShimmerUtilities.parseSensorData(sensorData: x, dataType: SensorDataType.i12MSB)!)
let rawDataY = Double(ShimmerUtilities.parseSensorData(sensorData: y, dataType: SensorDataType.i12MSB)!)
let rawDataZ = Double(ShimmerUtilities.parseSensorData(sensorData: z, dataType: SensorDataType.i12MSB)!)
if (calibrationEnabled && AlignmentMatrix.count == 3 && SensitivityMatrix.count == 3 && OffsetVector.count == 3){
// 0x14-0x16 are big-endian on both generations, in different encodings
// (log-and-stream-common docs/SHIMMER3_STREAMING_DATA_FORMAT.md section 5).
// A Shimmer3R sends the ADXL371's 12-bit value, left-justified. A Shimmer3
// sends its MPU9x50/ICM-20948 accelerometer as a plain int16; decoding that
// as 12 bits read it at a sixteenth of its value (DEV-1338).
let dataType = HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3.rawValue ? SensorDataType.i16MSB : SensorDataType.i12MSB
let rawDataX = Double(ShimmerUtilities.parseSensorData(sensorData: x, dataType: dataType)!)
let rawDataY = Double(ShimmerUtilities.parseSensorData(sensorData: y, dataType: dataType)!)
let rawDataZ = Double(ShimmerUtilities.parseSensorData(sensorData: z, dataType: dataType)!)
// Raw only on a Shimmer3, as in the Java API: its accelerometer here has no
// calibration parameters stored anywhere, and the defaults above are the
// ADXL371's, which would only give a calibrated-looking value with no meaning.
let isShimmer3R = HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3R.rawValue
if (calibrationEnabled && isShimmer3R && AlignmentMatrix.count == 3 && SensitivityMatrix.count == 3 && OffsetVector.count == 3){
let data:[Double] = [rawDataX,rawDataY,rawDataZ]

let(calData)=IMUSensor.calibrateInertialSensorData(data,AlignmentMatrix,SensitivityMatrix,OffsetVector)
Expand Down
4 changes: 4 additions & 0 deletions ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol {
exgSensor = EXGSensor()
pressureTempSensor = PressureTempSensor(hwid: REV_HW_MAJOR)
battVoltageSensor = BattVoltageSensor()
// 0x14-0x16 carry this generation's MPU9x50/ICM-20948 accelerometer, and
// HighGAccelSensor decodes them by its HardwareVersion (DEV-1338). Without
// this it kept the UNKNOWN default, or a Shimmer3R's from an earlier connection.
highGAccelSensor = HighGAccelSensor(hwid: REV_HW_MAJOR)
} else if(REV_HW_MAJOR==HardwareType.Shimmer3R.rawValue){
lnAccelSensor = LNAccelSensor(hwid: REV_HW_MAJOR)
magSensor = MagSensor(hwid: REV_HW_MAJOR)
Expand Down
45 changes: 45 additions & 0 deletions ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,49 @@ class SensorTest: XCTestCase {
let name = [GSRSensor.GSR_SKIN_RESISTANCE, Sensor.SensorFormats.Calibrated.rawValue, Sensor.SensorUnits.kiloOhms.rawValue].joined(separator: "_")
return objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: name)!]
}

// DEV-1338: channels 0x14-0x16 are a left-justified 12-bit value on a
// Shimmer3R and a plain big-endian int16 on a Shimmer3.
func testSensorHighGAccelDecodePerGeneration(){
// Shimmer3R: resting ADXL371 counts 1, -5, -10, each shifted left 4 and sent MSB first.
XCTAssertEqual(highGAccelRaw(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue,
packet: [0x00, 0x10, 0xFF, 0xB0, 0xFF, 0x60]), [1, -5, -10])
// Shimmer3: -300, 1234, -16384 as big-endian int16. The 12-bit decode read -19, 77, -1024.
XCTAssertEqual(highGAccelRaw(hwid: Shimmer3Protocol.HardwareType.Shimmer3.rawValue,
packet: [0xFE, 0xD4, 0x04, 0xD2, 0xC0, 0x00]), [-300, 1234, -16384])
}

// DEV-1338: a Shimmer3's alt accel is raw only, as in the Java API. The
// calibration defaults are the Shimmer3R ADXL371's, and nothing replaces them
// on a Shimmer3, so a calibrated value there would have no meaning.
func testSensorHighGAccelCalibratedOnlyOnShimmer3R(){
let packet: [UInt8] = [0xFE, 0xD4, 0x04, 0xD2, 0xC0, 0x00]
let calibratedX = [HighGAccelSensor.HIGHG_ACCEL_X, Sensor.SensorFormats.Calibrated.rawValue, Sensor.SensorUnits.meterPerSecondSquared.rawValue].joined(separator: "_")
let rawX = [HighGAccelSensor.HIGHG_ACCEL_X, Sensor.SensorFormats.Raw.rawValue, Sensor.SensorUnits.meterPerSecondSquared.rawValue].joined(separator: "_")

let shimmer3 = highGAccelCluster(hwid: Shimmer3Protocol.HardwareType.Shimmer3.rawValue, packet: packet, calibrationEnabled: true)
XCTAssertFalse(shimmer3.SignalNames.contains(calibratedX), "a Shimmer3 has no calibration for this accelerometer")
XCTAssertTrue(shimmer3.SignalNames.contains(rawX), "a Shimmer3 still reports the raw value")

let shimmer3R = highGAccelCluster(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue, packet: packet, calibrationEnabled: true)
XCTAssertTrue(shimmer3R.SignalNames.contains(calibratedX), "a Shimmer3R is still calibrated")
XCTAssertTrue(shimmer3R.SignalNames.contains(rawX))
}

private func highGAccelRaw(hwid: Int, packet: [UInt8]) -> [Double] {
let objectCluster = highGAccelCluster(hwid: hwid, packet: packet, calibrationEnabled: false)
return [HighGAccelSensor.HIGHG_ACCEL_X, HighGAccelSensor.HIGHG_ACCEL_Y, HighGAccelSensor.HIGHG_ACCEL_Z].map { axis in
let name = [axis, Sensor.SensorFormats.Raw.rawValue, Sensor.SensorUnits.meterPerSecondSquared.rawValue].joined(separator: "_")
return objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: name)!]
}
}

private func highGAccelCluster(hwid: Int, packet: [UInt8], calibrationEnabled: Bool) -> ObjectCluster {
let sensor = HighGAccelSensor(hwid: hwid)
sensor.calibrationEnabled = calibrationEnabled
sensor.packetIndexHighGAccelX = 0
sensor.packetIndexHighGAccelY = 2
sensor.packetIndexHighGAccelZ = 4
return sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test"))
}
}
Loading