|
| 1 | +// |
| 2 | +// AltMagSensor.swift |
| 3 | +// ShimmerBluetooth |
| 4 | +// |
| 5 | +// Created by Shimmer Engineering on 26/05/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public class AltMagSensor : IMUSensor , SensorProcessing{ |
| 11 | + |
| 12 | + public enum Range3R: UInt8 { |
| 13 | + case RANGE_4Ga = 0x0 |
| 14 | + case RANGE_8Ga = 0x1 |
| 15 | + case RANGE_12Ga = 0x2 |
| 16 | + case RANGE_16Ga = 0x3 |
| 17 | + |
| 18 | + public static func fromValue(_ value : UInt8) -> Range3R? { |
| 19 | + switch value{ |
| 20 | + case 0: |
| 21 | + return .RANGE_4Ga |
| 22 | + case 1: |
| 23 | + return .RANGE_8Ga |
| 24 | + case 2: |
| 25 | + return .RANGE_12Ga |
| 26 | + case 3: |
| 27 | + return .RANGE_16Ga |
| 28 | + default: |
| 29 | + return nil |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + var Current3RRange = Range3R.RANGE_4Ga |
| 34 | + |
| 35 | + public var packetIndexAltMagX:Int = -1 |
| 36 | + public var packetIndexAltMagY:Int = -1 |
| 37 | + public var packetIndexAltMagZ:Int = -1 |
| 38 | + public static let ALT_MAGNETOMETER_X = "Alt Magnetometer X" |
| 39 | + public static let ALT_MAGNETOMETER_Y = "Alt Magnetometer Y" |
| 40 | + public static let ALT_MAGNETOMETER_Z = "Alt Magnetometer Z" |
| 41 | + var calibBytes_4Ga: [UInt8] = [] |
| 42 | + var calibBytes_8Ga: [UInt8] = [] |
| 43 | + var calibBytes_12Ga: [UInt8] = [] |
| 44 | + var calibBytes_16Ga: [UInt8] = [] |
| 45 | + var altMagRange = 0 |
| 46 | + var CALIBRATION_ID = 41 |
| 47 | + var AlignmentMatrix : [[Double]] = [[]] |
| 48 | + var SensitivityMatrix : [[Double]] = [[]] |
| 49 | + var OffsetVector : [Double] = [] |
| 50 | + var AlignmentMatrix_4Ga:[[Double]] = [[]] |
| 51 | + var SensitivityMatrix_4Ga:[[Double]] = [[]] |
| 52 | + var OffsetVector_4Ga:[Double]=[] |
| 53 | + var AlignmentMatrix_8Ga:[[Double]] = [[]] |
| 54 | + var SensitivityMatrix_8Ga:[[Double]] = [[]] |
| 55 | + var OffsetVector_8Ga:[Double]=[] |
| 56 | + var AlignmentMatrix_12Ga:[[Double]] = [[]] |
| 57 | + var SensitivityMatrix_12Ga:[[Double]] = [[]] |
| 58 | + var OffsetVector_12Ga:[Double]=[] |
| 59 | + var AlignmentMatrix_16Ga:[[Double]] = [[]] |
| 60 | + var SensitivityMatrix_16Ga:[[Double]] = [[]] |
| 61 | + var OffsetVector_16Ga:[Double]=[] |
| 62 | + |
| 63 | + public func get3RRange()->Range3R{ |
| 64 | + return Current3RRange |
| 65 | + } |
| 66 | + |
| 67 | + public func processData(sensorPacket: [UInt8], objectCluster: ObjectCluster) -> ObjectCluster { |
| 68 | + let x = Array(sensorPacket[packetIndexAltMagX..<packetIndexAltMagX+2]) |
| 69 | + let y = Array(sensorPacket[packetIndexAltMagY..<packetIndexAltMagY+2]) |
| 70 | + let z = Array(sensorPacket[packetIndexAltMagZ..<packetIndexAltMagZ+2]) |
| 71 | + let rawDataX = Double(ShimmerUtilities.parseSensorData(sensorData: x, dataType: SensorDataType.i16)!) |
| 72 | + let rawDataY = Double(ShimmerUtilities.parseSensorData(sensorData: y, dataType: SensorDataType.i16)!) |
| 73 | + let rawDataZ = Double(ShimmerUtilities.parseSensorData(sensorData: z, dataType: SensorDataType.i16)!) |
| 74 | + if (calibrationEnabled){ |
| 75 | + let data:[Double] = [rawDataX,rawDataY,rawDataZ] |
| 76 | + |
| 77 | + let(calData)=IMUSensor.calibrateInertialSensorData(data,AlignmentMatrix,SensitivityMatrix,OffsetVector) |
| 78 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_X, formatName: SensorFormats.Calibrated.rawValue, unitName: SensorUnits.localflux.rawValue, value: calData![0]) |
| 79 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_Y, formatName: SensorFormats.Calibrated.rawValue, unitName: SensorUnits.localflux.rawValue, value: calData![1]) |
| 80 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_Z, formatName: SensorFormats.Calibrated.rawValue, unitName: SensorUnits.localflux.rawValue, value: calData![2]) |
| 81 | + print("M X : \(calData![0]) , M Y : \(calData![1]), M Z : \(calData![2])") |
| 82 | + } |
| 83 | + print("MR X : \(rawDataX) , MR Y : \(rawDataY), MR Z : \(rawDataZ)") |
| 84 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_X, formatName: SensorFormats.Raw.rawValue, unitName: SensorUnits.localflux.rawValue, value: rawDataX) |
| 85 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_Y, formatName: SensorFormats.Raw.rawValue, unitName: SensorUnits.localflux.rawValue, value: rawDataY) |
| 86 | + objectCluster.addData(sensorName: AltMagSensor.ALT_MAGNETOMETER_Z, formatName: SensorFormats.Raw.rawValue, unitName: SensorUnits.localflux.rawValue, value: rawDataZ) |
| 87 | + |
| 88 | + return objectCluster |
| 89 | + } |
| 90 | + |
| 91 | + public override func parseSensorCalibrationDump(bytes: [UInt8]){ |
| 92 | + var sensorID = Int(bytes[0]) + (Int(bytes[1])<<8) |
| 93 | + if bytes[0] == CALIBRATION_ID { |
| 94 | + var range = bytes[2] |
| 95 | + var calbytes = bytes |
| 96 | + calbytes.removeFirst(12) |
| 97 | + if(HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3.rawValue){ |
| 98 | + (AlignmentMatrix,SensitivityMatrix,OffsetVector) = parseIMUCalibrationParameters(bytes: calbytes) |
| 99 | + }else if(HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3R.rawValue){ |
| 100 | + if range==0{ |
| 101 | + calibBytes_4Ga = calbytes |
| 102 | + (AlignmentMatrix_4Ga,SensitivityMatrix_4Ga,OffsetVector_4Ga) = parseIMUCalibrationParameters(bytes: calbytes) |
| 103 | + } |
| 104 | + if range==1{ |
| 105 | + calibBytes_8Ga = calbytes |
| 106 | + (AlignmentMatrix_8Ga,SensitivityMatrix_8Ga,OffsetVector_8Ga) = parseIMUCalibrationParameters(bytes: calbytes) |
| 107 | + } |
| 108 | + if range==2{ |
| 109 | + calibBytes_12Ga = calbytes |
| 110 | + (AlignmentMatrix_12Ga,SensitivityMatrix_12Ga,OffsetVector_12Ga) = parseIMUCalibrationParameters(bytes: calbytes) |
| 111 | + } |
| 112 | + if range==3{ |
| 113 | + calibBytes_16Ga = calbytes |
| 114 | + (AlignmentMatrix_16Ga,SensitivityMatrix_16Ga,OffsetVector_16Ga) = parseIMUCalibrationParameters(bytes: calbytes) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public func setInfoMem(infomem: [UInt8]) { |
| 121 | + |
| 122 | + var enabled = Int(infomem[ConfigByteLayoutShimmer3.idxSensors2]>>ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagRange) & 1 |
| 123 | + if (enabled == 1){ |
| 124 | + sensorEnabled = true |
| 125 | + } else { |
| 126 | + sensorEnabled = false |
| 127 | + } |
| 128 | + altMagRange = (Int(infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte2] >> ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagRange) & ConfigByteLayoutShimmer3.maskLSM303DLHCMagRange) |
| 129 | + |
| 130 | + if(HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3R.rawValue){ |
| 131 | + if (altMagRange == 0){ |
| 132 | + Current3RRange = Range3R.RANGE_4Ga |
| 133 | + AlignmentMatrix = AlignmentMatrix_4Ga |
| 134 | + SensitivityMatrix = SensitivityMatrix_4Ga |
| 135 | + OffsetVector = OffsetVector_4Ga |
| 136 | + } |
| 137 | + if (altMagRange == 1){ |
| 138 | + Current3RRange = Range3R.RANGE_8Ga |
| 139 | + AlignmentMatrix = AlignmentMatrix_8Ga |
| 140 | + SensitivityMatrix = SensitivityMatrix_8Ga |
| 141 | + OffsetVector = OffsetVector_8Ga |
| 142 | + } |
| 143 | + if (altMagRange == 2){ |
| 144 | + Current3RRange = Range3R.RANGE_12Ga |
| 145 | + AlignmentMatrix = AlignmentMatrix_12Ga |
| 146 | + SensitivityMatrix = SensitivityMatrix_12Ga |
| 147 | + OffsetVector = OffsetVector_12Ga |
| 148 | + } |
| 149 | + if (altMagRange == 3){ |
| 150 | + Current3RRange = Range3R.RANGE_16Ga |
| 151 | + AlignmentMatrix = AlignmentMatrix_16Ga |
| 152 | + SensitivityMatrix = SensitivityMatrix_16Ga |
| 153 | + OffsetVector = OffsetVector_16Ga |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public func setLowPowerAltMag(enable: Bool, isShimmer3withUpdatedSensors: Bool, isShimmer3Sensor: Bool, samplingRate: Double, infomem: [UInt8])-> [UInt8]{ |
| 159 | + let LowPowerMagEnabled = enable |
| 160 | + var infomemtoupdate = infomem |
| 161 | + if(HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3R.rawValue){ |
| 162 | + if(!LowPowerMagEnabled){ |
| 163 | + if(samplingRate > 560){ |
| 164 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x01) |
| 165 | + }else if(samplingRate > 300){ |
| 166 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x11) |
| 167 | + }else if(samplingRate > 155){ |
| 168 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x21) |
| 169 | + }else if(samplingRate > 100){ |
| 170 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x31) |
| 171 | + }else if(samplingRate > 50){ |
| 172 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x31) |
| 173 | + }else if(samplingRate > 20){ |
| 174 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x3E) |
| 175 | + }else if(samplingRate > 10){ |
| 176 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x3A) |
| 177 | + }else{ |
| 178 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x08) |
| 179 | + } |
| 180 | + }else{ |
| 181 | + infomemtoupdate = updateInfoMemAltMagRate(infomem: infomem, magRate:0x08) |
| 182 | + } |
| 183 | + } |
| 184 | + return infomemtoupdate |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + public func updateInfoMem3RAltMagRange(infomem: [UInt8],range: Range3R) -> [UInt8]{ |
| 189 | + var infomemtoupdate = infomem |
| 190 | + print("oriinfomem: \(infomemtoupdate)") |
| 191 | + var altMagRange = 0 |
| 192 | + var calibBytes = calibBytes_4Ga |
| 193 | + if (range == Range3R.RANGE_4Ga){ |
| 194 | + altMagRange = 0 |
| 195 | + calibBytes = calibBytes_4Ga |
| 196 | + }else if (range == Range3R.RANGE_8Ga){ |
| 197 | + altMagRange = 1 |
| 198 | + calibBytes = calibBytes_8Ga |
| 199 | + } else if (range == Range3R.RANGE_12Ga){ |
| 200 | + altMagRange = 2 |
| 201 | + calibBytes = calibBytes_12Ga |
| 202 | + } else if (range == Range3R.RANGE_16Ga){ |
| 203 | + altMagRange = 3 |
| 204 | + calibBytes = calibBytes_16Ga |
| 205 | + } |
| 206 | + |
| 207 | + infomemtoupdate.replaceSubrange( |
| 208 | + ConfigByteLayoutShimmer3.idxLIS3MDLAltMagCalibration..<ConfigByteLayoutShimmer3.idxLIS3MDLAltMagCalibration + ConfigByteLayoutShimmer3.lengthGeneralCalibrationBytes, |
| 209 | + with: calibBytes[0..<ConfigByteLayoutShimmer3.lengthGeneralCalibrationBytes]) |
| 210 | + |
| 211 | + let orivalue = infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] |
| 212 | + let value = infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] & ~UInt8(ConfigByteLayoutShimmer3.maskLSM303DLHCMagRange<<ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagRange) |
| 213 | + let range = UInt8(altMagRange<<ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagRange) |
| 214 | + |
| 215 | + print("orivalue range: \(orivalue)") |
| 216 | + print("value: \(value)") |
| 217 | + print("range: \(range)") |
| 218 | + |
| 219 | + infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] = value | range |
| 220 | + print("updatedinfomem: \(infomemtoupdate)") |
| 221 | + |
| 222 | + return infomemtoupdate |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | + public func updateInfoMemAltMagRate(infomem: [UInt8],magRate: UInt8) -> [UInt8]{ |
| 227 | + var infomemtoupdate = infomem |
| 228 | + |
| 229 | + print("oriinfomem : \(infomemtoupdate)") |
| 230 | + |
| 231 | + let orivalue = infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] |
| 232 | + let value = infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] & ~UInt8(ConfigByteLayoutShimmer3.maskLSM303DLHCMagSamplingRate<<ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagSamplingRate) |
| 233 | + let range = UInt8(magRate<<ConfigByteLayoutShimmer3.bitShiftLSM303DLHCMagSamplingRate) |
| 234 | + |
| 235 | + print("orivalue range : \(orivalue)") |
| 236 | + print("value : \(value)") |
| 237 | + print("range : \(range)") |
| 238 | + |
| 239 | + infomemtoupdate[ConfigByteLayoutShimmer3.idxConfigSetupByte2] = value | range |
| 240 | + |
| 241 | + return infomemtoupdate |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | + |
0 commit comments