# Find rotation to align two vectors by rotation about two arbitrary axes

1. Feb 10, 2012

### Johan_L

Hello,
Background:
We have a machine with two rotational axes (b,c) attached to each other. By design the B axis is rotating about the Y-axis and the c-axis is attached on top of the b axis.

Ideally the axes are orthogonal but in practice they are not. However we can measure the directions of the axes. Hence we have the axes of rotations described as two vectors.

The target is to align an arbitrary vector that is attached to the c-axis of rotation to a vector that is described in the global coordinates by changing the rotation angle of the b and c axis. We can construct the full rotation matrix but how do we find the angles b and c that aligns the vectors. From CAD we can see that there are multiple solutions.0
16

We can find numerous examples on how to get one angle and one axis of rotation from a rotation matrix. We can also find a lot of examples on how to split the transform into three Euler angles.

We know that the problem is possible to solve numerically but is there a closed form solution?

#### Attached Files:

• ###### Configuration of axes.png
File size:
54 KB
Views:
147
2. Feb 10, 2012

### chiro

Hey Johan L and welcome to the forums.

The best way I see to do this is to use the axis-angle method of rotation.

Basically this works by having an axis of rotation and an angle which you rotate an existing point (or vector depending on your notation) around an axis by a specific angle.

The way to do this mathematically is well known and you can use matrix methods to do this or you can use quaternion methods and then convert the results to a matrix which you then just use matrix multiplication.

Now to get the axis of rotation, what you need to do is take the cross product of the two vectors and normalize the result. This will give you a vector that is perpendicular to both the start vector and the final vector which means you have a vector which to 'rotate around'.

Do get the angle you use the dot product of the two vectors (final and start) and make sure you are working with normalized vectors so that you get the right term (i.e. cos(theta) instead a cos(theta) for some value of a <> 1).

That should do the trick.

3. Feb 10, 2012

### Johan_L

Thanks for the answer that would give us one axis of rotation and the corresponding angle. Our hardware constrains us to rotation about b and c and hence we need to find those two angles.
Is there a way to "split" the rotation calculated by the method you suggest into rotations b and c?

4. Feb 10, 2012

### chiro

Basically do the same thing as the above but in the context of your b and c axis.

So once you have found your vector above, you now do this in the context of your b-axis and your c-axis. This time however you have got the actual axis of rotation so now you just have to find the angles.

There needs to be some level of orthogonality (i.e. the axis are not linearly dependent) which of course is the case due to the physical nature of the design.

It might actually be a good idea to use composition of matrices and then finding the appropriate inverse to get your final transformation.

How much do you know about composing matrices? Have you ever worked with matrix composition to compose rotations before?

I don't have the solution in front of me right now, but basically what I would do is to create a composition of the rotations that fit the system (so basically what this means is that you have a system Ax = b where A represents the linear system that transforms x to b or start vector to finish vector) and then you decompose A into submatrices that correspond to rotations around b and c axis.

So given Ax = b, and A = BC, then this means you want to find (A^-1) = (BC)^-1 where B represents the rotation information around axis B and C represents the one for axis C.

For these matrices you fill in the appropriate information for matrices B and C based on the axis-angle formulation of the matrix (for this see: http://en.wikipedia.org/wiki/Rotation_matrix#Conversion_from_and_to_axis-angle)

Then you will get a linear system involving your two angles which you can solve with linear algebra techniques.

If you need any further help I'll do my best to help you.

5. Feb 10, 2012

### Johan_L

We have gotten about that far. y=Rb.Rc.x
• Where x and y are known, the vector to align and the targetvector
• Rb is a matrix with only the angle b unknown, the axis of rotation is known
• Rc is a matrix with only the angle c unknown, the axis of rotation is known

We can as you say calculate a transformation R with a rotation axis, the cross product between the vectors and find the angle acos(dotproduct)

The problem is to find angles b and c given that.

I guess the question really is how hard this is, we are intermediate skill at best. If this leads to complicated calculations we will just go for a numerical solution straight away.

6. Feb 10, 2012

### chiro

Basically you have to expand the equations and at some point use a numerical solution.

If you want to know more about the specifics, what you can do is take the system and expand out the algebra to get systems that are in terms of x and y (x being angle for R and y being angle for z).

The thing is that because we are dealing with angles, we are not going to get nice simple equations because we will have cos and sin terms which muck up things.

As long as you have a simple root finding algorithm (of which you can get tonnes of these for free on the internet and have a look at the pseudo-version of the algorithms if you need to write your own code for legal reasons), you will be able to do this. Since you are working with mechanical equipment, I am going to go out on a limb and say that you probably have some kind of code to do numerical routines like this but I would appreciate that you correct me if need be.

So basically the first step is to expand everything out and get a system of equations for cos(x) and cos(y) or sin(x) and sin(y) [You can replace the sins with cos terms using standard trig identities] and once you get the answers then you adjust for the quadrants that the answers are in and you should get the answer.

7. Feb 10, 2012

### chiro

Also I should have pointed this out above, but didn't.

With the above approach you don't need to do any cross-product operations.

The Matrix RbRc = R contains all the information for rotating x into y. We don't have to do anything else. Once you expand out the equations with your trig functions in terms of angles for b and c, then you will have to use a root finding algorithm to get your angles. After you get a solution and adjust your answers for the right co-ordinate, then you have your complete answer for the angles.

Again, don't worry about the cross-product: that was just the case because it was the simplest answer and it wasn't known that you needed to do it via your mechanical rotations b and c and not just get the simplest solution based on an unconstrained system.

8. Feb 13, 2012

### Johan_L

We tried the numerical approach and validated it against the CAD model. it seems to work fine. See below for the Maxima code we used. Thanks for the help.

In each iteration do:
Get the signed angle between the rotatedVect and the targetvector projected on the plane with normal vector rotatdedWorkTableRotationAxis
Rotate rotatedvect by angle about rotatdedWorkTableRotationAxis
Get the signed angle between the rotatedVect and the targetvector projected on the plane with normal vector TiltRotationAxis
Rotate rotatedvect by angle about rotatdedWorkTableRotationAxis
Rotate rotatdedWorkTableRotationAxis by angle about rotatdedWorkTableRotationAxis
Code (Text):

c:0$b:0$
cStep:0$bStep:0$
rotatedVect:VectorInTableCoordinates$rotatdedWorkTableRotationAxis:WorkTableRotationAxis$
for i:1 while i<=6 do
(
cStep:0,
bStep:0,
cStep:GetRotationAngleTo(rotatedVect,TargetVector,rotatdedWorkTableRotationAxis),
c:c+cStep,

rotatedVect:Rotate(rotatedVect,cStep,rotatdedWorkTableRotationAxis),
bStep:GetRotationAngleTo(rotatedVect,TargetVector,TiltRotationAxis),
b:b+bStep,

rotatedVect:Rotate(rotatedVect,bStep,TiltRotationAxis),
rotatdedWorkTableRotationAxis:Rotate(rotatdedWorkTableRotationAxis,bStep,TiltRotationAxis),
print(iteration+ i),
print(b/deg),
print(c/deg)

);
c/deg;
b/deg;