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
- 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.
- 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
| Variable | Typical range | Lowest result | Highest result | Swing (°) | Local step | Change in result |
|---|---|---|---|---|---|---|
| χcmd Commanded course | 0 to 360 ° | −170 | 178 | 348 | +10% | +1 ° |
| χ Actual course | 0 to 360 ° | −170 | 178 | 348 | +10% | −35 ° |
Sweep of Commanded course
| Commanded course (°) | Course error (°) |
|---|---|
| 0 | 10 |
| 45 | 55 |
| 90 | 100 |
| 135 | 145 |
| 180 | −170 |
| 225 | −125 |
| 270 | −80 |
| 315 | −35 |
| 360 | 10 |
Sweep one variable
Variables and how each one contributes
Course error
What the equation gives you. Everything else feeds it.
Commanded course
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
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
- Raw difference: χcmd − χ = 10 − 350 = −340°.
- Sine and cosine of it: sin = 0.342, cos = 0.9397.
- 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