Repository navigation
Fix cs_quat2euler degrees and duplicate CasADi function names #152
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
malikdurmus
wants to merge
1
commit into
learnsyslab:main
Choose a base branch
from
malikdurmus:fix-casadi-rotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from typing import TYPE_CHECKING | ||
|
|
||
| import array_api_strict as xp | ||
| import casadi as cs | ||
| import numpy as np | ||
| import pytest | ||
| from scipy.spatial.transform import Rotation as R | ||
|
|
@@ -240,3 +241,32 @@ def test_rpy2matrix_symbolic(): | |
| # compare casadi/symbolic implementation to scipy | ||
| mat_cs = np.array(rotation.cs_rpy2matrix_func(rpy)) | ||
| assert np.allclose(mat_cs, mat_scipy, atol=tol), "Symbolic rpy->matrix differs from scipy." | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.parametrize("seq", ["xyz", "zyx", "zxz", "XYZ", "ZYX", "ZXZ"]) | ||
| @pytest.mark.parametrize("degrees", [False, True]) | ||
| def test_quat2euler_symbolic(seq: str, degrees: bool): | ||
| rot = R.random(100) | ||
| quat = cs.MX.sym("quat", 4) | ||
| quat2euler = cs.Function("quat2euler", [quat], [rotation.cs_quat2euler(quat, seq, degrees)]) | ||
| period = 360.0 if degrees else 2 * np.pi | ||
| for q, euler_scipy in zip(rot.as_quat(), rot.as_euler(seq, degrees=degrees)): | ||
| euler_cs = np.array(quat2euler(q)).flatten() | ||
| diff = (euler_cs - euler_scipy + period / 2) % period - period / 2 # Wrap to [-p/2, p/2) | ||
| assert np.allclose(diff, 0.0, atol=tol), "Symbolic quat->euler differs from scipy." | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_symbolic_function_names_unique(): | ||
| fns = [ | ||
| rotation.cs_ang_vel2rpy_rates, | ||
| rotation.cs_rpy_rates2ang_vel, | ||
| rotation.cs_ang_vel_deriv2rpy_rates_deriv, | ||
| rotation.cs_rpy_rates_deriv2ang_vel_deriv, | ||
| rotation.cs_quat2matrix_func, | ||
| rotation.cs_rpy2matrix_func, | ||
| ] | ||
|
Comment on lines
+262
to
+269
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we collect these automatically instead? |
||
| names = [fn.name() for fn in fns] | ||
| # Duplicate names produce conflicting C symbols when generating code for several functions | ||
| assert len(set(names)) == len(names), f"CasADi function names are not unique: {names}" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably never reaches gimbal locks (which have some switching logic), so it would be great to add these as well. E.g.