using System; using System.Xml.Linq; namespace HC_APTBS.Models { /// /// 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. /// /// /// The bench ADC uses a 10-bit (1024-step) range over 0–5 V. /// Conversion: V = (rawCanBusValue × 5000 / 1024) / 1000 /// Engineering value = V × ((MaxVal − MinVal) / (MaxVolt − MinVolt)) × Gain + Offset /// /// public class SensorConfiguration { /// 1-based sensor channel number. public short Number { get; set; } = 1; /// Display name for the sensor (e.g. "Pressure"). public string SensorName { get; set; } = "Sensor"; /// Minimum input voltage at the sensor connector (V). public double MinVolt { get; set; } = 0; /// Maximum input voltage at the sensor connector (V). public double MaxVolt { get; set; } = 5; /// Engineering-unit value corresponding to . public double MinVal { get; set; } = 0; /// Engineering-unit value corresponding to . public double MaxVal { get; set; } = 15; /// Multiplicative gain correction applied after the range mapping. public double Gain { get; set; } = 1; /// Additive offset correction applied after the gain. public double Offset { get; set; } = 0; /// /// Converts a raw CAN bus ADC count to a calibrated engineering-unit value. /// /// 10-bit ADC count from the CAN frame. /// Calibrated value in engineering units. 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; } /// Serialises this sensor configuration to XML. 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)); /// Deserialises a sensor configuration from XML. 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; } /// Creates the default pressure sensor calibration for channel 1. 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 assign) { if (!string.IsNullOrEmpty(value)) try { assign(value!); } catch { /* Ignore malformed XML values */ } } } }