Here is the logic for an AOI I've created to do scaling. There are two pictures attached also.
###################
// AOI to scale an input, filter and limit its output.
// First we want to remember the last output value. Used in the filter calculation.
LastEngVal := Engr_Out;
LastSigVal := Signal_Out;
// Now calculate the analog signal value from the input data. THis will give us the output of ex 4.25 or 9.87
NewSigVal := (Input - Input_Min) * ( ( Input_Sig_Max - Input_Sig_Min) / (Input_Max - Input_Min)) + Input_Sig_Min;
// For now lets not filter the calculated signal value.
Signal_Out:= NewSigVal;
// Now we will calculate the engineering value using the signal value, Sensor signal min/max and engineering min/max
NewEngVal := (Signal_Out - Sensor_Sig_Min) * ( ( Sensor_Engr_Max - Sensor_Engr_Min) / (Sensor_Sig_Max - Sensor_Sig_Min)) + Sensor_Engr_Min;
// Use the New value, last value, and filter coefficient to filter the output. Limit the filter coefficient to 0-1
//Filter coefficient . Value from 0-1. Filter calculation performed is:
// [(NewValue * (1-Filter_Coeff))] + [LastValue * Filter_Coeff]. A value fo 0 is no filtering, and value of .99 is highly filtered.
If Filter_Coeff < 0 then
Filter_Coeff := 0;
Elsif Filter_Coeff > 1 then
Filter_Coeff :=1 ;
End_if;
Engr_Out := ( NewEngVal * (1-Filter_Coeff)) + ( LastEngVal * Filter_Coeff);
// If the clamp bool is true, then limit the output to the min/max engr values
if ClampOutput then
if Engr_Out < Sensor_Engr_Min then
Engr_Out := Sensor_Engr_Min;
Elsif Engr_Out > Sensor_Engr_Max then
Engr_Out := Sensor_Engr_Max;
End_if;
End_If;
##############################