I am looking for someone who can help me with a piece of code.
The plan is to connect a real aviation compass (HSI) to a sim through a Arduino.
Basically the compass is a motor with a position sensor.
The position sensor and the simulator will provide a compass heading (0 to 360 degrees) to the Arduino.
The problem is, I don't know how to let the Arduino decide if the motor has to turn left or right to get to the correct heading the fastest.
for example:
If the current heading is 0 and the simulator tells the Arduino to go to 90 degrees.
the motor has to turn counter clockwise until to new value is reached.
and if the current heading is at 0 and the sim tells the Arduino to go to 270 degrees.
the motor needs to turn clockwise until to new value is reached.
Anybody an idea how to accomplish this?
thanks all in advance,
Subtract the current heading from the desired heading. If the difference is more than 180 go one way to the closest if greater than 180 go the other way.
If you are at 0 and want to go to 90 that is less than 180 so go 90 CW. To go from 0 to 270, that is more than 180 so go 90 CCW to get to 270.
hdg targ delta
-90 -90 0
-90 -45 45 right
-90 0 90 right
-90 45 135 right
-90 90 180 right
-45 -90 -45 left
-45 -45 0
-45 0 45 right
-45 45 90 right
-45 90 135 right
0 -90 -90 left
0 -45 -45 left
0 0 0
0 45 45 right
0 90 90 right
45 -90 -135 left
45 -45 -90 left
45 0 -45 left
45 45 0
45 90 45 right
90 -90 -180 left
90 -45 -135 left
90 0 -90 left
90 45 -45 left
90 90 0