Wrap angle function#

float uz_signals_wrap(float number, float limit)#

wraps a number to the range of [0, limit]

Parameters:
  • number – number to wrap

  • limit – defines the wrapping limit of [0, limit]

Returns:

wrapped number

Example#

Listing 66 Example function call wrap an angle to the range of 0 to \(2\pi\)#
1#include "uz_signals.h"
2int main(void) {
3   theta = 13.0f;
4   float theta_wrapped = uz_signals_wrap(theta, 2.0f*UZ_PIf);
5}

Use-case#

This function limits values (e.g., angles) to \([0, 2\pi]\). The intended use case is for the electric rotor angle of a machine, which is usually a mechanic angle multiplied by the pole pairs.

../../../_images/angle_wrap.svg

Fig. 269 Example wrapping to \([0, 2\pi]\)#

Description#

Wraps the input number to the range of \([0, limit]\) by using the remainderf function. Therefore, technically other upper limits than \(2\pi\) can be used if desired. Only limits greater than 0 are allowed!

Hint

If a lower limit other than 0 is needed, e.g., as \([-\pi, \pi]\), apply \([0, limit]\) as limit and subtract \(\pi\) afterward.

Calculation#

If the number is already in the range, it will be unchanged. If it is not in the range, remainderf will be applied. If the result is positive, it is in the range and will be returned. Otherwise, the limit is added to the remainder and returned.