Esempio n. 1
0
        /// <summary>
        /// Resets the device, updates PROM data and clears current measurements
        /// </summary>
        public virtual void Reset()
        {
            // Send reset command
            Hardware.WriteJoinByte((byte)Ms5611Command.Reset, 0);

            // Wait for completion
            Task.Delay(TimeSpanExtensions.FromMicroseconds(ResetTime)).Wait();

            // Update PROM values
            ReadProm();

            // Clear measurement
            Measurement = Ms5611Measurement.Zero;
        }
Esempio n. 2
0
        /// <summary>
        /// Calculates the <see cref="Measurement"/> by applying the <see cref="Prom"/>
        /// coefficients and other rules to the raw measurements.
        /// </summary>
        protected virtual void Calculate(int rawPressure, int rawTemperature)
        {
            // Constants for calculation
            const double temperatureAt20C      = 2000;
            const double temperatureAt15C      = 1500;
            const double temperatureAtMinus15C = -1500;
            const double scale2Power7          = 128;
            const double scale2Power8          = 256;
            const double scale2Power15         = 32768;
            const double scale2Power16         = 65536;
            const double scale2Power21         = 2097152;
            const double scale2Power23         = 8388608;
            const double scale2Power31         = 2147483648;

            // First order pressure and temperature calculation...

            // Calculate reference temperature
            var referenceTemperature = Prom.C5TemperatureReference * scale2Power8;

            // Difference between actual and reference temperature
            var deltaTemperature = rawTemperature - referenceTemperature;

            // Actual temperature (-40...85°C with 0.01°C resolution)
            var temperature = temperatureAt20C + deltaTemperature *
                              Prom.C6TemperatureSensitivity / scale2Power23;

            // Calculate temperature compensated pressure

            // Offset at actual temperature
            var offset = Prom.C2PressureOffset * scale2Power16 +
                         (Prom.C4TemperatureFromPressureOffset * deltaTemperature) / scale2Power7;

            // Sensitivity at actual temperature
            var sensitivity = Prom.C1PressureSensitivity * scale2Power15 +
                              (Prom.C3TemperatureFromPressureSensitivity * deltaTemperature) / scale2Power8;

            // Second order temperature compensation...
            if (temperature < temperatureAt20C)
            {
                // Low temperature
                var temperature2 = Math.Pow(deltaTemperature, 2) / scale2Power31;
                var offset2      = 5 * Math.Pow(temperature - temperatureAt20C, 2) / 2;
                var sensitivity2 = offset2 / 2;
                if (temperature < temperatureAtMinus15C)
                {
                    // Very low temperature
                    var temperaturePlus15CSquared = Math.Pow(temperature + temperatureAt15C, 2);
                    offset2      = offset2 + 7 * temperaturePlus15CSquared;
                    sensitivity2 = sensitivity2 + 11 * temperaturePlus15CSquared / 2;
                }
                temperature -= temperature2;
                offset      -= offset2;
                sensitivity -= sensitivity2;
            }

            // Temperature compensated pressure (10...1200mbar with 0.01mbar resolution)
            var pressure = (rawPressure * sensitivity / scale2Power21 - offset) / scale2Power15;

            // Correct floating point to whole values and save in properties
            Measurement = new Ms5611Measurement(pressure / 100, temperature / 100);
        }
        /// <summary>
        /// Calculates the <see cref="Measurement"/> by applying the <see cref="Prom"/>
        /// coefficients and other rules to the raw measurements.
        /// </summary>
        protected virtual void Calculate(int rawPressure, int rawTemperature)
        {
            // Constants for calculation
            const double temperatureAt20C = 2000;
            const double temperatureAt15C = 1500;
            const double temperatureAtMinus15C = -1500;
            const double scale2Power7 = 128;
            const double scale2Power8 = 256;
            const double scale2Power15 = 32768;
            const double scale2Power16 = 65536;
            const double scale2Power21 = 2097152;
            const double scale2Power23 = 8388608;
            const double scale2Power31 = 2147483648;

            // First order pressure and temperature calculation...

            // Calculate reference temperature
            var referenceTemperature = Prom.C5TemperatureReference * scale2Power8;

            // Difference between actual and reference temperature
            var deltaTemperature = rawTemperature - referenceTemperature;

            // Actual temperature (-40...85°C with 0.01°C resolution)
            var temperature = temperatureAt20C + deltaTemperature *
                Prom.C6TemperatureSensitivity / scale2Power23;

            // Calculate temperature compensated pressure

            // Offset at actual temperature
            var offset = Prom.C2PressureOffset * scale2Power16 +
                (Prom.C4TemperatureFromPressureOffset * deltaTemperature) / scale2Power7;

            // Sensitivity at actual temperature
            var sensitivity = Prom.C1PressureSensitivity * scale2Power15 +
                (Prom.C3TemperatureFromPressureSensitivity * deltaTemperature) / scale2Power8;

            // Second order temperature compensation...
            if (temperature < temperatureAt20C)
            {
                // Low temperature
                var temperature2 = Math.Pow(deltaTemperature, 2) / scale2Power31;
                var offset2 = 5 * Math.Pow(temperature - temperatureAt20C, 2) / 2;
                var sensitivity2 = offset2 / 2;
                if (temperature < temperatureAtMinus15C)
                {
                    // Very low temperature
                    var temperaturePlus15CSquared = Math.Pow(temperature + temperatureAt15C, 2);
                    offset2 = offset2 + 7 * temperaturePlus15CSquared;
                    sensitivity2 = sensitivity2 + 11 * temperaturePlus15CSquared / 2;
                }
                temperature -= temperature2;
                offset -= offset2;
                sensitivity -= sensitivity2;
            }

            // Temperature compensated pressure (10...1200mbar with 0.01mbar resolution)
            var pressure = (rawPressure * sensitivity / scale2Power21 - offset) / scale2Power15;

            // Correct floating point to whole values and save in properties
            Measurement = new Ms5611Measurement(pressure / 100, temperature / 100);
        }
        /// <summary>
        /// Updates the display when the <see cref="Device"/> channels change.
        /// </summary>
        private void OnMeasurementUpdated(object sender, Ms5611Measurement measurement)
        {
            // Dump statistics to output
            WriteOutput(measurement.ToString());

            // Add data point to graph
            Graph.Add(measurement);

            // Update display
            DoPropertyChanged(nameof(Device));
            DoPropertyChanged(nameof(Graph));
        }