# End effector rotates 180 degrees when trying to set orientation with direction vector

I want to orient the robot endeffector using a direction vector that I calculate myself. The robot will always start from the same position with the endeffector pointing straight down. I already have a function that calculates a direction vector that is in the orientation that I want the endeffector to be. I have used the code in this answer to calculate the quaternion from the starting orientation to the goal orientation: https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another. For some example orientation the quaternion that the code calculates is:

0.38268343 0 0 0.92387953


and the correct quaternion that I want is:

0 0.92387953 0.38268343 0


Unfortunately, when I pass the data on to the robot the robot has the right orientation but spins it's endeffector 180 degrees. My goal is to keep the endeffector from spinning and just rotate around it self in the angle that I want it to. I have multiple questions about this:

1. What is the reason for the rotation around itself?
2. How would I make sure the robot only moves to the correct orientation but does not actually change the way the end effector is facing?
3. How can I control the rotation to make the endeffector rotate at an angle that I want? For example: 20 degrees clock or counter clockwise.

When you convert the physical motions to mathematical symbolism, you apply mathematical assumptions about the results. For instance, does your algorithm’s acos function restrict the results to [0, 2$$\pi$$], or to [-$$\pi$$, +$$\pi$$]? You can always normalize the motions by adding $$\pm 2 \pi$$ to the solutions, but you need to be careful not to cause windup of the axes (particularly the cables driving the axes). You could implement a “nearest solution” approach to minimize the joint rotations in these configurations which are near to the wrist singularity.