Skip to content

Add add_cost method in the inverse kinematics #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Giulero
Copy link
Collaborator

@Giulero Giulero commented Jul 22, 2025

This pull request enhances the inverse kinematics solver in src/adam/casadi/inverse_kinematics.py by introducing new features and improving code clarity. The key changes include the addition of a homogeneous transformation matrix for the base, a new method to retrieve optimization variables, and functionality to add custom cost terms to the optimization problem.

New features:

  • Homogeneous transformation matrix:

    • Added self.base_homogeneous, which computes the homogeneous transformation matrix of the base using its position and quaternion. ([src/adam/casadi/inverse_kinematics.pyR64-R66](https://github.com/ami-iit/adam/pull/130/files#diff-92636529c4bff705bc52b6921d13c06319f84c74610c40fc35d376e6a589d8a7R64-R66))
  • Retrieve optimization variables:

    • Introduced the get_opti_variables method, which returns a dictionary of all optimization variables, including the new homogeneous transformation matrix. ([src/adam/casadi/inverse_kinematics.pyR88-R106](https://github.com/ami-iit/adam/pull/130/files#diff-92636529c4bff705bc52b6921d13c06319f84c74610c40fc35d376e6a589d8a7R88-R106))
  • Custom cost terms:

    • Added the add_cost method to allow users to add custom cost terms to the optimization problem, enhancing flexibility. ([src/adam/casadi/inverse_kinematics.pyR581-R589](https://github.com/ami-iit/adam/pull/130/files#diff-92636529c4bff705bc52b6921d13c06319f84c74610c40fc35d376e6a589d8a7R581-R589))

📚 Documentation preview 📚: https://adam-docs--130.org.readthedocs.build/en/130/

@Giulero Giulero requested a review from Copilot July 22, 2025 09:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds extensibility features to the inverse kinematics solver by introducing methods to retrieve optimization variables and add custom cost terms. The changes also include a precomputed homogeneous transformation matrix for the base.

  • Addition of get_opti_variables() method to expose optimization variables to users
  • Implementation of add_cost() method for adding custom cost terms to the optimization problem
  • Precomputation of base homogeneous transformation matrix from position and quaternion

- "base_pos": Position of the base (3D vector).
- "base_quat": Quaternion representing the base orientation (4D vector with x, y, z, w).
- "joint_var": Joint variables (1D vector with length equal to the number of joints).
- "base_homogeneous": Homogeneous transformation matrix of the base (4x4 matrix, from position and quaternion).`
Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an extra backtick at the end of the docstring line. It should end with a period instead.

Suggested change
- "base_homogeneous": Homogeneous transformation matrix of the base (4x4 matrix, from position and quaternion).`
- "base_homogeneous": Homogeneous transformation matrix of the base (4x4 matrix, from position and quaternion).

Copilot uses AI. Check for mistakes.


def add_cost(self, cost: cs.MX):
"""Add a custom cost term to the optimization problem.

Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add_cost method lacks documentation explaining how the cost term will be used in the optimization (e.g., whether it's added to existing costs or how it affects the objective function).

Suggested change
This method appends the provided cost term to the `cost_terms` list.
During the `_finalize_problem` step, all cost terms in this list are
combined using `cs.sum(cs.vertcat(*self.cost_terms))` and minimized
as part of the objective function.
Note: This method ensures that the optimization graph is still modifiable
before adding the cost term. Once the problem is finalized (after the
first call to `solve()`), no new cost terms can be added.

Copilot uses AI. Check for mistakes.


- "base_pos": Position of the base (3D vector).
- "base_quat": Quaternion representing the base orientation (4D vector with x, y, z, w).
- "joint_var": Joint variables (1D vector with length equal to the number of joints).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not remember how fixed joints are handled in adam, but I am not sure if this sentence is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I am not sure about the "joint variables" name. Perhaps "joint positions" is more clear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants