@@ -460,6 +460,15 @@ 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 (weight * cs .sumsqr (self .joint_pos - self .joint_position_target ))
471+
463472 def update_target_position (self , frame : str , position : np .ndarray ):
464473 """Update the target position for a frame.
465474
@@ -522,6 +531,22 @@ def update_target(
522531 else :
523532 raise RuntimeError ("Unknown target type" )
524533
534+ def update_joint_regularization (self , joint_values : np .ndarray ):
535+ """Update the joint regularization target.
536+
537+ Args:
538+ joint_values (np.ndarray): The new joint values to regularize towards.
539+ """
540+ if not hasattr (self , "joint_position_target" ):
541+ raise RuntimeError (
542+ "Joint regularization has not been added. Call add_joint_regularization() first."
543+ )
544+ if joint_values is None :
545+ joint_values = np .zeros (self .ndof ) if self ._cached_sol is None else self ._cached_sol .value (self .joint_pos )
546+ self .opti .set_value (self .joint_position_target , joint_values )
547+ else :
548+ self .opti .set_value (self .joint_position_target , joint_values )
549+
525550 def set_initial_guess (self , base_transform : np .ndarray , joint_values : np .ndarray ):
526551 """Set the initial guess for the optimization problem.
527552
0 commit comments