-
Notifications
You must be signed in to change notification settings - Fork 1
add solver and motion generator docs #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # ---------------------------------------------------------------------------- | ||
| # Copyright (c) 2021-2025 DexForce Technology Co., Ltd. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
| import time | ||
| import torch | ||
| import numpy as np | ||
| from copy import deepcopy | ||
| from embodichain.lab.sim import SimulationManager, SimulationManagerCfg | ||
| from embodichain.lab.sim.objects import Robot | ||
| from embodichain.lab.sim.robots import CobotMagicCfg | ||
| from embodichain.lab.sim.planners.utils import TrajectorySampleMethod | ||
| from embodichain.lab.sim.planners.motion_generator import MotionGenerator | ||
|
|
||
|
|
||
| def move_robot_along_trajectory( | ||
| robot: Robot, arm_name: str, qpos_list: list[torch.Tensor], delay: float = 0.1 | ||
| ): | ||
| """ | ||
| Set the robot joint positions sequentially along the given joint trajectory. | ||
| Args: | ||
| robot: Robot instance. | ||
| arm_name: Name of the robot arm. | ||
| qpos_list: List of joint positions (torch.Tensor). | ||
| delay: Time delay between each step (seconds). | ||
| """ | ||
| for q in qpos_list: | ||
| robot.set_qpos(qpos=q.unsqueeze(0), joint_ids=robot.get_joint_ids(arm_name)) | ||
| time.sleep(delay) | ||
|
|
||
|
|
||
| def create_demo_trajectory( | ||
| robot: Robot, arm_name: str | ||
| ) -> tuple[list[torch.Tensor], list[np.ndarray]]: | ||
| """ | ||
| Generate a three-point trajectory (start, middle, end) for demonstration. | ||
| Args: | ||
| robot: Robot instance. | ||
| arm_name: Name of the robot arm. | ||
| Returns: | ||
| qpos_list: List of joint positions (torch.Tensor). | ||
| xpos_list: List of end-effector poses (numpy arrays). | ||
| """ | ||
| qpos_fk = torch.tensor( | ||
| [[0.0, np.pi / 4, -np.pi / 4, 0.0, np.pi / 4, 0.0]], dtype=torch.float32 | ||
| ) | ||
| xpos_begin = robot.compute_fk(name=arm_name, qpos=qpos_fk, to_matrix=True) | ||
| xpos_mid = deepcopy(xpos_begin) | ||
| xpos_mid[0, 2, 3] -= 0.1 # Move down by 0.1m in Z direction | ||
| xpos_final = deepcopy(xpos_mid) | ||
| xpos_final[0, 0, 3] += 0.2 # Move forward by 0.2m in X direction | ||
|
|
||
| qpos_begin = robot.compute_ik(pose=xpos_begin, name=arm_name)[1][0] | ||
| qpos_mid = robot.compute_ik(pose=xpos_mid, name=arm_name)[1][0] | ||
| qpos_final = robot.compute_ik(pose=xpos_final, name=arm_name)[1][0] | ||
| return [qpos_begin, qpos_mid, qpos_final], [ | ||
| xpos_begin[0], | ||
| xpos_mid[0], | ||
| xpos_final[0], | ||
| ] | ||
|
|
||
|
|
||
| def main(): | ||
| np.set_printoptions(precision=5, suppress=True) | ||
| torch.set_printoptions(precision=5, sci_mode=False) | ||
|
|
||
| # Initialize simulation | ||
| sim = SimulationManager(SimulationManagerCfg(headless=False, sim_device="cpu")) | ||
| sim.build_multiple_arenas(1) | ||
| sim.set_manual_update(False) | ||
|
|
||
| # Robot configuration | ||
| cfg_dict = {"uid": "CobotMagic"} | ||
| robot: Robot = sim.add_robot(cfg=CobotMagicCfg.from_dict(cfg_dict)) | ||
| arm_name = "left_arm" | ||
|
|
||
| # # Generate trajectory points | ||
| qpos_list, xpos_list = create_demo_trajectory(robot, arm_name) | ||
|
|
||
| # Initialize motion generator | ||
| motion_generator = MotionGenerator( | ||
| robot=robot, | ||
| uid=arm_name, | ||
| planner_type="toppra", | ||
| default_velocity=0.2, | ||
| default_acceleration=0.5, | ||
| ) | ||
|
|
||
| # Joint space trajectory | ||
| out_qpos_list, _ = motion_generator.create_discrete_trajectory( | ||
| qpos_list=[q.numpy() for q in qpos_list], | ||
| is_linear=False, | ||
| sample_method=TrajectorySampleMethod.QUANTITY, | ||
| sample_num=20, | ||
| ) | ||
| move_robot_along_trajectory(robot, arm_name, out_qpos_list) | ||
|
|
||
| # Cartesian space trajectory | ||
| out_qpos_list, _ = motion_generator.create_discrete_trajectory( | ||
| xpos_list=[x.numpy() for x in xpos_list], | ||
| is_linear=True, | ||
| sample_method=TrajectorySampleMethod.QUANTITY, | ||
| sample_num=20, | ||
| ) | ||
| move_robot_along_trajectory(robot, arm_name, out_qpos_list) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # ---------------------------------------------------------------------------- | ||
| # Copyright (c) 2021-2025 DexForce Technology Co., Ltd. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
| import time | ||
| import numpy as np | ||
| import torch | ||
|
|
||
| from IPython import embed | ||
|
|
||
| from embodichain.lab.sim.objects import Robot | ||
| from embodichain.lab.sim import SimulationManager, SimulationManagerCfg | ||
| from embodichain.lab.sim.robots import DexforceW1Cfg | ||
|
|
||
|
|
||
| def main(): | ||
| # Set print options for better readability | ||
| np.set_printoptions(precision=5, suppress=True) | ||
| torch.set_printoptions(precision=5, sci_mode=False) | ||
|
|
||
| # Initialize simulation | ||
| sim_device = "cpu" | ||
| sim = SimulationManager( | ||
| SimulationManagerCfg( | ||
| headless=False, sim_device=sim_device, width=2200, height=1200 | ||
| ) | ||
| ) | ||
|
|
||
| sim.build_multiple_arenas(1) | ||
| sim.set_manual_update(False) | ||
|
|
||
| robot: Robot = sim.add_robot(cfg=DexforceW1Cfg.from_dict({"uid": "dexforce_w1"})) | ||
| arm_name = "left_arm" | ||
| # Set initial joint positions for left arm | ||
| qpos_fk_list = [ | ||
| torch.tensor([[0.0, 0.0, 0.0, -np.pi / 2, 0.0, 0.0, 0.0]], dtype=torch.float32), | ||
| ] | ||
| robot.set_qpos(qpos_fk_list[0], joint_ids=robot.get_joint_ids(arm_name)) | ||
|
|
||
| time.sleep(0.5) | ||
|
|
||
| fk_xpos_batch = torch.cat(qpos_fk_list, dim=0) | ||
|
|
||
| fk_xpos_list = robot.compute_fk(qpos=fk_xpos_batch, name=arm_name, to_matrix=True) | ||
|
|
||
| start_time = time.time() | ||
| res, ik_qpos = robot.compute_ik( | ||
| pose=fk_xpos_list, | ||
| name=arm_name, | ||
| # joint_seed=qpos_fk_list[0], | ||
| return_all_solutions=True, | ||
| ) | ||
| end_time = time.time() | ||
| print( | ||
| f"Batch IK computation time for {len(fk_xpos_list)} poses: {end_time - start_time:.6f} seconds" | ||
| ) | ||
|
|
||
| if ik_qpos.dim() == 3: | ||
| first_solutions = ik_qpos[:, 0, :] | ||
| else: | ||
| first_solutions = ik_qpos | ||
| robot.set_qpos(first_solutions, joint_ids=robot.get_joint_ids(arm_name)) | ||
|
|
||
| ik_xpos_list = robot.compute_fk(qpos=first_solutions, name=arm_name, to_matrix=True) | ||
|
|
||
| print("fk_xpos_list: ", fk_xpos_list) | ||
| print("ik_xpos_list: ", ik_xpos_list) | ||
|
|
||
| embed(header="Test SRSSolver example. Press Ctrl-D to exit.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.