From 54a6b02f2b8f2bee872658dc97d488164a3b53e1 Mon Sep 17 00:00:00 2001 From: Mark Nolan Date: Sun, 25 Oct 2026 06:55:07 +0000 Subject: [PATCH 1/2] DEV-1338: decode a Shimmer3's alt accel as a big-endian int16 HighGAccelSensor decoded channels 0x14-0x16 as i12MSB on every hardware. That is right for a Shimmer3R (the ADXL371's 12-bit value, left-justified). A Shimmer3 sends its MPU9x50/ICM-20948 accelerometer on those IDs as a plain big-endian int16 (log-and-stream-common SHIMMER3_STREAMING_DATA_FORMAT.md section 5), so it read at a sixteenth of its value. Decode it as i16MSB on Shimmer3, mirroring GyroSensor's per-hardware branch. Shimmer3R is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../ShimmerBluetooth/HighGAccelSensor.swift | 12 +++++++--- .../ShimmerBluetoothTests/SensorTest.swift | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift index 132dab9..9a8d85d 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift @@ -26,9 +26,15 @@ public class HighGAccelSensor : IMUSensor , SensorProcessing{ let x = Array(sensorPacket[packetIndexHighGAccelX.. [Double] { + let sensor = HighGAccelSensor(hwid: hwid) + sensor.calibrationEnabled = false + sensor.packetIndexHighGAccelX = 0 + sensor.packetIndexHighGAccelY = 2 + sensor.packetIndexHighGAccelZ = 4 + let objectCluster = sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test")) + 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)!] + } + } } From 3a16718e2b9625940ea0f39f2c82bd91c8df9575 Mon Sep 17 00:00:00 2001 From: JongChern Date: Wed, 4 Nov 2026 10:55:35 +0800 Subject: [PATCH 2/2] DEV-1338: build the alt accel sensor for a Shimmer3, and keep it raw there The int16 decode keys off HighGAccelSensor's HardwareVersion, but createSensors() rebuilt that sensor only for a Shimmer3R. On a Shimmer3 it kept the UNKNOWN default (or a Shimmer3R's from an earlier connection), so the 12-bit decode still ran on a real device. The Shimmer3 branch now builds it with the device's hardware version. A Shimmer3 also got a calibrated value, from the ADXL371 defaults that nothing replaces on that generation. The Java API defines this channel as raw only, since no calibration for it is stored anywhere, so this API now calibrates it on a Shimmer3R only. testSensorHighGAccelCalibratedOnlyOnShimmer3R covers the second change. The createSensors() line is not covered: Shimmer3Protocol cannot be constructed in a test. Co-Authored-By: Claude Opus 5.5 --- .../ShimmerBluetooth/HighGAccelSensor.swift | 6 +++- .../ShimmerBluetooth/Shimmer3Protocol.swift | 4 +++ .../ShimmerBluetoothTests/SensorTest.swift | 33 +++++++++++++++---- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift index 9a8d85d..e223be6 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/HighGAccelSensor.swift @@ -35,7 +35,11 @@ public class HighGAccelSensor : IMUSensor , SensorProcessing{ 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)!) - if (calibrationEnabled && AlignmentMatrix.count == 3 && SensitivityMatrix.count == 3 && OffsetVector.count == 3){ + // 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) diff --git a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift index 5a108b5..ca8416e 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift @@ -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) diff --git a/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift b/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift index 58836cb..f245588 100644 --- a/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift +++ b/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift @@ -497,16 +497,37 @@ class SensorTest: XCTestCase { 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 sensor = HighGAccelSensor(hwid: hwid) - sensor.calibrationEnabled = false - sensor.packetIndexHighGAccelX = 0 - sensor.packetIndexHighGAccelY = 2 - sensor.packetIndexHighGAccelZ = 4 - let objectCluster = sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test")) + 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")) + } }