import numpy as np
class DualQuaternion:
def __init__(self, real=None, dual=None):
self.real = real if real is not None else np.quaternion(1, 0, 0, 0)
self.dual = dual if dual is not None else np.quaternion(0, 0, 0, 0)
def __mul__(self, other):
if isinstance(other, DualQuaternion):
return DualQuaternion(self.real * other.real, self.real * other.dual + self.dual * other.real)
def conjugate(self):
return DualQuaternion(self.real.conjugate(), self.dual.conjugate())
def inverse(self):
real_inv = self.real.inverse()
return DualQuaternion(real_inv, (-self.dual) * real_inv * real_inv)
def to_translation(self):
t = 2 * (self.dual * self.real.conjugate())
return np.array([t.x, t.y, t.z])
def to_rotation(self):
return self.real.normalized()
def to_transformation_matrix(self):
r = self.to_rotation()
t = self.to_translation()
r_mat = np.asarray(r.to_rotation_matrix())
t_mat = np.column_stack((r_mat, t))
return np.row_stack((t_mat, [0, 0, 0, 1]))
@staticmethod
def from_axis_angle_translation(axis, angle, translation):
axis_angle = np.quaternion(np.cos(angle / 2), *(np.sin(angle / 2) * axis))
dual = 0.5 * np.quaternion(0, *translation) * axis_angle
return DualQuaternion(axis_angle, dual)
def fabrik(joints, target, max_iterations=100, tolerance=1e-6):
max_angles = [joint[2] for joint in joints]
min_angles = [joint[1] for joint in joints]
curr_angles = [joint[0] for joint in joints]
# Initial end-effector position
end_effector = get_end_effector_position(joints)
# FABRIK loop
for _ in range(max_iterations):
if np.linalg.norm(target - end_effector) <= tolerance:
break
# Forward reaching
target_dq = DualQuaternion.from_axis_angle_translation(joints[-1][3], curr_angles[-1], np.array(joints[-1][4]))
joints[-1][0] = np.clip(joints[-1][0], min_angles[-1], max_angles[-1])
end_effector = target_dq.to_transformation_matrix() @ np.array([0, 0, 0, 1])
end_effector = end_effector[:-1]
for i in range(len(joints) - 2, -1, -1):
joint = joints[i]
joint_dq_inv = DualQuaternion.from_axis_angle_translation(joint[3], curr_angles[i] + joint[5], np.array(joint[4])).inverse()
joint_to_target = joint_dq_inv * DualQuaternion(real=np.quaternion(0, *(end_effector - target))) * joint_dq_inv.conjugate()
end_effector += joint_to_target.to_translation()
joint[0] = np.clip(joint[0], min_angles[i], max_angles[i])
# Backward reaching
base_dq = DualQuaternion.from_axis_angle_translation(joints[0][3], curr_angles[0], np.array(joints[0][4]))
base_dq = base_dq.inverse()
for i in range(len(joints) - 1):
joint = joints[i]
joint_dq_inv = DualQuaternion.from_axis_angle_translation(joint[3], curr_angles[i + 1] + joint[5], np.array(joint[4])).inverse()
joint_to_end_effector = joint_dq_inv * DualQuaternion(real=np.quaternion(0, *(target - end_effector))) * joint_dq_inv.conjugate()
end_effector += joint_to_end_effector.to_translation()
joint[0] = np.clip(joint[0], min_angles[i], max_angles[i])
end_effector = get_end_effector_position(joints)
return curr_angles
def get_end_effector_position(joints):
end_effector = np.array([0, 0, 0, 1])
for joint in joints:
joint_dq = DualQuaternion.from_axis_angle_translation(joint[3], joint[0] + joint[5], np.array(joint[4]))
end_effector = joint_dq.to_transformation_matrix() @ end_effector
return end_effector[:-1]
if __name__ == "__main__":
# Example usage
joints = [
[0, -np.pi / 2, np.pi / 2, [0, 0, 1], [0, 0, 0], 0],
[0, -np.pi / 2, np.pi / 2, [1, 0, 0], [3, 0, 0], np.pi / 2],
[0, -np.pi / 2, np.pi / 2, [0, 1, 0], [6, 0, 0], 0],
[0, -np.pi / 2, np.pi / 2, [1, 0, 0], [6, 0, -3], np.pi / 2],
[0, -np.pi / 2, np.pi / 2, [0, 1, 0], [6, 0, -6], 0],
[0, -np.pi / 2, np.pi / 2, [1, 0, 0], [9, 0, -6], np.pi / 2],
]
target = np.array([5, 0, -5])
result_angles = fabrik(joints, target)
print("Angles pour atteindre la cible :", result_angles)
print("End-effector position :", get_end_effector_position(joints))
Cet algorithme implémente l'algorithme FABRIK pour un bras robotisé 6 axes en utilisant les dual quaternions. Les axes sont des liaisons pivots avec un seul degré de liberté. La rotation autour des axes est limitée par un angle minimum et un angle maximum. Les dual quaternions sont utilisés dans les calculs pour représenter les transformations pour chaque joint du bras. L'algorithme prend en entrée les caractéristiques des joints du bras sous la forme de dual quaternions et calcule les angles pour atteindre la position cible.