Skip to content

Commit 0537024

Browse files
authored
Merge pull request #164 from gbionics/dev/joint-regularization-task
add joint regularization term and update
2 parents 5a4fff3 + 7278929 commit 0537024

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/adam/casadi/inverse_kinematics.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,17 @@ def add_target(
460460
else:
461461
raise ValueError("Unsupported target type")
462462

463+
def add_joint_regularization(self, weight: float = 1e-3):
464+
"""Add a regularization term to the cost function to minimize joint movement.
465+
466+
Args:
467+
weight (float): Weight for the regularization term.
468+
"""
469+
self.joint_position_target = self.opti.parameter(self.ndof)
470+
self.cost_terms.append(
471+
weight * cs.sumsqr(self.joint_pos - self.joint_position_target)
472+
)
473+
463474
def update_target_position(self, frame: str, position: np.ndarray):
464475
"""Update the target position for a frame.
465476
@@ -522,6 +533,26 @@ def update_target(
522533
else:
523534
raise RuntimeError("Unknown target type")
524535

536+
def update_joint_regularization(self, joint_values: np.ndarray):
537+
"""Update the joint regularization target.
538+
539+
Args:
540+
joint_values (np.ndarray): The new joint values to regularize towards.
541+
"""
542+
if not hasattr(self, "joint_position_target"):
543+
raise RuntimeError(
544+
"Joint regularization has not been added. Call add_joint_regularization() first."
545+
)
546+
if joint_values is None:
547+
joint_values = (
548+
np.zeros(self.ndof)
549+
if self._cached_sol is None
550+
else self._cached_sol.value(self.joint_pos)
551+
)
552+
self.opti.set_value(self.joint_position_target, joint_values)
553+
else:
554+
self.opti.set_value(self.joint_position_target, joint_values)
555+
525556
def set_initial_guess(self, base_transform: np.ndarray, joint_values: np.ndarray):
526557
"""Set the initial guess for the optimization problem.
527558

0 commit comments

Comments
 (0)