% // Normalizes any number to an arbitrary range, but used here for wrapping angles when going below min or above max
% template <typename T>
% static T normalizeAngle( const T& value, const T& start = 0.0, const T& end = M_2PI)
% {
%     const T width       = end - start   ;   //
%     const T offsetValue = value - start ;   // value relative to 0
% 
%     return value == value ? ( ( offsetValue - ( floor( offsetValue / width ) * width ) ) + start ) : value;

function [a] = normalize_angle(angle_in, limitLow, limitHi)

if nargin<2
    limitLow = 0;
    limitHi  = 360; 
end

width = limitHi - limitLow;
offsetValue = angle_in - limitLow;

a = offsetValue - floor( offsetValue ./ width ) .* width + limitLow;

end

