public Task <Temps> ReadThermometer(int index) { // Get a random raw value 0 - 499 kelvin int kelvin = this.random.Next(0, 500); double celcius = TempUtils.KelvinToCelcius(kelvin); var temps = new Temps { Kelvin = kelvin, Celcius = celcius, Farenheight = TempUtils.CelciusToFarenheight(celcius), }; return(Task.FromResult(temps)); }
public async Task <Temps> ReadThermometer(int index) { if (index < 0 || index > 7) { throw new IndexOutOfRangeException("Index must be from 0-7"); } using (await this.thermometerLock.LockAsync()) { await this.initTask; double sum = 0; const int numSamples = 3; for (int i = 0; i < numSamples; i++) { var reading = this.mcp.Read(Channels[index]); sum += reading.NormalizedValue; await Task.Delay(100); } double averageValue = sum / numSamples; //Debug.WriteLine($"Reading for thermometer {index} is {reading.RawValue}, Normalized: {reading.NormalizedValue}"); double voltage = averageValue * InputVoltage; double resistance = TempUtils.GetThermistorResistenceFromVoltage(3.3, voltage, BalancingResistorOhms); //Debug.WriteLine($"Got Resistance {resistance} from voltage {voltage}"); CoefficientSet coefficients = Coefficients[0]; var temps = new Temps(); if (double.IsInfinity(resistance)) { temps.Kelvin = 0; } else { temps.Kelvin = TempUtils.ResistanceToTemp(coefficients.A, coefficients.B, coefficients.C, resistance); } temps.Celcius = TempUtils.KelvinToCelcius(temps.Kelvin); temps.Farenheight = TempUtils.CelciusToFarenheight(temps.Celcius); return(temps); } }
public void TestTheseNumbers() { double a, b, c; a = -1.373357407E-3; b = 4.914938378E-4; c = -5.890760444E-7; double tempK = TempUtils.ResistanceToTemp(a, b, c, 101219); Assert.AreEqual(295, Math.Round(tempK)); double tempC = TempUtils.KelvinToCelcius(tempK); Assert.AreEqual(22, Math.Round(tempC)); double tempF = TempUtils.CelciusToFarenheight(tempC); Assert.AreEqual(71, Math.Round(tempF)); }
public void TestAgain() { // 33500 / 122F double a, b, c; a = -1.373357407E-3; b = 4.914938378E-4; c = -5.890760444E-7; double tempK = TempUtils.ResistanceToTemp(a, b, c, 33500); Assert.AreEqual(325, Math.Round(tempK)); double tempC = TempUtils.KelvinToCelcius(tempK); Assert.AreEqual(51, Math.Round(tempC)); double tempF = TempUtils.CelciusToFarenheight(tempC); Assert.AreEqual(124, Math.Round(tempF)); }