1- import typing
2-
31import numpy as np
42
53from frankik ._core import (
@@ -48,9 +46,7 @@ def __init__(self, robot_type: str = RobotType.FR3):
4846 self .q_home = kQDefault
4947
5048 @staticmethod
51- def pose_inverse (
52- T : np .ndarray [tuple [typing .Literal [4 ], typing .Literal [4 ]], np .dtype [np .float64 ]]
53- ) -> np .ndarray [tuple [typing .Literal [4 ], typing .Literal [4 ]], np .dtype [np .float64 ]]:
49+ def pose_inverse (T : np .ndarray ) -> np .ndarray :
5450 """Compute the inverse of a homogeneous transformation matrix.
5551 Args:
5652 T (np.ndarray): A 4x4 homogeneous transformation matrix.
@@ -66,9 +62,9 @@ def pose_inverse(
6662
6763 def forward (
6864 self ,
69- q0 : np .ndarray [ tuple [ typing . Literal [ 7 ]], np . dtype [ np . float64 ]] ,
70- tcp_offset : np .ndarray [ tuple [ typing . Literal [ 4 ], typing . Literal [ 4 ]], np . dtype [ np . float64 ]] | None = None ,
71- ) -> np .ndarray [ tuple [ typing . Literal [ 4 ], typing . Literal [ 4 ]], np . dtype [ np . float64 ]] :
65+ q0 : np .ndarray ,
66+ tcp_offset : np .ndarray | None = None ,
67+ ) -> np .ndarray :
7268 """Compute the forward kinematics for the given joint configuration.
7369 Args:
7470 q0 (np.ndarray): A 7-element array representing joint angles.
@@ -83,11 +79,15 @@ def forward(
8379
8480 def inverse (
8581 self ,
86- pose : np .ndarray [ tuple [ typing . Literal [ 4 ], typing . Literal [ 4 ]], np . dtype [ np . float64 ]] ,
87- q0 : np .ndarray [ tuple [ typing . Literal [ 7 ]], np . dtype [ np . float64 ]] | None = None ,
88- tcp_offset : np .ndarray [ tuple [ typing . Literal [ 4 ], typing . Literal [ 4 ]], np . dtype [ np . float64 ]] | None = None ,
82+ pose : np .ndarray ,
83+ q0 : np .ndarray | None = None ,
84+ tcp_offset : np .ndarray | None = None ,
8985 q7 : float | None = None ,
90- ) -> np .ndarray [tuple [typing .Literal [7 ]], np .dtype [np .float64 ]] | None :
86+ global_solution : bool = False ,
87+ joint_weight : np .ndarray | None = None ,
88+ q7_sample_interval = 40 ,
89+ q7_sample_size = 60 ,
90+ ) -> np .ndarray | None :
9191 """Compute the inverse kinematics for the given end-effector pose.
9292
9393 Args:
@@ -96,15 +96,33 @@ def inverse(
9696 tcp_offset (np.ndarray, optional): A 4x4 homogeneous transformation matrix representing
9797 the tool center point offset. Defaults to None.
9898 q7 (float, optional): The angle of the seventh joint, used for FR3 robot IK. If None then it will be sampled. Defaults to None.
99+ global_solution (bool, optional): Whether to consider global ik solutions. Defaults to False.
100+ joint_weight (np.ndarray, optional): Weights for calculating the distance between the solution and q0. Defaults to None.
101+ q7_sample_interval (int, optional): The interval for sampling q7. Defaults to 40.
102+ q7_sample_size (int, optional): The number of samples for q7. Defaults to 60.
99103
100104 Returns:
101105 np.ndarray | None: A 7-element array representing the joint angles if a solution is found; otherwise, None.
102106 """
107+ if joint_weight is None :
108+ joint_weight = np .ones (7 )
109+ if q0 is None :
110+ q0 = self .q_home
111+
103112 new_pose = pose @ self .pose_inverse (tcp_offset ) if tcp_offset is not None else pose
104113 if q7 is not None :
105114 q = ik (new_pose , q0 , q7 , is_fr3 = (self .robot_type == RobotType .FR3 )) # type: ignore
106115 else :
107- q = ik_sample_q7 (new_pose , q0 , is_fr3 = (self .robot_type == RobotType .FR3 )) # type: ignore
116+ qs = ik_sample_q7 (
117+ new_pose ,
118+ q0 ,
119+ is_fr3 = (self .robot_type == RobotType .FR3 ),
120+ sample_size = q7_sample_size ,
121+ sample_interval = q7_sample_interval ,
122+ full_ik = global_solution ,
123+ ) # type: ignore
124+ q_diffs = np .sum ((np .array (qs ) - q0 ) * joint_weight , axis = 1 ) ** 2
125+ q = qs [np .argmin (q_diffs )]
108126
109127 if np .isnan (q ).any ():
110128 return None
0 commit comments