'Get the rudder rotation angle of the robot's compass

there is an electronic compass with angles of 1-360 degrees (or 0-359, whatever)

https://i.stack.imgur.com/Q7RmH.jpg

there are variables

  • Yaw - compass readings degrees
  • HDG - established direction rudder degrees
  • rudder - the degrees to turn the steering wheel (need to get)

variables to check

HDG:20 Yaw:340 =need to get rudder=40

HDG:50 Yaw:90 =need to get rudder=-40


What is the formula to get the angle by which to rotate?

the problem is to find the shortest turn in the right direction..

it seems simple, but it broke my mind

something like

if (Yaw<=180) {
rudder=set HDG-Yaw;
} else {
rudder=360-Yaw+setHDG;
}

but here is an error if setHDG is 350 and Yaw=20 ... gives 330 and it should be 30



Solution 1:[1]

if (setHDG>=Yaw) 
      rudder=360-(360-setHDG+Yaw);
else 
      rudder=setHDG-Yaw;
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 richardec