Files
HC_APTBS/Models/SensorConfiguration.cs
2026-04-11 12:45:18 +02:00

107 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Xml.Linq;
namespace HC_APTBS.Models
{
/// <summary>
/// Calibration parameters for an analogue sensor channel.
/// Maps a raw ADC counts value (from the bench CAN bus) to an
/// engineering-unit value using a voltage-range to value-range linear mapping,
/// then applies a gain and offset correction.
///
/// <para>
/// The bench ADC uses a 10-bit (1024-step) range over 05 V.
/// Conversion: V = (rawCanBusValue × 5000 / 1024) / 1000
/// Engineering value = V × ((MaxVal MinVal) / (MaxVolt MinVolt)) × Gain + Offset
/// </para>
/// </summary>
public class SensorConfiguration
{
/// <summary>1-based sensor channel number.</summary>
public short Number { get; set; } = 1;
/// <summary>Display name for the sensor (e.g. "Pressure").</summary>
public string SensorName { get; set; } = "Sensor";
/// <summary>Minimum input voltage at the sensor connector (V).</summary>
public double MinVolt { get; set; } = 0;
/// <summary>Maximum input voltage at the sensor connector (V).</summary>
public double MaxVolt { get; set; } = 5;
/// <summary>Engineering-unit value corresponding to <see cref="MinVolt"/>.</summary>
public double MinVal { get; set; } = 0;
/// <summary>Engineering-unit value corresponding to <see cref="MaxVolt"/>.</summary>
public double MaxVal { get; set; } = 15;
/// <summary>Multiplicative gain correction applied after the range mapping.</summary>
public double Gain { get; set; } = 1;
/// <summary>Additive offset correction applied after the gain.</summary>
public double Offset { get; set; } = 0;
/// <summary>
/// Converts a raw CAN bus ADC count to a calibrated engineering-unit value.
/// </summary>
/// <param name="rawCanBusValue">10-bit ADC count from the CAN frame.</param>
/// <returns>Calibrated value in engineering units.</returns>
public double GetValueFromRaw(double rawCanBusValue)
{
// Convert ADC counts → volts (10-bit ADC, 5 V reference)
double volts = rawCanBusValue * 5000.0 / 1024.0 / 1000.0;
// Map voltage range → engineering-unit range
double value = volts * ((MaxVal - MinVal) / (MaxVolt - MinVolt));
return value * Gain + Offset;
}
/// <summary>Serialises this sensor configuration to XML.</summary>
public XElement ToXml()
=> new XElement("sensor",
new XAttribute("num", Number),
new XAttribute("name", SensorName),
new XElement("Gain", Gain),
new XElement("Offset", Offset),
new XElement("MinVolt", MinVolt),
new XElement("MaxVolt", MaxVolt),
new XElement("MinVal", MinVal),
new XElement("MaxVal", MaxVal));
/// <summary>Deserialises a sensor configuration from XML.</summary>
public static SensorConfiguration FromXml(XElement element)
{
var sc = new SensorConfiguration();
TryParse(element.Attribute("num")?.Value, v => sc.Number = short.Parse(v));
TryParse(element.Attribute("name")?.Value, v => sc.SensorName = v);
TryParse(element.Element("Gain")?.Value, v => sc.Gain = double.Parse(v));
TryParse(element.Element("Offset")?.Value, v => sc.Offset = double.Parse(v));
TryParse(element.Element("MinVolt")?.Value, v => sc.MinVolt = double.Parse(v));
TryParse(element.Element("MaxVolt")?.Value, v => sc.MaxVolt = double.Parse(v));
TryParse(element.Element("MinVal")?.Value, v => sc.MinVal = double.Parse(v));
TryParse(element.Element("MaxVal")?.Value, v => sc.MaxVal = double.Parse(v));
return sc;
}
/// <summary>Creates the default pressure sensor calibration for channel 1.</summary>
public static SensorConfiguration DefaultPressureSensor()
=> new SensorConfiguration
{
Number = 1,
SensorName = "Pressure",
Offset = -4.1,
MinVolt = 0.5,
MaxVolt = 3.2,
MinVal = 0,
MaxVal = 20
};
private static void TryParse(string? value, Action<string> assign)
{
if (!string.IsNullOrEmpty(value))
try { assign(value!); } catch { /* Ignore malformed XML values */ }
}
}
}