Robotic Mechanics and Modeling/Kinematics/Additional Examples for Rotations

From Wikiversity
Jump to navigation Jump to search

Rotation in 2D with rotation matrix[edit | edit source]

Group 5[edit | edit source]

Rotation of the vector by using a rotation matrix:

import numpy as np
import matplotlib.pyplot as plt

myVector = np.array([[2],[3]])
rotAng = 135 #deg

def rotate(vector, angle):
    angle = np.pi*angle/180 #to radians
    rotMat = np.array([[np.cos(angle),-np.sin(angle)],[np.sin(angle),np.cos(angle)]])
    return np.array(np.dot(rotMat,vector))

newVector = rotate(myVector,rotAng)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.quiver(0,0,myVector[0],myVector[1],scale_units='xy',scale=1,angles='xy',color='b')
ax.quiver(0,0,newVector[0],newVector[1],scale_units='xy',scale=1,angles='xy',color='r')
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
ax.set_xlabel('X',size=20)
ax.set_ylabel('Y',size=20)

plt.savefig('2D_rotation_135_deg_vector23.png', dpi=300, bbox_inches='tight')

Example 2 (Team 6, Spring 2020)[edit | edit source]

3D Rotation about X,Y,and Z Rotation Matrices[edit | edit source]

Assume you wanted to rotate the reference frame of an object and it required 1 rotation about all 3 axis, in the order of X, then Y, then Z. This example doesnt deal with translation along any of the new axis'. This example demonstrates how to rotate a vector 90 degrees about the X, 45 degrees about the Y, and 120 degrees about the Z, and then display the original and rotated vector.

%reset -f
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

O = np.array([[2,3,5]])
O=O.T
print(O)

alpha=90*np.pi/180 
beta=45*np.pi/180 
gamma=120*np.pi/180

XRot=np.array([[1,0,0],
             [0,np.cos(alpha),-np.sin(alpha)],
             [0,np.sin(alpha),np.cos(alpha)]])
YRot=np.array([[np.cos(beta),0,np.sin(beta)],
             [0,1,0],
             [-np.sin(beta),0,np.cos(beta)]])
ZRot=np.array([[np.cos(gamma),-np.sin(gamma),0],
             [np.sin(gamma),np.cos(gamma),0], 
             [0,0,1]])
    
XO=np.dot(XO,O)
YXO=np.dot(YRot,XO)
RzyxO=np.dot(ZRot,RyxO)

Origin=np.array([0,0,0,O[0],O[1],O[2]])
Xorigin=np.array([0,0,0,XO[0],XO[1],XO[2]])
YXOrigin=np.array([0,0,0,YXO[0],YXO[1],YXO[2]])
ZYXOrigin=np.array([0,0,0,ZYXO[0],ZYXO[1],ZYXO[2]])

fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
ax.quiver(Origin[0],Origin[1],Origin[2],Origin[3],Origin[4],Origin[5],pivot='tail',normalize=False)
ax.quiver(RxOrigin[0],XOrigin[1],XOrigin[2],XOrigin[3],XOrigin[4],XOrigin[5],pivot='tail',color='red',normalize=False)
ax.set_xlim([-3, 4])
ax.set_ylim([-1, 4])
ax.set_zlim([-1, 4])


ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Original Vector (Blue) 3 Times rotated vector about X, Y, and Z axis, in that order')

Example 3 (Team 4, Spring 2020)[edit | edit source]

If we assume a clock's hands are two vectors, initially positioned at 12 o’clock, use rotation vectors to adjust the minute and hour hands to the proper positions for 6:25.

%reset -f
import matplotlib.pyplot as plt
import numpy as np

#We are going to print a clock which shows 6:25
#Can change hour and minute to anything within 1-12 and 0-59 respectively
hour = 6
minute = 25

#convert to radians
#hour hand is adjusted to account for the additional movement from the minute hand
theta_min=-1*(minute/60)*np.pi*2
theta_hour=-1*((hour+minute/60)/12)*np.pi*2

#Calculate rotation matrices
R_min=np.array([[np.cos(theta_min),-np.sin(theta_min)],
            [np.sin(theta_min),np.cos(theta_min)]])
R_hour=np.array([[np.cos(theta_hour),-np.sin(theta_hour)],
                 [np.sin(theta_hour),np.cos(theta_hour)]])

#Base Vector for min (at 12, theta=0)
Min_Vector=np.array([[0,1.5]])
Min_Vector=Min_Vector.T

#Base Vector for hour (at 12, theta=0)
Hour_Vector=np.array([[0,1]])
Hour_Vector=Hour_Vector.T

#Rotate minute and hour vectors
Min_RV=np.dot(R_min,Min_Vector)
Hour_RV=np.dot(R_hour,Hour_Vector)

#plot the vectors
fig=plt.figure()
ax=fig.add_subplot(111)
ax.quiver(0,0,Min_RV[0],Min_RV[1],scale=5)
ax.quiver(0,0,Hour_RV[0],Hour_RV[1],scale=5,color='r')
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])
plt.axis('off')