Your Equation

Library / Guidance and trajectory following / GUI-01

Guidance and trajectory following

Wrapped course error

Difference between a commanded and an actual course, wrapped into the range -180° to +180° so that the controller always turns the short way round.

Ref
GUI-01
Rev
v1
Field
Guidance and trajectory following
Result
eχ [°]

Courses are angles that repeat every 360°, so the difference between two of them must be wrapped into −180° to +180° before it is used as an error. Taking atan2 of the sine and cosine of the difference does this reliably: the result is the shortest signed turn from the actual course to the commanded one, positive for a turn to the right (clockwise).

Difference between a commanded and an actual course, wrapped into the range -180° to +180° so that the controller always turns the short way round.

Calculator

Solve for
°
°
0360
°
0360
  • Naive difference without wrapping, χcmd − χ−340 °
  • Size of the turn needed, |eχ|20 °
  • Turn direction (+1 right, −1 left)1

What drives the result

Each variable is moved across its typical range while the others stay at the values above. The longest bar is the key driver.

Course error (°) current value 20 °
χcmdCommanded courseKey driver
−170178
χActual course
−170178
  • With a commanded course of 10° and an actual course of 350°, the raw difference is −340°, which would command a turn almost all the way round. The wrapped error is 20°, a short turn to the right.
  • The error always lies between −180° and +180°. At exactly 180° the two directions are equally short, so the sign is arbitrary, and a controller needs a rule (or hysteresis) there.
Chart data as tables

Ranked variables

VariableTypical rangeLowest resultHighest resultSwing (°)Local stepChange in result
χcmd Commanded course0 to 360 °−170178348+10%+1 °
χ Actual course0 to 360 °−170178348+10%−35 °

Sweep of Commanded course

Commanded course (°)Course error (°)
010
4555
90100
135145
180−170
225−125
270−80
315−35
36010

Sweep one variable

Course error (°)Hover, touch, or use the arrow keys on the chart
−200−1000100200 0100200300 Commanded course (°) now 20

Variables and how each one contributes

eχ

Course error

Result · °

What the equation gives you. Everything else feeds it.

χcmd

Commanded course

° · typical 0 to 360

The commanded course in degrees, clockwise from north. Raising the command raises the error one for one, until the shortest way round flips at 180°. Raising it by 10° (to 20°) moves the result by +50 %, from 20 to 30 °.

χ

Actual course

° · typical 0 to 360

The actual course in degrees, clockwise from north. Raising the actual course lowers the error one for one, until the shortest way round flips. Lowering it by 10° (to 340°) moves the result by +50 %, from 20 to 30 °.

Worked example with the values above

  1. Raw difference: χcmd − χ = 10 − 350 = −340°.
  2. Sine and cosine of it: sin = 0.342, cos = 0.9397.
  3. eχ = atan2(0.342, 0.9397) = 20°.

Assumptions and limits

  • Both courses in degrees (or both in radians), measured in the same direction and from the same reference.
  • A turn that can be made in either direction at will.

Common mistakes

  • Subtracting without wrapping, which commands a nearly full turn whenever the courses straddle north.
  • Mixing degrees and radians inside sin and cos, or using a remainder operator that behaves differently for negative numbers.

Statistics and uncertainty

Near ±180° a tiny noise in either course flips the sign of the error, and with it the commanded turn direction, so a controller can chatter between left and right turns. Hysteresis or a held turn direction removes it. Away from that point the wrapping adds no error of its own.

Where it is used

  • Heading and course controllers, where a naive subtraction fails at the 0°/360° boundary.
  • Preparing a course error for use by a path-following guidance law.

Maths you need

  • Angles that wrap: 0° and 360° are the same direction
  • The atan2 function: angle from sine and cosine, in the correct quadrant

Related formulas

Practise this formula