@@ -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