I'm trying to build a sort of 3 dimensional robotic arm that mimics the movement of a virtual robotic arm inside Unreal Engine. I'm using Arduino Mega for the hardware side of the arm.
I have a MPU6050 gyroscope on the robotic arm and I wrote a simple code to mimic the virtual arm's rotation in the real world.
For example, when the virtual arm's yaw angle is at 0, the hardware arm's angle is also 0, which is set by the MPU6050 gyro.
So when the virtual arm moves 90 degrees left, the hardware arm rotates left till the MPU6050 gives a reading of around 90 degrees then stops.
The problem is, Unreal Engine forwards the arm's rotational value in a range of -180 to 180 degrees and the hardware is hard-coded to either go left if the desired rotation is below the current rotation. Aka if the desired rotation is -90 degrees, and current rotation is 0, for left and vice versa, the robotic arm goes right for a +90 desired rotation. This becomes an issue as I reach the end limits till -179 or 179 as the virtual arm will go to -178, -179 then +179, +178, +177 and so on and in order to replicate that desired rotation, the hardware arm would turn left till -179, then start spinning right until it reaches +179.
I'd like to implement some sort of shortest path algorithm that would take the distance between the current and desired rotation and choose whether to go left or right based on which part is the shortest.
Help appreciated. I hope I was able to clarify what exactly it is I'm trying to achieve.