Trigonometric Functions

Overview

Sine and cosine pairs. KSL functions that rotate about a coordinate axis take the angle as the two-element array {sin(theta), cos(theta)} rather than the angle itself. Sine and cosine are among the most expensive operations in a kinematics calculation, and the same joint angle is usually needed by several functions in one time step. ksl_dc() computes the pair once, and every function that uses it then needs only multiplications and additions:

double dc[2];
ksl_dc(theta, dc);              /* dc = {sin(theta), cos(theta)} */
ksl_product_ddrz(&D, dc, &Do);  /* Do = D * Rz(theta) */

Angle ranges. ksl_normalizeRadians() and ksl_normalizeDegrees() wrap an angle into \([0, 2\pi)\) or \([0, 360)\).

Continuous angles. The standard atan2(y, x) returns an angle in \((-\pi, \pi]\), so an angle that keeps increasing, such as a wheel or a motor shaft, jumps by \(2\pi\) every revolution. ksl_catan2pi() returns the angle that matches atan2(y, x) and is closest to a previous value, so the result stays continuous through any number of turns, as long as the angle changes by less than \(\pi/4\) between calls. ksl_catan1pi() is the version for when the signs of x and y are not known and the angle is only defined up to a multiple of \(\pi\). The Euler angle functions use them to keep angles continuous (see Axes and Euler Angles).

These functions are declared in ksl/trig.h, which must be included separately from ksl/ksl.h.

Functions

ksl_dc()

compute double precision direction cosines from an angle specified in radians

ksl_dcf()

compute single precision direction cosines from an angle specified in radians

ksl_normalizeDegrees()

Normalize double precision angle in degrees.

ksl_normalizeDegreesf()

Normalize single precision angle in degrees.

ksl_normalizeRadians()

Normalize angle in radians.

ksl_normalizeRadiansf()

Normalize angle in radians.

ksl_catan2pi()

returns a continuous double precision angle obtained from atan2(y, x)

ksl_catan2pif()

Single precision version of ksl_catan2pi().

ksl_catan1pi()

returns a continuous double precision angle from atan2(y, x) when x and y are known only up to a common sign

ksl_catan1pif()

Single precision version of ksl_catan1pi().

inline void ksl_dc(const double thetai, double dc[2])

compute double precision direction cosines from an angle specified in radians

Parameters:
  • thetai – [in] input angle

  • dc – [out] direction cosines, with sine stored in dc[0] and cosine is stored in dc[1]

inline void ksl_dcf(const float thetai, float dc[2])

compute single precision direction cosines from an angle specified in radians

Parameters:
  • thetai – [in] input angle

  • dc – [out] direction cosines, with sine stored in dc[0] and cosine is stored in dc[1]

inline double ksl_normalizeDegrees(double angle)

Normalize double precision angle in degrees.

Parameters:
  • angle – [in] angle in degrees

Returns:

the angle wrapped to the range [0, 360)

inline float ksl_normalizeDegreesf(float angle)

Normalize single precision angle in degrees.

Parameters:
  • angle – [in] angle in degrees

Returns:

the angle wrapped to the range [0, 360)

double ksl_normalizeRadians(double angle)

Normalize angle in radians.

Parameters:
  • angle – [in] angle in radians

Returns:

the angle wrapped to the range [0, 2 pi)

float ksl_normalizeRadiansf(float angle)

Normalize angle in radians.

Parameters:
  • angle – [in] angle in radians

Returns:

the angle wrapped to the range [0, 2 pi)

double ksl_catan2pi(const double y, const double x, double theta)

returns a continuous double precision angle obtained from atan2(y, x)

Returns the angle with sine y and cosine x (up to a common positive factor) that is nearest to the reference angle theta, typically the value returned by the previous call. The result is atan2(y, x) shifted by a multiple of 2 pi:

\[ \theta_{new} = \theta + \operatorname{remainder}(\operatorname{atan2}(y, x) - \theta,\ 2\pi) \]

so it lies within pi of theta and is continuous in time as long as the angle changes by less than pi between calls, in either direction. theta may be any multiple of a full turn.

Both x and y must have their correct signs. If they are known only up to a common sign, the angle is known only to within pi; use ksl_catan1pi.

Parameters:
  • y – [in] sine of the angle, or a positive multiple of it

  • x – [in] cosine of the angle, the same multiple

  • theta – [in] reference angle in radians

Returns:

the angle nearest to theta

float ksl_catan2pif(const float y, const float x, float theta)

Single precision version of ksl_catan2pi().

double ksl_catan1pi(const double y, const double x, double theta)

returns a continuous double precision angle from atan2(y, x) when x and y are known only up to a common sign

If x and y may both have the wrong sign, atan2(y, x) gives the angle only to within pi. This function returns the angle in that family nearest to the reference angle theta, typically the value returned by the previous call:

\[ \theta_{new} = \theta + \operatorname{remainder}(\operatorname{atan2}(y, x) - \theta,\ \pi) \]

so it lies within pi/2 of theta and is continuous in time as long as the angle changes by less than pi/2 between calls, in either direction. When the signs of x and y are known, ksl_catan2pi determines the angle to within 2 pi.

Parameters:
  • y – [in] sine of the angle, or a nonzero multiple of it

  • x – [in] cosine of the angle, the same multiple

  • theta – [in] reference angle in radians

Returns:

the angle nearest to theta

float ksl_catan1pif(const float y, const float x, float theta)

Single precision version of ksl_catan1pi().