← All exercises

Clamp with Overflow

medium

Constrain a numeric input between Min and Max bounds and raise flags when limits are hit.

What it teaches

A clamp that also reports when its limits engage. Pin the value to Max (and raise a 'too high' flag) or to Min (and raise a 'too low' flag); inside the range, the value passes through with both flags off. Useful when downstream logic needs to know the rail was hit.

Inputs and outputs

TagTypeDefaultDescription
In_ValueREALInput value to clamp
Cfg_MinREAL10Lower bound limit
Cfg_MaxREAL90Upper bound limit
Out_ClampedREALConstrained output value
Sts_TooHighBOOLToo high flag
Sts_TooLowBOOLToo low flag

Required behavior

  • Out_Clamped equals In_Value when it is between Cfg_Min and Cfg_Max inclusive.
  • Out_Clamped is pinned to Cfg_Max and Sts_TooHigh is 1 when In_Value is greater than Cfg_Max.
  • Out_Clamped is pinned to Cfg_Min and Sts_TooLow is 1 when In_Value is less than Cfg_Min.
  • Sts_TooHigh and Sts_TooLow are 0 when In_Value is within limits.