7
7
ORIENTATION_COEFF = 1.
8
8
9
9
10
- def inverse_kinematic_optimization (chain , target_frame , starting_nodes_angles , regularization_parameter = None , max_iter = None , orientation_mode = None , no_position = False ):
10
+ def inverse_kinematic_optimization (chain , target_frame , starting_nodes_angles , regularization_parameter = None , max_iter = None , orientation_mode = None , no_position = False , optimizer = "least_squares" ):
11
11
"""
12
12
Computes the inverse kinematic on the specified target
13
13
@@ -32,7 +32,14 @@ def inverse_kinematic_optimization(chain, target_frame, starting_nodes_angles, r
32
32
* "all": Target the three axes
33
33
no_position: bool
34
34
Do not optimize against position
35
+ optimizer: str
36
+ The solver to use. Choices:
37
+ * "least_squares": Use scipy.optimize.least_squares
38
+ * "scalar": Use scipy.optimize.minimize
35
39
"""
40
+ if optimizer not in ["least_squares" , "scalar" ]:
41
+ raise ValueError ("Unknown solver: {}" .format (optimizer ))
42
+
36
43
# Begin with the position
37
44
target = target_frame [:3 , - 1 ]
38
45
@@ -124,15 +131,22 @@ def optimize_total(x):
124
131
# Compute bounds
125
132
real_bounds = [link .bounds for link in chain .links ]
126
133
# real_bounds = real_bounds[chain.first_active_joint:]
127
- real_bounds = np . moveaxis ( chain .active_from_full (real_bounds ), - 1 , 0 )
134
+ real_bounds = chain .active_from_full (real_bounds )
128
135
129
136
logs .logger .info ("Bounds: {}" .format (real_bounds ))
130
137
131
138
if max_iter is not None :
132
139
logs .logger .info ("max_iter is not used anymore in the IK, using it as a parameter will raise an exception in the future" )
133
140
134
141
# least squares optimization
135
- res = scipy .optimize .least_squares (optimize_total , chain .active_from_full (starting_nodes_angles ), bounds = real_bounds )
142
+ if optimizer == "scalar" :
143
+ def optimize_scalar (x ):
144
+ return np .linalg .norm (optimize_total (x ))
145
+ res = scipy .optimize .minimize (optimize_scalar , chain .active_from_full (starting_nodes_angles ), bounds = real_bounds )
146
+ elif optimizer == "least_squares" :
147
+ # We need to unzip the bounds
148
+ real_bounds = np .moveaxis (real_bounds , - 1 , 0 )
149
+ res = scipy .optimize .least_squares (optimize_total , chain .active_from_full (starting_nodes_angles ), bounds = real_bounds )
136
150
137
151
if res .status != - 1 :
138
152
logs .logger .info ("Inverse kinematic optimisation OK, termination status: {}" .format (res .status ))
0 commit comments