diff --git a/ShimmerAPI/ShimmerAPI/Sensors/AbstractSensor.cs b/ShimmerAPI/ShimmerAPI/Sensors/AbstractSensor.cs new file mode 100644 index 0000000..aaca61c --- /dev/null +++ b/ShimmerAPI/ShimmerAPI/Sensors/AbstractSensor.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShimmerAPI.Sensors +{ + public interface AbstractSensor + { + int SENSOR_ID { get; } + int ShimmerHardwareVersion { get; } + Dictionary> CalibDetails { get; } + + } +} diff --git a/ShimmerAPI/ShimmerAPI/Sensors/GyroSensor.cs b/ShimmerAPI/ShimmerAPI/Sensors/GyroSensor.cs index 767a315..dabd3b2 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/GyroSensor.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/GyroSensor.cs @@ -2,29 +2,148 @@ using System; using System.Collections.Generic; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - public class GyroSensor + public class GyroSensor : AbstractSensor { public readonly int CALIBRATION_ID = 2; - public double[,] AlignmentMatrixAccel = new double[3, 3]; - public double[,] SensitivityMatrixAccel = new double[3, 3]; - public double[,] OffsetVectorAccel = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + public readonly int SHIMMER_MPU9X50_GYRO = 30; + public readonly int SHIMMER_LSM6DSV_GYRO = 38; + public int SENSOR_ID { get; private set; } + public int ShimmerHardwareVersion { get; private set; } + public Dictionary> CalibDetails { get; private set; } + + public double[,] AlignmentMatrixGyro = new double[3, 3]; + public double[,] SensitivityMatrixGyro = new double[3, 3]; + public double[,] OffsetVectorGyro = new double[3, 1]; + + public GyroSensor(int hardwareVersion) { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + public void CreateDefaultCalibParams() + { + if (ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + SENSOR_ID = SHIMMER_LSM6DSV_GYRO; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_125DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_250DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_500DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_1000DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + }, + { + 4, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_2000DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + }, + { + 5, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV, + SENSITIVITIY_MATRIX_GYRO_4000DPS_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV + } + } + }; + } + else { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixAccel, SensitivityMatrixAccel, OffsetVectorAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("Gyro calibration parameters"); + SENSOR_ID = SHIMMER_MPU9X50_GYRO; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3, + SENSITIVITIY_MATRIX_GYRO_250DPS_SHIMMER3, + OFFSET_VECTOR_GYRO_SHIMMER3 + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3, + SENSITIVITIY_MATRIX_GYRO_500DPS_SHIMMER3, + OFFSET_VECTOR_GYRO_SHIMMER3 + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3, + SENSITIVITIY_MATRIX_GYRO_1000DPS_SHIMMER3, + OFFSET_VECTOR_GYRO_SHIMMER3 + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_GYRO_SHIMMER3, + SENSITIVITIY_MATRIX_GYRO_2000DPS_SHIMMER3, + OFFSET_VECTOR_GYRO_SHIMMER3 + } + } + }; } + if (CalibDetails.TryGetValue(0, out var defaultCalib)) + { + AlignmentMatrixGyro = defaultCalib[0]; + SensitivityMatrixGyro = defaultCalib[1]; + OffsetVectorGyro = defaultCalib[2]; + } + } + + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixGyro, SensitivityMatrixGyro, OffsetVectorGyro) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + System.Console.WriteLine("Gyro calibration parameters"); } } } diff --git a/ShimmerAPI/ShimmerAPI/Sensors/HighGAccel.cs b/ShimmerAPI/ShimmerAPI/Sensors/HighGAccel.cs index 0dcb856..99a8336 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/HighGAccel.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/HighGAccel.cs @@ -2,29 +2,67 @@ using System; using System.Collections.Generic; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - public class HighGAccel + public class HighGAccel : AbstractSensor { public readonly int CALIBRATION_ID = 2; - public double[,] AlignmentMatrixAccel = new double[3, 3]; - public double[,] SensitivityMatrixAccel = new double[3, 3]; - public double[,] OffsetVectorAccel = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + public readonly int ALT_ACCEL = 33; + public readonly int SHIMMER_ADXL371_ACCEL_HIGHG = 40; + public int SENSOR_ID { get; private set; } + public int ShimmerHardwareVersion { get; private set; } + public Dictionary> CalibDetails { get; private set; } + public double[,] AlignmentMatrixAltAccel = new double[3, 3]; + public double[,] SensitivityMatrixAltAccel = new double[3, 3]; + public double[,] OffsetVectorAltAccel = new double[3, 1]; + + public HighGAccel(int hardwareVersion) { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + public void CreateDefaultCalibParams() + { + if (ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixAccel, SensitivityMatrixAccel, OffsetVectorAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("High-G Accel calibration parameters"); + SENSOR_ID = SHIMMER_ADXL371_ACCEL_HIGHG; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_HIGH_G_ACCEL_SHIMMER3R_ADXL371, + SENSITIVITY_MATRIX_HIGH_G_ACCEL_200G_SHIMMER3R_ADXL371, + OFFSET_VECTOR_ACCEL_HIGH_G_SHIMMER3R_ADXL371 + } + } + }; + } + else + { + SENSOR_ID = ALT_ACCEL; } + if(CalibDetails != null) + { + if (CalibDetails.TryGetValue(0, out var defaultCalib)) + { + AlignmentMatrixAltAccel = defaultCalib[0]; + SensitivityMatrixAltAccel = defaultCalib[1]; + OffsetVectorAltAccel = defaultCalib[2]; + } + } + + } + + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixAltAccel, SensitivityMatrixAltAccel, OffsetVectorAltAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + System.Console.WriteLine("High-G Accel calibration parameters"); } } } diff --git a/ShimmerAPI/ShimmerAPI/Sensors/LNAccel.cs b/ShimmerAPI/ShimmerAPI/Sensors/LNAccel.cs index 5257d47..6c3a62e 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/LNAccel.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/LNAccel.cs @@ -1,31 +1,105 @@ using ShimmerAPI.Utilities; using System; using System.Collections.Generic; +using System.Linq; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - public class LNAccel + public class LNAccel : AbstractSensor { public readonly int CALIBRATION_ID = 2; + public readonly int SHIMMER_ANALOG_ACCEL = 2; + public readonly int SHIMMER_LSM6DSV_ACCEL_LN = 37; + public int SENSOR_ID { get; private set; } + public int ShimmerHardwareVersion { get; private set; } + public Dictionary> CalibDetails { get; private set; } + public double[,] AlignmentMatrixAccel = new double[3, 3]; public double[,] SensitivityMatrixAccel = new double[3, 3]; public double[,] OffsetVectorAccel = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + + public LNAccel(int hardwareVersion) { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + private void CreateDefaultCalibParams() + { + if(ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + SENSOR_ID = SHIMMER_LSM6DSV_ACCEL_LN; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_LOW_NOISE_ACCEL_SHIMMER3R_LSM6DSV, + SENSITIVITY_MATRIX_LOW_NOISE_ACCEL_2G_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_ACCEL_LOW_NOISE_SHIMMER3R_LSM6DSV + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_LOW_NOISE_ACCEL_SHIMMER3R_LSM6DSV, + SENSITIVITY_MATRIX_LOW_NOISE_ACCEL_4G_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_ACCEL_LOW_NOISE_SHIMMER3R_LSM6DSV + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_LOW_NOISE_ACCEL_SHIMMER3R_LSM6DSV, + SENSITIVITY_MATRIX_LOW_NOISE_ACCEL_8G_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_ACCEL_LOW_NOISE_SHIMMER3R_LSM6DSV + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_LOW_NOISE_ACCEL_SHIMMER3R_LSM6DSV, + SENSITIVITY_MATRIX_LOW_NOISE_ACCEL_16G_SHIMMER3R_LSM6DSV, + OFFSET_VECTOR_ACCEL_LOW_NOISE_SHIMMER3R_LSM6DSV + } + } + }; + } + else { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixAccel, SensitivityMatrixAccel, OffsetVectorAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("LN Accel calibration parameters"); + SENSOR_ID = SHIMMER_ANALOG_ACCEL; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_LOW_NOISE_ACCEL_SHIMMER3_KXTC9_2050, + SENSITIVITY_MATRIX_LOW_NOISE_ACCEL_SHIMMER3_KXTC9_2050, + OFFSET_VECTOR_ACCEL_LOW_NOISE_SHIMMER3_KXTC9_2050 + } + } + }; } + if (CalibDetails.TryGetValue(0, out var defaultCalib)) + { + AlignmentMatrixAccel = defaultCalib[0]; + SensitivityMatrixAccel = defaultCalib[1]; + OffsetVectorAccel = defaultCalib[2]; + } } + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixAccel, SensitivityMatrixAccel, OffsetVectorAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + Console.WriteLine("LN Accel calibration parameters retrieved successfully."); + } } } diff --git a/ShimmerAPI/ShimmerAPI/Sensors/MagSensor.cs b/ShimmerAPI/ShimmerAPI/Sensors/MagSensor.cs index f3f7462..36318b9 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/MagSensor.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/MagSensor.cs @@ -2,29 +2,103 @@ using System; using System.Collections.Generic; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - class MagSensor + public class MagSensor : AbstractSensor { public readonly int CALIBRATION_ID = 2; + public readonly int SHIMMER_LSM303_MAG = 32; + public readonly int SHIMMER_LIS2MDL_MAG = 42; + public int SENSOR_ID { get; private set; } + public int ShimmerHardwareVersion { get; private set; } + public Dictionary> CalibDetails { get; private set; } public double[,] AlignmentMatrixMag = new double[3, 3]; public double[,] SensitivityMatrixMag = new double[3, 3]; public double[,] OffsetVectorMag = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + + public MagSensor(int hardwareVersion) { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + public void CreateDefaultCalibParams() + { + if (ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixMag, SensitivityMatrixMag, OffsetVectorMag) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("Mag calibration parameters"); + SENSOR_ID = SHIMMER_LIS2MDL_MAG; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3R_LIS3MDL, + SENSITIVITY_MATRIX_MAG_4GA_SHIMMER3R_LIS3MDL, + OFFSET_VECTOR_MAG_SHIMMER3R_LIS3MDL + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3R_LIS3MDL, + SENSITIVITY_MATRIX_MAG_8GA_SHIMMER3R_LIS3MDL, + OFFSET_VECTOR_MAG_SHIMMER3R_LIS3MDL + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3R_LIS3MDL, + SENSITIVITY_MATRIX_MAG_12GA_SHIMMER3R_LIS3MDL, + OFFSET_VECTOR_MAG_SHIMMER3R_LIS3MDL + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3R_LIS3MDL, + SENSITIVITY_MATRIX_MAG_16GA_SHIMMER3R_LIS3MDL, + OFFSET_VECTOR_MAG_SHIMMER3R_LIS3MDL + } + }, + }; + } + else + { + SENSOR_ID = SHIMMER_LSM303_MAG; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3_LSM303AH, + SENSITIVITY_MATRIX_MAG_50GA_SHIMMER3_LSM303AH, + OFFSET_VECTOR_MAG_SHIMMER3_LSM303AH + } + } + }; } + if (CalibDetails.TryGetValue(0, out var defaultCalib)) + { + AlignmentMatrixMag = defaultCalib[0]; + SensitivityMatrixMag = defaultCalib[1]; + OffsetVectorMag = defaultCalib[2]; + } + + } + + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixMag, SensitivityMatrixMag, OffsetVectorMag) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + System.Console.WriteLine("Mag calibration parameters"); } } } diff --git a/ShimmerAPI/ShimmerAPI/Sensors/WRAccel.cs b/ShimmerAPI/ShimmerAPI/Sensors/WRAccel.cs index 4b26deb..d41b04d 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/WRAccel.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/WRAccel.cs @@ -2,29 +2,129 @@ using System; using System.Collections.Generic; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - public class WRAccel + public class WRAccel : AbstractSensor { public readonly int CALIBRATION_ID = 2; - public double[,] AlignmentMatrixAccel = new double[3, 3]; - public double[,] SensitivityMatrixAccel = new double[3, 3]; - public double[,] OffsetVectorAccel = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + public readonly int SHIMMER_LSM303_ACCEL = 31; + public readonly int SHIMMER_LIS2DW12_ACCEL_WR = 39; + public int ShimmerHardwareVersion { get; private set; } + public int SENSOR_ID { get; private set; } + public Dictionary> CalibDetails { get; private set; } + public double[,] AlignmentMatrixAccel2 = new double[3, 3]; + public double[,] SensitivityMatrixAccel2 = new double[3, 3]; + public double[,] OffsetVectorAccel2 = new double[3, 1]; + + public WRAccel(int hardwareVersion) + { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } + + public void CreateDefaultCalibParams() { + if (ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + SENSOR_ID = SHIMMER_LIS2DW12_ACCEL_WR; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3R_LIS2DW12, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_2G_SHIMMER3R_LIS2DW12, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3R_LIS2DW12 + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3R_LIS2DW12, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_4G_SHIMMER3R_LIS2DW12, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3R_LIS2DW12 + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3R_LIS2DW12, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_8G_SHIMMER3R_LIS2DW12, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3R_LIS2DW12 + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3R_LIS2DW12, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_16G_SHIMMER3R_LIS2DW12, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3R_LIS2DW12 + } + } + }; + } + else + { + SENSOR_ID = SHIMMER_LSM303_ACCEL; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3_LSM303AH, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_2G_SHIMMER3_LSM303AH, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3_LSM303AH + } + }, + { + 2, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3_LSM303AH, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_4G_SHIMMER3_LSM303AH, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3_LSM303AH + } + }, + { + 3, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3_LSM303AH, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_8G_SHIMMER3_LSM303AH, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3_LSM303AH + } + }, + { + 1, + new List + { + ALIGNMENT_MATRIX_WIDE_RANGE_ACCEL_SHIMMER3_LSM303AH, + SENSITIVITY_MATRIX_WIDE_RANGE_ACCEL_16G_SHIMMER3_LSM303AH, + OFFSET_VECTOR_ACCEL_WIDE_RANGE_SHIMMER3_LSM303AH + } + } + }; + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + if (CalibDetails.TryGetValue(0, out var defaultCalib)) { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixAccel, SensitivityMatrixAccel, OffsetVectorAccel) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("WR Accel calibration parameters"); + AlignmentMatrixAccel2 = defaultCalib[0]; + SensitivityMatrixAccel2 = defaultCalib[1]; + OffsetVectorAccel2 = defaultCalib[2]; } + } + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixAccel2, SensitivityMatrixAccel2, OffsetVectorAccel2) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + System.Console.WriteLine("WR Accel calibration parameters"); } } } diff --git a/ShimmerAPI/ShimmerAPI/Sensors/WRMag.cs b/ShimmerAPI/ShimmerAPI/Sensors/WRMag.cs index d5d58d9..f916633 100644 --- a/ShimmerAPI/ShimmerAPI/Sensors/WRMag.cs +++ b/ShimmerAPI/ShimmerAPI/Sensors/WRMag.cs @@ -2,29 +2,67 @@ using System; using System.Collections.Generic; using System.Text; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerAPI.Sensors { - public class WRMag + public class WRMag : AbstractSensor { public readonly int CALIBRATION_ID = 2; - public double[,] AlignmentMatrixMag = new double[3, 3]; - public double[,] SensitivityMatrixMag = new double[3, 3]; - public double[,] OffsetVectorMag = new double[3, 1]; - public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + public readonly int ALT_MAG = 34; + public readonly int SHIMMER_LIS3MDL_MAG = 41; + public int ShimmerHardwareVersion { get; private set; } + public int SENSOR_ID { get; private set; } + public Dictionary> CalibDetails { get; private set; } + public double[,] AlignmentMatrixMag2 = new double[3, 3]; + public double[,] SensitivityMatrixMag2 = new double[3, 3]; + public double[,] OffsetVectorMag2 = new double[3, 1]; + + public WRMag(int hardwareVersion) { + ShimmerHardwareVersion = hardwareVersion; + CreateDefaultCalibParams(); + } - var packetType = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 2); - var sensorID = ((int)packetType[0]) + ((int)packetType[1] << 8); - if (sensorID == CALIBRATION_ID) + public void CreateDefaultCalibParams() + { + if (ShimmerHardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) { - var rangebytes = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var lengthsensorcal = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 1); - var ts = ProgrammerUtilities.CopyAndRemoveBytes(ref sensorcalibrationdump, 8); - (AlignmentMatrixMag, SensitivityMatrixMag, OffsetVectorMag) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); - System.Console.WriteLine("WR Mag calibration parameters"); + SENSOR_ID = SHIMMER_LIS3MDL_MAG; + CalibDetails = new Dictionary>() + { + { + 0, + new List + { + ALIGNMENT_MATRIX_MAG_SHIMMER3R_LIS2MDL, + SENSITIVITY_MATRIX_MAG_50GA_SHIMMER3R_LIS2MDL, + OFFSET_VECTOR_MAG_SHIMMER3R_LIS2MDL + } + } + }; + } + else + { + SENSOR_ID = ALT_MAG; } + if(CalibDetails != null) + { + if (CalibDetails.TryGetValue(0, out var defaultCalib)) + { + AlignmentMatrixMag2 = defaultCalib[0]; + SensitivityMatrixMag2 = defaultCalib[1]; + OffsetVectorMag2 = defaultCalib[2]; + } + } + + } + + public void RetrieveKinematicCalibrationParametersFromCalibrationDump(byte[] sensorcalibrationdump) + { + (AlignmentMatrixMag2, SensitivityMatrixMag2, OffsetVectorMag2) = UtilCalibration.RetrieveKinematicCalibrationParametersFromCalibrationDump(sensorcalibrationdump); + System.Console.WriteLine("WR Mag calibration parameters"); } } } diff --git a/ShimmerAPI/ShimmerAPI/Shimmer4LogAndStream.cs b/ShimmerAPI/ShimmerAPI/Shimmer4LogAndStream.cs index 4edea79..292f8be 100644 --- a/ShimmerAPI/ShimmerAPI/Shimmer4LogAndStream.cs +++ b/ShimmerAPI/ShimmerAPI/Shimmer4LogAndStream.cs @@ -9,7 +9,7 @@ namespace ShimmerAPI [Obsolete("This class is unfinished and should not be used.")] public abstract class Shimmer4LogAndStream : ShimmerLogAndStream { - LNAccel LowNoiseAccel = new LNAccel(); + LNAccel LowNoiseAccel = new LNAccel((int)ShimmerBluetooth.ShimmerVersion.SHIMMER4SDK); public Shimmer4LogAndStream(String devID) : base(devID) { diff --git a/ShimmerAPI/ShimmerAPI/ShimmerBluetooth.cs b/ShimmerAPI/ShimmerAPI/ShimmerBluetooth.cs index f4ce4c1..b3e6ef3 100644 --- a/ShimmerAPI/ShimmerAPI/ShimmerBluetooth.cs +++ b/ShimmerAPI/ShimmerAPI/ShimmerBluetooth.cs @@ -75,6 +75,7 @@ using static com.shimmerresearch.radioprotocol.LiteProtocolInstructionSet.Types; using ShimmerAPI.Utilities; using static ShimmerAPI.ShimmerBluetooth; +using ShimmerAPI.Sensors; namespace ShimmerAPI { @@ -118,7 +119,7 @@ public abstract class ShimmerBluetooth : ShimmerDevice protected double FirmwareIdentifier; protected int FirmwareInternal; protected String FirmwareVersionFullName; - protected List ListofSensorChannels = new List(); + public List ListofSensorChannels = new List(); protected Boolean Orientation3DEnabled = false; protected Boolean EnableGyroOnTheFlyCalibration = false; protected int ListSizeGyroOnTheFly = 100; @@ -170,8 +171,8 @@ public abstract class ShimmerBluetooth : ShimmerDevice public double[,] SensitivityMatrixAccel2 = new double[3, 3] { { 38, 0, 0 }, { 0, 38, 0 }, { 0, 0, 38 } }; public double[,] OffsetVectorAccel2 = new double[3, 1] { { 2048 }, { 2048 }, { 2048 } }; public double[,] AlignmentMatrixAltAccel = new double[3, 3] { { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0, -1 } }; - public double[,] SensitivityMatrixAltAccel = new double[3, 3] { { 16, 0, 0 }, { 0, 16, 0 }, { 0, 0, 16 } }; - public double[,] OffsetVectorAltAccel = new double[3, 1] { { 0 }, { 0 }, { 0 } }; + public double[,] SensitivityMatrixAltAccel = new double[3, 3] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; + public double[,] OffsetVectorAltAccel = new double[3, 1] { { 10 }, { 10 }, { 10 } }; //Default Values for Magnetometer Calibration @@ -258,6 +259,15 @@ public abstract class ShimmerBluetooth : ShimmerDevice protected long ShimmerRealWorldClock = 0; protected List UnalignedBytesReceived = new List(); + protected List mMapOfSensorClasses = new List(); + + protected LNAccel SensorLNAccel; + protected WRAccel SensorWRAccel; + protected HighGAccel SensorHighGAccel; + protected GyroSensor SensorGyro; + protected MagSensor SensorMag; + protected WRMag SensorWRMag; + public enum BTCRCMode { OFF = 0, @@ -704,7 +714,7 @@ public enum ExpansionBoardDetectShimmer3 public static readonly double[,] SENSITIVITIY_MATRIX_GYRO_500DPS_SHIMMER3R_LSM6DSV = new double[3, 3] { { 57, 0, 0 }, { 0, 57, 0 }, { 0, 0, 57 } }; //Default Values for Gyroscope Calibration public static readonly double[,] SENSITIVITIY_MATRIX_GYRO_1000DPS_SHIMMER3R_LSM6DSV = new double[3, 3] { { 29, 0, 0 }, { 0, 29, 0 }, { 0, 0, 29 } }; //Default Values for Gyroscope Calibration public static readonly double[,] SENSITIVITIY_MATRIX_GYRO_2000DPS_SHIMMER3R_LSM6DSV = new double[3, 3] { { 14, 0, 0 }, { 0, 14, 0 }, { 0, 0, 14 } }; //Default Values for Gyroscope Calibration - public static readonly double[,] SENSITIVITIY_MATRIX_GYRO_40000DPS_SHIMMER3R_LSM6DSV = new double[3, 3] { { 7, 0, 0 }, { 0, 7, 0 }, { 0, 0, 7 } }; //Default Values for Gyroscope Calibration + public static readonly double[,] SENSITIVITIY_MATRIX_GYRO_4000DPS_SHIMMER3R_LSM6DSV = new double[3, 3] { { 7, 0, 0 }, { 0, 7, 0 }, { 0, 0, 7 } }; //Default Values for Gyroscope Calibration public static readonly double[,] OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV = new double[3, 1] { { 0 }, { 0 }, { 0 } }; //Default Values for Gyroscope Calibration // Shimmer3r LN Accel @@ -917,6 +927,22 @@ public void StartConnectThread() protected int ConnectWaitDurationinmS = 500; + protected void InitializeSensors() + { + SensorLNAccel = new LNAccel(HardwareVersion); + mMapOfSensorClasses.Add(SensorLNAccel); + SensorGyro = new GyroSensor(HardwareVersion); + mMapOfSensorClasses.Add(SensorGyro); + SensorHighGAccel = new HighGAccel(HardwareVersion); + mMapOfSensorClasses.Add(SensorHighGAccel); + SensorWRAccel = new WRAccel(HardwareVersion); + mMapOfSensorClasses.Add(SensorWRAccel); + SensorMag = new MagSensor(HardwareVersion); + mMapOfSensorClasses.Add(SensorMag); + SensorWRMag = new WRMag(HardwareVersion); + mMapOfSensorClasses.Add(SensorWRMag); + } + public void Connect() { if (!IsConnectionOpen()) @@ -970,6 +996,7 @@ public void Connect() if (GetFirmwareIdentifier() == FW_IDENTIFIER_LOGANDSTREAM) { //WriteBatteryFrequency(0); + InitializeSensors(); ReadExpansionBoard(); InitializeShimmer3SDBT(); } @@ -994,6 +1021,7 @@ public void Connect() if (GetFirmwareIdentifier() == FW_IDENTIFIER_LOGANDSTREAM) { //WriteBatteryFrequency(0); + InitializeSensors(); ReadExpansionBoard(); InitializeShimmer3SDBT(); } @@ -2643,7 +2671,7 @@ protected void RetrieveKinematicCalibrationParametersFromPacket(byte[] bufferCal } else if (GyroRange == 5) { - SensitivityMatrixGyro = SENSITIVITIY_MATRIX_GYRO_40000DPS_SHIMMER3R_LSM6DSV; + SensitivityMatrixGyro = SENSITIVITIY_MATRIX_GYRO_4000DPS_SHIMMER3R_LSM6DSV; } AlignmentMatrixGyro = ALIGNMENT_MATRIX_GYRO_SHIMMER3R_LSM6DSV; OffsetVectorGyro = OFFSET_VECTOR_GYRO_SHIMMER3R_LSM6DSV; @@ -7220,7 +7248,7 @@ protected double[] CalibratePressure280SensorData(double UP, double UT) caldata[1] = T;///10; // TODO divided by 10 in BMP180, needed here? return caldata; } - protected double[] CalibratePressure390SensorData(double UP, double UT) + public double[] CalibratePressure390SensorData(double UP, double UT) { double uncompTemp = UT; double partialDataT1; diff --git a/ShimmerAPI/ShimmerAPI/ShimmerDevice.cs b/ShimmerAPI/ShimmerAPI/ShimmerDevice.cs index 4f7519f..47570b6 100644 --- a/ShimmerAPI/ShimmerAPI/ShimmerDevice.cs +++ b/ShimmerAPI/ShimmerAPI/ShimmerDevice.cs @@ -28,7 +28,8 @@ public enum ShimmerVersion SHIMMER2 = 1, SHIMMER2R = 2, SHIMMER3 = 3, - SHIMMER3R = 10 + SHIMMER3R = 10, + SHIMMER4SDK = 58 } protected double CalibrateTimeStamp(double timeStamp) { diff --git a/ShimmerAPI/ShimmerAPI/ShimmerLogAndStream.cs b/ShimmerAPI/ShimmerAPI/ShimmerLogAndStream.cs index f6e80a3..2276efa 100644 --- a/ShimmerAPI/ShimmerAPI/ShimmerLogAndStream.cs +++ b/ShimmerAPI/ShimmerAPI/ShimmerLogAndStream.cs @@ -8,6 +8,10 @@ using System.ComponentModel; using System.Diagnostics; using ShimmerAPI.Utilities; +using ShimmerAPI.Sensors; +using NUnit.Framework.Internal; +using System.Collections; +using static ShimmerAPI.ShimmerLogAndStream; namespace ShimmerAPI { @@ -187,6 +191,9 @@ public enum ChannelContents STRAIN_LOW = 0x28, GsrRaw = 0x1C } + + public Dictionary>> mapOfSensorCalibration = new Dictionary>>(); + // btsd changes private int trialConfig; private int interval; @@ -243,6 +250,8 @@ public enum ChannelContents private long configtime = 0; public byte[] storedConfig = new byte[118]; + private List calibDumpResponse = new List(); + // btsd changes private bool dataReceived; // btsd changes @@ -652,7 +661,7 @@ public override void StartStreaming() } // btsd changes - private void readInfoMem() + protected virtual void readInfoMem() { //string status_text = ""; //PChangeStatusLabel("Inquiring Shimmer Info"); // need to be in a UI thread for update @@ -854,6 +863,8 @@ public override void WriteShimmerName() int InfoMemIndex = 0; byte[] InfoMem = new byte[128*3]; byte[] CalibDump = new byte[128]; + private Dictionary mCalibBytesDescriptions; + private byte[] mCalibBytes; [Obsolete("This method is unfinished and should not be used.")] public void WriteShimmer3Infomem(byte[] infoMem) @@ -931,8 +942,6 @@ public void ReadShimmer3InfoMem() [Obsolete("This method is unfinished and should not be used.")] public void ReadCalibDump() { - - List calibDumpResponse = new List(); byte[] byteResult = SendGetMemoryCommand((byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_CALIB_DUMP_COMMAND, 0x80, 0x00, 0x00); @@ -964,6 +973,10 @@ public void ReadCalibDump() } } + CalibByteDumpParse(calibDumpResponse.ToArray()); + + Console.WriteLine(calibDumpResponse); + /* WriteBytes(new byte[4] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_CALIB_DUMP_COMMAND, 0x80, 0x00, 0x00 }, 0, 4); @@ -993,6 +1006,231 @@ public void ReadCalibDump() */ } + public object GetSensorDetails(int sensorId) + { + if(mMapOfSensorClasses != null) + { + foreach (AbstractSensor sensorClass in mMapOfSensorClasses) + { + if(sensorClass.SENSOR_ID == sensorId) + { + return sensorClass; + } + } + } + return null; + } + + public object GetSensorCalibDetails(int sensorId) + { + if (mMapOfSensorClasses != null) + { + foreach (AbstractSensor sensorClass in mMapOfSensorClasses) + { + if (sensorClass.SENSOR_ID == sensorId) + { + return sensorClass; + } + } + } + return null; + } + + public Dictionary>> GetMapOfSensorCalibrationAll() + { + + foreach (AbstractSensor sensorClass in mMapOfSensorClasses) + { + Dictionary> returnVal = sensorClass.CalibDetails; + if(returnVal != null) + { + mapOfSensorCalibration.Add(sensorClass.SENSOR_ID, returnVal); + } + + } + + return mapOfSensorCalibration; + } + + protected byte[] GenerateVersionByteArrayNew() + { + byte[] byteArray = new byte[8]; + + int index = 0; + byteArray[index++] = (byte)(HardwareVersion & 0xFF); + byteArray[index++] = (byte)((HardwareVersion >> 8) & 0xFF); + + byteArray[index++] = (byte)((int)FirmwareIdentifier & 0xFF); + byteArray[index++] = (byte)(((int)FirmwareIdentifier >> 8) & 0xFF); + + byteArray[index++] = (byte)(FirmwareMajor & 0xFF); + byteArray[index++] = (byte)((FirmwareMajor >> 8) & 0xFF); + + byteArray[index++] = (byte)(FirmwareMinor & 0xFF); + byteArray[index++] = (byte)(FirmwareMinor & 0xFF); + + return byteArray; + } + + public void CalibByteDumpParse(byte[] calibBytesAll) + { + mCalibBytesDescriptions = new Dictionary(); + mCalibBytes = calibBytesAll; + + if (calibBytesAll.Length > 2) + { + mCalibBytesDescriptions[0] = "PacketLength_LSB"; + mCalibBytesDescriptions[1] = "PacketLength_MSB"; + + var packetLength = calibBytesAll.Take(2).ToArray(); + var svoBytes = calibBytesAll.Skip(2).Take(8).ToArray(); + + //no need this functionality for the time being, as the info parsed should only be relevent to this method and not to any class variables + var(hwVersion, fwIden, fwMajor, fwMinor, fwInternal) = parseVersionByteArray(svoBytes); + + mCalibBytesDescriptions[2] = $"HwID_LSB (" + hwVersion + ")"; + mCalibBytesDescriptions[3] = "HwID_MSB"; + mCalibBytesDescriptions[4] = $"FwID_LSB (" + fwIden + ")"; + mCalibBytesDescriptions[5] = "FwID_MSB"; + mCalibBytesDescriptions[6] = "FWVerMjr_LSB"; + mCalibBytesDescriptions[7] = "FWVerMjr_MSB"; + mCalibBytesDescriptions[8] = "FWVerMinor"; + mCalibBytesDescriptions[9] = "FWVerInternal"; + + int currentOffset = 10; + var remainingBytes = calibBytesAll.Skip(10).ToArray(); + + while (remainingBytes.Length > 12) + { + var sensorIdBytes = remainingBytes.Take(2).ToArray(); + int sensorId = ((sensorIdBytes[1] << 8) | sensorIdBytes[0]) & 0xFFFF; + + //var sensorDetails = GetSensorDetails(sensorId); + //var sensorName = sensorDetails?.mSensorDetailsRef?.mGuiFriendlyLabel ?? ""; + + mCalibBytesDescriptions[currentOffset] = $"SensorID_LSB ({sensorId})"; + mCalibBytesDescriptions[currentOffset + 1] = "SensorID_MSB"; + + mCalibBytesDescriptions[currentOffset + 2] = "SensorRange"; + int rangeValue = remainingBytes[2] & 0xFF; + + mCalibBytesDescriptions[currentOffset + 3] = "CalibByteLength"; + int calibLength = remainingBytes[3] & 0xFF; + + mCalibBytesDescriptions[currentOffset + 4] = "CalibTimeTicks_LSB"; + mCalibBytesDescriptions[currentOffset + 11] = "CalibTimeTicks_MSB"; + + var calibTimeBytesTicks = remainingBytes.Skip(4).Take(8).ToArray(); + + int endIndex = 12 + calibLength; + if (remainingBytes.Length >= endIndex) + { + mCalibBytesDescriptions[currentOffset + 12] = "Calib_Start"; + mCalibBytesDescriptions[currentOffset + endIndex - 1] = "Calib_End"; + + var calibBytes = remainingBytes.Skip(12).Take(calibLength).ToArray(); + + if (sensorId == SensorLNAccel.SENSOR_ID && rangeValue == LNAccelRange) + { + SensorLNAccel.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + SensitivityMatrixAccel = SensorLNAccel.SensitivityMatrixAccel; + AlignmentMatrixAccel = SensorLNAccel.AlignmentMatrixAccel; + OffsetVectorAccel = SensorLNAccel.OffsetVectorAccel; + } + else if (sensorId == SensorGyro.SENSOR_ID && rangeValue == GyroRange) + { + SensorGyro.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + SensitivityMatrixGyro = SensorGyro.SensitivityMatrixGyro; + AlignmentMatrixGyro = SensorGyro.AlignmentMatrixGyro; + OffsetVectorGyro = SensorGyro.OffsetVectorGyro; + } + else if (sensorId == SensorWRAccel.SENSOR_ID && rangeValue == AccelRange) + { + SensorWRAccel.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + SensitivityMatrixAccel2 = SensorWRAccel.SensitivityMatrixAccel2; + AlignmentMatrixAccel2 = SensorWRAccel.AlignmentMatrixAccel2; + OffsetVectorAccel2 = SensorWRAccel.OffsetVectorAccel2; + } + else if (sensorId == SensorMag.SENSOR_ID && rangeValue == MagGain) + { + SensorMag.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + SensitivityMatrixMag = SensorMag.SensitivityMatrixMag; + AlignmentMatrixMag = SensorMag.AlignmentMatrixMag; + OffsetVectorMag = SensorMag.OffsetVectorMag; + } + else if (sensorId == SensorHighGAccel.SENSOR_ID) + { + SensorHighGAccel.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + SensitivityMatrixAltAccel = SensorHighGAccel.SensitivityMatrixAltAccel; + AlignmentMatrixAltAccel = SensorHighGAccel.AlignmentMatrixAltAccel; + OffsetVectorAltAccel = SensorHighGAccel.OffsetVectorAltAccel; + } + else if (sensorId == SensorWRMag.SENSOR_ID) + { + SensorWRMag.RetrieveKinematicCalibrationParametersFromCalibrationDump(calibBytes); + + AlignmentMatrixMag2 = SensorWRMag.AlignmentMatrixMag2; + OffsetVectorMag2 = SensorWRMag.OffsetVectorMag2; + SensitivityMatrixMag2 = SensorWRMag.SensitivityMatrixMag2; + } + + + remainingBytes = remainingBytes.Skip(endIndex).ToArray(); + currentOffset += endIndex; + } + else + { + break; + } + } + } + } + + + public (int,int,int,int,int) parseVersionByteArray(byte[] byteArray) + { + int hardwareVersion; + int firmwareIdentifier; + int firmwareMajor; + int firmwareMinor; + int firmwareInternal; + + if ((byteArray.Length == 7) || (byteArray.Length == 8)) + { + + int index = 0; + if (byteArray.Length == 7) + { + hardwareVersion = byteArray[index++] & 0xFF; + } + else // == 8 + { + hardwareVersion = (byteArray[index++] | (byteArray[index++] << 8)) & 0xFFFF; + } + + firmwareIdentifier = ((byteArray[index++]) | (byteArray[index++] << 8)) & 0xFFFF; + firmwareMajor = ((byteArray[index++]) | (byteArray[index++] << 8)) & 0xFFFF; + firmwareMinor = byteArray[index++] & 0xFF; + firmwareInternal = byteArray[index++] & 0xFF; + return (hardwareVersion, firmwareIdentifier, firmwareMajor, firmwareMinor, firmwareInternal); + } + else + { + throw new Exception("Not enough bytes"); + } + } + + + public List GetCalibrationDump() + { + return calibDumpResponse; + } + protected byte[] SendGetMemoryCommand(byte cmd, byte val0, byte val1, byte val2) { @@ -1103,7 +1341,7 @@ protected byte[] ConvertSystemTimeToShimmerRwcDataBytes() // btsd changes - private void waitTilTimeOut() + protected void waitTilTimeOut() { int timeout = 150; while (timeout > 0) @@ -1986,7 +2224,10 @@ public override void SDBT_switch(byte b) buffer.Add((byte)ReadByte()); } tempCalibDump = buffer.ToArray(); + Debug.WriteLine("CALIB_DUMP Received:\t" + UtilShimmer.BytesToHexString(tempCalibDump)); tempCalibDump = ProgrammerUtilities.RemoveLastBytes(tempCalibDump, (int)BluetoothCRCMode); + Debug.WriteLine("CALIB_DUMP concat:\t" + UtilShimmer.BytesToHexString(tempCalibDump)); + break; } case (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.TRIAL_CONFIG_RESPONSE: diff --git a/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3RSimulator.cs b/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3RSimulator.cs index a19d94b..4cb7e8d 100644 --- a/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3RSimulator.cs +++ b/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3RSimulator.cs @@ -29,6 +29,35 @@ protected override void TxFirmwareVersion() mBuffer.Add((byte)0x00); mBuffer.Add((byte)0x01); } + + public byte[] GetPressureResoTest() + { + byte[] pressureResoResTest = + { + (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 + }; + return pressureResoResTest; + } + + public byte[] GetTestDataPacket() + { + byte[] newPacket = { + 188, 19, 112, 203, 7, 9, 8, 190, 4, 24, 251, 7, 2, 240, 186, + (byte)0x00, (byte)0xCF, (byte)0x7F, (byte)0x00, (byte)0x17, (byte)0x64, + 128, 186, 181, 80, 4, 169, 40, 128, 127, 255, 255, 253, 80, 71 + }; + return newPacket; + } + + public String[] GetTestDataType() + { + String[] dataType = { "u24", "i16", "i16", "i16", "i16", "i16", "i16", "u24", "u24", "u8", "i24r", "i24r", "u8", "i24r", "i24r", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; + return dataType; + } } } diff --git a/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3Simulator.cs b/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3Simulator.cs index a4a08ac..a221ab4 100644 --- a/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3Simulator.cs +++ b/ShimmerAPI/ShimmerAPI/Simulators/ShimmerLogAndStreamS3Simulator.cs @@ -15,6 +15,9 @@ public class ShimmerLogAndStreamS3Simulator : ShimmerLogAndStream public bool isConnectionOpen = false; protected BlockingCollection mBuffer = new BlockingCollection(1000); // Fixed size 1000 + public bool isGetBmp390CalibrationCoefficientsCommand = false; + public bool isGetPressureCalibrationCoefficientsCommand = false; + public bool mIsNewBMPSupported; public ShimmerLogAndStreamS3Simulator(string devID, string bComPort) : base(devID) @@ -44,6 +47,10 @@ public ShimmerLogAndStreamS3Simulator(string devID, string bComPort, double samp { ComPort = bComPort; } + public void SetIsNewBMPSupported(bool isNewBMPSupported) + { + mIsNewBMPSupported = isNewBMPSupported; + } public override string GetShimmerAddress() { @@ -183,9 +190,10 @@ protected override void WriteBytes(byte[] buffer, int index, int length) } else if (buffer[0] == (byte)PacketTypeShimmer3.GET_BMP280_CALIBRATION_COEFFICIENTS_COMMAND) { + isGetPressureCalibrationCoefficientsCommand = true; mBuffer.Add((byte)0xff); - mBuffer.Add((byte)0x9f); - byte[] bytes = UtilShimmer.HexStringToByteArray("7A6A0D6632007F9016D7D00BBC1B2AFFF9FF8C3CF8C67017"); + mBuffer.Add((byte)0xa6); + byte[] bytes = UtilShimmer.HexStringToByteArray("19011D6BBA643200859289D6D00BC918CBFFF9FF7B1A1FEE4DFC"); foreach (byte byteValue in bytes) { mBuffer.Add(byteValue); @@ -211,7 +219,15 @@ protected override void WriteBytes(byte[] buffer, int index, int length) { mBuffer.Add((byte)0xff); mBuffer.Add((byte)0x02); - byte[] bytes = UtilShimmer.HexStringToByteArray("800202FF01080001"); + byte[] bytes; + if (GetShimmerVersion() == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + bytes = UtilShimmer.HexStringToByteArray("800202FF01080000000001"); + } + else + { + bytes = UtilShimmer.HexStringToByteArray("800202FF01080001"); + } foreach (byte byteValue in bytes) { mBuffer.Add(byteValue); @@ -236,7 +252,7 @@ protected override void WriteBytes(byte[] buffer, int index, int length) { mBuffer.Add((byte)0xff); mBuffer.Add((byte)0x99); - byte[] bytes = UtilShimmer.HexStringToByteArray("8000005201030002000000160702000015BE0FB95C7E330000083C081F0825005300530054019C009C0100FEFF9C1E0000150000000000000000000000000000332C332C332C009C009C000000009C1E0001150000000000000000FDC00474FFC219D31957133C009CFF9C0100FE009C1E00021500000000000000000000000000000C"); + byte[] bytes = UtilShimmer.HexStringToByteArray("1802030003000000100B0200001520902D20D23300000805083907F9005300530054009C039C00FFF9019C1E0000150000000000000000000000000000332C332C332C009C009C000000009C1E0001154B57A224D23300000043FFCDFEEA19301911198A039CFF9DF50409049D1E00021500000000000000000000000000000C"); foreach (byte byteValue in bytes) { mBuffer.Add(byteValue); @@ -247,7 +263,7 @@ protected override void WriteBytes(byte[] buffer, int index, int length) { mBuffer.Add((byte)0xff); mBuffer.Add((byte)0x99); - byte[] bytes = UtilShimmer.HexStringToByteArray("808000D00CD00CD0009C009C000000009C1E0003150000000000000000000000000000066806680668009C009C000000009C1F00001539D4942779330000FFF5018CFD240683067F0692FF9C00640000F8FE9C1F000115000000000000000000000000000000D100D100D1009C0064000000009C1F0002150000000000000000FFEB01"); + byte[] bytes = UtilShimmer.HexStringToByteArray("D00CD00CD0009C009C000000009C1E0003150000000000000000000000000000066806680668009C009C000000009C1F00001533B3A220D2330000FFF60047F7C7064B067606729CFFFFFF64FDFA019C1F0001150000000000000000000000000000032F032F032F9C000000640000009C1F0002150000000000000000000000"); foreach (byte byteValue in bytes) { mBuffer.Add(byteValue); @@ -258,7 +274,7 @@ protected override void WriteBytes(byte[] buffer, int index, int length) { mBuffer.Add((byte)0xff); mBuffer.Add((byte)0x99); - byte[] bytes = UtilShimmer.HexStringToByteArray("5400015AFDF8034203410348009C0064FF00FC009C1F000315000000000000000000000000000001A201A201A2009C0064000000009C200000150000000000000000002AFBEDFC21010700E500FD009C0064000000009C"); + byte[] bytes = UtilShimmer.HexStringToByteArray("0000000198019801989C000000640000009C1F00031500000000000000000000000000000087008700879C000000640000009C20000115F5980C0000000000000000000000044C044C03D49C000000640000009C2000021500000000000000000000000000000357035702F89C000000640000009C2000031500000000000000"); foreach (byte byteValue in bytes) { mBuffer.Add(byteValue); @@ -274,5 +290,145 @@ protected override void WriteBytes(byte[] buffer, int index, int length) } + protected override void readInfoMem() + { + //string status_text = ""; + //PChangeStatusLabel("Inquiring Shimmer Info"); // need to be in a UI thread for update + SetDataReceived(false); + + // btsd changes 2 + string status_text = "Acquiring Accelerometer Range..."; + CustomEventArgs newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring Accelerometer Range..."; + //worker.ReportProgress(15, status_text); + ReadAccelRange(); + + if (HardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + ReadLNAccelRange(); + } + + status_text = "Acquiring ADC Sampling Rate..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring ADC Sampling Rate..."; + //worker.ReportProgress(20, status_text); + ReadSamplingRate(); + + status_text = "Acquiring Magnetometer Range..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring Magnetometer Range..."; + //worker.ReportProgress(25, status_text); + ReadMagRange(); + + status_text = "Acquiring Gyro Range..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring Gyro Range..."; + //worker.ReportProgress(30, status_text); + ReadGyroRange(); + + status_text = "Acquiring Accel Sampling Rate..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring Accel Sampling Rate..."; + //worker.ReportProgress(35, status_text); + ReadAccelSamplingRate(); + + status_text = "Acquiring Calibration settings..."; + //PControlForm.status_text = "Acquiring Calibration settings..."; + //worker.ReportProgress(40, status_text); + if (HardwareVersion == (int)ShimmerBluetooth.ShimmerVersion.SHIMMER3R) + { + ReadCalibDump(); + //ReadCalibrationParameters("All"); + } + else + { + ReadCalibDump(); + //ReadCalibrationParameters("All"); + } + + status_text = "Acquiring EXG1 configure settings..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring EXG1 configure settings..."; + //worker.ReportProgress(45, status_text); + ReadEXGConfigurations(1); + + status_text = "Acquiring EXG2 configure settings..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring EXG2 configure settings..."; + //worker.ReportProgress(50, status_text); + ReadEXGConfigurations(2); + + //There is an inquiry below so no need for this + status_text = "Acquiring Internal Exp Power setting..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + ReadInternalExpPower(); + + status_text = "Acquiring trial configure settings..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring trial configure settings..."; + //worker.ReportProgress(55, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_TRIAL_CONFIG_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring center name..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring center name..."; + //worker.ReportProgress(60, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_CENTER_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring shimmer name..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring shimmer name..."; + //worker.ReportProgress(65, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_SHIMMERNAME_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring experiment ID..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring experiment ID..."; + //worker.ReportProgress(70, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_EXPID_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring Multi Shimmer settings..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring Multi Shimmer settings..."; + //worker.ReportProgress(75, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_MYID_COMMAND }, 0, 1); + waitTilTimeOut(); + + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_NSHIMMER_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring configure time..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring configure time..."; + //worker.ReportProgress(80, status_text); + WriteBytes(new byte[1] { (byte)ShimmerBluetooth.PacketTypeShimmer3SDBT.GET_CONFIGTIME_COMMAND }, 0, 1); + waitTilTimeOut(); + + status_text = "Acquiring general settings..."; + newEventArgs = new CustomEventArgs((int)ShimmerIdentifier.MSG_IDENTIFIER_NOTIFICATION_MESSAGE, (object)status_text); + OnNewEvent(newEventArgs); + //PControlForm.status_text = "Acquiring general settings..."; + //worker.ReportProgress(85, status_text); + Inquiry(); + } + } } diff --git a/ShimmerAPI/ShimmerAPI/Utilities/UtilShimmer.cs b/ShimmerAPI/ShimmerAPI/Utilities/UtilShimmer.cs index f46afa6..38a3df5 100644 --- a/ShimmerAPI/ShimmerAPI/Utilities/UtilShimmer.cs +++ b/ShimmerAPI/ShimmerAPI/Utilities/UtilShimmer.cs @@ -65,5 +65,84 @@ public static String BytesToHexString(byte[] bytes) return null; } } + + public static byte[] ConvertMilliSecondsToShimmerRtcDataBytesLSB(long milliseconds) + { + byte[] rtcTimeArray = ConvertMilliSecondsToShimmerRtcDataBytesMSB(milliseconds); + Array.Reverse(rtcTimeArray); + return rtcTimeArray; + } + + public static byte[] ConvertMilliSecondsToShimmerRtcDataBytesMSB(long milliseconds) + { + long milisecondTicks = (long)(milliseconds * 32.768); + byte[] rtcTimeArray = BitConverter.GetBytes(milisecondTicks); + + Array.Reverse(rtcTimeArray); + + return rtcTimeArray; + } + + public static double[,] DeepCopyDoubleMatrix(double[,] input) + { + if (input == null) + return null; + + int rows = input.GetLength(0); + int cols = input.GetLength(1); + + double[,] result = new double[rows, cols]; + + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + result[r, c] = input[r, c]; + } + } + return result; + } + public static string GetDebugString(int mRangeValue, List sensorClass) + { + string debugString = $"RangeValue: {mRangeValue}\n"; + debugString += GenerateDebugStringPerProperty("Default Offset Vector", sensorClass[2]); + debugString += GenerateDebugStringPerProperty("Default Sensitivity", sensorClass[1]); + debugString += GenerateDebugStringPerProperty("Default Alignment", sensorClass[0]); + + return debugString; + } + + public static String GenerateDebugStringPerProperty(String property, double[,] calMatrix) + { + String debugString = property + " =\n"; + if (calMatrix == null) + { + debugString += "NULL\n"; + } + else + { + debugString += UtilShimmer.DoubleArrayToString(calMatrix); + } + return debugString; + } + + public static string DoubleArrayToString(double[,] doubleArray) + { + StringBuilder returnString = new StringBuilder(); + int rows = doubleArray.GetLength(0); + int cols = doubleArray.GetLength(1); + + for (int x = 0; x < rows; x++) + { + for (int y = 0; y < cols; y++) + { + returnString.Append(doubleArray[x, y]).Append("\t"); + } + returnString.AppendLine(); + } + + return returnString.ToString(); + } + } } diff --git a/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3RSimulatorTest.cs b/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3RSimulatorTest.cs index c675f76..a946f3d 100644 --- a/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3RSimulatorTest.cs +++ b/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3RSimulatorTest.cs @@ -1,8 +1,17 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShimmerAPI; +using ShimmerAPI.Sensors; using ShimmerAPI.Simulators; +using ShimmerAPI.Utilities; using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Collections.Concurrent; +using System.Linq; using System.Threading; using System.Threading.Tasks; +using static ShimmerAPI.ShimmerBluetooth; +using System.Runtime.Serialization; namespace ShimmerBluetoothTests { @@ -11,6 +20,7 @@ public class ShimmerLogAndStreamS3RSimulatorTest { ShimmerLogAndStreamS3RSimulator mDevice; String ComPort = "COM99"; + ConcurrentQueue cq = new ConcurrentQueue(); [TestInitialize] public void SetUp() @@ -21,13 +31,12 @@ public void SetUp() [TestMethod] public void Test001_testConnectandDisconnect() { - //Comment out test as it is not completed - /* if (mDevice != null) { try { mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); Thread.Sleep(30000); if (!mDevice.IsConnected()) { @@ -39,10 +48,395 @@ public void Test001_testConnectandDisconnect() Assert.Fail(); } - if (!mDevice.GetShimmerVersion().Equals(10)) //Shimmer3R + if (!mDevice.GetShimmerVersion().Equals(10)) { Assert.Fail(); } + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test002_ConnectandTestBMP390() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + try + { + mDevice.CalculateBMP390PressureCalibrationCoefficientsResponse(mDevice.GetPressureResoTest()); + + string[] sensorDataType = { "u24" }; + + byte[] sensorDataP2 = { 0x00, 0x17, 0x64 }; + byte[] sensorDataT2 = { 0x00, 0xCF, 0x7F }; + + long[] uncalibResultP2 = ProgrammerUtilities.ParseData(sensorDataP2, sensorDataType); + long[] uncalibResultT2 = ProgrammerUtilities.ParseData(sensorDataT2, sensorDataType); + double[] bmpX80caldata2 = new double[2]; + bmpX80caldata2 = mDevice.CalibratePressure390SensorData(uncalibResultP2[0], uncalibResultT2[0]); + Bmp3QuantizedCalibData.TLin = bmpX80caldata2[1]; + + //Assert.AreEqual(resultP2, 100912.81758676282); + //Assert.AreEqual(resultT2, 23.26587201654911); + Assert.AreEqual(Math.Round(bmpX80caldata2[0], 4), 100912.8176); + Assert.AreEqual(Math.Round(bmpX80caldata2[1], 4), 23.2659); + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test003_ConnectandTestCalibParamRead() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + Thread.Sleep(30000); + + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + System.Console.WriteLine("deviceCalBytes : " + UtilShimmer.BytesToHexString(deviceCalBytes)); + mDevice.CalibByteDumpParse(deviceCalBytes); + + Object returnValue = mDevice.mapOfSensorCalibration; + + Assert.IsNotNull(returnValue); + + Dictionary>> mapOfKinematicSensorCalibrationAll = returnValue as Dictionary>>; + + foreach (int sensorId in mapOfKinematicSensorCalibrationAll.Keys) + { + Dictionary> route1CalParamMapPerSensor = mapOfKinematicSensorCalibrationAll[sensorId]; + + Object sensorDetails = mDevice.GetSensorDetails(sensorId); + Assert.IsNotNull(sensorDetails); + + Console.WriteLine("sensorId : " + sensorId); + + Dictionary> mapOfKinematicCalibPerRange = mapOfKinematicSensorCalibrationAll[sensorId]; + foreach (var calibDetails in mapOfKinematicCalibPerRange) + { + Console.WriteLine(UtilShimmer.GetDebugString(calibDetails.Key, calibDetails.Value)); + } + Console.WriteLine("\n"); + + } + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test004_ConnectandTestDefaultLNAccelAndGyroCalibParam() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + + mDevice.WriteLNAccelRange(0); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] lnAccelOffset = mDevice.OffsetVectorAccel; + Assert.IsTrue(lnAccelOffset.Cast().SequenceEqual(new double[,] { { 0 }, { 0 }, { 0 } }.Cast())); + + double[,] lnAccelAlignment = mDevice.AlignmentMatrixAccel; //it become -0.1 + Assert.IsTrue(lnAccelAlignment.Cast().SequenceEqual(new double[,] { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } }.Cast())); + + double[,] lnAccelSensitivity0 = mDevice.SensitivityMatrixAccel; + Assert.IsTrue(lnAccelSensitivity0.Cast().SequenceEqual(new double[,] { { 1672, 0, 0 }, { 0, 1672, 0 }, { 0, 0, 1672 } }.Cast())); + + mDevice.WriteLNAccelRange(1); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] lnAccelSensitivity1 = mDevice.SensitivityMatrixAccel; + Assert.IsTrue(lnAccelSensitivity1.Cast().SequenceEqual(new double[,] { { 836, 0, 0 }, { 0, 836, 0 }, { 0, 0, 836 } }.Cast())); + + mDevice.WriteLNAccelRange(2); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] lnAccelSensitivity2 = mDevice.SensitivityMatrixAccel; + Assert.IsTrue(lnAccelSensitivity2.Cast().SequenceEqual(new double[,] { { 418, 0, 0 }, { 0, 418, 0 }, { 0, 0, 418 } }.Cast())); + + mDevice.WriteLNAccelRange(3); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] lnAccelSensitivity3 = mDevice.SensitivityMatrixAccel; + Assert.IsTrue(lnAccelSensitivity3.Cast().SequenceEqual(new double[,] { { 209, 0, 0 }, { 0, 209, 0 }, { 0, 0, 209 } }.Cast())); + + + mDevice.WriteGyroRange(0); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroOffset = mDevice.OffsetVectorGyro; + Assert.IsTrue(gyroOffset.Cast().SequenceEqual(new double[,] { { 0 }, { 0 }, { 0 } }.Cast())); + + double[,] gyroAlignment = mDevice.AlignmentMatrixGyro; + Assert.IsTrue(gyroAlignment.Cast().SequenceEqual(new double[,] { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } }.Cast())); + + double[,] gyroSensitivity0 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity0.Cast().SequenceEqual(new double[,] { { 229, 0, 0 }, { 0, 229, 0 }, { 0, 0, 229 } }.Cast())); + + mDevice.WriteGyroRange(1); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroSensitivity1 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity1.Cast().SequenceEqual(new double[,] { { 114, 0, 0 }, { 0, 114, 0 }, { 0, 0, 114 } }.Cast())); + + mDevice.WriteGyroRange(2); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroSensitivity2 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity2.Cast().SequenceEqual(new double[,] { { 57, 0, 0 }, { 0, 57, 0 }, { 0, 0, 57 } }.Cast())); + + mDevice.WriteGyroRange(3); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroSensitivity3 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity3.Cast().SequenceEqual(new double[,] { { 29, 0, 0 }, { 0, 29, 0 }, { 0, 0, 29 } }.Cast())); + + mDevice.WriteGyroRange(4); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroSensitivity4 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity4.Cast().SequenceEqual(new double[,] { { 14, 0, 0 }, { 0, 14, 0 }, { 0, 0, 14 } }.Cast())); + + mDevice.WriteGyroRange(5); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] gyroSensitivity5 = mDevice.SensitivityMatrixGyro; + Assert.IsTrue(gyroSensitivity5.Cast().SequenceEqual(new double[,] { { 7, 0, 0 }, { 0, 7, 0 }, { 0, 0, 7 } }.Cast())); + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test005_ConnectandTestDefaultWRAccelCalibParam() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + mDevice.CalibByteDumpParse(deviceCalBytes); + + double[,] wrAccelOffset = mDevice.OffsetVectorAccel2; + Assert.IsTrue(wrAccelOffset.Cast().SequenceEqual(new double[,] { { 0 }, { 0 }, { 0 } }.Cast())); + double[,] wrAccelAlignment = mDevice.AlignmentMatrixAccel2; + Assert.IsTrue(wrAccelAlignment.Cast().SequenceEqual(new double[,] { { 0, -1, 0 }, { -1, 0, 0 }, { 0, 0, -1 } }.Cast())); + + mDevice.WriteAccelRange(0); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] wrAccelSensitivity0 = mDevice.SensitivityMatrixAccel2; + Assert.IsTrue(wrAccelSensitivity0.Cast().SequenceEqual(new double[,] { { 1671, 0, 0 }, { 0, 1671, 0 }, { 0, 0, 1671 } }.Cast())); + + mDevice.WriteAccelRange(1); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] wrAccelSensitivity1 = mDevice.SensitivityMatrixAccel2; + Assert.IsTrue(wrAccelSensitivity1.Cast().SequenceEqual(new double[,] { { 836, 0, 0 }, { 0, 836, 0 }, { 0, 0, 836 } }.Cast())); + + mDevice.WriteAccelRange(2); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] wrAccelSensitivity2 = mDevice.SensitivityMatrixAccel2; + Assert.IsTrue(wrAccelSensitivity2.Cast().SequenceEqual(new double[,] { { 418, 0, 0 }, { 0, 418, 0 }, { 0, 0, 418 } }.Cast())); + + mDevice.WriteAccelRange(3); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] wrAccelSensitivity3 = mDevice.SensitivityMatrixAccel2; + Assert.IsTrue(wrAccelSensitivity3.Cast().SequenceEqual(new double[,] { { 209, 0, 0 }, { 0, 209, 0 }, { 0, 0, 209 } }.Cast())); + + + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test006_ConnectandTestDefaultMagCalibParam() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + mDevice.CalibByteDumpParse(deviceCalBytes); + + double[,] magOffset = mDevice.OffsetVectorMag; + Assert.IsTrue(magOffset.Cast().SequenceEqual(new double[,] { { 0 }, { 0 }, { 0 } }.Cast())); + double[,] magAlignment = mDevice.AlignmentMatrixMag; + Assert.IsTrue(magAlignment.Cast().SequenceEqual(new double[,] { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } }.Cast())); + + mDevice.WriteMagRange(0); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] magSensitivity0 = mDevice.SensitivityMatrixMag; + Assert.IsTrue(magSensitivity0.Cast().SequenceEqual(new double[,] { { 6842, 0, 0 }, { 0, 6842, 0 }, { 0, 0, 6842 } }.Cast())); + + mDevice.WriteMagRange(1); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] magSensitivity1 = mDevice.SensitivityMatrixMag; + Assert.IsTrue(magSensitivity1.Cast().SequenceEqual(new double[,] { { 3421, 0, 0 }, { 0, 3421, 0 }, { 0, 0, 3421 } }.Cast())); + + mDevice.WriteMagRange(2); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] magSensitivity2 = mDevice.SensitivityMatrixMag; + Assert.IsTrue(magSensitivity2.Cast().SequenceEqual(new double[,] { { 2281, 0, 0 }, { 0, 2281, 0 }, { 0, 0, 2281 } }.Cast())); + + mDevice.WriteMagRange(3); + mDevice.CalibByteDumpParse(deviceCalBytes); + double[,] magSensitivity3 = mDevice.SensitivityMatrixMag; + Assert.IsTrue(magSensitivity3.Cast().SequenceEqual(new double[,] { { 1711, 0, 0 }, { 0, 1711, 0 }, { 0, 0, 1711 } }.Cast())); + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test007_ConnectandTestDefaultHighGAccelCalibParam() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + mDevice.CalibByteDumpParse(deviceCalBytes); + + double[,] altAccelOffset = mDevice.OffsetVectorAltAccel; + Assert.IsTrue(altAccelOffset.Cast().SequenceEqual(new double[,] { { 10 }, { 10 }, { 10 } }.Cast())); + double[,] altAccelAlignment = mDevice.AlignmentMatrixAltAccel; + Assert.IsTrue(altAccelAlignment.Cast().SequenceEqual(new double[,] { { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0, -1 } }.Cast())); + double[,] altAccelSensitivity0 = mDevice.SensitivityMatrixAltAccel; + Assert.IsTrue(altAccelSensitivity0.Cast().SequenceEqual(new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }.Cast())); + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test008_ConnectandTestDefaultWRMagCalibParam() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(true); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + mDevice.CalibByteDumpParse(deviceCalBytes); + + double[,] wrMagOffset = mDevice.OffsetVectorMag2; + Assert.IsTrue(wrMagOffset.Cast().SequenceEqual(new double[,] { { 0 }, { 0 }, { 0 } }.Cast())); + double[,] wrMagAlignment = mDevice.AlignmentMatrixMag2; + Assert.IsTrue(wrMagAlignment.Cast().SequenceEqual(new double[,] { { -1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } }.Cast())); + double[,] wrMagSensitivity0 = mDevice.SensitivityMatrixMag2; + Assert.IsTrue(wrMagSensitivity0.Cast().SequenceEqual(new double[,] { { 667, 0, 0 }, { 0, 667, 0 }, { 0, 0, 667 } }.Cast())); + } catch (Exception ex) { @@ -53,7 +447,6 @@ public void Test001_testConnectandDisconnect() { Assert.Fail("mDevice is null"); } - */ } } -} +} \ No newline at end of file diff --git a/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3SimulatorTest.cs b/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3SimulatorTest.cs index b9b2d6d..f95564f 100644 --- a/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3SimulatorTest.cs +++ b/ShimmerAPI/ShimmerUnitTests/ShimmerLogAndStreamS3SimulatorTest.cs @@ -1,11 +1,15 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShimmerAPI; +using ShimmerAPI.Sensors; using ShimmerAPI.Simulators; +using ShimmerAPI.Utilities; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; +using static ShimmerAPI.ShimmerBluetooth; namespace ShimmerBluetoothTests { @@ -15,6 +19,7 @@ public class ShimmerLogAndStreamS3SimulatorTest ShimmerLogAndStreamS3Simulator mDevice; String ComPort = "COM99"; ConcurrentQueue cq = new ConcurrentQueue(); + protected int SetEnabledSensors = (int)SensorBitmapShimmer3R.SENSOR_BMP380_PRESSURE; [TestInitialize] public void SetUp() @@ -29,6 +34,7 @@ public void Test001_testConnectandDisconnect() { try { + mDevice.SetIsNewBMPSupported(false); mDevice.StartConnectThread(); Thread.Sleep(30000); if (!mDevice.IsConnected()) @@ -45,6 +51,115 @@ public void Test001_testConnectandDisconnect() { Assert.Fail(); } + + try + { + mDevice.Disconnect(); + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + + } + + [TestMethod] + public void Test002_testConnectandDisconnect_NewBMPSupported() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + mDevice.SetIsNewBMPSupported(false); + Thread.Sleep(30000); + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + if (!mDevice.GetFirmwareVersionFullName().Equals("LogAndStream 0.16.9")) + { + Assert.Fail(); + } + + if (!mDevice.GetShimmerVersion().Equals(3)) //Shimmer3 + { + Assert.Fail(); + } + + mDevice.ReadPressureCalibrationCoefficients(); + if (!mDevice.isGetPressureCalibrationCoefficientsCommand) + { + Assert.Fail(); + } + + + } + catch (Exception ex) + { + Assert.Fail($"Test aborted due to exception: {ex.Message}"); + } + } + else + { + Assert.Fail("mDevice is null"); + } + } + + [TestMethod] + public void Test003_ConnectandTestCalibParamRead() + { + if (mDevice != null) + { + try + { + mDevice.StartConnectThread(); + Thread.Sleep(30000); + + if (!mDevice.IsConnected()) + { + Assert.Fail(); + } + + byte[] deviceCalBytes = mDevice.CalibByteDumpGenerate(); + System.Console.WriteLine("deviceCalBytes : " + UtilShimmer.BytesToHexString(deviceCalBytes)); + mDevice.CalibByteDumpParse(deviceCalBytes); + + Object returnValue = mDevice.mapOfSensorCalibration; + + Assert.IsNotNull(returnValue); + + Dictionary>> mapOfKinematicSensorCalibrationAll = returnValue as Dictionary>>; + + foreach (int sensorId in mapOfKinematicSensorCalibrationAll.Keys) + { + Dictionary> route1CalParamMapPerSensor = mapOfKinematicSensorCalibrationAll[sensorId]; + + Object sensorDetails = mDevice.GetSensorDetails(sensorId); + Assert.IsNotNull(sensorDetails); + + Console.WriteLine("sensorId : " + sensorId); + + Dictionary> mapOfKinematicCalibPerRange = mapOfKinematicSensorCalibrationAll[sensorId]; + foreach (var calibDetails in mapOfKinematicCalibPerRange) + { + Console.WriteLine(UtilShimmer.GetDebugString(calibDetails.Key, calibDetails.Value)); + } + Console.WriteLine("\n"); + + } + } catch (Exception ex) { @@ -55,7 +170,6 @@ public void Test001_testConnectandDisconnect() { Assert.Fail("mDevice is null"); } - } }