Here is a function to transform a vector from robot's local frame to global or from global to local (depending on bGlobalToLocal parameter). Here arrRobotOrientation is always in global frame.
def transform(self, arrVector, arrRobotOrientation, bGlobalToLocal):
roll, pitch, yaw = arrRobotOrientation
# Define rotation matrices for roll, pitch, and yaw
R_x = np.array([
[1, 0, 0],
[0, np.cos(roll), -np.sin(roll)],
[0, np.sin(roll), np.cos(roll)]
])
R_y = np.array([
[np.cos(pitch), 0, np.sin(pitch)],
[0, 1, 0],
[-np.sin(pitch), 0, np.cos(pitch)]
])
# R_y = np.array([
# [np.cos(pitch), 0, -np.sin(pitch)],
# [0, 1, 0],
# [np.sin(pitch), 0, np.cos(pitch)]
# ])
R_z = np.array([
[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]
])
# Combine rotation matrices in Yaw -> Pitch -> Roll order
R = R_z @ R_y @ R_x
if bGlobalToLocal:
# Invert the transformation by transposing the rotation matrix
R = R.T
# Transform the vector
arrTransformed = R @ arrVector
return arrTransformed
I have created test cases similar to this one (and it fails most of them):
arrVector = np.array([1, 0, 0])
arrRobotOrientation = np.array([0, 0, 0]) # No rotation
bGlobalToLocal = True
result = self.transform(arrVector, arrRobotOrientation, bGlobalToLocal)
print("Expected: [1, 0, 0]:", result)
Please help.