-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[FEATURE] Add relative quaternion getter #2764
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1346,21 +1346,60 @@ def get_pos(self, envs_idx=None): | |
| return self._solver.get_links_pos(self.base_link_idx, envs_idx)[..., 0, :] | ||
|
|
||
| @gs.assert_built | ||
| def get_quat(self, envs_idx=None): | ||
| def get_quat(self, envs_idx=None, *, relative=False): | ||
| """ | ||
| Returns quaternion of the entity's base link. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| envs_idx : None | array_like, optional | ||
| The indices of the environments. If None, all environments will be considered. Defaults to None. | ||
| relative : bool, optional | ||
| If True, return the quaternion relative to the initial (not current!) quaternion. | ||
| The returned quaternion ``delta`` satisfies | ||
| ``abs_quat == transform_quat_by_quat(init_quat, delta)``. | ||
| Equivalently, ``delta == transform_quat_by_quat(inv_quat(init_quat), abs_quat)``. | ||
| Defaults to False. | ||
|
|
||
| Returns | ||
| ------- | ||
| quat : torch.Tensor, shape (4,) or (n_envs, 4) | ||
| The quaternion of the entity's base link. | ||
| """ | ||
| return self._solver.get_links_quat(self.base_link_idx, envs_idx)[..., 0, :] | ||
| The quaternion of the entity's base link (absolute or relative). | ||
| """ | ||
| abs_quat = self._solver.get_links_quat(self.base_link_idx, envs_idx)[..., 0, :] | ||
| if not relative: | ||
| return abs_quat | ||
|
|
||
| has_free_root_qpos = self.base_link.n_joints == 1 and self.base_link.joints[0].type == gs.JOINT_TYPE.FREE | ||
| if not has_free_root_qpos: | ||
| if self._solver._options.batch_links_info: | ||
| init_quat = qd_to_torch( | ||
| self._solver.links_info.quat, | ||
| envs_idx, | ||
| self.base_link_idx, | ||
| transpose=True, | ||
| copy=True, | ||
| ) | ||
| if self._solver.n_envs == 0: | ||
| init_quat = init_quat[0, 0] | ||
| else: | ||
| init_quat = init_quat[:, 0] | ||
| else: | ||
| init_quat = torch.as_tensor(self.base_link.quat, dtype=abs_quat.dtype, device=abs_quat.device) | ||
| else: | ||
| q_start = self.base_link.q_start | ||
| init_quat = qd_to_torch( | ||
| self._solver.qpos0, | ||
| envs_idx, | ||
| slice(q_start + 3, q_start + 7), | ||
| transpose=True, | ||
| copy=True, | ||
| ) | ||
|
Comment on lines
+1391
to
+1397
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.
When Useful? React with 👍 / 👎. |
||
| if self._solver.n_envs == 0: | ||
| init_quat = init_quat[0] | ||
|
|
||
| init_quat = init_quat.to(dtype=abs_quat.dtype, device=abs_quat.device) | ||
| return gu.transform_quat_by_quat(gu.inv_quat(init_quat), abs_quat) | ||
|
|
||
| @gs.assert_built | ||
| def get_vel(self, envs_idx=None): | ||
|
|
||
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.
When
relative=True, this branch assumes any non-fixed base link has a free-root quaternion inqpos0, butattach()can flipbase_link.is_fixedtoFalsefor originally fixed-base entities attached to a non-fixed parent (seelink._is_fixed &= parent_link.is_fixed). In that case the base link does not have a root quaternion slot, soslice(q_start + 3, q_start + 7)reads unrelatedqpos0entries (or from an invalid start), producing incorrect relative quaternions for attached entities. This regression is new because the previousget_quat()only returned absolute state and did not depend onq_start.Useful? React with 👍 / 👎.